How to integrate PayKun Payment Gateway in PHP

Integrate PayKun Payment Gateway in PHP

In this tutorial, I am going to show you how to integrate the PayKun payment gateway for a website in PHP programming language with example code snippets.

Before I go forward, let me tell you in brief about PayKun.

The PayKun is one of the amazing payment gateways in India that will help websites to accept payment online using their API credentials. Business owners can apply for this payment service to use on their website without any setup fee and any kind of paper work. It is very easy and hassle-free to use their service.

Integrate PayKun Payment Gateway in PHP

It is quite easy to integrate this payment service in PHP using the PayKun library available on GitHub. You can do it by using the composer project or by directly downloading the library from GitHub.

Direct download:

If you are not willing to get it using the composer, then you can directly download it from GitHub and include the required files in the PHP file.

Composer setup:

You need to run the command given below to use it with composer:

composer require paykun/checkout

 

First of all, create paymentform.php and write our HTML code for the HTML payment form

<form method="POST" action="request.php">
    <input type="hidden" name="response_url" value="response.php">
     

        Amount<br>
        <input type="text" name="amount" value="25.00" readonly="readonly"/> 
 
        Product Name<br>
        <input type="text" name="product_name" value="PRODUCT 123" readonly="readonly" />  
   
      
        Full Name<br>
        <input type="text" name="full_name" placeholder="Full Name" />
 
        Email<br>
        <input type="text" name="email" placeholder="Email" /> 
    
 
        Contact No<br>
        <input type="text" name="customer_phone" maxlength="10" placeholder="Contact No" /> 
 
        Address<br>
        <input type="text" name="address" placeholder="Address" />  
    
      
        Country<br>
        <input type="text" name="country" placeholder="country" value="India" />  
      
        State<br>
        <input type="text" name="state" id="state" placeholder="State" />  
    
   
        City<br>
        <input type="text" name="city" maxlength="10"  id="city" placeholder="City" />  
     
        Postal Code<br>
        <input type="text" name="postalcode" id="postalcode" placeholder="Postal Code" /> 
    
    
        <button type="submit" class="btn btn-primary mt-4 float-right">Pay Now</button>
    
</form>

After that, create our config.php file. This file will contain all the credentials of PayKun API. Below is the PHP code that will be inside the file:

<?php
// Merchant ID, Access token and encryption key from PayKun
$merchant_id = "XXXXXXXXXX";
$access_token = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$enc_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX";

?>

Now it’s time to process our form data and send it to the PayKun server. So, create our request.php file and write the necessary code to do this task:

<?php
// Include the config.php file
require 'config.php';
// Include the required PHP files provided by PauKun GitHub library
require 'src/Payment.php';
require 'src/Validator.php';
require 'src/Crypto.php';
// For composer
// use Paykun\Checkout\Payment; 


$order_id = time().rand(11111,99999);
$customer_name = $_POST['full_name'];
$product_name   = $_POST['product_name'];
$email          = $_POST['email'];
$amount         = $_POST['amount'];
$customer_phone = $_POST['customer_phone'];
$country        = $_POST['country'];
$state          = $_POST['state'];
$city           = $_POST['city'];
$postal_code     = $_POST['postalcode'];
$address        = $_POST['address'];
$response_url   = $_POST['response_url'];

// Change the false value into true for production mode. Currently it is in test or sandbox mode
$obj = new Payment($merchant_id, $access_token, $enc_key, false, true); 
$success_url = $response_url;
$fail_url    = $response_url;
// Initialize the Order
$obj->initOrder($order_id, $product_name,  $amount, $success_url,  $fail_url);
// Customer info
$obj->addCustomer($customer_name, $email, $customer_phone);
// Shipping address
$obj->addShippingAddress($country, $state, $city, $postal_code, $address);
// Billing Address
$obj->addBillingAddress($country, $state, $city, $postal_code, $address);
//Ccustom fields if required
$obj->setCustomFields(array('field_1' => 'Test 1', 'field_2' => 'Test 2', 'field_3' => 'Test 3'));
//Render template and submit the form
echo $obj->submit();
?>

In the above PHP code, we have created an object and pass all the required variables. Check the comment messages to know what are the required variables representing. After that, send parameters to the methods to send order data, customer data, shipping and billing address and submit the data.

 

Now create our response.php file to get transaction data back from PayKun to process it as per our requirement. You can process the data for any purpose. You can take any action in PHP for successful or failed payment. Below is the PHP code to do it inside the response.php file:

<?php
include('config.php');
require 'src/Payment.php';
require 'src/Validator.php';
require 'src/Crypto.php';
//For composer
//use Paykun\Checkout\Payment;

// Change the false value into true for production mode. Currently it is in test or sandbox mode
$obj = new Payment($merchant_id, $access_token, $enc_key, false, true);
$response = $obj->getTransactionInfo($_REQUEST['payment-id']);
$message = '';
if(is_array($response) && !empty($response)) {

    if($response['status'] && $response['data']['transaction']['status'] == "Success") {
        $message = 'Payment was made successfully';
    }
    else {
        //tampered or failed
        $message = 'Transaction failed';
    } 
}

?>
  
    
      <h2>PayKun Payment Gateway Integration using PHP</h2>
    
  <div style="text-align: center;">
    <h3><?php print $message;?></h3>
      <p class="lead">Please check out your email for further instructions.</p>
      
      <p class="lead">
      <a class="btn btn-primary btn-sm" href="#" role="button">Continue to home</a>
    </p>
  </div>

Here, we are getting the transaction data using the getTransactionInfo() method from the class. We just have to pass the payment ID we get from the success or failure URL to make it work.

You can process the data you get from “$transaction_data” on your website in PHP for any purpose.

 

Notice for those who use composer

In the request.php and response.php file, you can find the code to be used for the composer is in the comment. The code will work for non-composer. But after removing the lines given below from these files:

require 'src/Payment.php';
require 'src/Validator.php';
require 'src/Crypto.php';

And then use the code:

use Paykun\Checkout\Payment;

It will start working for composer users.

 

For production mode

Also, currently, our code in sandbox or testing mode. But you can enable for production mode just by changing the given line of code:

$obj = new Payment($merchant_id, $access_token, $enc_key, false, true);

and replace it with the code given below:

$obj = new Payment($merchant_id, $access_token, $enc_key, true, true);

As you can see, just changing the “false” into “true” will make it enable for production mode. You have to replace it in both the request.php and response.php file.

2 responses to “How to integrate PayKun Payment Gateway in PHP”

  1. Rajat says:

    Sir,
    this is problems
    Fatal error: Class ‘Payment’ not found in C:\wamp\www\preparation_com\action\paykun\request.php on line 26

Leave a Reply