3. Handle Return URL and Confirm Status

Learn in detail about handling the return URL and how to confirm the payment status.

Your customer can complete the payment in two ways,

  • Your customer is redirected to a third-party page such as: bank OTP page, app login page, etc.
    (or)
  • Your customer needs to approve the payment through an asynchronous process, for example: by approving a payment request on their phone.

While the first method requires redirection to the third-party page, the second method does not. In case the customer is redirected from your website to a third-party page, you would also want them to return back to your application. The return_url provided in the create order API call is used for this.

🚧

Return URL

Once your customer has completed the payment on the bank's OTP page, we will redirect them back to your return_url. This will be a link-based redirection, so if you have provided a return_url of the format - https://b8af79f41056.eu.ngrok.io?order_id={order_id} - your customer will be redirected to the URL - https://b8af79f41056.eu.ngrok.io?order_id=order_271vfuhh1o4h6bQIigqyOx74YiJ1T

Once you receive a request on this URL, you must first confirm if the order_id belongs to the same order.
The last step is to check the status of the order in Cashfree Payments system and update the order at your end. You can do it through a simple GET request as shown below.

curl --request GET \
  --url https://sandbox.cashfree.com/pg/orders/order_271vWwzSQOHe01ZVXpEcguVxQSRqr \
  --header 'Content-Type: application/json' \
  --header 'x-api-version: <<x-api-version>>' \
  --header 'x-client-id: <appID>' \
  --header 'x-client-secret: <secret Key>'
{
  "cf_order_id": 1539553,
  "created_at": "2021-07-19T16:13:35+05:30",
  "customer_details": {
    "customer_id": "7112AAA812234",
    "customer_name": null,
    "customer_email": "[email protected]",
    "customer_phone": "9908734801"
  },
  "entity": "order",
  "order_amount": 5.01,
  "order_currency": "INR",
  "order_expiry_time": "2021-08-18T16:13:34+05:30",
  "order_id": "order_271vWwzSQOHe01ZVXpEcguVxQSRqr",
  "order_meta": {
    "return_url": "https://b8af79f41056.eu.ngrok.io?order_id={order_id}",
    "notify_url": "https://b8af79f41056.eu.ngrok.io/webhook.php",
    "payment_methods": null
  },
  "order_note": null,
  "order_status": "PAID",
  "payment_session_id": "session_VnheAG4KDGWpJ2qI936z5aaZ3cJFe4jOtj0hDp7x9I49RQzucQ0b2hvi5axrMRqprXmTPegGBG15f3fl76aueRvnSFVNhw7AkUxPaDmQPoVv",
  "payments": {
    "url": "https://sandbox.cashfree.com/pg/orders/order_271vWwzSQOHe01ZVXpEcguVxQSRqr/payments"
  },
  "refunds": {
    "url": "https://sandbox.cashfree.com/pg/orders/order_271vWwzSQOHe01ZVXpEcguVxQSRqr/refunds"
  },
  "settlements": {
    "url": "https://sandbox.cashfree.com/pg/orders/order_271vWwzSQOHe01ZVXpEcguVxQSRqr/settlements"
  }
}

There are a few things you should ensure:

  1. Make sure you check the order status. The order_status can be one of ACTIVE, PAID or EXPIRED. Only process successful orders that are still in the ACTIVE status. If an order is in the PAID status, you do not want to re-order it again.
  2. You must fetch the order status from your backend using the /orders/:orderID API call as shown below.
<?php

$orderId = "order_HDUp8wlq";
$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://sandbox.cashfree.com/pg/orders/$orderId",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET',
  CURLOPT_HTTPHEADER => array(
    'x-client-id: {{appId}}',
    'x-client-secret: {{secretKey}}',
    'x-api-version: 2021-05-21'
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if(!$err) {
	$result = json_decode($response, true);
	if($result["order_status"] == 'PAID'){
		echo "This order is paid!";
	} else {
		echo "Order has not been paid!";
	}
} else {
	echo  $err;
}

?>
curl --request GET \
  --url https://sandbox.cashfree.com/pg/orders/order_1471yUGAj97hSGoOB \
  --header 'Content-Type: application/json' \
  --header 'x-api-version: 2021-05-21' \
  --header 'x-client-id: {{appId}}' \
  --header 'x-client-secret: {{secretKey}}'
  
  // response
  {
  "cf_order_id": 594193729,
  "created_at": "2021-09-22T13:46:51+05:30",
  "customer_details": {
    "customer_id": "P9999711",
    "customer_name": null,
    "customer_email": "[email protected]",
    "customer_phone": "9899049110"
  },
  "entity": "order",
  "order_amount": 1.00,
  "order_currency": "INR",
  "order_expiry_time": "2021-10-22T13:46:51+05:30",
  "order_id": "order_1471yUGAj97hSGoOB",
  "order_meta": {
    "return_url": "http://localhost:1774/resp.php?order_id={order_id}&order_token={order_token}",
    "notify_url": "https://b8af79f41056.eu.ngrok.io/webhook.php",
    "payment_methods": null
  },
  "order_note": null,
  "order_splits": [],
  "order_status": "PAID",
  "order_tags": null,
  "order_token": "9AtvrNXIXgQqFsJbaRVc",
  "payment_link": "https://payments.cashfree.com/order/#9AtvrNXIXgQqFsJbaRVc",
  "payments": {
    "url": "https://api.cashfree.com/pg/orders/order_1471yUGAj97hSGoOB/payments"
  },
  "refunds": {
    "url": "https://api.cashfree.com/pg/orders/order_1471yUGAj97hSGoOB/refunds"
  },
  "settlements": {
    "url": "https://api.cashfree.com/pg/orders/order_1471yUGAj97hSGoOB/settlements"
  }
}

Ask a question about Integration

You can ask questions, and participate in discussions about Cashfree Payments PG API in the Cashfree Discord channel.