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
- css
Step 1: Create a file named constants.php and define Key ID and Secret Key
1 2 3 4 5 6 |
<?php // credential define('RAZOR_KEY_ID', 'rzp_test_XXXXXXXXXXXXXXXX'); define('RAZOR_KEY_SECRET', 'XXXXXXXXXXXX'); ?> |
Step 2: Create HTML form named index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
<?php require_once "constants.php"; include('templates/header.php'); ?> <form name="razorpay_frm_payment" class="razorpay-frm-payment" id="razorpay-frm-payment" method="post"> <input type="hidden" name="merchant_order_id" id="merchant_order_id" value="12345"> <input type="hidden" name="language" value="EN"> <input type="hidden" name="currency" id="currency" value="INR"> <input type="hidden" name="surl" id="surl" value="http://localhost/razorpay-payment-gateway-integration-using-php/success.php"> <input type="hidden" name="furl" id="furl" value="http://localhost/razorpay-payment-gateway-integration-using-php/failed.php"> <section class="showcase"> <div class="container"> <div class="pb-2 mt-4 mb-2 border-bottom"> <h2>Razorpay Payment Gateway Integration using PHP with cURL</h2> </div> <div class="row align-items-center"> <div class="form-group col-md-6"> <label for="inputEmail4">Amount</label> <input type="text" class="form-control" id="amount" name="amount" placeholder="amount" value="5.00" readonly="readonly"> </div> <div class="form-group col-md-6"> <label for="inputEmail4">Full Name</label> <input type="text" name="billing_name" class="form-control" id="billing-name" Placeholder="Name" required> </div> </div> <div class="row align-items-center"> <div class="form-group col-md-6"> <label for="inputEmail4">Email</label> <input type="email" name="billing_email"class="form-control" id="billing-email" Placeholder="Email" required> </div> <div class="form-group col-md-6"> <label for="inputEmail4">Phone</label> <input type="text" name="billing_phone" class="form-control" id="billing-phone" Placeholder="Phone" required> </div> </div> <div class="row align-items-center"> <div class="form-group col-md-6"> <label for="inputEmail4">Address</label> <input type="text" name="billing_address" class="form-control" Placeholder="Address"> </div> <div class="form-group col-md-6"> <label for="inputEmail4">Country</label> <input type="text" name="billing_country" class="form-control" Placeholder="Country"> </div> </div> <div class="row align-items-center"> <div class="form-group col-md-6"> <label for="inputEmail4">State</label> <input type="text" name="billing_state" class="form-control" Placeholder="State"> </div> <div class="form-group col-md-6"> <label for="inputEmail4">Zipcode</label> <input type="text" name="billing_zip" class="form-control" Placeholder="Zipcode"> </div> </div> <div class="row"> <div class="col"> <button type="button" class="btn btn-success mt-4 float-right" id="razor-pay-now"><i class="fa fa-credit-card" aria-hidden="true"></i> Pay</button> </div> </div> </div> </section> </form> <?php include('templates/footer.php');?> <script src="https://checkout.razorpay.com/v1/checkout.js"></script> <script type="text/javascript"> jQuery(document).on('click', '#razor-pay-now', function (e) { var total = (jQuery('form#razorpay-frm-payment').find('input#amount').val() * 100); var merchant_order_id = jQuery('form#razorpay-frm-payment').find('input#merchant_order_id').val(); var merchant_surl_id = jQuery('form#razorpay-frm-payment').find('input#surl').val(); var merchant_furl_id = jQuery('form#razorpay-frm-payment').find('input#furl').val(); var card_holder_name_id = jQuery('form#razorpay-frm-payment').find('input#billing-name').val(); var merchant_total = total; var merchant_amount = jQuery('form#razorpay-frm-payment').find('input#amount').val(); var currency_code_id = jQuery('form#razorpay-frm-payment').find('input#currency').val(); var key_id = "<?php echo RAZOR_KEY_ID; ?>"; var store_name = 'Web Haunt'; var store_description = 'Payment'; var store_logo = 'https://webhaunt.com/wp-content/uploads/2016/11/webhaunt.png'; var email = jQuery('form#razorpay-frm-payment').find('input#billing-email').val(); var phone = jQuery('form#razorpay-frm-payment').find('input#billing-phone').val(); jQuery('.text-danger').remove(); if(card_holder_name_id=="") { jQuery('input#billing-name').after('<small class="text-danger">Please enter full mame.</small>'); return false; } if(email=="") { jQuery('input#billing-email').after('<small class="text-danger">Please enter valid email.</small>'); return false; } if(phone=="") { jQuery('input#billing-phone').after('<small class="text-danger">Please enter valid phone.</small>'); return false; } var razorpay_options = { key: key_id, amount: merchant_total, name: store_name, description: store_description, image: store_logo, netbanking: true, currency: currency_code_id, prefill: { name: card_holder_name_id, email: email, contact: phone }, notes: { soolegal_order_id: merchant_order_id, }, handler: function (transaction) { jQuery.ajax({ url:'callback.php', type: 'post', data: {razorpay_payment_id: transaction.razorpay_payment_id, merchant_order_id: merchant_order_id, merchant_surl_id: merchant_surl_id, merchant_furl_id: merchant_furl_id, card_holder_name_id: card_holder_name_id, merchant_total: merchant_total, merchant_amount: merchant_amount, currency_code_id: currency_code_id}, dataType: 'json', success: function (res) { if(res.msg){ alert(res.msg); return false; } window.location = res.redirectURL; } }); }, "modal": { "ondismiss": function () { // code here } } }; // obj var objrzpv1 = new Razorpay(razorpay_options); objrzpv1.open(); e.preventDefault(); }); </script> |
Step 3: Create a file named callback.php
The file handles the payment process.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
<?php require_once "constants.php"; // initialized cURL Request function get_curl_handle($payment_id, $data) { $url = 'https://api.razorpay.com/v1/payments/' . $payment_id . '/capture'; $key_id = RAZOR_KEY_ID; $key_secret = RAZOR_KEY_SECRET; $params = http_build_query($data); //cURL Request $ch = curl_init(); //set the url, number of POST vars, POST data curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_USERPWD, $key_id . ':' . $key_secret); curl_setopt($ch, CURLOPT_TIMEOUT, 60); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $params); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); return $ch; } if (!empty($_POST['razorpay_payment_id']) && !empty($_POST['merchant_order_id'])) { $json = array(); $razorpay_payment_id = $_POST['razorpay_payment_id']; $merchant_order_id = $_POST['merchant_order_id']; $currency_code = $_POST['currency_code_id']; // store temprary data $dataFlesh = array( 'card_holder_name' => $_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 <br/>' . $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.
1 |
<script src="https://checkout.razorpay.com/v1/checkout.js"></script> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
<script src="https://checkout.razorpay.com/v1/checkout.js"></script> <script type="text/javascript"> jQuery(document).on('click', '#razor-pay-now', function (e) { var total = (jQuery('form#razorpay-frm-payment').find('input#amount').val() * 100); var merchant_order_id = jQuery('form#razorpay-frm-payment').find('input#merchant_order_id').val(); var merchant_surl_id = jQuery('form#razorpay-frm-payment').find('input#surl').val(); var merchant_furl_id = jQuery('form#razorpay-frm-payment').find('input#furl').val(); var card_holder_name_id = jQuery('form#razorpay-frm-payment').find('input#billing-name').val(); var merchant_total = total; var merchant_amount = jQuery('form#razorpay-frm-payment').find('input#amount').val(); var currency_code_id = jQuery('form#razorpay-frm-payment').find('input#currency').val(); var key_id = "<?php echo RAZOR_KEY_ID; ?>"; var store_name = 'Web Haunt'; var store_description = 'Payment'; var store_logo = 'https://webhaunt.com/wp-content/uploads/2016/11/webhaunt.png'; var email = jQuery('form#razorpay-frm-payment').find('input#billing-email').val(); var phone = jQuery('form#razorpay-frm-payment').find('input#billing-phone').val(); jQuery('.text-danger').remove(); if(card_holder_name_id=="") { jQuery('input#billing-name').after('<small class="text-danger">Please enter full mame.</small>'); return false; } if(email=="") { jQuery('input#billing-email').after('<small class="text-danger">Please enter valid email.</small>'); return false; } if(phone=="") { jQuery('input#billing-phone').after('<small class="text-danger">Please enter valid phone.</small>'); return false; } var razorpay_options = { key: key_id, amount: merchant_total, name: store_name, description: store_description, image: store_logo, netbanking: true, currency: currency_code_id, prefill: { name: card_holder_name_id, email: email, contact: phone }, notes: { soolegal_order_id: merchant_order_id, }, handler: function (transaction) { jQuery.ajax({ url:'callback.php', type: 'post', data: {razorpay_payment_id: transaction.razorpay_payment_id, merchant_order_id: merchant_order_id, merchant_surl_id: merchant_surl_id, merchant_furl_id: merchant_furl_id, card_holder_name_id: card_holder_name_id, merchant_total: merchant_total, merchant_amount: merchant_amount, currency_code_id: currency_code_id}, dataType: 'json', success: function (res) { if(res.msg){ alert(res.msg); return false; } window.location = res.redirectURL; } }); }, "modal": { "ondismiss": function () { // code here } } }; // obj var objrzpv1 = new Razorpay(razorpay_options); objrzpv1.open(); e.preventDefault(); }); </script> |
Step 5: Create a file named success.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php include('templates/header.php'); ?> <section class="showcase"> <div class="container"> <div class="text-center"> <h1 class="display-3">Thank You!</h1> <p class="lead">Your payment has been received successfully.</p> <hr> <p> Having trouble? <a href="mailto:contact@webhaunt.com">Contact us</a> </p> <p class="lead"> <a class="btn btn-primary btn-sm" href="#" role="button">Continue to homepage</a> </p> </div> </div> </section> <br><br><br><br><br><br> <?php include('templates/footer.php');?> |
Step 6: Create a file named failed.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php include('templates/header.php');?> <section class="showcase"> <div class="container"> <div class="text-center"> <h1 class="display-3">Thank You!</h1> <p class="lead text-danger">Your transaction has been declined.</p> <hr> <p> Having trouble? <a href="mailto:contact@webhaunt.com">Contact us</a> </p> <p class="lead"> <a class="btn btn-primary btn-sm" href="#" role="button">Continue to homepage</a> </p> </div> </div> </section> <br><br><br><br><br><br> <?php include('templates/footer.php');?> |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<!DOCTYPE html> <html lang="en"> <head> <link rel="canonical" href="https://www.webhaunt.com/" /> <meta name="author" content="WebHaunt"> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="description" content=""> <meta name="author" content=""> <link rel="icon" href="https://webhaunt.com/wp-content/uploads/2016/11/webhaunt-150x142.png" sizes="32x32" /> <link rel="icon" href="https://webhaunt.com/wp-content/uploads/2016/11/webhaunt.png" sizes="192x192" /> <link rel="apple-touch-icon-precomposed" href="https://webhaunt.com/wp-content/uploads/2016/11/webhaunt.png" /> <meta name="msapplication-TileImage" content="https://webhaunt.com/wp-content/uploads/2016/11/webhaunt.png" /> <title>Razorpay Payment Gateway Integration using PHP with cURL | Web Haunt</title> <!-- Bootstrap core CSS --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" /> <!-- Custom fonts for this template --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.7.2/css/all.min.css" /> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/simple-line-icons/2.4.1/css/simple-line-icons.css" /> <link href="https://fonts.googleapis.com/css?family=Lato:300,400,700,300italic,400italic,700italic" rel="stylesheet" type="text/css"> <!-- Custom styles for this template --> <link href="css/style.css" rel="stylesheet"> </head> <body> <!-- Navigation --> <nav class="navbar navbar-expand-lg navbar-dark bg-dark static-top header-bg-dark" style="background: ##FFFFFF!;"> <div class="container"> <a class="navbar-brand font-weight-bold" href="https://webhaunt.com"><h1>Web Haunt</h1></a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarResponsive"> <ul class="navbar-nav ml-auto"> <li class="nav-item active"> <a class="nav-link" href="https://webhaunt.com/">Home <span class="sr-only">(current)</span> </a> </li> <li class="nav-item"> <a class="nav-link" href="https://webhaunt.com/php-script-free-demo/">LIVE DEMO</a> </li> </ul> </div> </div> </nav> |
footer.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
<!-- Footer --> <footer class="footer bg-light footer-bg-dark"> <div class="container"> <div class="row"> <div class="col-lg-6 h-100 text-center text-lg-left my-auto"> <ul class="list-inline mb-2"> <li class="list-inline-item"> <a href="#">About</a> </li> <li class="list-inline-item">⋅</li> <li class="list-inline-item"> <a href="#">Contact</a> </li> <li class="list-inline-item">⋅</li> <li class="list-inline-item"> <a href="#">Terms of Use</a> </li> <li class="list-inline-item">⋅</li> <li class="list-inline-item"> <a href="#">Privacy Policy</a> </li> </ul> <p class="small mb-4 mb-lg-0" style="color: #E6ECF0;">Copyright © 2017 - <?php print date('Y', time());?> <a style="color: #E6ECF0;" href="https://webhaunt.com/">WEBHAUNT.COM</a> All rights reserved.</p> </div> <div class="col-lg-6 h-100 text-center text-lg-right my-auto"> <ul class="list-inline mb-0"> <li class="list-inline-item mr-3"> <a href="#"> <i class="fab fa-facebook fa-2x fa-fw"></i> </a> </li> <li class="list-inline-item mr-3"> <a href="#"> <i class="fab fa-twitter-square fa-2x fa-fw"></i> </a> </li> <li class="list-inline-item"> <a href="#"> <i class="fab fa-instagram fa-2x fa-fw"></i> </a> </li> </ul> </div> </div> </div> </footer> <!-- Bootstrap core JavaScript --> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/js/bootstrap.bundle.min.js"></script> </body> </html> |