Integrate Razorpay in CakePHP 3

|
| By Webner

Step 1: Create RazorPay Account

First of all you need a test account for integration. Click the below link https://dashboard.razorpay.com/#/access/signup and set up a test account there:

razorpay

Step 2: Get your API Keys:

After successful login, you will see your dashboard. In the dashboard click on the “Settings Tab” on the left hand side navigation bar:

razorpay1

When you click on the settings tab you will see below screen:

razorpay2

There are three tabs. Go to the API Keys tab and get your keys from there. You can also download your keys in a csv file. It allows Test/Live both type of accounts. Select the appropriate mode type for which you need api keys

razorpay3

Step 3: Get order id with curl

$razorapi = API_KEY
$razorsecret = SECRET_KEY
$url = 'https://api.razorpay.com/v1/orders';
$data = array("amount" => TOTAL_AMOUNT ,"currency" => "INR","receipt"=>"receipt#1");
$postdata = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$razorapi:$razorsecret");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
curl_close($ch);
$decodedData = json_decode($result, true);
$orderId = $decodedData[‘id’];

Note: Pass this order Id in the form

Step 4: Get Payment Token:

Add below script to your form:
<form action="RETURN_URL" method="POST">
<script
src="https://checkout.razorpay.com/v1/checkout.js"
data-key="API_KEY" // Enter the Key ID generated from the Dashboard
data-amount="TOTAL_AMOUNT" // Amount is in currency subunits. Default currency is INR. Hence, 50000 refers to 50000 paise
data-currency="INR"
data-order_id="ORDER_ID"//This is a sample Order ID. Pass the `id` obtained in the response of the previous step.
data-buttontext="Pay with Razorpay"
data-name="ACCOUNT_HOLDER_NAME"
data-description="Test transaction"
data-prefill.name="USER_NAME"
data-prefill.email="USER_EMAIL"
></script>
</form>

Step 5: You can install Razorpay using a composer or by downloading the required ZIP file.

Run the below command on your composer

composer require razorpay/razorpay:2.*

Step 6: Verify payment

Verify the payment on razorpay redirect URL
$success = false;
try{
$api = new Api(API_KEY,SECRET_KEY);
$attributes = array('razorpay_signature' => $_REQUEST["razorpay_signature"], 'razorpay_payment_id' => $_REQUEST["razorpay_payment_id"] , 'razorpay_order_id' => $_REQUEST["razorpay_order_id"]);
$order = $api->utility->verifyPaymentSignature($attributes);
$success = true;
}
catch(Exception $e){
$success = false;
}
if ($success === true)
{
// save order detail in database
}

Leave a Reply

Your email address will not be published. Required fields are marked *