Introduction

Backend JAVA SDK to integrate PhonePe Autopay APIs.

Installation

Requirements:

  1. Java 8 or later

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'
}

Test Credentials

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

String clientId = "<clientId>";
String clientSecret = "<clientSecret>";
Integer clientVersion = "<clientVersion>"; 

Quick start:

Class Initialisation

Create an instance of the SubscriptionClient class:

Example usage:

import com.phonepe.sdk.pg.Env;
import com.phonepe.sdk.pg.subscription.v2.SubscriptionClient;
 
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
 
SubscriptionClient subscriptionClient = SubscriptionClient.getInstance(clientId, clientSecret, clientVersion, env);

For details information on Class Initialisation – Refer here

Subscription Setup

1. Setup Via UPI_INTENT

The setup() method is used to initiate the subscription setup by using the following builder for UPI_INTENT: PgPaymentRequest.SubscriptionSetupUpiIntentBuilder()

Code:

import com.phonepe.sdk.pg.common.models.request.PgPaymentRequest;
import com.phonepe.sdk.pg.common.models.response.PgPaymentResponse;
import java.util.UUID;
 
String merchantOrderId = UUID.randomUUID().toString();
String merchantSubscriptionId = UUID.randomUUID().toString();
long amount = 200;                                                  //In paisa
AuthWorkflowType authWorkFlowType = AuthWorkflowType.TRANSACTION;   //It can also be AuthWorkFlowType.PENNY_DROP
AmountType amountType = AmountType.FIXED;                           //It can also be AmountType.VARIBALE
Frequency frequency = Frequency.ON_DEMAND;
long maxAmount = 200;                                               //In paisa
 
PgPaymentRequest setupRequest = PgPaymentRequest.SubscriptionSetupUpiIntentBuilder()
        .merchantOrderId(merchantOrderId)
        .merchantSubscriptionId(merchantSubscriptionId)
        .amount(amount)
        .authWorkflowType(authWorkFlowType)
        .amountType(amountType)
        .maxAmount(maxAmount)
        .frequency(frequency)
        .build();
 
PgPaymentResponse response = client.setup(setupRequest);
String state = response.state;

For detailed information on Setup Via UPI_INTENT – Refer here

2. Setup via UPI_COLLECT

The setup() method is used to initiate the subscription setup by using the following builder for UPI_COLLECT: PgPaymentRequest.SubscriptionSetupUpiCollectBuilder()

import com.phonepe.sdk.pg.common.models.request.PgPaymentRequest;
import com.phonepe.sdk.pg.common.models.response.PgPaymentResponse;
import java.util.UUID;
 
String merchantOrderId = UUID.randomUUID().toString();
String merchantSubscriptionId = UUID.randomUUID().toString();
long amount = 200;                                                  //In paisa
String vpa = "9999999999@ibl";
AuthWorkflowType authWorkFlowType = AuthWorkflowType.TRANSACTION;   //It can also be AuthWorkFlowType.PENNY_DROP
AmountType amountType = AmountType.FIXED;                           //It can also be AmountType.VARIBALE
Frequency frequency = Frequency.ON_DEMAND;
long maxAmount = 200;                                               //In paisa
 
PgPaymentRequest setupRequest = PgPaymentRequest.SubscriptionSetupUpiCollectBuilder()
        .merchantOrderId(merchantOrderId)
        .merchantSubscriptionId(merchantSubscriptionId)
        .amount(amount)
        .vpa(vpa)
        .authWorkflowType(authWorkFlowType)
        .amountType(amountType)
        .maxAmount(maxAmount)
        .frequency(frequency)
        .build();
 
PgPaymentResponse setupResponse = client.setup(setupRequest);
String state = setupResponse.getState();

For detailed information on Setup via UPI_COLLECT – Refer here

Notify Request

The notify() method can be used to send a notification corresponding to the subscription ID used at the time of setup by using the following builder: PgPaymentRequest.SubscriptionNotifyRequestBuilder()

import com.phonepe.sdk.pg.common.models.request.PgPaymentRequest;
import com.phonepe.sdk.pg.common.models.response.PgPaymentResponse;
import java.util.UUID;
 
String merchantOrderId = UUID.randomUUID().toString();      //MERCHANT_ORDER_ID will be used in the redeem call
String merchantSubscriptionId = "<MERCHANT_SUBSCRIPTION_ID>";
RedemptionRetryStrategy redemptionRetryStrategy = RedemptionRetryStrategy.STANDARD;
boolean autoDebit = false;
long amount = 200;                                      //In paisa
 
PgPaymentRequest subscriptionRequest = PgPaymentRequest.SubscriptionNotifyRequestBuilder()
        .merchantOrderId(merchantOrderId)
        .merchantSubscriptionId(merchantSubscriptionId)
        .autoDebit(autoDebit)
        .redemptionRetryStrategy(redemptionRetryStrategy)
        .amount(200)
        .build();
 
PgPaymentResponse notifyResponse = client.notify(request);
String state = notifyResponse.getState();

For detailed information on Notify – Refer here

Redeem Request

The redeem() method is used to start the subscription using same merchantOrderId passed in the notify call

import com.phonepe.sdk.pg.subscription.v2.models.response.SubscriptionRedeemResponseV2;
 
String merchantOrderId = "<MERCHANT_ORDER_ID>";         //Same merchantOrderId used at the time of notify
 
SubscriptionRedeemResponseV2 response = client.redeem(merchantOrderId);
String state = response.getState();

For detailed information on Redeem – Refer here

Subscription Cancellation

The cancelSubscription() method is used to cancel/stop the subscription

import com.phonepe.sdk.pg.Env;
import com.phonepe.sdk.pg.subscription.v2.SubscriptionClient;
 
String merchantSubscriptionId = "<MERCHANT_SUBSCRIPTION_ID>";       //MERCHANT_SUBSCRIPTION_ID used for setup and notify
 
 
subscriptionClient.cancelSubscription(merchantSubscriptionId);

The response is void and it will stop/cancel the subscription for given ID.

For detailed information on Cancellation – Refer here

Subscription Status

The getSubscriptionStatus() method is used to retrieve the status for a particular subscription ID (Merchant generated ID)

import com.phonepe.sdk.pg.subscription.v2.models.response.SubscriptionStatusResponseV2;
 
String merchantSubscriptionId = "<MERCHANT_SUBSCRIPTION_ID>";
 
SubscriptionStatusResponseV2 statusResponse = client.getSubscriptionStatus(merchantSubscriptionId);
String state = statusResponse.getState();

For detailed information on Subscription Status – Refer here

Order Status

The getOrderStatus() method is used to retrieve the status for a particular order ID (Merchant generated ID)

import com.phonepe.sdk.pg.common.models.response.OrderStatusResponse;
 
String merchantOrderId = "<MERCHANT_ORDER_ID>";
 
OrderStatusResponse statusResponse = client.getOrderStatus(merchantOrderId);
String state = statusResponse.getState();

For detailed information on Order Status – Refer here