Java Setup

Java SDK

A Java library for integrating with PhonePe APIs.

Installation

Requires Java 17 or later

Maven users

Add the dependency to your project’s POM file


<dependency>
    <groupId>com.phonepe</groupId>
    <artifactId>pg-sdk-java</artifactId>
    <version>1.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:1.1.0'
}

Onboarding

To get started, you will need three details. Reach out to the Integration team.

String merchantId = “merchantId”;
String saltKey = “saltKey”;
Integer saltIndex = “saltIndex”;

Quick start

To create an instance of the PhonePeClient class, you need to provide the following parameters:

import com.phonepe.sdk.pg.Env;
import com.phonepe.sdk.pg.payments.v1.PhonePePaymentClient;

String merchantId = "merchantId";
String saltKey = "saltKey";
Integer saltIndex = "saltIndex";
Env env = Env.UAT;

boolean shouldPublishEvents = true;
PhonePePaymentClient phonepeClient = new PhonePePaymentClient(merchantId, saltKey, saltIndex, env, shouldPublishEvents);

For Java SDK Version <= 1.0.1, the imports should be:
from phonepe.sdk.pg.payments

For Java SDK Version > 1.0.1, the imports should be:
from phonepe.sdk.pg.payments.v1

Initiate a transaction via the Pay Page

To utilize the PayPage instrument, we use the PgPayRequest’s PayPagePayRequestBuilder. You will get initiate the transaction using the pay function:

import com.phonepe.sdk.pg.common.http.PhonePeResponse;  
import com.phonepe.sdk.pg.payments.v1.models.request.PgPayRequest;  
import com.phonepe.sdk.pg.payments.v1.models.response.PayPageInstrumentResponse;  
import com.phonepe.sdk.pg.payments.v1.models.response.PgPayResponse;  
import java.util.UUID;

String merchantId="merchantId";  
String merchantTransactionId = UUID.randomUUID().toString().substring(0,34);
long amount=100;  
String merchantUserId="merchantUserId";  
String callbackurl="https://www.merchant.com/callback";
String redirecturl="https://www.merchant.com/redirect";
String redirectMode="REDIRECT";

PgPayRequest pgPayRequest=PgPayRequest.PayPagePayRequestBuilder()  
    .amount(amount)  
    .merchantId(merchantId)  
    .merchantTransactionId(merchantTransactionId)  
    .callbackUrl(callbackurl)  
    .merchantUserId(merchantUserId)  
    .redirectUrl(redirecturl)
    .redirectMode(redirectMode)
    .build();

PhonePeResponse<PgPayResponse> payResponse=phonepeClient.pay(pgPayRequest);  
PayPageInstrumentResponse payPageInstrumentResponse=(PayPageInstrumentResponse)payResponse.getData().getInstrumentResponse();  
String url=payPageInstrumentResponse.getRedirectInfo().getUrl();

Checking the validity of the callback

Let’s now verify the validity of the callback received from Phonepe using the verifyResponse function. You need to pass two things to the verifyResponse function:

The x_verify property in the headers of the callback response obtained from Phonepe passed as a String.
The response received from PhonePe on the merchant endpoint passed as a String.

String xVerify = a005532637c6a6e4a4b08ebc6f1144384353305a9cd253d995067964427cd0bb###1;  
String response = "{"response":"eyJzdWNjZXNzIjpmYWxzZSwiY29kZSI6IlBBWU1FTlRfRVJST1IiLCJtZXNzYWdlIjoiUGF5bWVudCBGYWlsZWQiLCJkYXRhIjp7Im1lcmNoYW50SWQiOiJtZXJjaGFudElkIiwibWVyY2hhbnRUcmFuc2FjdGlvbklkIjoibWVyY2hhbnRUcmFuc2FjdGlvbklkIiwidHJhbnNhY3Rpb25JZCI6IkZUWDIzMDYwMTE1NDMxOTU3MTYzMjM5IiwiYW1vdW50IjoxMDAsInN0YXRlIjoiRkFJTEVEIiwicmVzcG9uc2VDb2RlIjoiUkVRVUVTVF9ERUNMSU5FX0JZX1JFUVVFU1RFRSIsInBheW1lbnRJbnN0cnVtZW50IjpudWxsfX0="}";
  
boolean value = phonepeClient.verifyResponse(xVerify,response);

If the callback signature is verified, the value will be true.

Check Status of a transaction

Let see the details for the transaction after the payment is completed via UPI using the checkStatus function.

import com.phonepe.sdk.pg.common.http.PhonePeResponse;  
import com.phonepe.sdk.pg.payments.v1.models.response.PgPaymentInstrument;  
import com.phonepe.sdk.pg.payments.v1.models.response.PgTransactionStatusResponse;  
import com.phonepe.sdk.pg.payments.v1.models.response.UPIPaymentInstrumentResponse;

PhonePeResponse<PgTransactionStatusResponse> statusResponse=phonepeClient.checkStatus(merchantTransactionId);  
PgPaymentInstrument pgPaymentInstrument=statusResponse.getData().getPaymentInstrument();  
final UPIPaymentInstrumentResponse upiPaymentInstrumentResponse=(UPIPaymentInstrumentResponse)pgPaymentInstrument;  
String utr=upiPaymentInstrumentResponse.getUtr();  
String ifsc=upiPaymentInstrumentResponse.getIfsc();

Refund of a transaction

If you want to refund the transaction that was just processed using the refund function.

import com.phonepe.sdk.pg.common.http.PhonePeResponse;  
import com.phonepe.sdk.pg.payments.v1.models.request.PgRefundRequest;  
import com.phonepe.sdk.pg.payments.v1.models.response.PgRefundResponse;

String merchantId="merchantId";  
String merchantTransactionId="refundMerchantTranscationId";  
String originalTransactionId="forwardTranscationId";  
long amount=100;  
String callbackUrl="https://www.merchant.com/notify";  
PgRefundRequest pgRefundRequest=PgRefundRequest.builder()  
    .amount(amount)  
    .callbackUrl(callbackUrl)  
    .merchantId(merchantId)  
    .merchantTransactionId(merchantTransactionId)  
    .originalTransactionId(originalTransactionId)  
    .build();

PhonePeResponse<PgRefundResponse> refundResponse=phonepeClient.refund(pgRefundRequest);  
String responseCode=refundResponse.getData().getResponseCode();

Dealing with failed transaction

If you want to check the status of the transaction that failed.

import com.phonepe.sdk.pg.common.http.PhonePeResponse;  
import com.phonepe.sdk.pg.payments.v1.models.response.PgTransactionStatusResponse;

String merchantTransactionId="merchantTransactionId";  
PhonePeResponse<PgTransactionStatusResponse> statusResponse=phonepeClient.checkStatus(merchantTransactionId);  
boolean success=statusResponse.getSuccess();  
String code=statusResponse.getCode();  
String message=statusResponse.getMessage();  
String responseCode=statusResponse.getData().getResponseCode();  
String state=statusResponse.getData().getState();  
String transactionId=statusResponse.getData().getTransactionId();