Backend JAVA SDK to integrate PhonePe PG APIs.
Installation
Requirements:
Add the dependency to your project’s POM file:
Maven users
Add the dependency to your project’s POM file:
<dependency>
<groupId>com.phonepe</groupId>
<artifactId>pg-sdk-java</artifactId>
<version>2.1.0</version>
</dependency>
Add the PhonePe repository where the PhonePe SDK artifact is hosted to the distributionManagement:
<repositories>
<repository>
<id>io.cloudrepo</id>
<name> PhonePe JAVA SDK</name>
<url>https://phonepe.mycloudrepo.io/public/repositories/phonepe-pg-sdk-java</url>
</repository>
</repositories>
Gradle users
Add the following to your project’s build.gradle file.
In the repositories section, add the URL for the PhonePe repository, and include the pg-sdk-java JAR in your dependencies.
repositories {
maven {
url "https://phonepe.mycloudrepo.io/public/repositories/phonepe-pg-sdk-java"
}
}
dependencies {
implementation 'com.phonepe:pg-sdk-java:2.1.0'
}
Onboarding
To get your client ID and secret, refer to the PhonePe business dashboard in Production. For UAT, you can reach out to the Integration team.
String clientId = "<clientId>";
String clientSecret = "<clientSecret>";
Integer clientVersion = "<clientVersion>";
Quick start:
Class Initialisation
To create an instance of the StandardCheckoutClient
class, you need the keys received at the time of onboarding.
Example usage:
import com.phonepe.sdk.pg.Env;
import com.phonepe.sdk.pg.payments.v2.StandardCheckoutClient;
String clientId = "<clientId>";
String clientSecret = "<clientSecret>";
Integer clientVersion = 1; //insert your client version here
Env env = Env.SANDBOX; //change to Env.PRODUCTION when you go live
StandardCheckoutClient client = StandardCheckoutClient.getInstance(clientId, clientSecret,
clientVersion, env);
For detailed information on Class Initialisation – Refer here
Initiate an order using Checkout Page
To use the PhonePe Payment Gateway, we create the request using the StandardCheckoutPayRequest.builder() and then send this request to the pay() function.
You will get to initiate the order using the pay function :
code :
import com.phonepe.sdk.pg.payments.v2.models.request.StandardCheckoutPayRequest;
import com.phonepe.sdk.pg.payments.v2.models.response.StandardCheckoutPayResponse;
String merchantOrderId = UUID.randomUUID()
.toString();
long amount = 100;
String redirectUrl = "https://www.merchant.com/redirect";
StandardCheckoutPayRequest standardCheckoutPayRequest = StandardCheckoutPayRequest.buidler()
.merchantOrderId(merchantOrderId)
.amount(amount)
.redirectUrl(redirectUrl)
.build();
StandardCheckoutPayResponse standardCheckoutPayResponse = client.pay(standardCheckoutPayRequest);
String checkoutPageUrl = standardCheckoutPayResponse.getRedirectUrl();
The data will be in a StandardCheckoutPayResponse object.
User should be redirected to the checkoutPageUrl received in the response.
For detailed information on Initiate Payment – Refer here
Check Status of a order
To check the status of the order.
import com.phonepe.sdk.pg.common.models.response.OrderStatusResponse;
String merchantOrderId = "<merchantOrderId>"; //created at the time of order creation
OrderStatusResponse orderStatusResponse = client.getOrderStatus(merchantOrderId);
String state = orderStatusResponse.getState();
Returns an OrderStatusResponse Object
For detailed information on Check Order Status – Refer here
Order Callback Handling
Verify the validity of the callback received from PhonePe using the validateCallback function. You need to pass four parameters to the function
- username
- password
- authorization (received in callback headers)
- responseBody (received in the callback payload)
code :
import com.phonepe.sdk.pg.common.models.response.CallbackResponse;
String authorizationHeaderData = "5d8abfd6b13a5d1b74e885269474a3dd5bd6e9500a8cb4dee2924215fad3f711"; // received in the response headers
String phonepeS2SCallbackResponseBodyString = "{\"event\": \"pg.order.completed\",\"payload\": {}}"; // callback body as string
String usernameConfigured = "<MERCHANT_USERNAME>";
String passwordConfigured = "<MERCHANT_PASSWORD>";
CallbackResponse callbackResponse = client.validateCallback(usernameConfigured, passwordConfigured, authorizationHeaderData, phonepeS2SCallbackResponseBodyString);
String orderId = callbackResponse.getPayload().getOrderId();
String state = callbackResponse.getPayload().getState();
The
validateCallback
will throw PhonePeException, if the callback is invalid.
Possible order callback states:
- checkout.order.completed
- checkout.order.failed
- checkout.transaction.attempt.failed
For detailed information on Order Callback Handling – Refer here
Create Order SDK Integration
This function is used when the merchant is using the Frontend SDK and backend is in Java. Merchant can call the function to get the token which can be used by the frontend to initiate an order
Use case – When your backend is in Java and you are using a Frontend SDK as well
The createSdkOrder()
function is used to create a order.
import java.util.UUID;
import com.phonepe.sdk.pg.payments.v2.models.request.CreateSdkOrderRequest;
import com.phonepe.sdk.pg.payments.v2.models.response.CreateSdkOrderResponse;
String merchantOrderId = UUID.randomUUID()
.toString();
long amount = 1000;
String redirectUrl = "https://redirectUrl.com";
CreateSdkOrderRequest createSdkOrderRequest = CreateSdkOrderRequest.StandardCheckoutBuilder()
.merchantOrderId(merchantOrderId)
.amount(amount)
.redirectUrl(redirectUrl)
.build();
CreateSdkOrderResponse createSdkOrderResponse = client.createSdkOrder(createSdkOrderRequest);
String token = createSdkOrderResponse.getToken();
The function returns a CreateSdkOrderResponse object from which merchant should retrieve the Order Token.
For detailed information on Created Order SDK – Refer here