Ask Question



Need a Website Or Web Application Or Any Help In Code , Contact Us: +91 9437911966 (Whatsapp), Note: Paid Service
Students Tutorial app relased. Download Now.

How To Integrate Paykun Payment Gateway in PHP


Paykun is one of the best and user-friendly online payment gateway. It comes with all modern payment options like debit card, credit card, net banking, UPI, EMI option, etc.

It is very easy to integrate into your website, App. Also you can send the Payment link for a particular amount through the Paykun payment button.

Please follow the below steps to integrate Paykun on your PHP website.

  1. Apply Paykun by click here
  2. Then download the code from Github by click on the clone or download button.
  3. Then generate the credentials by click on the setting-->security-->Generate.

You need three credentials

Merchant Id: You can get it from the My Account section

accessToken: Can generate the by click on the setting-->security-->Generate.

encryptionKey: Can generate the by click on the setting-->security-->Generate.

request.php

<!DOCTYPE html>
<html>
<head>
	<title>PayKun Demo</title>
</head>
<body>
<center>   
	<table border="1">

<form method="POST" action="submit.php">
	<h1>PayKun Demo</h1>
		

	<tr>
		<td><label>full name</label></td>
		<td><input type="text" name="full_name"></td>
	</tr>

	<tr>
		<td><label>product name</label></td>
		<td><input type="text" name="product_name"></td>
	</tr>
	
	<tr>
		<td><label>email</label></td>
		<td><input type="email" name="email"></td>
	</tr>

	<tr>
		<td><label>amount</label></td>
		<td><input type="text" name="amount"></td>
	</tr>

	<tr>
		<td><label>contact No</label></td>
		<td><input type="text" maxlength="10" name="contact"></td>
	</tr>

	<tr>
		<td><label>country</label></td>	
		<td><input type="text" name="country" value="india"></td>
	</tr>
	
	<tr>
		<td><label>state</label></td>	
		<td><input type="text" name="state"></td>
	</tr>
		
	<tr>
		<td><label>city</label></td>
		<td><input type="text" name="city"></td>
	</tr>

	<tr>
		<td><label>postal code</label></td>
		<td><input type="text" name="postalcode" maxlength="6"></td>
	</tr>

	<tr>
		<td><label>address</label></td>
		<td><input type="text" name="address"></td>
	</tr>
	
	<tr>
		<td colspan="2">
			<center colspan="2"><input type="submit" value="submit"></center>
		</td>
	</tr>

</form>
</table>
</center>

</body>
</html>

submit.php

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

$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'];
/**


*  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 \Paykun\Checkout\Payment('<merchantId>', '<accessToken>', '<encryptionKey>', true, true);
$successUrl = str_replace("request.php", "success", $_SERVER['HTTP_REFERER']);
$failUrl 	= str_replace("request.php", "failed", $_SERVER['HTTP_REFERER']);

/* Initializing Order*/

$obj->initOrder(generateByMicrotime(), $product_name,  $amount, $successUrl,  $failUrl, 'USD');

/*Add Customer*/


$obj->addCustomer($fname, $email, $contact);

/* Add Shipping address*/
$obj->addShippingAddress('', '', '', '', '');
$obj->addBillingAddress('', '', '', '', '');
/* Add Billing Address*/
/*Enable if require custom fields*/

$obj->setCustomFields(array('udf_1' => 'Some Dummy text'));

/*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));
}


?>