PayKun Payment Gateway Integration using PHP
PayKun is the best payment gateway for Indian businesses. It is a safe, secure and easy way to collect payments from the customers. It is gaining popularity because of its trending payment gateway technology and infrastructure, up to the mark services and cost-effectiveness. It is suitable for any kind of venture, may it be blogging, YouTuber, offline shop, online eCommerce store, a tech business, a small startup, large enterprise, etc.
It provides online payment options, webhooks, payment links, master links, etc making it an easy tool to get the payments. This article provides an easy stepwise process on how to integrate the PayKun Payment Gateway with PHP platform.
In this tutorial, you will learn you how to implement PayKun Payment Gateway Integration using PHP. This is a very simple example, you can just copy paste and change according to your requirement.
PayKun Payment Gateway features
- Provides 120+ payment options at one place in one checkout
- PCI DSS Level 1 compliant, follows 256 Bit AES encryption and SSL Certified, in short totally non-hackable.
- Easy and fast to integrate with its readily available Plugins and SDKs for all major platforms.
- Supports website as well as application integration.
- Payment links/master links can be used to collect online payments in case of no website
- Available Smart Merchant Dashboard for analytics and comparison with graphical representation, downloadable reports and transaction management tool.
- Android and iOS PayKun Mobile Apps for convenient access to the dashboard.
- Robust Customer and Client Support, Technical Support and Dispute Resolution Centre.
Before started to implement the REST API with PHP and MySQL, look files structure:
- paykun-payment-gateway-integration-using-php
- assets
- css
- style.css
- images
- css
- templates
- header.php
- footer.php
- config.php
- index.php
- request.php
- response.php
- vendor
- assets
Step 1: PayKun Account Sign Up
- Sign up with basic details
- Verify Mobile Number and Email Address during the process
- Login to your account with a registered email address and password.
- Register with the required details and documents.
- Fast Verification and Activation of your merchant account by the PayKun Team
- Integration of the PayKun Payment Gateway
- Start collecting online payments
Access Test/Sandbox Mode
- Access the technical documentation and the Test Mode from My Account section (the top-right corner “Profile drop-down”)
- Get the look-n-feel and experience of the checkout.
- Note: (Note- You need to set: isLive=false in your integration kit for test mode.)
Generate API Encryption Key
- Merchant ID is available in ( the top-right corner “Profile drop-down”)
- Access Token and Encryption Key can be generated from Settings > Security > Generate API Key
Note: (Note: If you have generated API Key before then you will see the date of the API Key generated, since you will not be able to retrieve the old API Key (For security reasons) they have provided the re-generate option, so you can re-generate API Key in case you have lost the old one.)
Installation Step
If your project uses composer, run the below command
1 |
composer require paykun/checkout |
Direct Download Zip file
Download the PayKun-PHP zip file. Download the Source code.zip file and include Payment.php in your application.
Step 2: Create configuration file named
config.php
1 2 3 4 5 6 7 8 9 10 |
<?php // vendor autoload include('vendor/autoload.php'); // PAYKUM MERCHANT ID define('PAYKUM_MERCHANT_ID', '0987654321'); // PAYKUM ACCESS TOKEN define('PAYKUM_ACCESS_TOKEN', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX'); // PAYKUM ENCRYPTION KEY define('PAYKUM_ENCRYPTION_KEY', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX'); ?> |
Step 3: Create a files named
index.php
and request.php
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 |
<?php include('templates/header.php');?> <section class="showcase"> <div class="container"> <div class="pb-2 mt-4 mb-2 border-bottom"> <h2>PayKun Payment Gateway Integration using PHP</h2> </div> <form method="POST" action="request.php"> <input type="hidden" name="response_url" value="response.php"> <div class="row align-items-center"> <div class="form-group col-md-6"> <label for="inputEmail4">Amount</label> <input type="text" name="amount" class="form-control" id="amt" value="25.00" readonly="readonly"/> </div> <div class="form-group col-md-6"> <label for="inputEmail4">Product Name</label> <input type="text" name="product_name" class="form-control" id="product-name" value="PRODUCT 101" readonly="readonly" /> </div> </div> <div class="row align-items-center"> <div class="form-group col-md-6"> <label for="inputEmail4">Full Name</label> <input type="text" name="full_name" class="form-control" id="full-name" placeholder="Full Name" /> </div> <div class="form-group col-md-6"> <label for="inputEmail4">Email</label> <input type="text" name="email" class="form-control" id="email" placeholder="Email" /> </div> </div> <div class="row align-items-center"> <div class="form-group col-md-6"> <label for="inputEmail4">Contact No</label> <input type="text" name="contact" class="form-control" maxlength="10" id="contact" placeholder="Contact No" /> </div> <div class="form-group col-md-6"> <label for="inputEmail4">Address</label> <input type="text" name="address" class="form-control" id="address" placeholder="Address" /> </div> </div> <div class="row align-items-center"> <div class="form-group col-md-6"> <label for="inputEmail4">Country</label> <input type="text" name="country" class="form-control" maxlength="10" id="country" placeholder="country" value="india" /> </div> <div class="form-group col-md-6"> <label for="inputEmail4">State</label> <input type="text" name="state" class="form-control" id="state" placeholder="State" /> </div> </div> <div class="row align-items-center"> <div class="form-group col-md-6"> <label for="inputEmail4">City</label> <input type="text" name="city" class="form-control" maxlength="10" id="city" placeholder="City" /> </div> <div class="form-group col-md-6"> <label for="inputEmail4">Postal Code</label> <input type="text" name="postalcode" class="form-control" id="postalcode" placeholder="Postal Code" /> </div> </div> <div class="row align-items-center"> <div class="col"> <button type="submit" class="btn btn-primary mt-4 float-right">Pay Now</button> </div> </div> </form> </div> </section> <?php include('templates/footer.php');?> |
request.php
Note: Set true for production environment and false for sandbox or testing mode
For sandbox integration use:
$obj = new Payment(PAYKUM_MERCHANT_ID, PAYKUM_ACCESS_TOKEN, PAYKUM_ENCRYPTION_KEY, false, true);
For live integration use:
$obj = new Payment(PAYKUM_MERCHANT_ID, PAYKUM_ACCESS_TOKEN, PAYKUM_ENCRYPTION_KEY, true, true);
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 |
<?php // include config file include('config.php'); use Paykun\Checkout\Payment; $fname = $_POST['full_name']; $product_name = $_POST['product_name']; $email = $_POST['email']; $amount = $_POST['amount']; $contact = $_POST['contact']; $country = $_POST['country']; $state = $_POST['state']; $city = $_POST['city']; $postalcode = $_POST['postalcode']; $address = $_POST['address']; $response_url = $_POST['response_url']; /** * Parameters requires to initialize an object of Payment are as follow. * mid => Merchant Id provided by Paykun * accessToken => Access Token provided by Paykun * encKey => Encryption provided by Paykun * isLive => Set true for production environment and false for sandbox or testing mode * isCustomTemplate => Set true for non composer projects, will disable twig template */ $obj = new Payment(PAYKUM_MERCHANT_ID, PAYKUM_ACCESS_TOKEN, PAYKUM_ENCRYPTION_KEY, false, true); $successUrl = $response_url; $failUrl = $response_url; // Initializing Order $obj->initOrder(generateByMicrotime(), $product_name, $amount, $successUrl, $failUrl); // Add Customer $obj->addCustomer($fname, $email, $contact); // Add Shipping address $obj->addShippingAddress('india', $state, $city, $postalcode, $address); // Add Billing Address $obj->addBillingAddress('india', $state, $city, $postalcode, $address); //Enable if require custom fields $obj->setCustomFields(array('udf_1' => 'Test 1', 'udf_2' => 'Test 2', 'udf_3' => 'Test 3', 'udf_4' => 'Test 4', 'udf_5' => 'Test ')); //Render template and submit the form echo $obj->submit(); /* Check for transaction status * Once your success or failed url called then create an instance of Payment same as above and then call getTransactionInfo like below * $obj = new Payment('merchantUId', 'accessToken', 'encryptionKey', true, true); //Second last false if sandbox mode * $transactionData = $obj->getTransactionInfo(Get payment-id from the success or failed url); * Process $transactionData as per your requirement * * */ function generateByMicrotime() { $microtime = str_replace('.', '', microtime(true)); return (substr($microtime, 0, 14)); } ?> |
Step 4: 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 |
<?php include('config.php'); use Paykun\Checkout\Payment; $obj = new Payment(PAYKUM_MERCHANT_ID, PAYKUM_ACCESS_TOKEN, PAYKUM_ENCRYPTION_KEY, false, true); $response = $obj->getTransactionInfo($_REQUEST['payment-id']); $msg = ''; if(is_array($response) && !empty($response)) { if($response['status'] && $response['data']['transaction']['status'] == "Success") { $msg = '<div class="alert alert-success" role="alert">Payment has been done successfully.</div>'; } else { //tampered or failed $msg = '<div class="alert alert-danger" role="alert">Transaction failed</div>'; } } ?> <?php include('templates/header.php');?> <section class="showcase"> <div class="container"> <div class="pb-2 mt-4 mb-2 border-bottom"> <h2>PayKun Payment Gateway Integration using PHP</h2> </div> <div class="text-center"> <h1 class="display-3">Thank You!</h1> <h3><?php print $msg;?></h3> <p class="lead"><strong>Please check your email</strong> for further instructions on how to complete your account setup.</p> <hr> <p> Having trouble? <a href="mailto:info@codersmag.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> <?php include('templates/footer.php');?> |
Create files named (header.php and footer.php)
This file contains the header and footer 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 |
<!DOCTYPE html> <html lang="en"> <head> <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=""> <title>PayKun Payment Gateway Integration using PHP | Tech Arise</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="assets/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://techarise.com"><h1>Tech Arise</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://techarise.com">Home <span class="sr-only">(current)</span> </a> </li> <li class="nav-item"> <a class="nav-link" href="https://techarise.com/php-free-script-demos/">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 |
<!-- 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="text-muted small mb-4 mb-lg-0">Copyright © 2011 - <?php print date('Y', time());?> <a href="https://techarise.com/">TECHARISE.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> |