Introduction
PhonePe is a popular digital payments platform in India, offering a range of payment solutions for both individuals and businesses. If you're looking to integrate PhonePe payments into your application or website, this guide will walk you through the process of using the PhonePe API and integrating the PhonePe gateway.
You can check the complete code here.
You can view the phonepe api docs here.
Step 1: Sign Up for a PhonePe Merchant Account
To start using the PhonePe API, you'll need to sign up for a merchant account on the PhonePe website. This will provide you with the necessary credentials and access to the API documentation.
Business Registration - https://www.phonepe.com/business-solutions/payment-gateway/
Step 2: Set Up Your Development Environment
Before you can start integrating the PhonePe API, you'll need to set up your development environment. This typically involves installing any required dependencies and setting up a project structure.
Here we will be looking at the python example But you can use any other language because we are not using SDK here.
Step 3: Obtain API Credentials
Once you have your merchant account set up, you'll need to obtain the API credentials. These credentials will be used to authenticate your requests to the PhonePe API.
For testing, you can follow the examples -https://developer.phonepe.com/v1/docs/uat-testing/
The given examples here are for merchant_id
and salt_key
might not work.
So, try these credentials for testing -
merchant_id - PGTESTPAYUAT86
salt_index - 1
salt_key - 96434309-7796-489d-8924-ab56988a6076
Step 4: Understand the PhonePe API Endpoints
The PhonePe API provides several endpoints that you can use to initiate payments, check payment status, and manage other aspects of your integration. It's important to understand the different endpoints and their respective parameters to ensure a smooth integration process.
Step 5: Implement the Payment Flow
To initiate a payment using the PhonePe API, you'll need to make a POST request to the /pg/v1/pay endpoint. This endpoint requires various parameters such as the payment amount, merchant ID, merchant transaction ID, and callback URL.
Here's an example code snippet in Python that demonstrates how to initiate a payment using the PhonePe API:
import hashlib
import requests
import base64
import uuid
import json
import constants
def create_sha256_string(input_string):
sha256_hash = hashlib.sha256(input_string.encode())
encoded_string = sha256_hash.hexdigest()
return encoded_string
def string_to_base64(input_string):
encoded_string = base64.b64encode(input_string.encode())
return encoded_string.decode()
def phonepePaymentURL(amount: int):
orderID = "pp-"+str(uuid.uuid4())
userID = "user-"+str(uuid.uuid4())
merchantTransactionID = "MT"+str(uuid.uuid4())
mobileNumber = "9999999998" # test mobile number
email = "test@gmai.com"
payload = {
"amount": amount*100,
"merchantId": constants.merchant_id,
"merchantTransactionId": merchantTransactionID,
"merchantUserId": userID,
"redirectUrl": constants.webhook_url,
"redirectMode": "POST",
"callbackUrl": constants.webhook_url,
"merchantOrderId": orderID,
"mobileNumber": mobileNumber,
"email": email,
"message": "Payment for " + orderID,
"paymentInstrument": {
"type": "PAY_PAGE"
}
}
json_data = json.dumps(payload)
base64_request = string_to_base64(json_data)
# X-VERIFY header -- SHA256(Base64 encoded payload + “/pg/v1/pay” + salt key) + ### + salt index
finalXHeader = create_sha256_string(base64_request + "/pg/v1/pay" + constants.salt_key)+"###"+constants.salt_index
req = {
"request": base64_request
}
finalHeader = {
"Content-Type": "application/json",
"X-VERIFY": finalXHeader
}
response = requests.post(constants.payment_url, headers=finalHeader, json=req)
if response.status_code == 200:
return response.json()
else:
return "Something went wrong - " + response.text
res = phonepePaymentURL(100)
data = res.json()
print(json.dumps(data))
print()
paymentURL = data["data"]["instrumentResponse"]["redirectInfo"]["url"]
transactionID = data["data"]["merchantTransactionId"]
print("transaction_id - ",transactionID)
print("payment_url - ",paymentURL)
print()
In this example, we generate a unique order ID, user ID, and merchant transaction ID. We then construct a payload with the necessary parameters and encode it in base64.
We also generate an X-VERIFY header using the payload, endpoint, and salt key. Finally, we make a POST request to the PhonePe API endpoint with the appropriate headers and payload.
Open the payment_url
in browser and use these test card details for payments - https://developer.phonepe.com/v1/docs/uat-testing/#Debit-Card
Step 6: Handle Payment Callbacks
After initiating a payment, PhonePe will send a callback to the specified callback URL. It's important to handle this callback properly to ensure a seamless payment experience. The callback will typically contain information about the payment status, transaction ID, and other relevant details.
For testing webhook, this website can be used - https://webhook.site/
Top comments (0)