Razorpay Payment Gateway Integration using PHP with cURL

Razorpay is a popular payment solution provider available to businesses selling online in India.Razorpay provides clean, fast, secure payments services with hassle free integration. It allows online business to accept and process payments payments modes like including major Credit Cards, Debit Cards, Net-banking, Wallets & UPI. With the easiest integration, completely online onboarding, feature filled checkout and best in class performance, quickly go live with Razorpay and experience the future of payments.
In this tutorial, you will learn how to integrate Razorpay Payment Gateway using PHP with cURL.This is a very simple example, you can just copy paste and change according to your requirement.
Create Account and Integrate Payment Gateway
  • At first Create Account.
  • After successful sign up and generate Key ID and Secret Key
Razorpay Payment Gateway features:
  • All the major payment method like debit, credit card, netbanking
  • Razorpay payment gateway is very safe and secure
  • Easy and hassle free integration
  • Payment gateway kit available on multiple programming language
  • UPI and Multi Bank EMI options
Before started to implement the Razorpay Payment Gateway Integration using PHP with cURL, look files structure:
  • razorpay-payment-gateway-integration-using-php
    • css
      • style.css
    • images
    • templates
      • header.php
      • footer.php
    • constants.php
    • index.php
    • callback.php
    • success.php
    • failed.php
Step 1: Create a file named constants.php and define Key ID and Secret Key
	
Step 2: Create HTML form named index.php

Razorpay Payment Gateway Integration using PHP with cURL

Step 3: Create a file named callback.php
The file handles the payment process.
 $_POST['card_holder_name_id'],
    'merchant_amount' => $_POST['merchant_amount'],
    'merchant_total' => $_POST['merchant_total'],
    'surl' => $_POST['merchant_surl_id'],
    'furl' => $_POST['merchant_furl_id'],
    'currency_code' => $currency_code,
    'order_id' => $_POST['merchant_order_id'],
    'razorpay_payment_id' => $_POST['razorpay_payment_id'],
);

$paymentInfo = $dataFlesh;
$order_info = array('order_status_id' => $_POST['merchant_order_id']);
$amount = $_POST['merchant_total'];
$currency_code = $_POST['currency_code_id'];
// bind amount and currecy code
$data = array(
    'amount' => $amount,
    'currency' => $currency_code,
);
$success = false;
$error = '';
try {
    $ch = get_curl_handle($razorpay_payment_id, $data);
    //execute post
    $result = curl_exec($ch);
    $data = json_decode($result);
   
    $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    if ($result === false) {
        $success = false;
        $error = 'Curl error: ' . curl_error($ch);
    } else {
        $response_array = json_decode($result, true);
        //Check success response
        if ($http_status === 200 and isset($response_array['error']) === false) {
            $success = true;
        } else {
            $success = false;
            if (!empty($response_array['error']['code'])) {
                $error = $response_array['error']['code'] . ':' . $response_array['error']['description'];
            } else {
                $error = 'Invalid Response 
' . $result; } } } //close connection curl_close($ch); } catch (Exception $e) { $success = false; $error = 'Request to Razorpay Failed'; } if ($success === true) { if (!$order_info['order_status_id']) { $json['redirectURL'] = $_POST['merchant_surl_id']; } else { $json['redirectURL'] = $_POST['merchant_surl_id']; } } else { $json['redirectURL'] = $_POST['merchant_furl_id']; } $json['msg'] = ''; } else { $json['msg'] = 'An error occured. Contact site administrator, please!'; } header('Content-Type: application/json'); echo json_encode($json); ?>
Step 4: Put the below script code in index.php file
We will use the chekout.js of razorpay payment gateway for duduction of payment.
	

 
Step 5: Create a file named success.php

Thank You!

Your payment has been received successfully.


Having trouble? Contact us

Continue to homepage







Step 6: Create a file named failed.php

Thank You!

Your transaction has been declined.


Having trouble? Contact us

Continue to homepage







Create header.php and footer.php section of the webpage. The Bootstrap library is used to provide a better UI, so, include it in the header and footer section.
header.php



  
    
  
  
  
  
  
  
  
  
  Razorpay Payment Gateway Integration using PHP with cURL | Web Haunt
  
  
  
  
  
  
  
   


  
  
footer.php