PhonePe Payment Gateway Python SDK
Official PhonePe Payment Gateway Python SDK. NOT on PyPI — must install from PhonePe's private registry using a special pip command. TWO packages cause confusion: 'phonepe' on PyPI (1.0.1) is a community wrapper using old v1 API — NOT the official SDK. Official SDK is 'phonepe_sdk' from phonepe.mycloudrepo.io. Amount in paise (1 INR = 100 paise). Client must be initialized only ONCE — reinitializing raises PhonePeException. Mandatory status check polling after payment.
Warnings
- breaking Official PhonePe SDK is NOT on PyPI. 'pip install phonepe_sdk' alone fails. Must use --index-url pointing to PhonePe's private registry.
- breaking 'phonepe' on PyPI (1.0.1) is a community package using old PhonePe v1 API — NOT the official SDK. Using it risks API deprecation and lacks official support.
- breaking StandardCheckoutClient must be initialized ONLY ONCE per application. Reinitializing raises PhonePeException.
- breaking Amount is in paise (1 INR = 100 paise) — same as Razorpay. 500 INR = 50000 paise. Passing rupees directly will undercharge.
- gotcha Status check after payment is MANDATORY. PhonePe docs require polling get_order_status() after redirect — do not rely on redirect callback alone.
- gotcha SANDBOX uses test credentials from PhonePe merchant dashboard. Sandbox and Production have different client_id/secret pairs.
Install
-
pip install --index-url https://phonepe.mycloudrepo.io/public/repositories/phonepe-pg-sdk-python --extra-index-url https://pypi.org/simple phonepe_sdk
Imports
- StandardCheckoutClient
from phonepe.sdk.pg.payments.v2.standard_checkout_client import StandardCheckoutClient from phonepe.sdk.pg.payments.v2.models.request.standard_checkout_pay_request import StandardCheckoutPayRequest from phonepe.sdk.pg.env import Env import uuid # Initialize ONCE — reinitializing raises PhonePeException client = StandardCheckoutClient( client_id='<client_id>', client_secret='<client_secret>', client_version=1, env=Env.SANDBOX # or Env.PRODUCTION ) # Create payment order_id = str(uuid.uuid4()) request = StandardCheckoutPayRequest.build_request( merchant_order_id=order_id, amount=10000, # 100 INR in paise redirect_url='https://yourapp.com/callback' ) response = client.pay(request) print(response.redirect_url) # redirect user here
Quickstart
# Install from PhonePe registry (NOT PyPI):
# pip install --index-url https://phonepe.mycloudrepo.io/public/repositories/phonepe-pg-sdk-python --extra-index-url https://pypi.org/simple phonepe_sdk
from phonepe.sdk.pg.payments.v2.standard_checkout_client import StandardCheckoutClient
from phonepe.sdk.pg.payments.v2.models.request.standard_checkout_pay_request import StandardCheckoutPayRequest
from phonepe.sdk.pg.env import Env
import uuid
# Initialize once at app startup
client = StandardCheckoutClient(
client_id='<client_id>', # from PhonePe dashboard
client_secret='<client_secret>',
client_version=1,
env=Env.SANDBOX
)
# Create payment
order_id = str(uuid.uuid4())
request = StandardCheckoutPayRequest.build_request(
merchant_order_id=order_id,
amount=50000, # 500 INR in paise
redirect_url=f'https://yourapp.com/callback?order_id={order_id}'
)
response = client.pay(request)
# Redirect user to response.redirect_url
print('Redirect to:', response.redirect_url)
# After redirect, check status (mandatory)
status = client.get_order_status(merchant_order_id=order_id)
print('Payment state:', status.state) # COMPLETED, FAILED, PENDING