Integrating PhonePe Payment Gateway in Spring Boot with Example
11/28/20233 min read
Introduction
Integrating a payment gateway into your Spring Boot application is crucial for enabling secure and seamless transactions. PhonePe, being one of the leading digital payment platforms in India, provides a robust and reliable payment gateway solution. In this tutorial, we will explore how to integrate the PhonePe Payment Gateway into a Spring Boot application, along with a practical example.
Prerequisites
Before we begin, make sure you have the following:
- Basic knowledge of Spring Boot and web development
- A PhonePe merchant account
- A Spring Boot project set up and ready to use
Step 1: Obtain PhonePe Merchant Account Credentials
In order to integrate the PhonePe Payment Gateway, you need to have a PhonePe merchant account. If you don't have one, sign up for a merchant account on the PhonePe website and obtain the necessary credentials such as Merchant ID, Merchant Key, and Callback URL.
Step 2: Add PhonePe SDK Dependency
Open your Spring Boot project in your preferred IDE and navigate to the pom.xml
file. Add the following dependency to integrate the PhonePe SDK into your project:
<dependency>
<groupId>com.phonepe</groupId>
<artifactId>phonepe-sdk</artifactId>
<version>1.0.0</version>
</dependency>
Step 3: Configure PhonePe Payment Gateway
Next, you need to configure the PhonePe Payment Gateway in your Spring Boot application. Open the application.properties
file and add the following properties:
# PhonePe Payment Gateway Configuration
phonepe.merchant.id=YOUR_MERCHANT_ID
phonepe.merchant.key=YOUR_MERCHANT_KEY
phonepe.callback.url=YOUR_CALLBACK_URL
Replace YOUR_MERCHANT_ID
, YOUR_MERCHANT_KEY
, and YOUR_CALLBACK_URL
with your actual merchant credentials and the URL where PhonePe will send the payment response.
Step 4: Implement Payment Request Flow
Now, let's implement the payment request flow in your Spring Boot application. Create a new controller class, for example, PaymentController.java
, and add the following code:
import com.phonepe.sdk.PhonePe;
import com.phonepe.sdk.model.PaymentRequest;
import com.phonepe.sdk.model.PaymentResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PaymentController {
@Autowired
private PhonePe phonePe;
@PostMapping("/payment")
public PaymentResponse initiatePayment(@RequestBody PaymentRequest paymentRequest) {
return phonePe.initiatePayment(paymentRequest);
}
}
In this code snippet, we have created a REST endpoint /payment
that accepts a PaymentRequest
object as the request body. The phonePe.initiatePayment()
method is used to initiate the payment request to PhonePe.
Step 5: Create Payment Request DTO
Next, create a DTO (Data Transfer Object) class for the payment request. This class will hold the necessary information required to initiate the payment. Create a new class, for example, PaymentRequest.java
, and add the following code:
public class PaymentRequest {
private String orderId;
private String amount;
private String description;
// Add getters and setters
}
Make sure to add the necessary getters and setters for the fields.
Step 6: Handle Payment Response
After the payment request is initiated, PhonePe will send the payment response to the callback URL provided. We need to handle this response in our application. Create a new controller class, for example, PaymentCallbackController.java
, and add the following code:
import com.phonepe.sdk.PhonePe;
import com.phonepe.sdk.model.PaymentCallbackResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PaymentCallbackController {
@Autowired
private PhonePe phonePe;
@PostMapping("/payment/callback")
public void handlePaymentCallback(@RequestBody PaymentCallbackResponse paymentCallbackResponse) {
// Handle the payment response here
// You can update your database, send notifications, etc.
}
}
In this code snippet, we have created a REST endpoint /payment/callback
that accepts a PaymentCallbackResponse
object as the request body. Inside the handlePaymentCallback()
method, you can handle the payment response as per your application's requirements.
Step 7: Test the Payment Flow
Now that we have implemented the payment flow, it's time to test it. Start your Spring Boot application and make a POST request to /payment
with a PaymentRequest
object as the request body. You can use tools like Postman or cURL to test the API.
Once the payment request is initiated, PhonePe will handle the rest of the payment flow, including user authentication, payment confirmation, and redirecting the user back to the callback URL.
Conclusion
Congratulations! You have successfully integrated the PhonePe Payment Gateway into your Spring Boot application. You can now accept secure and seamless payments from your users. Remember to handle the payment response appropriately in your application to ensure a smooth user experience.
Feel free to explore the PhonePe SDK documentation for more advanced features and customization options to enhance your payment integration further.