The atom payment gateway is a very popular payment gateway in India. It provides different types of options to make a payment such as debit cards, credit cards, UPI, Internet banking, etc. Nowadays, UPI (Unified Payment Interface) is a mostly used a payment option for digital payment in India.
When it comes to integrating payment gateway in any website or any project then the developer worried about it that how can I integrate the atom payment gateway but its integration is very easy because they already provide a library to make a payment by calling a method of that library.
This payment gateway provides a user-friendly user interface (UI) to make a successful payment. Any project or any website must be user friendly because the user doesn’t want to use that type of website which looks not good. It is available for the website or app both. It also provides a POS machine.
In this tutorial, I am going to teach you how to integrate the atom payment gateway into PHP?
How to integrate the atom payment gateway in PHP?
We will know the integration process of this payment gateway. But before going to discuss their integration process, I would like to tell you some common error that you will face at the time of this payment gateway integration. Let’s see which error occurs and why?
It gives a common error to many users “Insufficient Merchant Information. Sorry unable to process your request…!” This message was given by payment gateway if you do not provide proper data. Most of the time users can’t identify what mistake they are doing?
The mistake done by users are sending wrong login credentials, wrong environment value according to URL, not sending transaction id randomly generated.
Integration process:
This integration is done in PHP 7.3 version. Before going on implementation, you must have an account on the atom website. After creating an account, you will get a login username, password, request hash key, response hash key, account, client code, etc. Apart from it, you will also get documentation. You must read the documentation because every time code may be updated by the payment gateway.
First, I have a library provided by the atom payment gateway developer but it is customized by me for easy implementation. You can also customize it according to your knowledge and requirement.
Atompay.php
<?php
class Atompay {
private $login;
private $password;
private $transactionType;
private $productId;
private $amount;
private $transactionCurrency;
private $transactionAmount;
private $clientCode;
private $transactionId;
private $transactionDate;
private $customerAccount;
private $customerName;
private $customerEmailId;
private $customerMobile;
private $customerBillingAddress;
private $returnUrl;
private $mode = "test";
private $transactionUrl;
private $nbType = "NBFundTransfer";
private $ccType = "CCFundTransfer";
private $reqHashKey = "";
private $respHashKey = "";
public function getReqHashKey() {
return $this->reqHashKey;
}
public function setReqHashKey($reqHashKey) {
$this->reqHashKey = $reqHashKey;
}
public function getRespHashKey() {
return $this->respHashKey;
}
public function setRespHashKey($respHashKey) {
$this->respHashKey = $respHashKey;
}
public function getLogin() {
return $this->login;
}
public function setLogin($login) {
$this->login = $login;
}
public function getPassword() {
return $this->password;
}
public function setPassword($password) {
$this->password = $password;
}
public function getTransactionType() {
return $this->transactionType;
}
public function setTransactionType($transactionType) {
$this->transactionType = $transactionType;
}
public function getProductId() {
return $this->productId;
}
public function setProductId($productId) {
$this->productId = $productId;
}
public function getAmount() {
return $this->amount;
}
public function setAmount($amount) {
$this->amount = $amount;
}
/**
* @return the $transactionCurrency
*/
public function getTransactionCurrency() {
return $this->transactionCurrency;
}
/**
* @param string $transactionCurrency
*/
public function setTransactionCurrency($transactionCurrency) {
$this->transactionCurrency = $transactionCurrency;
}
/**
* @return the $transactionAmount
*/
public function getTransactionAmount() {
return $this->transactionAmount;
}
/**
* @param string $transactionAmount
*/
public function setTransactionAmount($transactionAmount) {
$this->transactionAmount = $transactionAmount;
}
/**
* @return the $transactionId
*/
public function getTransactionId() {
return $this->transactionId;
}
/**
* @param string $transactionId
*/
public function setTransactionId($transactionId) {
$this->transactionId = $transactionId;
}
/**
* @return the $transactionDate
*/
public function getTransactionDate() {
return $this->transactionDate;
}
/**
* @param string $transactionDate
*/
public function setTransactionDate($transactionDate) {
$this->transactionDate = $transactionDate;
}
/**
* @return the $customerAccount
*/
public function getCustomerAccount() {
return $this->customerAccount;
}
/**
* @param string $customerAccount
*/
public function setCustomerAccount($customerAccount) {
$this->customerAccount = $customerAccount;
}
/**
* @return the $customerName
*/
public function getCustomerName() {
return $this->customerName;
}
/**
* @param string $customerName
*/
public function setCustomerName($customerName) {
$this->customerName = $customerName;
}
/**
* @return the $customerEmailId
*/
public function getCustomerEmailId() {
return $this->customerEmailId;
}
/**
* @param string $customerEmailId
*/
public function setCustomerEmailId($customerEmailId) {
$this->customerEmailId = $customerEmailId;
}
/**
* @return the $customerMobile
*/
public function getCustomerMobile() {
return $this->customerMobile;
}
/**
* @param string $customerMobile
*/
public function setCustomerMobile($customerMobile) {
$this->customerMobile = $customerMobile;
}
/**
* @return the $customerBillingAddress
*/
public function getCustomerBillingAddress() {
return $this->customerBillingAddress;
}
/**
* @param string $customerBillingAddress
*/
public function setCustomerBillingAddress($customerBillingAddress) {
$this->customerBillingAddress = $customerBillingAddress;
}
/**
* @return the $returnUrl
*/
public function getReturnUrl() {
return $this->returnUrl;
}
/**
* @param string $returnUrl
*/
public function setReturnUrl($returnUrl) {
$this->returnUrl = $returnUrl;
}
/**
* @return the $mode
*/
public function getMode() {
return $this->mode;
}
/**
* @param string $mode
*/
public function setMode($mode) {
$this->mode = $mode;
}
/**
* @return the $transactionUrl
*/
public function getTransactionUrl() {
return $this->transactionUrl;
}
/**
* @param string $transactionUrl
*/
public function setTransactionUrl($transactionUrl) {
$this->transactionUrl = $transactionUrl;
}
public function getnbType() {
return $this->nbType;
}
public function getccType() {
return $this->ccType;
}
private function setUrl() {
$port = 443;
if ($this->getMode() == "live") {
$url = "https://payment.atomtech.in/paynetz/epi/fts";
} else {
$url = "https://paynetzuat.atomtech.in/paynetz/epi/fts";
}
$this->setTransactionUrl($url);
$this->setPort($port);
}
public function setClientCode($clientCode) {
if ($clientCode == NULL || $clientCode == "") {
$this->clientCode = urlencode(base64_encode(123));
} else {
$this->clientCode = urlencode(base64_encode($clientCode));
}
}
private function getClientCode() {
return $this->clientCode;
}
private function setPort($port) {
$this->port = $port;
}
private function getPort() {
return $this->port;
}
public function getChecksum() {
$str = $this->login . $this->password . "NBFundTransfer" . $this->productId . $this->transactionId . $this->amount . "INR";
$signature = hash_hmac("sha512", $str, $this->reqHashKey, false);
return $signature;
}
private function getData() {
$strReqst = "";
$strReqst .= "login=" . $this->getLogin();
$strReqst .= "&pass=" . $this->getPassword();
//$txnType = $this->getTransactionType();
//if($txnType== 'NB'){
$strReqst .= "&ttype=NBFundTransfer";
//}else{
//$strReqst .= "&ttype=".$this->getccType();
//}
$strReqst .= "&prodid=" . $this->getProductId();
$strReqst .= "&amt=" . $this->getAmount();
$strReqst .= "&txncurr=" . $this->getTransactionCurrency();
$strReqst .= "&txnscamt=" . $this->getTransactionAmount();
$strReqst .= "&ru=" . $this->getReturnUrl();
$strReqst .= "&clientcode=" . $this->getClientCode();
$strReqst .= "&txnid=" . $this->getTransactionId();
$strReqst .= "&date=" . $this->getTransactionDate();
$strReqst .= "&udf1=" . $this->getCustomerName();
$strReqst .= "&udf2=" . $this->getCustomerEmailId();
$strReqst .= "&udf3=" . $this->getCustomerMobile();
$strReqst .= "&udf4=" . $this->getCustomerBillingAddress();
$strReqst .= "&custacc=" . $this->getCustomerAccount();
$strReqst .= "&signature=" . $this->getChecksum();
return $strReqst;
}
/**
* This function returns transaction token url
* @return string
*/
public function getPGUrl() {
if ($this->mode != null && $this->mode != "") {
try {
$this->setUrl();
$data = $this->getData();
$this->writeLog($data);
return $this->transactionUrl . "?" . $data;
} catch (Exception $ex) {
echo "Error while getting transaction token : " . $ex->getMessage();
return;
}
} else {
return "Please set mode live or test";
}
}
private function writeLog($data) {
$fileName = "date" . date("Y-m-d") . ".txt";
if (!file_exists('log')) {
mkdir('log/', 0777, true);
}
$fp = fopen("log/" . $fileName, 'a+');
$data = date("Y-m-d H:i:s") . " - " . $data;
fwrite($fp, $data);
fclose($fp);
}
// RESPONSE
public function validateResponse($responseParams) {
$str = $responseParams["mmp_txn"] . $responseParams["mer_txn"] . $responseParams["f_code"] . $responseParams["prod"] . $responseParams["discriminator"] . $responseParams["amt"] . $responseParams["bank_txn"];
$signature = hash_hmac("sha512", $str, $this->respHashKey, false);
if ($signature == $responseParams["signature"]) {
return true;
} else {
return false;
}
}
public function atomResponse($responseHashKey) {
$this->setRespHashKey($responseHashKey);
if ($this->validateResponse($_POST)) {
return $_POST;
} else {
return "Invalid Signature";
}
}
// CREATE
public function atomRequest($env, $login, $password, $product, $amount, $currency, $returnUrl, $clientCode, $name, $email, $mobile, $address, $account, $requestHashKey) {
//Setting all values here
$transactionId = rand(1, 1000000);
$datenow = date("d/m/Y h:m:s");
$transactionDate = str_replace(" ", "%20", $datenow);
$this->setMode($env);
$this->setLogin($login);
$this->setPassword($password);
$this->setProductId($product);
$this->setAmount($amount);
$this->setTransactionCurrency($currency);
$this->setTransactionAmount($amount);
$this->setReturnUrl($returnUrl);
$this->setClientCode($clientCode);
$this->setTransactionId($transactionId);
$this->setTransactionDate($transactionDate);
$this->setCustomerName($name);
$this->setCustomerEmailId($email);
$this->setCustomerMobile($mobile);
$this->setCustomerBillingAddress($address);
$this->setCustomerAccount($account);
$this->setReqHashKey($requestHashKey);
$url = $this->getPGUrl();
header("Location: " . $url);
}
}
?>
The next page is used for designing a form that will display on the user browser.
index.php
<!DOCTYPE html>
<html>
<head>
<title>Atom Payment Gateway Integration in PHP</title>
</head>
<body>
<div>
<form method="post" action="request.php">
<label>Name :</label>
<input type="text" name="name_field"><br><br>
<label>Email :</label>
<input type="email" name="email"><br><br>
<label>Mobile :</label>
<input type="number" name="mobile"><br><br>
<label>Address :</label>
<textarea name="address"></textarea><br><br>
<label>Amount :</label>
<input type="number" name="amount"><br><br>
<button type="submit" style="background: green;color: white;" name="save">Pay Button</button>
</form>
</div>
</body>
</html>
On this page, there are five inputs fields in a form that will receive user input. At last, a pay button is added for sending a request to the payment gateway.
The next file is used to accept the request of form data and process it according to your requirement and send it to the library for payment processing. On this page, the return URL must be on any public domain. It will not work on localhost.
request.php
<?php
require_once 'Atompay.php';
if (isset($_POST['save'])) {
$name = $_POST['name_field'];
$email = $_POST['email'];
$address = $_POST['address'];
$mobile = $_POST['mobile'];
$amount = $_POST['amount'];
$env = "live"; //test or live
$login = 121120; //change your username
$password = 'abc@555'; //change your password
$product = "abcdemo"; // change your product name
$currency = "INR"; //change your currency
$returnUrl = 'http://www.example.com/response.php'; //return url of your website where you will create a response page
$clientCode = '45554'; //client code
$account = '63524'; //account
$requestHashKey = '79797erea8777dasfas'; //request hash key provided by atom
$atompay = new Atompay(); //create object of library
$atompay->atomRequest($env, $login, $password, $product, $amount, $currency, $returnUrl, $clientCode, $name, $email, $mobile, $address, $account, $requestHashKey); //call that library function by various data
}
In this file, I have included the library page. After that, receiving form data by post method of PHP and then creating an object of that library and calling their respective function to make a payment by various parameters.
On this page, environment value will be changed according to your project status that means if your project is used for testing purpose then pass the env value as test otherwise pass live.
The next page is a response page where we are getting the response that will be processed by the payment gateway.
response.php
<?php
require_once 'Atompay.php';
$atompay = new Atompay();
$response = $atompay->atomResponse('dfasdasewr46556456'); // change your response hash key
if (!is_array($response)) {
return "Response is not getting";
}
if (strtolower($response['f_code']) == 'ok') {
echo "Success";
} elseif (strtolower($response['f_code']) == 'c') {
echo "Cancel";
} else {
echo "Failed";
}
In this file, I have included a library and created an object of that library and calling their respective response function to get a response. On this page, you need to change the response hash key that you get from the atom.
Now, it will provide a response in an array format. If the response will not be in array format then return response is not getting otherwise it will check response with different status.
“Ok” status represents your payment process is successful and inside this condition, you can insert response data to your database. “c” represents cancel status, when any user does not make a payment and they cancelled payment then this condition will work. The next part represent a failed condition.
Video Tutorial: Click here to see video tutorial
Tutorials in Hindi: Click here to read this tutorial in Hindi language
Conclusion and Final Words
The integration of payment gateway is not a hard task but before integrating payment gateway, you must have proper knowledge about code that you are implementing because a little mistake changes your payment status.
Always try to use a secure method of implementation. Do not keep requests and response hash key on the client-side. Keep always it at the server side insecure way. So, no one can still your credentials and important data.
In this tutorial, you learned about the integration of the atom payment gateway in PHP. I hope this tutorial will be very helpful for you and others.
Hello Sir I have to more my extra field in atom payment gateway in response so how to get those field
You have to access data by using an object or array index according to the response.
Atom payment gateway using laravel
I will update an article on it as soon as possible.
ok thank you sir