PayUMoney Payment Gateway Integration in PHP
A payment gateway is an online service that authorizes and processes payments for online businesses.PayUMoney is the most polular payment Gateway in India. Easily integrate PayUMoney in your Web Application to let your customer to pay through Credit Cards, Debit Cards, Net Banking etc. In this tutorial how to integrate PayUmoney Payment Gateway in your Web Application.A simple code explained in this tutorial will guide you to enable PayUMoney payment for your application.
Steps for integration PayUMoney Payment Gateway.
Step 1: Go to PayUmoney developer guide and then to website integration
Click on PHP Kit for download and Extract the downloaded zip which contains 2 files: index.php and response.php. Deploy these files on your server
Step 2: Create Account
Sign up as a merchant/wallet account.
Before you get started, take a look at the folders and files structure of PayUMoney Payment Gateway Integration in PHP
- payumoney-payment-gateway-integration-php
- css
- style.css
- images
- templates
- header.php
- footer.php
- config.php
- index.php
- request.php
- response.php
- css
Step 3: At time of sign up, you need to use your valid email address. As you are creating test account, you can enter whatever details for the live/testing purpose.
PayUmoney Merchant Test Key & Test Salt Credentials (Copy paste these credentials
config.php
)
1 2 3 4 5 |
<?php // Test Key and Test Salt define('MERCHANT_KEY', "xxxxxxxx"); define('SALT', "xxxxxxxx"); ?> |
Step 4:Create a file request.php and index.php
Calculate the request hash on the server and pass it
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php if(strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') == 0){ //Request hash $contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : ''; if(strcasecmp($contentType, 'application/json') == 0){ $data = json_decode(file_get_contents('php://input')); $hash=hash('sha512', $data->key.'|'.$data->txnid.'|'.$data->amount.'|'.$data->pinfo.'|'.$data->fname.'|'.$data->email.'|||||'.$data->udf5.'||||||'.$data->salt); $json=array(); $json['success'] = $hash; echo json_encode($json); } exit(0); } ?> |
index.php
For test integration use URL:
https://sboxcheckout-static.citruspay.com/bolt/run/bolt.min.js
For live integration use URL:
https://checkout-static.citruspay.com/bolt/run/bolt.min.js
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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
<?php include_once('config.php'); include('templates/header.php'); ?> <?php function getCallbackUrl() { $protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://"; return $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . 'response.php'; } ?> <section class="showcase"> <div class="container"> <div class="pb-2 mt-4 mb-2 border-bottom"> <h2>PayUMoney Payment Gateway Integration in PHP</h2> </div> <div class="row"> <div class="col-md-12 gedf-main"> <form action="#" id="payment_form"> <input type="hidden" id="udf5" name="udf5" value="BOLT_KIT_PHP7" /> <input type="hidden" id="surl" name="surl" value="<?php echo getCallbackUrl(); ?>" /> <input type="hidden" id="key" name="key" placeholder="Merchant Key" value="<?php print MERCHANT_KEY;?>" /> <input type="hidden" id="salt" name="salt" placeholder="Merchant Salt" value="<?php print SALT; ?>" /> <input type="hidden" id="txnid" name="txnid" placeholder="Transaction ID" value="<?php echo "Txn" . rand(10000,99999999)?>" /> <div class="row align-items-center"> <div class="form-group col-md-6"> <label for="inputEmail4">Amount</label> <input type="email" class="form-control" id="amount" name="amount" placeholder="Amount" value="100.00"> </div> <div class="form-group col-md-6"> <label for="inputEmail4">Product Info</label> <input type="text" class="form-control" id="pinfo" name="pinfo" placeholder="Product Info" value="Product-001"> </div> </div> <div class="row align-items-center"> <div class="form-group col-md-6"> <label for="inputEmail4">Frist Name</label> <input type="text" class="form-control" id="fname" name="fname" placeholder="First Name" value=""> </div> <div class="form-group col-md-6"> <label for="inputEmail4">Email</label> <input type="email" class="form-control" id="email" name="email" placeholder="Email ID" value=""> </div> </div> <div class="row align-items-center"> <div class="form-group col-md-6"> <label for="inputEmail4">Mobile/Cell Number</label> <input type="text" class="form-control" id="mobile" name="mobile" placeholder="Mobile/Cell Number" value=""> </div> </div> <div class="row justify-content-start mt-4"> <div class="col"> <button type="submit" class="btn btn-primary mt-4 float-right" onclick="launchBOLT(); return false;">Pay Now</button> </div> </div> <input type="hidden" id="hash" name="hash" placeholder="Hash" value="" /> </form> <hr> <div class="row"> <div class="col-md-3">(Visa) Card Name: Test</div> <div class="col-md-3">Card Number: 4012001037141112</div> <div class="col-md-3">Expiry Date : <?php print date('m/y',strtotime('+1 years', time()));?></div> <div class="col-md-3">CVV : 123</div> </div> <div class="row"> <div class="col-md-3">(Master) Card Name: Test</div> <div class="col-md-3">Card Number: 5123456789012346</div> <div class="col-md-3">Expiry Date : <?php print date('m/y',strtotime('+1 years', time()));?></div> <div class="col-md-3">CVV : 123</div> </div> </div> </div> </div> </section> <?php include('templates/footer.php'); ?> <!-- BOLT Sandbox/test //--> <script id="bolt" src="https://sboxcheckout-static.citruspay.com/bolt/run/bolt.min.js" bolt- color="e34524" bolt-logo="http://boltiswatching.com/wp-content/uploads/2015/09/Bolt-Logo-e14421724859591.png"></script> <!-- BOLT Production/Live //--> <!--// script id="bolt" src="https://checkout-static.citruspay.com/bolt/run/bolt.min.js" bolt-color="e34524" bolt-logo="http://boltiswatching.com/wp-content/uploads/2015/09/Bolt-Logo-e14421724859591.png"></script //--> <script type="text/javascript"><!-- $('#payment_form').bind('keyup blur', function(){ $.ajax({ url: 'request.php', type: 'post', data: JSON.stringify({ key: $('#key').val(), salt: $('#salt').val(), txnid: $('#txnid').val(), amount: $('#amount').val(), pinfo: $('#pinfo').val(), fname: $('#fname').val(), email: $('#email').val(), mobile: $('#mobile').val(), udf5: $('#udf5').val() }), contentType: "application/json", dataType: 'json', success: function(json) { if (json['error']) { $('#alertinfo').html('<i class="fa fa-info-circle"></i>'+json['error']); } else if (json['success']) { $('#hash').val(json['success']); } } }); }); //--> </script> <script type="text/javascript"><!-- function launchBOLT() { bolt.launch({ key: $('#key').val(), salt: $('#salt').val(), txnid: $('#txnid').val(), hash: $('#hash').val(), amount: $('#amount').val(), firstname: $('#fname').val(), email: $('#email').val(), phone: $('#mobile').val(), productinfo: $('#pinfo').val(), udf5: $('#udf5').val(), surl : $('#surl').val(), furl: $('#surl').val(), mode: 'dropout' }, { responseHandler: function(BOLT){ console.log( BOLT.response.txnStatus ); if(BOLT.response.txnStatus != 'CANCEL') { //Salt is passd here for demo purpose only. For practical use keep salt at server side only. var fr = '<form action=\"'+$('#surl').val()+'\" method=\"post\">' + '<input type=\"hidden\" name=\"key\" value=\"'+BOLT.response.key+'\" />' + '<input type=\"hidden\" name=\"salt\" value=\"'+$('#salt').val()+'\" />' + '<input type=\"hidden\" name=\"txnid\" value=\"'+BOLT.response.txnid+'\" />' + '<input type=\"hidden\" name=\"amount\" value=\"'+BOLT.response.amount+'\" />' + '<input type=\"hidden\" name=\"productinfo\" value=\"'+BOLT.response.productinfo+'\" />' + '<input type=\"hidden\" name=\"firstname\" value=\"'+BOLT.response.firstname+'\" />' + '<input type=\"hidden\" name=\"email\" value=\"'+BOLT.response.email+'\" />' + '<input type=\"hidden\" name=\"udf5\" value=\"'+BOLT.response.udf5+'\" />' + '<input type=\"hidden\" name=\"mihpayid\" value=\"'+BOLT.response.mihpayid+'\" />' + '<input type=\"hidden\" name=\"status\" value=\"'+BOLT.response.status+'\" />' + '<input type=\"hidden\" name=\"hash\" value=\"'+BOLT.response.hash+'\" />' + '</form>'; var form = jQuery(fr); jQuery('body').append(form); form.submit(); } }, catchException: function(BOLT){ alert( BOLT.message ); } }); } //-- </script> |
Step 5: jQuery and Ajax Code:
We will also need to use Ajax code in index.php make Ajax request/response data.The details of the parameters to be passed as data object are detailed in transaction request/response parameters section.
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 |
<script type="text/javascript"><!-- $('#payment_form').bind('keyup blur', function(){ $.ajax({ url: 'request.php', type: 'post', data: JSON.stringify({ key: $('#key').val(), salt: $('#salt').val(), txnid: $('#txnid').val(), amount: $('#amount').val(), pinfo: $('#pinfo').val(), fname: $('#fname').val(), email: $('#email').val(), mobile: $('#mobile').val(), udf5: $('#udf5').val() }), contentType: "application/json", dataType: 'json', success: function(json) { if (json['error']) { $('#alertinfo').html('<i class="fa fa-info-circle"></i>'+json['error']); } else if (json['success']) { $('#hash').val(json['success']); } } }); }); //--> </script> <script type="text/javascript"><!-- function launchBOLT() { bolt.launch({ key: $('#key').val(), txnid: $('#txnid').val(), hash: $('#hash').val(), amount: $('#amount').val(), firstname: $('#fname').val(), email: $('#email').val(), phone: $('#mobile').val(), productinfo: $('#pinfo').val(), udf5: $('#udf5').val(), surl : $('#surl').val(), furl: $('#surl').val(), mode: 'dropout' }, { responseHandler: function(BOLT){ console.log( BOLT.response.txnStatus ); if(BOLT.response.txnStatus != 'CANCEL') { //Salt is passd here for demo purpose only. For practical use keep salt at server side only. var fr = '<form action=\"'+$('#surl').val()+'\" method=\"post\">' + '<input type=\"hidden\" name=\"key\" value=\"'+BOLT.response.key+'\" />' + '<input type=\"hidden\" name=\"salt\" value=\"'+$('#salt').val()+'\" />' + '<input type=\"hidden\" name=\"txnid\" value=\"'+BOLT.response.txnid+'\" />' + '<input type=\"hidden\" name=\"amount\" value=\"'+BOLT.response.amount+'\" />' + '<input type=\"hidden\" name=\"productinfo\" value=\"'+BOLT.response.productinfo+'\" />' + '<input type=\"hidden\" name=\"firstname\" value=\"'+BOLT.response.firstname+'\" />' + '<input type=\"hidden\" name=\"email\" value=\"'+BOLT.response.email+'\" />' + '<input type=\"hidden\" name=\"udf5\" value=\"'+BOLT.response.udf5+'\" />' + '<input type=\"hidden\" name=\"mihpayid\" value=\"'+BOLT.response.mihpayid+'\" />' + '<input type=\"hidden\" name=\"status\" value=\"'+BOLT.response.status+'\" />' + '<input type=\"hidden\" name=\"hash\" value=\"'+BOLT.response.hash+'\" />' + '</form>'; var form = jQuery(fr); jQuery('body').append(form); form.submit(); } }, catchException: function(BOLT){ alert( BOLT.message ); } }); } //-- </script> |
Step 6: Create a file named response.php
if Payment process done success/failure.
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 |
<?php include_once('config.php'); include('templates/header.php'); $postdata = $_POST; $msg = ''; if (isset($postdata ['key'])) { $key = $postdata['key']; $salt = SALT; $txnid = $postdata['txnid']; $amount = $postdata['amount']; $productInfo = $postdata['productinfo']; $firstname = $postdata['firstname']; $email = $postdata['email']; $udf5 = $postdata['udf5']; $mihpayid = $postdata['mihpayid']; $status = $postdata['status']; $resphash = $postdata['hash']; //Calculate response hash to verify $keyString = $key.'|'.$txnid.'|'.$amount.'|'.$productInfo.'|'.$firstname.'|'.$email.'|||||'.$udf5.'|||||'; $keyArray = explode("|",$keyString); $reverseKeyArray = array_reverse($keyArray); $reverseKeyString = implode("|",$reverseKeyArray); $CalcHashString = strtolower(hash('sha512', $salt.'|'.$status.'|'.$reverseKeyString)); if ($status == 'success' && $resphash == $CalcHashString) { $msg = '<div class="alert alert-success" role="alert">Transaction Successful and Hash Verified...</div>'; //Do success order processing here... } else { //tampered or failed $msg = '<div class="alert alert-danger" role="alert">Payment failed for Hasn not verified...</div>'; } } else exit(0); ?> <section class="showcase"> <div class="container"> <div class="pb-2 mt-4 mb-2 border-bottom"> <h2>PayUMoney Payment Gateway Integration in PHP</h2> </div> <div class="row"> <div class="col-md-12"><?php echo $msg; ?></div> </div> <div class="row"> <div class="col-md-12">Merchant Key: <?php echo $key; ?></div> </div> <div class="row"> <div class="col-md-12">Merchant Salt: <?php echo $salt; ?></div> </div> <div class="row"> <div class="col-md-12">Transaction/Order ID: <?php echo $txnid; ?></div> </div> <div class="row"> <div class="col-md-12">Amount: <?php echo $amount; ?></div> </div> <div class="row"> <div class="col-md-12">Product Info: <?php echo $productInfo; ?></div> </div> <div class="row"> <div class="col-md-12">Name: <?php echo $firstname; ?></div> </div> <div class="row"> <div class="col-md-12">Emaul ID: <?php echo $email; ?></div> </div> <div class="row"> <div class="col-md-12">Mihpayid: <?php echo $mihpayid; ?></div> </div> <div class="row"> <div class="col-md-12">Hash: <?php echo $resphash; ?></div> </div> <div class="row"> <div class="col-md-12">Transaction Status: <?php echo $status; ?></div> </div> </div> </section> <?php include('templates/footer.php'); ?> |
Test MERCHANT KEY, Salt and Credentials for PayUMoney Payment Gateway
KEY:
mGgkQuFR
SALT:
WD297ko2zb
(Visa) Card Name: Test
Card Number: 4012001037141112
Expiry Date : 05/20
CVV : 123
(Master) Card Name: Test
Card Number: 5123456789012346
Expiry Date : 05/20
CVV : 123