Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
7.3 kB
2
Indexable
Never
 packagecom.infy.ekart.payment.api;
importjava.security.NoSuchAlgorithmException; importjava.util.List;
importjavax.validation.Valid; importjavax.validation.constraints.Patern;
importorg.apache.commons.logging.Log; importorg.apache.commons.logging.LogFactory; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.core.env.Environment; importorg.springframework.htp.HtpStatus; importorg.springframework.htp.ResponseEntity; importorg.springframework.validation.annotation.Validated; importorg.springframework.web.bind.annotation.CrossOrigin; importorg.springframework.web.bind.annotation.DeleteMapping; importorg.springframework.web.bind.annotation.GetMapping; importorg.springframework.web.bind.annotation.PathVariable; importorg.springframework.web.bind.annotation.PostMapping; importorg.springframework.web.bind.annotation.PutMapping; importorg.springframework.web.bind.annotation.RequestBody; importorg.springframework.web.bind.annotation.RequestMapping; importorg.springframework.web.bind.annotation.RestControler; importorg.springframework.web.client.RestClientException; importorg.springframework.web.client.RestTemplate;
importcom.infy.ekart.payment.dto.CardDTO; importcom.infy.ekart.payment.dto.OrderDTO; importcom.infy.ekart.payment.dto.TransactionDTO; importcom.infy.ekart.payment.exception.EKartPaymentException; importcom.infy.ekart.payment.exception.PayOrderFalbackException; importcom.infy.ekart.payment.service.PaymentCircuitBreakerService; importcom.infy.ekart.payment.service.PaymentService;
importio.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
@CrossOrigin
@RestControler @RequestMapping(value="/payment-api") @Validated
publicclassPaymentAPI{
@Autowired privatePaymentServicepaymentService;
@Autowired privateEnvironmentenvironment;
@Autowired privatePaymentCircuitBreakerServicepaymentCircuitBreakerService;
@Autowired privateRestTemplatetemplate;
privatestaticfinalLoglogger=LogFactory.getLog(PaymentAPI.class);
 
 @PostMapping(value="/customer/{customerEmailId:.+}/cards") publicResponseEntity<String>addNewCard(@RequestBodyCardDTOcardDTO,
@Patern(regexp="[a-zA-Z0-9._]+@[a-zA-Z]{2,}\.[a-zA-Z][a-zA-Z.]+",message= "{invalid.email.format}")@PathVariable("customerEmailId")StringcustomerEmailId)
throwsEKartPaymentException,NoSuchAlgorithmException{ logger.info("Recieved request to add new card for customer : " +
cardDTO.getCustomerEmailId();
intcardId; cardId=paymentService.addCustomerCard(customerEmailId,cardDTO);
String message =
environment.getProperty("PaymentAPI.NEW_CARD_ADDED_SUCCESS"); StringtoReturn=message+cardId; toReturn=toReturn.trim(); returnnewResponseEntity<>(toReturn,HtpStatus.OK);
}
@PutMapping(value="/update/card")
public ResponseEntity<String> updateCustomerCard(@Valid @RequestBody CardDTO cardDTO)
throwsEKartPaymentException,NoSuchAlgorithmException{ logger.info("Recieved requestto update card :"+ cardDTO.getCardId()+ "of
customer:"
+cardDTO.getCustomerEmailId();
paymentService.updateCustomerCard(cardDTO);
String modificationSuccessMsg = environment.getProperty("PaymentAPI.UPDATE_CARD_SUCCESS");
returnnewResponseEntity<>(modificationSuccessMsg,HtpStatus.OK); }
@DeleteMapping(value="/customer/{customerEmailId:.+}/card/{cardID}/delete")
public ResponseEntity<String> deleteCustomerCard(@PathVariable("cardID") Integer cardID,
@Patern(regexp="[a-zA-Z0-9._]+@[a-zA-Z]{2,}\.[a-zA-Z][a-zA-Z.]+",message= "{invalid.email.format}")@PathVariable("customerEmailId")StringcustomerEmailId)
throwsEKartPaymentException{
logger.info("Recieved requestto delete card :"+ cardID + "ofcustomer:"+
customerEmailId);
paymentService.deleteCustomerCard(customerEmailId,cardID);
String modificationSuccessMsg = environment.getProperty("PaymentAPI.CUSTOMER_CARD_DELETED_SUCCESS"); returnnewResponseEntity<>(modificationSuccessMsg,HtpStatus.OK);
}
@GetMapping(value="/customer/{customerEmailId}/card-type/{cardType}")
public ResponseEntity<List<CardDTO>> getCardsOfCustomer(@PathVariable String customerEmailId,
@PathVariableStringcardType)throwsEKartPaymentException{ logger.info("Recievedrequesttofetch cardsofcustomer:"+customerEmailId+"
havingcardtypeas:"
 
 +cardType);
List<CardDTO> cardDTOs = paymentService.getCustomerCardOfCardType(customerEmailId,cardType);
returnnewResponseEntity<>(cardDTOs,HtpStatus.OK); }
@CircuitBreaker(name="paymentService",falbackMethod="payForOrderFalback") @PostMapping(value="/customer/{customerEmailId}/pay-order") /Annotatethismethodforhandlingresilience
/Getthe orderdetails from CustomerMS forthe given orderId (available in
TransactionDTO) /UpdatetheTransactiondetailswiththeobtainedOrderdetailsinabovestep,along
withtransactiondateandtotalprice
/ Authenticate the transaction details for the given customer by caling
authenticatePayment()methodofPaymentService /AddthetransactiondetailstothedatabasebycalingaddTransaction()methodof
PaymentService
/ Update the order status by caling updateOrderAfterPayment() method of
PaymentCircuitBreakerService /Settheappropriatesuccessorfailuremessageandreturnthesame publicResponseEntity<String>payForOrder(
@Patern(regexp="[a-zA-Z0-9._]+@[a-zA-Z]{2,}\.[a-zA-Z][a-zA-Z.]+",message= "{invalid.email.format}")@PathVariable("customerEmailId")StringcustomerEmailId,
@Valid@RequestBodyTransactionDTOtransactionDTO)
throws NoSuchAlgorithmException, EKartPaymentException, PayOrderFalbackException{
/writeyourlogichere ResponseEntity<OrderDTO>
template.getForEntity("htp:/CustomerMS/Ekart/customerorder- api/order"+transactionDTO.getOrder().getOrderId(),OrderDTO.class);
orderDetails=
transactionDTO.setTransactionDate(orderDetails.getBody().getDateOfOrder(); transactionDTO.setTotalPrice(orderDetails.getBody().getTotalPrice(); paymentService.authenticatePayment(customerEmailId,transactionDTO); paymentService.addTransaction(transactionDTO);
paymentCircuitBreakerService.updateOrderAfterPayment(transactionDTO.getOrder().getOrde rId(),transactionDTO.getTransactionStatus().toString();
String message= environment.getProperty("PaymentAPI.TRANSACTION_SUCCESSFULL_ONE")+transactionDT O.getTotalPrice()+environment.getProperty("PaymentAPI.TRANSACTION_SUCCESSFULL_TW O")+transactionDTO.getOrder().getOrderId()+environment.getProperty("PaymentAPI.TRANSA CTION_SUCCESSFULL_THREE")+transactionDTO.getTransactionId();
returnnewResponseEntity<>(message,HtpStatus.OK);
}
/Implementafalbackmethodhere
/ /Ifexception m essage is Paym ent.TRANSACTION_FAILED_CVV_NOT_M ATCHING then setmessageasPayment.TRANSACTION_FAILED_CVV_NOT_MATCHING
/Elseifexceptionmessagecontains"Ordernotfound"thenre-throw exceptionas RestClientException(withmessagesameasexceptionmessage)
/ElsesetthemessageasPaymentAPI.PAYMENT_FAILURE_FALLBACK /Returntheabovemessageasresponse
public ResponseEntity<String> payForOrderFalback(String customerEmailId,
 
 TransactionDTOtransactionDTO, RuntimeExceptionexception){
/writeyourlogichere Stringmessage=";
if(exception.getMessage().equals("Payment.TRANSACTION_FAILED_CVV_NOT_MATCHING") message=
environment.getProperty("Payment.TRANSACTION_FAILED_CVV_NOT_MATCHING"); elseif(exception.getMessage().contains("Ordernotfound")
thrownewRestClientException(exception.getMessage(); else
message = environment.getProperty("PaymentAPI.PAYMENT_FAILURE_FALLBACK");
returnnewResponseEntity<>(message,HtpStatus.BAD_REQUEST); }
}