Upi payment gateway integration in php/laravel In this post we will see a brief description with an example. I will provide step-by-step guidelines for the integration UPI payment gateway in your php application. There we need to register as in upi service provider. Here i am taking upigateway.com provider. First, you go to the given link and register there, after that make your merchant account and link with your bank account that's it, Then after you will receive an API KEY from the provider.
Unified Payments Interface (UPI) is a real-time payment system developed by the National Payments Corporation of India (NPCI). It facilitates interbank transactions by instantly transferring funds between two bank accounts on a mobile platform. UPI is designed to provide a seamless and user-friendly way to make payments and carry out financial transactions directly from a smartphone.
Here's how the UPI payment gateway works:
- 1. Mobile Application
- 2. Registration
- 3. Transaction Initiation
- 4. Two-Factor Authentication
- 5. Instant Settlemen
- 6. 24/7 Availability
- 7. Multiple Bank Accounts
- 8. Transaction History
Above all services provided by UPI service proided. Here i will give you a example and youtube tutorial how to integrate upi payment gateway in your ecommerce application with following simple steps.
Why You Should Use UPI :
Above all services provided by UPI service provided. Here I will give you an example and youtube tutorial on how to integrate upi payment gateway in your e-commerce application with the following simple steps.
UPI Payment gateway integration In this post we will finish in just three steps, in the first step we will create a customer registration form. Once the user fills in all the details and submits we will Redirect the request to the UPI payment service provider and then a QR code will be generated and that QR code user will scan and pay the payment, then after the request will redirect back to given callback url.
Step 1: Form & Redirection
Create a index file in your php app. Here i am using cURL giving exmaple. If you are using laravel framwork you cal use HTTP client request as well.
In this file, I will create a form with basic user details, and that form action we will redirect to the redirect file that we will create next. Just copy this form paste it in your file and save it, now we will move next.
index.php<!DOCTYPE html> <html> <head> <title>UPI Gateway - Webappfix</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> </head> <body> <div class="container p-5"> <div class="row"> <div class="col-md-7 mb-2"> <?php if (isset($_POST['payment'])) { $key = "YOU API KEY"; // Your Api Token https://merchant.upigateway.com/user/api_credentials $post_data = new stdClass(); $post_data->key = $key; $post_data->client_txn_id = (string) rand(100000, 999999); // you can use this field to store order id; $post_data->amount = $_POST['txnAmount']; $post_data->p_info = "product_name"; $post_data->customer_name = $_POST['customerName']; $post_data->customer_email = $_POST['customerEmail']; $post_data->customer_mobile = $_POST['customerMobile']; $post_data->redirect_url = "https://yourdomain.com/redirect_page.php"; // automatically ?client_txn_id=xxxxxx&txn_id=xxxxx will be added on redirect_url $post_data->udf1 = "extradata"; $post_data->udf2 = "extradata"; $post_data->udf3 = "extradata"; $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => 'https://merchant.upigateway.com/api/create_order', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => json_encode($post_data), CURLOPT_HTTPHEADER => array( 'Content-Type: application/json' ), )); $response = curl_exec($curl); curl_close($curl); $result = json_decode($response, true); if ($result['status'] == true) { echo '<script>location.href="' . $result['data']['payment_url'] . '"</script>'; exit(); } echo '<div class="alert alert-danger">' . $result['msg'] . '</div>'; } ?> <h2>Test Demo</h2> <span>Fill Payment Detail and Pay</span> <hr> <form action="" method="post"> <h4>Txn Amount:</h4> <input type="text" name="txnAmount" value="1" class="form-control" placeholder="Enter Txn Amount" readonly><br> <h4>Customer Name:</h4> <input type="text" name="customerName" placeholder="Enter Customer Name" class="form-control" required><br> <h4>Customer Mobile:</h4> <input type="text" name="customerMobile" placeholder="Enter Customer Mobile" maxlength="10" class="form-control" required><br> <h4>Customer Email:</h4> <input type="email" name="customerEmail" placeholder="Enter Customer Email" class="form-control" required><br> <input type="submit" name="payment" value="Payment" class="btn btn-primary"> </form> </div> </div> </div> </body> </html>
Step 2: Redirect Page
Once submitted user fills in the basic details form and redirects to this page. On this page, we will make a cURL request. This request we will send this to the UPI service provider. After copying this code you need to add api secrate key, That api key will provided by the service provider.
redirect_page.php<?php error_reporting(E_ERROR | E_PARSE); $echo = ""; if (isset($_GET['client_txn_id'])) { $key = ""; // Your Api Token https://merchant.upigateway.com/user/api_credentials $post_data = new stdClass(); $post_data->key = $key; $post_data->client_txn_id = $_GET['client_txn_id']; // you will get client_txn_id in GET Method $post_data->txn_date = date("d-m-Y"); // date of transaction $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => 'https://merchant.upigateway.com/api/check_order_status', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => json_encode($post_data), CURLOPT_HTTPHEADER => array( 'Content-Type: application/json' ), )); $response = curl_exec($curl); curl_close($curl); $result = json_decode($response, true); if ($result['status'] == true) { // Txn Status = 'created', 'scanning', 'success','failure' if ($result['data']['status'] == 'success') { $echo = '<div class="alert alert-danger"> Transaction Status : Success</div>'; $txn_data = $result['data']; // All the Process you want to do after successfull payment // Please also check the txn is already success in your database. } $txn_data = $result['data']; $echo = '<div class="alert alert-danger"> Transaction Status : ' . $result['data']['status'] . '</div>'; } } ?> <!DOCTYPE html> <html> <head> <title>Payment Gateway - Test Response</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> </head> <body> <div class="container p-5"> <div class="row"> <div class="col-md-8 mb-2"> <h2>Response</h2> <p>Payment Gateway - Test Response</p> <?php echo $echo; // show table of response if (isset($txn_data)) { echo '<table class="table table-bordered"> <thead> <tr> <th>Key</th> <th>Value</th> </tr> </thead> <tbody>'; foreach ($txn_data as $key => $value) { echo '<tr> <td>' . $key . '</td> <td>' . @$value . '</td> </tr>'; } echo '</tbody> </table>'; } ?> </div> </div> </div> </body> </html>
Step 3: Response Handle
This is a Response Handle code example. once the payment is successfully made or the payment is in case failed order that time request will redirect to our given Url page. I created a callback.php file. Copy this file and create it.
callback.php<?php // This is the callback file which will be called after payment is done directly from UPI gateway server. // you can set the webhook url at https://merchant.upigateway.com/user/api_credentials // You can also use IP check to prevent unauthorized access. // $ip = $_SERVER['REMOTE_ADDR']; // if($ip != '101.53.134.70'){ // die('Unauthorized Access'); // } if(isset($_POST['id']) && $_POST['client_txn_id']){ $id = $_POST['id']; // upi gateway transaction id $customer_vpa = $_POST['customer_vpa']; // upi id from which payment is made $amount = $_POST['amount']; // 1 $client_txn_id = $_POST['client_txn_id']; // client_txn_id set while creating order $customer_name = $_POST['customer_name']; // $customer_email = $_POST['customer_email']; // $customer_mobile = $_POST['customer_mobile']; // $p_info = $_POST['p_info']; // p_info set while creating order $upi_txn_id = $_POST['upi_txn_id']; // UTR or Merchant App Transaction ID $status = $_POST['status']; // failure $remark = $_POST['remark']; // Remark of Transaction $udf1 = $_POST['udf1']; // user defined data added while creating order $udf2 = $_POST['udf2']; // user defined data added while creating order $udf3 = $_POST['udf3']; // user defined data added while creating order $redirect_url = $_POST['redirect_url']; // redirect_url added while creating order $txnAt = $_POST['txnAt']; // 2023-05-11 date of transaction $createdAt = $_POST['createdAt']; // 2023-05-11T12%3A15%3A23.000Z if($_POST['status']){ echo "Transaction Successful"; // All the Process you want to do after successfull payment // Please also check the txn is already success in your database. } if($_POST['status'] == 'failure'){ echo "Transaction Failed"; } } ?>
We always thanks to you for reading our blogs.
Dharmesh Chauhan
(Swapinfoway Founder)Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us
Haresh Chauhan
(Co-Founder)We Are Also Recommending You :
- How To Configuration Laravel Supervisor
- Error : Using $this when not in object context update column laravel
- Laravel 9 Create Custom Auth Guard Multi Login Authentication System Tutorial
- Laravel 9 Telescope Integration Tutorial
- Error : Array to string conversion Laravel
- Laravel 6 validation with error message
- Microsoft Authentication Implement on Laravel App
- Arr::accessible() | Laravel Helper Function
- How to setup Google's Two Factor Authentication in Laravel
- How To Connect PostgreSQL With Laravel Application