How to integrate Razorpay Payment Gateway in PHP - Payment Gateway Tutorial

By Deepak Sharma | May, 30 2020 08:15

Hello Friends today I am talking about how we can integrate Razorpay Payment Gateway in PHP Based website, in simply and easy way with video tutorial. Believe me,  most of the time we are facing problem during integration of any type of payment gate way, but it is not much hard as we think. Today I shall tell you to integrate Razorpay Payment Gateway in PHP websites. Steps as follow :
  1. First of all we required razorpay merchant account.
  2. Login into your marchant account.
  3. Now to settings -> API keys -> generate test key, now export keys in excel for future requirement.
  4. Now download Razorpay php kit from github url : https://github.com/razorpay/razorpay-php/releases download razorpay-php.zip from 2.5.0 latest release.</li> <li>Now create database and tables to store response from payment gateway (razorpay db name) create it as per your requirement. </li> <li>Now create table payment_laser and add fields as pay_id, payment_id, order_id, signature_hash, created_at.</li> <li>Now extract download razorpay-php.zip kit into your wamp->www/xamp->htdocs folder and extract all files.</li> <li>Now create database config connection with your website.</li> <li> <xmp><?php $conn = mysqli_connect("localhost","root","","razorpay"); // Check connection if (mysqli_connect_errno()) {   echo "Failed to connect to MySQL: " . mysqli_connect_error();   exit(); } ?>
  5. Now goto project root folder ans create new file called demo.php.
  6. and write/past code below : 
  7. <html>     <head>         <title>How to integrate Razorpay Payment Gateway in PHP | Payment Gateway Tutorial</title>         <style>             .booking-div {                 padding: 15px;                 box-shadow: inset 0 0 0 1px #e0e5e9;                 border: 5px solid #fbfbfc;                 -webkit-border-radius: 5px;                 -moz-border-radius: 5px;                 border-radius: 5px;                 margin-bottom: 30px;                 background-color: #fff;                 width: 350px;                 margin: 0% 10%;             }             .b-fields .b-f-div label {                 background: #dbdbdb;                 font-size: 12px;                 margin-top: 0;                 position: absolute;                 padding: 10px;                 border-radius: 5px 0 0 5px;                 color: #787878;                 width: 60px;                 text-align: center;             }             input{                 width: 100%;                 height: 35px;                 border-radius: 4px;                 border: 1px solid #528ff0;                 padding-left: 5px;             }         </style>     </head>     <body style="    background-image: -webkit-linear-gradient(90deg,#528ff0,#528ff096,#528ff02b);">     <center>         <div style="margin: 8% 0 0">             <h1 style="font-size: 40px;font-family: sans-serif;">How to integrate Razorpay Payment Gateway in PHP <br> Payment Gateway Tutorial</h1>         </div>         <div class="booking-div">              <div class="booking-fields">                 <form action="pay.php" method="post">                     <table style="width: 100%;    font-family: sans-serif; text-align: left;color: #345d9f;">                         <tbody>                             <tr>                                 <th>Customer Name</th>                             </tr>                             <tr>                                 <td><input type="text" name="CUSTOMER_NAME" value="Yogendra Tomar"></td>                             </tr>                             <tr>                                 <th>Customer Email</th>                             </tr>                             <tr>                                 <td><input type="text" name="CUSTOMER_EMAIL" value="info@myinboxhub.co.in"></td>                             </tr>                             <tr>                                 <th>Customer Mobile</th>                             </tr>                             <tr>                                 <td><input type="text" name="CUSTOMER_MOBILE" value="Youtube"></td>                             </tr>                             <tr>                                 <th>Payment Amount (In RazorPay Amt to be in subunit of current. Eg. 10000 = Rs.100)</th>                             </tr>                             <tr>                                 <td><input type="text" name="PAY_AMT" value="10000"></td>                             </tr>                             <tr>                                 <td><input type="submit" value="Pay Now"></td>                             </tr>                         </tbody>                     </table>                 </form>             </div>         </div>     </center> </body> </html>
  8. Now again to route folder and create file called pay.php;
  9. And Write or paste code as below :
  10. <?php require_once 'configDB.php'; require_once 'razorpay-php/Razorpay.php'; use Razorpay\Api\Api; $keyId = 'XXXXXXXXXXXXX'; $secretKey = 'XXXXXXXXXXXXXXX'; $api = new Api($keyId, $secretKey); $CUSTOMER_NAME = $_POST['CUSTOMER_NAME']; $CUSTOMER_EMAIL = $_POST['CUSTOMER_EMAIL']; $CUSTOMER_MOBILE = $_POST['CUSTOMER_MOBILE']; $PAY_AMT = $_POST['PAY_AMT']; /*  * To create order to RazorPay  */ $order = $api->order->create(array(     'receipt' => rand(1000, 9999) . 'ORD',     'amount' => $PAY_AMT,     'payment_capture' => 1,     'currency' => 'INR',         ) ); ?> <meta name="viewport" content="width=device-width"> <!-- This form provided by the payment gated --> <form action="success.php" method="POST">     <script         src="https://checkout.razorpay.com/v1/checkout.js"         data-key="<?php echo $keyId ?>"           data-amount="<?php echo $order->amount ?>"          data-currency="INR"         data-order_id="<?php echo $order->id ?>"          data-buttontext="Pay with Razorpay"         data-name="Myinboxhub"         data-description="For Donation"         data-image="<?php echo 'https://myinboxhub.co.in/data/logo/logo.png'; ?>"         data-prefill.name="<?php echo $CUSTOMER_NAME; ?>"         data-prefill.email="<?php echo $CUSTOMER_EMAIL; ?>"         data-prefill.contact="<?php echo $CUSTOMER_MOBILE; ?>"         data-theme.color="#f0a43c"     ></script>     <input type="hidden" custom="Hidden Element" name="hidden"> </form>
  11. Wait replace your keys : with your razorpay merchant api key: we download them in STEP : 3
  12.  $keyId = 'XXXXXXXXXXXXX'; $secretKey = 'XXXXXXXXXXXXXXX';
  13. Now again goto project root folder and create file called success.php;
  14. And Write/Paste code as below : 
  15. <?php require_once 'configDB.php'; echo '<xmp>'; print_r($_POST); $sql = "INSERT INTO payment_laser (payment_id, order_id, signature_hash) VALUES ('".$_POST['razorpay_payment_id']."', '".$_POST['razorpay_order_id']."', '".$_POST['razorpay_signature']."')"; if ($conn->query($sql) === TRUE) {     echo "New record created successfully"; } else {     echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close(); die; ?>
  16. Now goto browser and open your project. 
  17. Now fill all fields and click on pay button. Then make dummy payment with razorpay and make is success using netbanking option. then you redirect to success.php and check out into your datbase table or new payment transaction log 
  18. Thanks you read this article. For demo tutorial check out video on this link <br> How to integrate Razorpay Payment Gateway Video Tutorial
   

0 comments

No comment posted yet

Leave a comment