Introduction

Backend Python SDK to integrate PhonePe Autopay APIs.

Installation

Requirements:

  1. Python 3.9 or later
pip install --index-url https://phonepe.mycloudrepo.io/public/repositories/phonepe-pg-sdk-python  --extra-index-url https://pypi.org/simple phonepe_sdk

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:

from phonepe.sdk.pg.subscription.v2.subscription_client import SubscriptionClient
from phonepe.sdk.pg.env import Env
 
client_id = "<YOUR_CLIENT_ID>"
client_secret = "<YOUR_CLIENT_SECRET>"
client_version = 1  # Insert your client version here
env = Env.SANDBOX  # Change to Env.PRODUCTION when you go live
 
subscription_client = SubscriptionClient.get_instance(client_id=client_id,
                                                      client_secret=client_secret,
                                                      client_version=client_version,
                                                      env=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.build_subscription_setup_upi_intent()

Code:

import uuid
from phonepe.sdk.pg.common.models.request.pg_payment_request import PgPaymentRequest
from phonepe.sdk.pg.subscription.v2.models.request.auth_workflow_type import AuthWorkflowType
from phonepe.sdk.pg.subscription.v2.models.request.amount_type import AmountType
from phonepe.sdk.pg.subscription.v2.models.request.frequency import Frequency
 
merchant_order_id = str(uuid.uuid4())
merchant_subscription_id = str(uuid.uuid4())
amount = 100
max_amount = 100
auth_workflow_type = AuthWorkflowType.TRANSACTION
amount_type = AmountType.FIXED
frequency = Frequency.ON_DEMAND
 
setup_request = PgPaymentRequest.build_subscription_setup_upi_intent(
    merchant_order_id=merchant_order_id,
    merchant_subscription_id=merchant_subscription_id,
    amount=amount,
    auth_workflow_type=auth_workflow_type,
    amount_type=amount_type,
    frequency=frequency,
    max_amount=max_amount,
)
 
setup_response = subscription_client.setup(setup_request)
intent_url = setup_response.intent_url

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.build_subscription_setup_upi_collect()

import uuid
from phonepe.sdk.pg.common.models.request.pg_payment_request import PgPaymentRequest
from phonepe.sdk.pg.subscription.v2.models.request.auth_workflow_type import AuthWorkflowType
from phonepe.sdk.pg.subscription.v2.models.request.amount_type import AmountType
from phonepe.sdk.pg.subscription.v2.models.request.frequency import Frequency
 
merchant_order_id = str(uuid.uuid4())
merchant_subscription_id = str(uuid.uuid4())
amount = 100
vpa = "<REPLACE_WITH_REQUIRED_VPA>"
auth_workflow_type = AuthWorkflowType.TRANSACTION
amount_type = AmountType.FIXED
frequency = Frequency.ON_DEMAND
max_amount = 100
 
setup_request = PgPaymentRequest.build_subscription_setup_upi_collect(
    merchant_order_id=merchant_order_id,
    merchant_subscription_id=merchant_subscription_id,
    amount=amount,
    vpa=vpa,
    auth_workflow_type=auth_workflow_type,
    amount_type=amount_type,
    frequency=frequency,
    max_amount=max_amount,
)
 
setup_response = subscription_client.setup(setup_request)

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.build_subscription_notify_request()

import uuid
from phonepe.sdk.pg.common.models.request.pg_payment_request import PgPaymentRequest
from phonepe.sdk.pg.subscription.v2.models.request.redemption_retry_strategy import RedemptionRetryStrategy
merchant_order_id = str(uuid.uuid4())
merchant_subscription_id = "<MERCHANT_SUBSCRIPTION_ID>" # merchant_subscription_id passed during the setup
redemption_retry_strategy = RedemptionRetryStrategy.STANDARD
auto_debit = False
amount = 100
 
notify_request = PgPaymentRequest.build_subscription_notify_request(
    merchant_order_id=merchant_order_id,
    merchant_subscription_id=merchant_subscription_id,
    auto_debit=auto_debit,
    redemption_retry_strategy=redemption_retry_strategy,
    amount=amount
)
 
notify_response = subscription_client.notify(notify_request)

For detailed information on Notify – Refer here

Redeem Request

The redeem() method is used to initiate/redeem the subscription and can only be made 24 hours after the notify() function is called

merchant_order_id = "<MERCHANT_ORDER_ID>" # merchant_order_id passed during the setup and notify
 
redeem_response = subscription_client.redeem(merchant_order_id)

For detailed information on Redeem – Refer here

Subscription Cancellation

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

merchant_subscription_id = "<MERCHANT_SUBSCRIPTION_ID>" # merchant_subscription_id passed during the setup and notify
 
subscription_client.cancel_subscription(merchant_subscription_id)

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

For detailed information on Cancellation – Refer here

Subscription Status

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

merchant_subscription_id = "<MERCHANT_SUBSCRIPTION_ID>" # merchant_subscription_id passed during the setup and notify
 
status_response = subscription_client.get_subscription_status(merchant_subscription_id)

For detailed information on Subscription Status – Refer here

Order Status

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

merchant_order_id = "<MERCHANT_ORDER_ID>" # merchant_order_id passed during the setup and notify
 
order_status_response = subscription_client.get_order_status(merchant_order_id)

For detailed information on Order Status – Refer here