Paytm Payment Gateway Integration with PHP

Today, We want to share with you Paytm Payment Gateway Integration with PHP.In this post we will show you wordpress plugin require another plugin, hear for callback url in paytm integration we will give you demo and example for implement.In this post, we will learn about paytm payment gateway integration in android with an example.

Paytm Payment Gateway Integration with PHP

There are the Following The simple About paytm gratification api Full Information With Example and source code.

As I will cover this Post with live Working example to develop How we can integrate Paytm payment gateway using PHP, so the Laravel 6 Paytm Payment Gateway Integration Using PHP is used for this example is following below.

Simple Paytm is a very famous and best way to make payments these some days. To step by step here explain into php Paytm source code integrate Paytm in our popular blog website we need to follow the simple below steps –

Step 1. Paytm create an account

First of all, we need to make an account devloper Like as at Sign Up/Register Paytm Account as well as here retrive our main merchant ID as well Secret Key.

and then we can start PHP source coding in our site to use the Paytm. First of all, we will setting / configure the Paytm with mode of the test credentials to test our source code.

Step 2. configure the Paytm with test credentials

(filename – config.php)

define('PAYTM_ENVIRONMENT', 'TEST'); // PROD for live payment
define('PAYTM_MERCHANT_KEY', 'xxxxxxxxxxxxxxx'); 
define('PAYTM_MERCHANT_MID', xxxxxxxxxxxxxxxx);
define('PAYTM_MERCHANT_WEBSITE', "WEBSTAGING");
define('PAYTM_REDIRECT_URL', "http://localhost/paytm/Response.php");
$PAYTM_TXN_URL='https://securegw-stage.paytm.in/theia/processTransaction';
if (PAYTM_ENVIRONMENT == 'PROD') {
	$PAYTM_TXN_URL='https://securegw.paytm.in/theia/processTransaction';
}
define('PAYTM_TXN_URL', $PAYTM_TXN_URL);

Step 3. create the payment form

Now I shall make a new file(index.php) to make the payment form which will have the amount to be payment/paid as well as other more useful needed details –

READ :  Laravel Autocomplete search from Database MySQL PHP

<?php
	require_once("config.php");

?>
<html>
<head>
<title>Here Merchant Check Out Page</title>
<meta name="GENERATOR" content="Evrsoft First Page">
</head>
<body>
	<h1>Merchant Check Out Page</h1>
	<pre>
	</pre>
	<form method="post" action="Redirect.php">
		<table border="1">
			<tbody>
				<tr>
					<th>S.No</th>
					<th>Label</th>
					<th>Value</th>
				</tr>
				<tr>
					<td>Step : 1</td>
					<td><label>Enter SHOP_ORDER_DATA_ID::*</label></td>
					<td><input id="SHOP_ORDER_DATA_ID" tabindex="1" maxlength="20" size="20"
						name="SHOP_ORDER_DATA_ID" autocomplete="off"
						value="<?php echo  "ORDS" . rand(10000,99999999)?>">
					</td>
				</tr>
				<tr>
					<td>Step : 2</td>
					<td><label>Enter Customer Id ::*</label></td>
					<td><input id="CUSTOMER_USER_ID" tabindex="2" maxlength="12" size="12" name="CUSTOMER_USER_ID" autocomplete="off" value="CUST001"></td>
				</tr>
				<tr>
					<td>Step : 3</td>
					<td><label>PAYTM_IND_ID ::*</label></td>
					<td><input id="PAYTM_IND_ID" tabindex="4" maxlength="12" size="12" name="PAYTM_IND_ID" autocomplete="off" value="Retail"></td>
				</tr>
				<tr>
					<td>Step : 4</td>
					<td><label>Enter Channel ::*</label></td>
					<td><input id="CHANNEL_ID" tabindex="4" maxlength="12"
						size="12" name="CHANNEL_ID" autocomplete="off" value="WEB">
					</td>
				</tr>
				<tr>
					<td>Step : 5</td>
					<td><label>Enter txnAmount*</label></td>
					<td><input title="EXT_TXN_DATA_AMOUNT" tabindex="10"
						type="text" name="EXT_TXN_DATA_AMOUNT"
						value="1">
					</td>
				</tr>
				<tr>
					<td></td>
					<td></td>
<input type="hidden" name="PAYTM_REDIRECT_URL" value="<?php echo PAYTM_REDIRECT_URL; ?>" />
					<td><input value="CheckOut" type="submit"	onclick=""></td>
				</tr>
			</tbody>
		</table>
		* - Mandatory Fields
	</form>
</body>
</html>

Step 4. Redirect.php

Here in this simple HTML form, I have set the form the action=”Redirect.php”, then we will make a new file with this name –

<?php
require_once("config.php");
require_once("encdec_paytm.php"); 

$liveCurleckSum = "";
$arguments = array();

$SHOP_ORDER_DATA_ID = $_POST["SHOP_ORDER_DATA_ID"];
$CUSTOMER_USER_ID = $_POST["CUSTOMER_USER_ID"];
$PAYTM_IND_ID = $_POST["PAYTM_IND_ID"];
$CHANNEL_ID = $_POST["CHANNEL_ID"];
$EXT_TXN_DATA_AMOUNT = $_POST["EXT_TXN_DATA_AMOUNT"];
$PAYTM_REDIRECT_URL=$_POST['PAYTM_REDIRECT_URL'];


$arguments["MID"] = PAYTM_MERCHANT_MID;
$arguments["SHOP_ORDER_DATA_ID"] = $SHOP_ORDER_DATA_ID;
$arguments["CUSTOMER_USER_ID"] = $CUSTOMER_USER_ID;
$arguments["PAYTM_IND_ID"] = $PAYTM_IND_ID;
$arguments["CHANNEL_ID"] = $CHANNEL_ID;
$arguments["EXT_TXN_DATA_AMOUNT"] = $EXT_TXN_DATA_AMOUNT;
$arguments["WEBSITE"] = PAYTM_MERCHANT_WEBSITE;
$arguments['PAYTM_REDIRECT_URL']=$PAYTM_REDIRECT_URL;

//Here checksum string will return by getChecksumFromArray() function.
$liveCurleckSum = getChecksumFromArray($arguments,PAYTM_MERCHANT_KEY);

?>
<html>
<head>
<title>pakainfo.com - Merchant Check Out Page</title>
</head>
<body>
	<center><h1>Please do not refresh this page...</h1></center>
		<form method="post" action="<?php echo PAYTM_TXN_URL ?>" name="f1">
		<table border="1">
			<tbody>
			<?php
			foreach($arguments as $name => $value) {
				echo '<input type="hidden" name="' . $name .'" value="' . $value . '">';
			}
			?>
			<input type="hidden" name="PAYTMSUMDATAHASH" value="<?php echo $liveCurleckSum ?>">
			</tbody>
		</table>
		<script type="text/javascript">
			document.f1.submit();
		</script>
	</form>
</body>
</html>

E-junkie: Sell digital downloads online

E-junkie Provides a Copy-paste buy-now, and cart buttons for selling downloads, codes and tangible products on any website, blog, social media, email and messenger!

Also see:

  1. The Top 10+ Best Webinar Software Platforms For 2020-2021
  2. Build Your Future Godaddy Careers And Jobs
  3. Introduction To Web Hosting Services

To handle the response of return call from paytm we need another file(Response.php) –

<?php
require_once("config.php");
require_once("encdec_paytm.php");

$paytmChecksum = "";
$arguments = array();
$isValidChecksum = "FALSE";

$arguments = $_POST;
$paytmChecksum = isset($_POST["PAYTMSUMDATAHASH"]) ? $_POST["PAYTMSUMDATAHASH"] : ""; //Sent by Paytm pg

$isValidChecksum = verifychecksum_e($arguments, PAYTM_MERCHANT_KEY, $paytmChecksum); //will return TRUE or FALSE string.


if($isValidChecksum == "TRUE") {
	echo "<b>Checksum matched and following are the transaction Information:</b>" . "<br/>";
	if ($_POST["STATUS"] == "TXN_SUCCESS") {
		echo "<b>Transaction status is success</b>" . "<br/>";
	}
	else {
		echo "<b>Transaction status is failure</b>" . "<br/>";
	}

	if (isset($_POST) && count($_POST)>0 )
	{ 
		foreach($_POST as $paramName => $paramValue) {
				echo "<br/>" . $paramName . " = " . $paramValue;
		}
	}
	

}
else {
	echo "<b>Checksum mismatched.</b>";
}

?>

Some Important Notes – Paytm Payment Gateway Integration with PHP

1. When I make an account for paytm payment gatway using PHP it asks us for a redirect URL where paytm will send the output. By default it will redirect to that URL, we can not change that.

READ :  Vuejs multiple image upload with preview component

If I want to pass the dynamic URL then I will need to contact support as well as ask them to enable the dynamic URL for our MERCHANT_ID for Paytm Payment Gateway Integration with PHP.

2. I can also pass some additional data while making a simple curl PHP call to paytm payment API as well as paytm will return that data in the output. But the name of that parameter can only be “MERC_UNQ_REF”. We can not pass user-defined parameter name.

3. SignIN data credentials for test MODE payment –

Mobile number – 9898989898
Password – 895689

encdec_paytm.php

All the functions of the (encdec_paytm.php) file are below –

<?php

function encrypt_e($input, $ky) {
	$key   = html_entity_decode($ky);
	$iv = "[email protected]#$^&*%98984141";
	$data = openssl_encrypt ( $input , "AES-128-CBC" , $key, 0, $iv );
	return $data;
}

function decrypt_e($crypt, $ky) {
	$key   = html_entity_decode($ky);
	$iv = "[email protected]#$^&*%98984141";
	$data = openssl_decrypt ( $crypt , "AES-128-CBC" , $key, 0, $iv );
	return $data;
}

function generateCallBack_e($length) {
	$random = "";
	srand((double) microtime() * 1000000);

	$data = "AbcDE123IJKLMN67QRSTUVWXYZ";
	$data .= "aBCdefghijklmn123opq45rs67tuv89wxyz";
	$data .= "0FGH45OP89";

	for ($i = 0; $i < $length; $i++) {
		$random .= substr($data, (rand() % (strlen($data))), 1);
	}

	return $random;
}

function liveStrCheck($value) {
	if ($value == 'null')
		$value = '';
	return $value;
}

function getChecksumFromArray($dataVolumeList, $key, $sort=1) {
	if ($sort != 0) {
		ksort($dataVolumeList);
	}
	$param = convertArrTwoString($dataVolumeList);
	$flag = generateCallBack_e(4);
	$startdStr = $param . "|" . $flag;
	$hash = hash("sha256", $startdStr);
	$hashString = $hash . $flag;
	$liveCurlecksum = encrypt_e($hashString, $key);
	return $liveCurlecksum;
}
function getChecksumFromString($param, $key) {
	
	$flag = generateCallBack_e(4);
	$startdStr = $param . "|" . $flag;
	$hash = hash("sha256", $startdStr);
	$hashString = $hash . $flag;
	$liveCurlecksum = encrypt_e($hashString, $key);
	return $liveCurlecksum;
}

function verifychecksum_e($dataVolumeList, $key, $liveCurlecksumvalue) {
	$dataVolumeList = removeCheckSumParam($dataVolumeList);
	ksort($dataVolumeList);
	$param = convertArrTwoStringForVerify($dataVolumeList);
	$hash_string_ptm = decrypt_e($liveCurlecksumvalue, $key);
	$flag = substr($hash_string_ptm, -4);

	$startdStr = $param . "|" . $flag;

	$web_hash_string = hash("sha256", $startdStr);
	$web_hash_string .= $flag;

	$FinalDestination = "FALSE";
	if ($web_hash_string == $hash_string_ptm) {
		$FinalDestination = "TRUE";
	} else {
		$FinalDestination = "FALSE";
	}
	return $FinalDestination;
}

function verifychecksum_eFromStr($param, $key, $liveCurlecksumvalue) {
	$hash_string_ptm = decrypt_e($liveCurlecksumvalue, $key);
	$flag = substr($hash_string_ptm, -4);

	$startdStr = $param . "|" . $flag;

	$web_hash_string = hash("sha256", $startdStr);
	$web_hash_string .= $flag;

	$FinalDestination = "FALSE";
	if ($web_hash_string == $hash_string_ptm) {
		$FinalDestination = "TRUE";
	} else {
		$FinalDestination = "FALSE";
	}
	return $FinalDestination;
}

function convertArrTwoString($dataVolumeList) {
	$findme   = 'REFUND';
	$findmepipe = '|';
	$argQry = "";
	$flag = 1;	
	foreach ($dataVolumeList as $key => $value) {
		$pos = strpos($value, $findme);
		$pospipe = strpos($value, $findmepipe);
		if ($pos !== false || $pospipe !== false) 
		{
			continue;
		}
		
		if ($flag) {
			$argQry .= liveStrCheck($value);
			$flag = 0;
		} else {
			$argQry .= "|" . liveStrCheck($value);
		}
	}
	return $argQry;
}

function convertArrTwoStringForVerify($dataVolumeList) {
	$argQry = "";
	$flag = 1;
	foreach ($dataVolumeList as $key => $value) {
		if ($flag) {
			$argQry .= liveStrCheck($value);
			$flag = 0;
		} else {
			$argQry .= "|" . liveStrCheck($value);
		}
	}
	return $argQry;
}

function redirect2PG($arguments, $key) {
	$hashString = getchecksumFromArray($arguments);
	$liveCurlecksum = encrypt_e($hashString, $key);
}

function removeCheckSumParam($dataVolumeList) {
	if (isset($dataVolumeList["PAYTMSUMDATAHASH"])) {
		unset($dataVolumeList["PAYTMSUMDATAHASH"]);
	}
	return $dataVolumeList;
}

function getTxnStatus($callServiceDataAPI) {
	return callServicesMain(PAYTM_STATUS_QUERY_URL, $callServiceDataAPI);
}

function getTxnStatusNew($callServiceDataAPI) {
	return paytmCallServices(PAYTM_STATUS_QUERY_NEW_URL, $callServiceDataAPI);
}

function initiateTxnRefund($callServiceDataAPI) {
	$CHECKSUM = getRefundChecksumFromArray($callServiceDataAPI,PAYTM_MERCHANT_KEY,0);
	$callServiceDataAPI["CHECKSUM"] = $CHECKSUM;
	return callServicesMain(PAYTM_REFUND_URL, $callServiceDataAPI);
}

function callServicesMain($paytmApiURI, $callServiceDataAPI) {
	$dataJsonOutput = "";
	$outputArguments = array();
	$JsonData =json_encode($callServiceDataAPI);
	$requestArg = 'JsonData='.urlencode($JsonData);
	$liveCurl = curl_init($paytmApiURI);
	curl_setopt($liveCurl, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
	curl_setopt($liveCurl, CURLOPT_POSTFIELDS, $requestArg);                                                                  
	curl_setopt($liveCurl, CURLOPT_RETURNTRANSFER, true); 
	curl_setopt ($liveCurl, CURLOPT_SSL_VERIFYHOST, 0);
	curl_setopt ($liveCurl, CURLOPT_SSL_VERIFYPEER, 0);
	curl_setopt($liveCurl, CURLOPT_HTTPHEADER, array(                                                                         
	'Content-Type: application/json', 
	'Content-Length: ' . strlen($requestArg))                                                                       
	);  
	$dataJsonOutput = curl_exec($liveCurl);   
	$outputArguments = json_decode($dataJsonOutput,true);
	return $outputArguments;
}

function paytmCallServices($paytmApiURI, $callServiceDataAPI) {
	$dataJsonOutput = "";
	$outputArguments = array();
	$JsonData =json_encode($callServiceDataAPI);
	$requestArg = 'JsonData='.urlencode($JsonData);
	$liveCurl = curl_init($paytmApiURI);
	curl_setopt($liveCurl, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
	curl_setopt($liveCurl, CURLOPT_POSTFIELDS, $requestArg);                                                                  
	curl_setopt($liveCurl, CURLOPT_RETURNTRANSFER, true); 
	curl_setopt ($liveCurl, CURLOPT_SSL_VERIFYHOST, 0);
	curl_setopt ($liveCurl, CURLOPT_SSL_VERIFYPEER, 0);
	curl_setopt($liveCurl, CURLOPT_HTTPHEADER, array(                                                                         
	'Content-Type: application/json', 
	'Content-Length: ' . strlen($requestArg))                                                                       
	);  
	$dataJsonOutput = curl_exec($liveCurl);   
	$outputArguments = json_decode($dataJsonOutput,true);
	return $outputArguments;
}
function getRefundChecksumFromArray($dataVolumeList, $key, $sort=1) {
	if ($sort != 0) {
		ksort($dataVolumeList);
	}
	$param = getRefundArray2Str($dataVolumeList);
	$flag = generateCallBack_e(4);
	$startdStr = $param . "|" . $flag;
	$hash = hash("sha256", $startdStr);
	$hashString = $hash . $flag;
	$liveCurlecksum = encrypt_e($hashString, $key);
	return $liveCurlecksum;
}
function getRefundArray2Str($dataVolumeList) {	
	$findmepipe = '|';
	$argQry = "";
	$flag = 1;	
	foreach ($dataVolumeList as $key => $value) {		
		$pospipe = strpos($value, $findmepipe);
		if ($pospipe !== false) 
		{
			continue;
		}
		
		if ($flag) {
			$argQry .= liveStrCheck($value);
			$flag = 0;
		} else {
			$argQry .= "|" . liveStrCheck($value);
		}
	}
	return $argQry;
}
function callRefundAPI($refundApiURL, $callServiceDataAPI) {
	$dataJsonOutput = "";
	$outputArguments = array();
	$JsonData =json_encode($callServiceDataAPI);
	$requestArg = 'JsonData='.urlencode($JsonData);
	$liveCurl = curl_init($paytmApiURI);	
	curl_setopt ($liveCurl, CURLOPT_SSL_VERIFYHOST, 0);
	curl_setopt ($liveCurl, CURLOPT_SSL_VERIFYPEER, 0);
	curl_setopt($liveCurl, CURLOPT_URL, $refundApiURL);
	curl_setopt($liveCurl, CURLOPT_POST, true);
	curl_setopt($liveCurl, CURLOPT_POSTFIELDS, $requestArg);  
	curl_setopt($liveCurl, CURLOPT_RETURNTRANSFER, true); 
	$headers = array();
	$headers[] = 'Content-Type: application/json';
	curl_setopt($liveCurl, CURLOPT_HTTPHEADER, $headers);  
	$dataJsonOutput = curl_exec($liveCurl);   
	$outputArguments = json_decode($dataJsonOutput,true);
	return $outputArguments;
}

Web Programming Tutorials Example with Demo

Read :

READ :  Vue js upload file-Image upload and move using Laravel

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Paytm Payment Gateway Integration with PHP.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.