Intent Cancel Request API
Intent Cancel Request API is used by merchants in order to cancel the raised intent generated requests like DQR, Collect Call, etc when the customer asks for the cancellation.
Request Headers
| Header Name | Header Value |
|---|---|
Content-Type | application/json |
X-VERIFY | SHA256(“/v1/intent/{merchantId}/{transactionId}/cancel” + salt key) + ### + salt index |
X-PROVIDER-ID | Used for the cases where the merchants are getting onboarded via their Providers |
X-CALLBACK-URL | Merchants need to pass their Callback URL to receive automated callbacks/ webhooks from Phonepe post performing transactions |
Path Parameters
| Parameter Name | Type | Description | Mandatory |
|---|---|---|---|
merchantId | STRING | Unique Merchant ID assigned to the merchant by PhonePe | Yes |
transactionId | STRING | Unique Transaction ID generated by the merchant to track this request to PhonePe | Yes |
Response Parameters
| Parameter Name | Type | Description |
|---|---|---|
success | BOOLEAN | Success status of the request |
code | ENUM | See below section for list of codes |
message | STRING | Short message about status |
data | OBJECT | Empty object |
Response Codes
The code in the above API response could be.
SUCCESSINTERNAL_SERVER_ERROR: There can be internal server error. Merchant should retry the cancel API request.INVALID_TRANSACTION_IDPAYMENT_ALREADY_COMPLETED: Payment has been succesful hence can’t cancel the request. For this case merchant should trigger successful transaction, generating the bill.
SampleResponseforSuccess
{
"success":true,
"code":"SUCCESS",
"message":"Your request has been successfully completed."
}SampleResponseforFailed
{
"success":false,
"code":"PAYMENT_ALREADY_COMPLETED",
"message":"Payment is already complete."
}Python SampleCode
from django.shortcuts import render
from django.http import HttpResponse
import requests
import base64
import hashlib
from rest_framework.decorators import api_view
import json
from django.http import JsonResponse
import qrcode
import os
txnid = "TEST20231004021525T"
baseUrl = 'https://mercury-uat.phonepe.com/enterprise-sandbox'
MID = 'MERCHANTUAT'
saltkey = 'f1fed176-917c-4c1b-b5ae-1e1d39e1f8d5'
keyindex = '1'
def canceltransaction(request):
merchantId = MID
transactionId = txnid
url = baseUrl + '/v1/intent/' + merchantId + '/' + transactionId + '/cancel'
# for Sha256 calculation
api_saltkey = saltkey
str_forSha256 = '/v1/intent/' + merchantId + '/' + transactionId + '/cancel' + api_saltkey
sha_value = hashlib.sha256(str_forSha256.encode('UTF-8')).hexdigest()
x_verify = sha_value + '###' + keyindex
headers = {
"Content-Type": "application/json",
"X-VERIFY": x_verify,
"x-callback-url":"https://webhook.site/83892277-ac4f-4bc8"
}
print(headers)
print(url)
res = requests.post(url=url, headers=headers)
return HttpResponse(res)