Read backend payment response
In order to receive payment result from 2C2P PGW, merchant required register return URL under 2C2P Merchant Dashboard.
Here are 4 steps to getting payment token:
- Step 1: Register merchant return URL.
- Step 2: Get payment response from POST method.
- Step 3: Verify response signature.
- Step 4: Get payment result.
Merchant server implementation : Download
Step 1: . Register merchant return URL.
- Payment result will be posted to merchant's Return URL.
- To register merchant's Return URL,
- Login to Merchant Dashboard, under Account > Options > Payment Result URL.
- Set Redirect API - Backend return URL.
Return URL | Description |
Redirect API - Backend return URL | A merchant backend return url for let 2C2P PGW to notify merchant regarding payment result after payment completed. Therefore, merchant can process particular payment result at their backend. |
Step 2: . Get payment response from POST method.
$encoded_payment_response = urldecode($_REQUEST["paymentResponse"]);
Step 3: Verify response signature.
$is_valid_signature = $pgw_helper->validateSignature($encoded_payment_response, $secret_key);
if($is_valid_signature) {
//Valid signature, Get payment result.
} else {
//Invalid signature, return error response
}
IMPORTANT
- Merchant must always validate signature value of response returned by 2C2P API to ensure integrity.
$payment_response = $pgw_helper->parseAPIResponse($encoded_payment_response);
$invoice_no = $payment_response->invoiceNo;
$resp_code = $payment_response->respCode;
Complete Code
Copy & Paste below source code and put this file in your Web Server and register the URL in Merchant Dashboard as 'return URL'.
<?php
//Import necessary classes
require "../utils/PaymentGatewayHelper.php";
//Get SecretKey from 2C2P PGW dashboard
$secret_key = "7jYcp4FxFdf0";
//Get payment response from POST method
$encoded_payment_response = urldecode($_REQUEST["paymentResponse"]);
//Important: Generate signature
//Init 2C2P PaymentGatewayHelper
$pgw_helper = new PaymentGatewayHelper();
//Important: Verify response signature
$is_valid_signature = $pgw_helper->validateSignature($encoded_payment_response, $secret_key);
if($is_valid_signature) {
//Parse payment response and convert JSON to std object
$payment_response = $pgw_helper->parseAPIResponse($encoded_payment_response);
//Get payment result
echo $invoice_no = $payment_response->invoiceNo;
echo "\n";
echo $resp_code = $payment_response->respCode;
} else {
//Return invalid response message and dont trust this payment response.
echo "Payment response has been modified by middle man attack, do not trust and use this payment response. Please contact 2c2p support.";
}
?>
Updated over 3 years ago