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

Java
<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.

Java
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:

Java
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);

<rdme-callout title=”” value=”For Java SDK Version **\ 1.0.1**, the imports should be:
from phonepe.sdk.pg.payments.**v1**” icon=”🚧” theme=”warn”>

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:

Java
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"; PgPayRequest pgPayRequest=PgPayRequest.PayPagePayRequestBuilder() .amount(amount) .merchantId(merchantId) .merchantTransactionId(merchantTransactionId) .callbackUrl(callbackurl) .merchantUserId(merchantUserId) .build(); PhonePeResponse<PgPayResponse> payResponse=phonepeClient.pay(pgPayRequest); PayPageInstrumentResponse payPageInstrumentResponse=(PayPageInstrumentResponse)payResponse.getData().getInstrumentResponse(); String url=payPageInstrumentResponse.getRedirectInfo().getUrl();

Initiate transaction using UPI Intent

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

Java
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.PgPayResponse; import com.phonepe.sdk.pg.payments.v1.models.response.UpiIntentInstrumentResponse; import java.util.UUID; long amount=100; String merchantId="merchantId"; String merchantTransactionId = UUID.randomUUID().toString().substring(0,34); String redirecturl="https://merchant.com/redirectUrl"; String callbackurl="https://www.merchant.com/callback"; String merchantUserId="merchantUserId"; String deviceOS="IOS"; String targetApp="PHONEPE"; PgPayRequest pgPayRequest=PgPayRequest.UPIIntentPayRequestBuilder() .amount(amount) .merchantId(merchantId) .merchantTransactionId(merchantTransactionId) .callbackUrl(callbackurl) .redirectUrl(redirecturl) .merchantUserId(merchantUserId) .deviceOS(deviceOS) .targetApp(targetApp) .build(); PhonePeResponse<PgPayResponse> payResponse=phonepeClient.pay(pgPayRequest); UpiIntentInstrumentResponse upiIntentInstrumentResponse=(UpiIntentInstrumentResponse)payResponse.getData().getInstrumentResponse(); String intentUrl=upiIntentInstrumentResponse.getIntentUrl();

You will get a URL given should be used to complete the transaction.

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.

Java
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.

Java
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.

Java
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.

Java
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();