packagecom.infy.ekart.payment.service;
importjava.security.NoSuchAlgorithmException; importjava.util.ArayList;
importjava.util.List;
importjava.util.Optional;
importjavax.transaction.Transactional;
importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.stereotype.Service;
importcom.infy.ekart.payment.dto.CardDTO; importcom.infy.ekart.payment.dto.TransactionDTO; importcom.infy.ekart.payment.dto.TransactionStatus; importcom.infy.ekart.payment.entity.Card; importcom.infy.ekart.payment.entity.Transaction; importcom.infy.ekart.payment.exception.EKartPaymentException; importcom.infy.ekart.payment.exception.PayOrderFalbackException; importcom.infy.ekart.payment.repository.CardRepository; importcom.infy.ekart.payment.repository.TransactionRepository; importcom.infy.ekart.payment.utility.HashingUtility;
@Service(value="paymentService")
@Transactional publicclassPaymentServiceImplimplementsPaymentService{
@Autowired privateCardRepositorycardRepository;
@Autowired
privateTransactionRepository transactionRepository;
@Overide
public IntegeraddCustomerCard(String customerEmailId,CardDTO cardDTO)throws EKartPaymentException,NoSuchAlgorithmException{
List<Card> listOfCustomerCards = cardRepository.findByCustomerEmailId(customerEmailId);
if(listOfCustomerCards.isEmpty()
throw new
EKartPaymentException("PaymentService.CUSTOMER_NOT_FOUND"); cardDTO.setHashCv(HashingUtility.getHashValue(cardDTO.getCv().toString(); CardnewCard=newCard();
newCard.setCardId(cardDTO.getCardId(); newCard.setNameOnCard(cardDTO.getNameOnCard(); newCard.setCardNumber(cardDTO.getCardNumber(); newCard.setCardType(cardDTO.getCardType(); newCard.setExpiryDate(cardDTO.getExpiryMonth()+"-"+cardDTO.getExpiryYear(); newCard.setCv(cardDTO.getHashCv(); newCard.setCustomerEmailId(cardDTO.getCustomerEmailId();
cardRepository.save(newCard);
returnnewCard.getCardID(); }
@Overide
publicvoidupdateCustomerCard(CardDTO cardDTO)throwsEKartPaymentException, NoSuchAlgorithmException{
Optional<Card>optionalCard=cardRepository.findById(cardDTO.getCardId();
Card card = optionalCard.orElseThrow() -> new EKartPaymentException("PaymentService.CARD_NOT_FOUND");
cardDTO.setHashCv(HashingUtility.getHashValue(cardDTO.getCv().toString(); card.setCardId(cardDTO.getCardId(); card.setNameOnCard(cardDTO.getNameOnCard(); card.setCardNumber(cardDTO.getCardNumber(); card.setCardType(cardDTO.getCardType(); card.setCv(cardDTO.getHashCv();
/card.setCv(cardDTO.getCv(); card.setExpiryDate(cardDTO.getExpiryMonth()+"-"+cardDTO.getExpiryYear(); card.setCustomerEmailId(cardDTO.getCustomerEmailId();
}
@Overide
public void deleteCustomerCard(String customerEmailId, Integer cardId) throws EKartPaymentException{
System.out.println(cardRepository);
List<Card> listOfCustomerCards = cardRepository.findByCustomerEmailId(customerEmailId);
if(listOfCustomerCards.isEmpty()
throw new
EKartPaymentException("PaymentService.CUSTOMER_NOT_FOUND");
Optional<Card>optionalCards=cardRepository.findById(cardId);
Card card = optionalCards.orElseThrow() -> new EKartPaymentException("PaymentService.CARD_NOT_FOUND");
cardRepository.delete(card); }
@Overide
public CardDTOgetCard(IntegercardId)throwsEKartPaymentException{
Optional<Card>optionalCards=cardRepository.findById(cardId);
Card card = optionalCards.orElseThrow() -> new EKartPaymentException("PaymentService.CARD_NOT_FOUND");
CardDTOcardDTO=newCardDTO(); cardDTO.setCardId(card.getCardID(); cardDTO.setNameOnCard(card.getNameOnCard(); cardDTO.setCardNumber(card.getCardNumber(); cardDTO.setCardType(card.getCardType(); cardDTO.setHashCv(card.getCv(); String[]expiryDate=card.getExpiryDate().split("-"); cardDTO.setExpiryMonth(expiryDate[1]); cardDTO.setExpiryYear(expiryDate[0]); cardDTO.setCustomerEmailId(card.getCustomerEmailId(); returncardDTO;
}
@Overide
public List<CardDTO> getCustomerCardOfCardType(String customerEmailId,String cardType)
throwsEKartPaymentException{
List<Card> cards = cardRepository.findByCustomerEmailIdAndCardType(customerEmailId,cardType);
if(cards.isEmpty() {
thrownewEKartPaymentException("PaymentService.CARD_NOT_FOUND"); }
List<CardDTO>cardDTOs=newArayList<CardDTO>(); for(Cardcard:cards)
{ CardDTOcardDTO=newCardDTO();
cardDTO.setCardId(card.getCardID(); cardDTO.setNameOnCard(card.getNameOnCard(); cardDTO.setCardNumber(card.getCardNumber(); cardDTO.setCardType(card.getCardType(); cardDTO.setHashCv("XXX"); String[]expiryDate=card.getExpiryDate().split("-"); cardDTO.setExpiryMonth(expiryDate[1]); cardDTO.setExpiryYear(expiryDate[0]); cardDTO.setCustomerEmailId(card.getCustomerEmailId();
cardDTOs.add(cardDTO); }
returncardDTOs; }
@Overide
public Integer addTransaction(TransactionDTO transactionDTO) throws EKartPaymentException,PayOrderFalbackException{
if(transactionDTO.getTransactionStatus().equals(TransactionStatus.TRANSACTION_FAILED) {
throw new PayOrderFalbackException("Payment.TRANSACTION_FAILED_CVV_NOT_MATCHING");
}
Transactiontransaction=newTransaction(); transaction.setCardId(transactionDTO.getCard().getCardId();
transaction.setOrderId(transactionDTO.getOrder().getOrderId(); transaction.setTotalPrice(transactionDTO.getTotalPrice(); transaction.setTransactionDate(transactionDTO.getTransactionDate(); transaction.setTransactionStatus(transactionDTO.getTransactionStatus(); transactionRepository.save(transaction);
returntransaction.getTransactionId(); }
@Overide
publicTransactionDTO authenticatePayment(StringcustomerEmailId,TransactionDTO transactionDTO)throwsEKartPaymentException,NoSuchAlgorithmException{
if(!transactionDTO.getOrder().getCustomerEmailId().equals(customerEmailId) {
throw new EKartPaymentException("PaymentService.ORDER_DOES_NOT_BELONGS");
}
if(!transactionDTO.getOrder().getOrderStatus().equals("PLACED")) {
throw new EKartPaymentException("PaymentService.TRANSACTION_ALREADY_DONE");
} CardDTOcardDTO=getCard(transactionDTO.getCard().getCardId(); if(!cardDTO.getCustomerEmailId().matches(customerEmailId)
{
throw new EKartPaymentException("PaymentService.CARD_DOES_NOT_BELONGS");
}
if(! cardDTO.getCardType().equals(transactionDTO.getOrder().getPaymentThrough()
{
throw new EKartPaymentException("PaymentService.PAYMENT_OPTION_SELECTED_NOT_MATCHING_ CARD_TYPE");
}
if(cardDTO.getHashCv().equals(HashingUtility.getHashValue(transactionDTO.getCard().getC v().toString()
{
transactionDTO.setTransactionStatus(TransactionStatus.TRANSACTION_SUCCESS); }
else {
transactionDTO.setTransactionStatus(TransactionStatus.TRANSACTION_FAILED); }
returntransactionDTO; }
}