How to fetch payment details using Razorpay PHP API?

Arijit Aich picture Arijit Aich · Mar 12, 2017 · Viewed 8.9k times · Source

I am trying to integrate the code for fetching payment details.

billno: This value will be taken from the URL.

My current code:

include 'razorpay/Razorpay.php';
use Razorpay\Api\Api;

$api = new Api('Secret ID', 'Secret Key');

$payment = $api->payment->fetch($_REQUEST['billno']);
$text =  json_encode($payment->toArray());
$obj = json_decode($text);
$shopping_id = $obj->{'notes'}->{'shopping_id'};

$rzp_amount = $obj->{'amount'};//xheck
$real_amount = $rzp_amount/100;
$rzp_key = $obj->{'id'};
$rzp_status = $obj->{'status'}; //Authorised cgecj
$rzp_descp = $obj->{'description'};
$rzp_mail = $obj->{'email'};
$rzp_phone = $obj->{'contact'};
$rzp_address = $obj->{'notes'}->{'address'};
$rzp_timestamp = $obj->{'created_at'};
$rzp_method = $obj->{'method'};

Extracting the billno using the code would give us the above mentioned $rzp variables.

Answer

Nemo picture Nemo · Mar 14, 2017

The payment->fetch call returns a Payment Entity, and has all the data members available for direct access. You don't have to decode JSON yourselves, that is taken care of by the SDK:

include 'razorpay/Razorpay.php';
use Razorpay\Api\Api;

$api = new Api('Secret ID', 'Secret Key');

$payment = $api->payment->fetch($_REQUEST['billno']);

echo $payment->amount;
print_r($payment->notes);

Disclaimer: I work for Razorpay