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 atom Payment Gateway in PHP With Database Connection


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

It is very easy to integrate in your website, App. Also you send Payment link for a perticular amount through Atom payment button.

Please follow the below four step to integrate Instamojo in your PHP website.

  1. Apply Atom by click on this link.
  2. Copy the below files and upload in a folder.
  3. Put your authenication key, private key and working key in the files where it required.
  4. Run the index.php file now.

Note: http://localhost list not working in instamojo payment gateway. So please add your domain name in redirection URL.

AtomAES.php

<?php

/*
 * Author  : Mr. Ankit Sharma (21/11/2018)
 * Designation/Company    : Web Developer/atom technologies ltd.
 */

class AtomAES {

    public function encrypt($data = '', $key = NULL, $salt = "") {
        if($key != NULL && $data != "" && $salt != ""){
            
            $method = "AES-256-CBC";
            
            /*Converting Array to bytes*/
            $iv = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
            $chars = array_map("chr", $iv);
            $IVbytes = join($chars);
            
            
            $salt1 = mb_convert_encoding($salt, "UTF-8"); /*Encoding to UTF-8*/
            $key1 = mb_convert_encoding($key, "UTF-8"); /*Encoding to UTF-8*/
            
            /*SecretKeyFactory Instance of PBKDF2WithHmacSHA1 Java Equivalent*/
            $hash = openssl_pbkdf2($key1,$salt1,'256','65536', 'sha1'); 
            
            $encrypted = openssl_encrypt($data, $method, $hash, OPENSSL_RAW_DATA, $IVbytes);
            
            return bin2hex($encrypted);
        }else{
            return "String to encrypt, Salt and Key is required.";
        }
    }

    public function decrypt($data="", $key = NULL, $salt = "") {
        if($key != NULL && $data != "" && $salt != ""){
            $dataEncypted = hex2bin($data);
            $method = "AES-256-CBC";
            
            /*Converting Array to bytes*/
            $iv = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
            $chars = array_map("chr", $iv);
            $IVbytes = join($chars);
            
            $salt1 = mb_convert_encoding($salt, "UTF-8");/*Encoding to UTF-8*/
            $key1 = mb_convert_encoding($key, "UTF-8");/*Encoding to UTF-8*/
            
            /*SecretKeyFactory Instance of PBKDF2WithHmacSHA1 Java Equivalent*/
            $hash = openssl_pbkdf2($key1,$salt1,'256','65536', 'sha1'); 
             
            $decrypted = openssl_decrypt($dataEncypted, $method, $hash, OPENSSL_RAW_DATA, $IVbytes);
            return $decrypted;
        }else{

            return "Encrypted String to decrypt, Salt and Key is required.";

        }
    }

}

?>

Transaction-Response.php

<?php

require_once 'AtomAES.php';

class TransactionResponse {

    private $respHashKey = "";
    private $responseEncryptionKey = "";
    private $salt = "";

    /**
     * @return string
     */
    public function getRespHashKey()
    {
        return $this->respHashKey;
    }
    
    public function setResponseEncypritonKey($key){
        $this->responseEncryptionKey = $key;
    }
    
    public function setSalt($saltEntered){
        $this->salt = $saltEntered;
    }

    /**
     * @param string $respHashKey
     */
    public function setRespHashKey($respHashKey)
    {
        $this->respHashKey = $respHashKey;
    }

    public function decryptResponseIntoArray($encdata){

        $atomenc = new AtomAES();
        $decrypted = $atomenc->decrypt($encdata, $this->responseEncryptionKey, $this->salt);
        $array_response = explode('&', $decrypted);
        $equalSplit = array();
        foreach ($array_response as $ar) {
            $equalSub = explode('=', $ar);
            $temp = array(
                $equalSub[0] => $equalSub[1],
            );
            $equalSplit += $temp;
        }
        
        return $equalSplit;

    }

    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;
        }

    }
}

	
 

Transaction-request.php

    <?php
require_once 'AtomAES.php';


/**
 * Version 1.0
 */
class TransactionRequest
{
    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 $salt = "";
    
	private $requestEncypritonKey = "";
    
	private $responseEncryptionKey = "";


    public function setRequestEncypritonKey($key){
        $this->requestEncypritonKey = $key;
    }
    
    public function setResponseEncypritonKey($key){
        $this->responseEncryptionKey = $key;
    }
    
    public function setSalt($saltEntered){
        $this->salt = $saltEntered;
    }
    
   
    /**
     * @return string
     */
    public function getReqHashKey()
    {
        return $this->reqHashKey;
    }

    /**
     * @param string $reqHashKey
     */
    public function setReqHashKey($reqHashKey)
    {
        $this->reqHashKey = $reqHashKey;
    }

    /**
     * @return string
     */
    public function getRespHashKey()
    {
        return $this->respHashKey;
    }

    /**
     * @param string $respHashKey
     */
    public function setRespHashKey($respHashKey)
    {
        $this->respHashKey = $respHashKey;
    }
	



    /**
     * @return the $login
     */
    public function getLogin()
    {
        return $this->login;
    }

    /**
     * @param string $login
     */
    public function setLogin($login)
    {
        $this->login = $login;
    }
	
	/**
     * @return the $password
     */
    public function getPassword()
    {
        return $this->password;
    }

    /**
     * @param string $password
     */
    public function setPassword($password)
    {
        $this->password = $password;
    }
	
	/**
     * @return the $transactionType
     */
    public function getTransactionType()
    {
        return $this->transactionType;
    }

    /**
     * @param string $transactionType
     */
    public function setTransactionType($transactionType)
    {
        $this->transactionType = $transactionType;
    }
	
	/**
     * @return the $productId
     */
    public function getProductId()
    {
        return $this->productId;
    }

    /**
     * @param string $productId
     */
    public function setProductId($productId)
    {
        $this->productId = $productId;
    }
	
	/**
     * @return the $amount
     */
    public function getAmount()
    {
        return $this->amount;
    }

    /**
     * @param string $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;
	}
	
	public function setUrl($url) {
		$port = 443;		
		$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);

        return $signature;
    }

    private function getData(){
		$strReqst = "";
		$strReqst .= "login=".$this->getLogin();
		$strReqst .= "&pass=".$this->getPassword();
		
        $strReqst .= "&ttype=NBFundTransfer";
        
		$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();
        $atomenc = new AtomAES();
        $encData = $atomenc->encrypt($strReqst, $this->requestEncypritonKey, $this->salt);
        return "login=".$this->getLogin()."&encdata=".strtoupper($encData);
    }

    /**
     * This function returns transaction token url
     * @return string
     */
    public function getPGUrl(){
        if ($this->mode != null && $this->mode != "") {
            try {
                $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";
		$fp = fopen("log/".$fileName, 'a+');
		$data = date("Y-m-d H:i:s")." - ".$data;
		fwrite($fp,$data);
		fclose($fp);
	}
    

}

index.php

<?php
    date_default_timezone_set('Asia/Kolkata');
    $datenow = date("d/m/Y h:m:s");
    $transactionDate = str_replace(" ", "%20", $datenow);
    $transactionId = 100;
    require_once 'TransactionRequest.php';
    $transactionRequest = new TransactionRequest();
    /*Setting all values here*/
    $transactionRequest->setLogin(192);
    $transactionRequest->setPassword("Test@123");
    $transactionRequest->setProductId("NSE");
    $transactionRequest->setAmount('15.55');
    $transactionRequest->setTransactionCurrency("INR");
    $transactionRequest->setTransactionAmount('15.55');
    $transactionRequest->setReturnUrl("http://localhost:8080/Atompay/response.php");
    $transactionRequest->setClientCode('NAVIN');
    $transactionRequest->setTransactionId($transactionId);
    $transactionRequest->setTransactionDate($transactionDate);
    $transactionRequest->setCustomerName("Test Name");
    $transactionRequest->setCustomerEmailId("test@test.com");
    $transactionRequest->setCustomerMobile("9999999999");
    $transactionRequest->setCustomerBillingAddress("Mumbai");
    $transactionRequest->setCustomerAccount("639827");
    $transactionRequest->setReqHashKey("KEY123657234");
    $transactionRequest->seturl("https://paynetzuat.atomtech.in/paynetz/epi/fts");
    $transactionRequest->setRequestEncypritonKey("8E41C78439831010F81F61C344B7BFC7");
    $transactionRequest->setSalt("8E41C78439831010F81F61C344B7BFC7");
    $url = $transactionRequest->getPGUrl();
    header("Location: $url");

response.php

<?php
    require_once 'TransactionResponse.php';
    $transactionResponse = new TransactionResponse();
    $transactionResponse->setRespHashKey("KEYRESP123657234");
    $transactionResponse->setResponseEncypritonKey("8E41C78439831010F81F61C344B7BFC7");
    $transactionResponse->setSalt("8E41C78439831010F81F61C344B7BFC7");
    $arrayofdata = $transactionResponse->decryptResponseIntoArray($_POST['encdata']);
    print_r($arrayofdata);