Initiate and Verify Refunds with Python SDK


This section explains how to initiate a refund for a successful transaction using Initiate Refund. You need to provide details like the original transaction ID, refund amount, and refund ID. After initiating the refund, you can use Refund Status to check whether the refund was successful, pending, or failed. These APIs help you manage the full refund lifecycle efficiently. Use them to ensure timely and transparent customer experiences.

Use the RefundRequest.build_refund_request() function to initiate a refund. This ensures the amount is returned to the customer’s original payment method.

You can use the RefundRequest.build_refund_request()  to create the refund request and the following are the attributes that merchant can pass.

Parameter NameData TypeMandatory
(Yes/No)
Description
merchant_refund_idStringYesUnique refund ID assigned by you
original_merchant_order_idStringYesThe original order ID against which the refund is requested
amountIntegerYesRefund amount in paisa. Must be at least 100 paisa and not exceed the order amount
Sample Request
from uuid import uuid4
from phonepe.sdk.pg.payments.v2.standard_checkout_client import StandardCheckoutClient
from phonepe.sdk.pg.common.models.request.refund_request import RefundRequest
from phonepe.sdk.pg.env import Env
 
client_id = "<YOUR_CLIENT_ID>"
client_secret = "<YOUR_CLIENT_SECRET>"
client_version = <CLIENT_VERSION>  # Insert your client version here
env = Env.SANDBOX  # Change to Env.PRODUCTION when you go live
should_publish_events = False
 
client = StandardCheckoutClient.get_instance(client_id=client_id,
                                                              client_secret=client_secret,
                                                              client_version=client_version,
                                                              env=env,
                                                              should_publish_events=should_publish_events)

unique_merchant_refund_id = str(uuid4())
original_merchant_order_id = "<YOUR_ORDER_ID_TO_REFUND>"
amount = 100
refund_request = RefundRequest.build_refund_request(merchant_refund_id=unique_merchant_refund_id,
                                                    original_merchant_order_id=original_merchant_order_id,
                                                    amount=amount)
refund_response = client.refund(refund_request=refund_request)
refund_state = refund_response.state

⚠️ Invalid Refund Amount!


The refund amount cannot exceed the initiated amount. It must always be less than or equal to the amount originally initiated.

The function returns a RefundResponse Object:

Parameter NameData TypeDescription
refund_idStringRefund ID generated by PhonePe PG.
stateStringThe status of the refund.
amountIntegerThe refunded amount (in paisa).

Refund Status is used to retrieve the current status and details of a refund request made against a payment. With the refund ID, you can track whether the refund is pending, successful, or failed, enabling you to manage and update refund workflows accordingly.

Request Parameters
Parameter NameData TypeMandatory
(Yes/No)
Description
refund_idStringYesRefund ID assigned by you at the time of initiation
Sample Request
from uuid import uuid4
from phonepe.sdk.pg.payments.v2.standard_checkout_client import StandardCheckoutClient
from phonepe.sdk.pg.env import Env
 
client_id = "<YOUR_CLIENT_ID>"
client_secret = "<YOUR_CLIENT_SECRET>"
client_version = <CLIENT_VERSION>  # Insert your client version here
env = Env.SANDBOX  # Change to Env.PRODUCTION when you go live
should_publish_events = False
 
client = StandardCheckoutClient.get_instance(client_id=client_id,
                                                              client_secret=client_secret,
                                                              client_version=client_version,
                                                              env=env,
                                                              should_publish_events=should_publish_events)

unqiue_merchant_refund_id = "<INSERT_YOUR_REFUND_ID>"  # replace with your refund id
refund_response = client.get_refund_status(merchant_refund_id=unqiue_merchant_refund_id)
refund_state = refund_response.state

It returns a RefundStatusResponse Object.

RefundStatusResponse
Parameter NameData TypeDescription
merchant_idStringYour merchant ID
merchant_refund_idStringThe refund ID assigned by you
original_merchant_order_idStringThe order ID for which the refund was initiated
amountIntegerRefund amount (in paisa)
stateStringThe current status of the refund
payment_detailslist<PaymentRefundDetail>List of transaction attempts related to the refund

The PaymentRefundDetail property contains a list of payment details for each payment attempt made against an order. The details of each payment are explained in the table below:

Parameter NameData TypeDescription
transaction_idStringThe transaction ID generated by PhonePe PG.
payment_modeStringThe payment method used:
UPI_INTENT
UPI_COLLECT
UPI_QR
CARD
TOKEN
NET_BANKING
timestampIntegerThe timestamp of the transaction attempt in epoch.
stateStringStatus of the transaction:
PENDING
COMPLETED
FAILED
error_codeStringError code(only if the transaction failed)
detailed_error_codeStringA more detailed error code(only if the transaction failed)
split_instrumentslist<InstrumentCombo>Payment instruments used:
ACCOUNT
CREDIT_CARD
DEBIT_CARD
NET_BANKING

In this section, you’ve learned how to initiate a refund and check its status. In the next section, you’ll understand how payment verification is handled using Webhooks, and how to manually verify the payment in case the webhook callback fails.

Is this article helpful?