Codxplore.com

Latest news:

Sahih al-Bukhari (সহীহ বুখারী) is a free Hadith application for android. This application is advertisement free. Download now https://play.google.com/store/apps/details?id=com.akramhossin.bukharisharif

Integrate Razorpay in Yii2


Integrate Razorpay in Yii2

Razorpay is the only payments solution in India that allows businesses to accept, 

process and disburse payments with its product suite. 

It gives you access to all payment modes including credit card, debit card, netbanking, 

UPI and popular wallets including JioMoney, Mobikwik, Airtel Money, FreeCharge, Ola Money and PayZapp.

Now we will try to integrate Razorpay in Yii2 application.

After signing up Razorpay will provide username and password for login to dashboard.

from dashboard we can switch between payment mode to LIVE and Test.

From left menu in Razorpay dashboard, click setting, you will redirect to setting page. 

now click Api Keys Tab. From this page we can generate Key ID and Key Secret

Copy key ID and Key Secret and save to a safe place. 

Note you need 2 Api Keys. One for Live Mode and another one for Test Mode.

Now we will go to https://razorpay.com/docs/api/x/postman-collection/ page and download the postman collection for Razorpay. 

in this postman collection we will find all the necessary apis for Razorpay integration.

Now we will create a function as actionRazorpayPayment in SiteController.php file. 

code as follows:


public function actionRazorpayPayment(){
    $expireStamp = date('Y-m-d H:i:s', strtotime("+25 min"));
    $expiredTime = strtotime($expireStamp);
    $items = [];
    $items['product_name'] = addslashes('Kaya Skin Clinic Daily Pore Minimising Toner');
    $jsonData = [
        'amount' => (float) (245 * 100),
        'currency' => "INR",
        'expire_by' => $expiredTime,
        'reference_id' => (string) time() . '-100005-125-12223',
        'description' => 'Order #100005 payment',
        'customer' => [
            'name' => 'Abc User',
            'contact' => "99999999",
            'email' => 'abc.usr@gmail.com',
        ],
        'notify' => [
            'sms' => true,
            'email' => true,
        ],
        'reminder_enable' => false,
        'callback_url' => \Yii::$app->urlManager->createAbsoluteUrl('site/razorpay-response'),
        'callback_method' => 'get',
        'notes' => $items,
    ];
    $authorization = base64_encode('Key ID:Key Secret');
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => 'https://api.razorpay.com/v1/payment_links',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS => json_encode($jsonData),
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_HTTPHEADER => array(
            'Content-Type: application/json',
            'Authorization: Basic '.$authorization
        ),
    ));
    $response = curl_exec($curl);
    curl_close($curl);
    $result = json_decode($response);
    if (!empty($response)) {
        return $this->redirect($result->short_url);
    } else {
        return $this->redirect(['site/index']);
    }
}


if everything works well, we will be redirect to Razorpay payment page.

https://razorpay.com/docs/payment-gateway/web-integration/standard/#test-payments in this page

we will get all the necessary info for test payment

after successful payment Razorpay will redirect us to callback_url, which we sent 

while sending payment request.

final step, we now we will create a function actionRazorpayResponse

code as follows:


public function actionRazorpayResponse(){
    $authorization = base64_encode('Key ID:Key Secret');
    $param = Yii::$app->request->queryParams;
    if (!empty($param)) {
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://api.razorpay.com/v1/payment_links/' . $param['razorpay_payment_link_id'],
            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_SSL_VERIFYPEER => false,
            CURLOPT_HTTPHEADER => array(
                'Content-Type: application/json',
                'Authorization: Basic '.$authorization
            ),
        ));
        $response = curl_exec($curl);
        curl_close($curl);
        $result = json_decode($response);
        print_r($result); 
    }
}

if everything goes well we will able to see payment details response from Razorpay.

Tags : MySQL, PHP, Yii, Yii2,

Views : 1037

Subscribe Us


Follow Us