Payment Integration With Paytm In a Spring Boot Application

Posted By : Mohd Altaf | 18-Sep-2023

Java Java Virtual Machine Spring boot

Loading...

Payment Integration With Paytm In Spring Boot Application

This article will teach us to integrate Paytm as a Payment Gateway in the Spring Boot Application. It is one of the popular services with the fastest and safest payment method.

Steps to Integrate Paytm as a Payment Gateway

  • Create a Spring Boot Application from Spring Initializr.
  • Get Paytm Properties such as Merchant ID, Merchant Key, etc

  • Log in or sign in to a Paytm Account.
  • Go to Developer Setting and API Keys.
  • Select Generate Test API Details.
  • You will get a Test Merchant ID and Test Merchant Key.
  • Configure the application.properties file

################ Paytm Properties #########################

paytm.payment.sandbox.merchantId:your merchant ID

paytm.payment.sandbox.merchantKey: your merchant key

paytm.payment.sandbox.channelId:WEB

paytm.payment.sandbox.industryTypeId:Retail

paytm.payment.sandbox.website:WEBSTAGING

paytm.payment.sandbox.paytmUrl:https://securegw-stage.paytm.in/order/process paytm.payment.sandbox.callbackUrl:http://localhost:8080/pgresponse

paytm.payment.sandbox.details.MID: ${paytm.payment.sandbox.merchantId}

paytm.payment.sandbox.details.CHANNEL_ID: ${paytm.payment.sandbox.channelId}

paytm.payment.sandbox.details.INDUSTRY_TYPE_ID: ${paytm.payment.sandbox.industryTypeId} paytm.payment.sandbox.details.WEBSITE: ${paytm.payment.sandbox.website} paytm.payment.sandbox.details.CALLBACK_URL: ${paytm.payment.sandbox.callbackUrl}

paytm.mobile = your paytm registered mobile number

paytm.email = your email address

Note: All the properties will be the same as mentioned in the application.properties, Only Merchant ID, Merchant Key, Mobile Number, and Email Address will be yours.

Also, Read Stripe Payment Gateway Integration In Java

  • Add the following maven repository in your pom.xml.

<repositories>

<repository>

<id>my-repo1</id>

<url> http://artifactorypg.paytm.in/artifactory/libs-release </url>

</repository>

</repositories>

  • Placed the dependency for checksum in pom.xml.

<dependency>

<groupId>com.paytm</groupId>

<artifactId>paytm-checksum</artifactId>

</dependency>

Also, Read Performing Basic Operation On AWS Bucket In Spring Boot Java

  • Controller code.

@Log4j2

@RestController

@RequestMapping("/booking")

public class BookingController {

@Autowired

private PaytmDetails paytmDetails;

@Value("${paytm.mobile}")

private String paytmMobile;

@Value("${paytm.email}")

private String paytmEmail;

@PostMapping(value = "/make-payment")

public ModelAndView getPaymentRedirect(@RequestParam String orderId, @RequestParam String txnAmount, @RequestParam String customerId) throws Exception {

log.info("1=========");

ModelAndView modelAndView = new ModelAndView("redirect:" + paytmDetails.getPaytmUrl());

log.info("2=========");

TreeMap<String, String> parameters = new TreeMap<>();

paytmDetails.getDetails().forEach((k, v) -> parameters.put(k, v));

parameters.put("MOBILE_NO", paytmMobile);

parameters.put("EMAIL", paytmEmail);

parameters.put("ORDER_ID", orderId);

parameters.put("TXN_AMOUNT", txnAmount);

parameters.put("CUST_ID", customerId);

String checkSum = getCheckSum(parameters);

parameters.put("CHECKSUMHASH", checkSum);

log.info("3=========");

modelAndView.addAllObjects(parameters);

return modelAndView;

}

@PostMapping(value = "/payment-response")

public ModelAndView getPaymentResponseRedirect(HttpServletRequest request) {

ModelAndView modelAndView = new ModelAndView("redirect:http://localhost:8080/#/payment");//my angular payment response landing url

Map<String, String[]> mapData = request.getParameterMap();

TreeMap<String, String> parameters = new TreeMap<String, String>();

String paytmChecksum = "";

for (Entry<String, String[]> requestParamsEntry : mapData.entrySet()) {

if ("CHECKSUMHASH".equalsIgnoreCase(requestParamsEntry.getKey())) {

paytmChecksum = requestParamsEntry.getValue()[0];

} else {

parameters.put(requestParamsEntry.getKey(), requestParamsEntry.getValue()[0]);

}

}

String result;

boolean isValideChecksum = false;

try {

isValideChecksum = validateCheckSum(parameters, paytmChecksum);

if (isValideChecksum && parameters.containsKey("RESPCODE")) {

if (parameters.get("RESPCODE").equals("01")) {

result = "Payment Successful";

} else {

result = "Payment Failed";

}

} else {

result = "Checksum mismatched";

}

} catch (Exception e) {

result = e.toString();

}

modelAndView.addObject("result", result);

parameters.remove("CHECKSUMHASH");

modelAndView.addObject("parameters", parameters);

return modelAndView;

}

private boolean validateCheckSum(TreeMap<String, String> parameters, String paytmChecksum) throws Exception {

return PaytmChecksum.verifySignature(parameters, paytmDetails.getMerchantKey(), paytmChecksum);

}

private String getCheckSum(TreeMap<String, String> parameters) throws Exception {

return PaytmChecksum.generateSignature(parameters, paytmDetails.getMerchantKey());

}

}