Skip to content

Instantly share code, notes, and snippets.

@unlimitedprograming
Created August 12, 2021 08:27
Show Gist options
  • Save unlimitedprograming/5d6060841fd95dad5c14a1b8a04cc389 to your computer and use it in GitHub Desktop.
Save unlimitedprograming/5d6060841fd95dad5c14a1b8a04cc389 to your computer and use it in GitHub Desktop.
<?php
namespace App\Classes;
use SimpleXMLElement;
class MyIpay {
public $MerchantId;
public $Password;
public $Key;
public $Version;
public $API_URL;
public function __construct($ipay){
$this->MerchantId = $ipay['merchantId'];
$this->Password = $ipay['password'];
$this->Key = $ipay['key'];
$this->Version = $ipay['version'];
$this->API_URL = $ipay['api_url'];
}
public function saleTxn($payAmount, $redirectUrl)
{
$MerRefID = rand(1000, 99999999);
$Action = 'SaleTxn';
$TxnAmount = sprintf('%0.2f',$payAmount);
$currencyCode = 'NPR';
$LanguageCode = 'eng';
$ReturnURL = $redirectUrl;
$MerVar1 = '';
$MerVar2 = '';
$MerVar3 = '';
$MerVar4 = '';
$sItemList = '';
//Parameters for SetExpressCheckout, which will be sent to ipay
$Invoice = "";
$Invoice .= "<req>".
"<mer_id>" . $this->MerchantId . "</mer_id>".
"<mer_txn_id>" .$MerRefID. "</mer_txn_id>".
"<action>" . $Action . "</action>".
"<txn_amt>" . $TxnAmount . "</txn_amt>".
"<cur>" . $currencyCode . "</cur>" .
"<lang>" .$LanguageCode. "</lang>";
if($ReturnURL != "") {
$Invoice .= "<ret_url>" . $ReturnURL . "</ret_url>";
}
if($MerVar1 != "") {
$Invoice .= "<mer_var1>" .$MerVar1. "</mer_var1>";
}
if($MerVar2 != "") {
$Invoice .= "<mer_var2>" .$MerVar2. "</mer_var2>";
}
if($MerVar3 != "") {
$Invoice .= "<mer_var3>" .$MerVar3. "</mer_var3>";
}
if($MerVar4 != "") {
$Invoice .= "<mer_var4>" .$MerVar4. "</mer_var4>";
}
if($sItemList != "") {
$Invoice .= "<item_list>" .$sItemList. "</item_list>";
}
$Invoice .= "</req>";
//adding hash val
$sHash = hash('sha256', $Invoice.$this->Key,false);
$ServerParam = 'VERSION='.$this->Version.'&PWD='.$this->Password.'&MERCHANTID='.$this->MerchantId.'&KEY='.$this->Key.'&HASH='.$sHash;
$url = $this->API_URL;
//$myXML = $ServerParam.'&PTINVOICE='.$Invoice;
$myXML = $ServerParam.'&PTINVOICE='.bin2hex($Invoice);
$options = array
(
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $myXML,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false
);
$curl = curl_init();
curl_setopt_array($curl, $options);
$response = curl_exec($curl);
curl_close($curl);
$myhashmap = array();
// Extract the response details.
$httpResponseAr = explode("&", $response);
foreach ($httpResponseAr as $i => $value) {
$tmpAr = explode("=", $value);
if(count($tmpAr) > 1) {
$myhashmap[strtoupper($tmpAr[0])] = $tmpAr[1];
}
}
if (strcasecmp($myhashmap['ERROR_CODE'], '000') == 0)
{
if (true) //(strcasecmp($myhashmap['HASH'], $sHash1) == 0)
{
$xml = new SimpleXMLElement($this->hextostr( $myhashmap['PTRECEIPT'] ));
$TxnUuid = (string)$xml->txn_uuid;
$ipg_txn_id = (string)$xml->ipg_txn_id;
$myhashmap['status'] = 1;
//$TxnUuid_hex = bin2hex($TxnUuid.'|'.$this->Key); // gayan
$TxnUuid_hex = $TxnUuid; // gayan
if(0 < strlen($TxnUuid))
{
$myhashmap['ipay_out__txn_uuid'] = $TxnUuid_hex;
}
if(0 < strlen(trim($ipg_txn_id)))
{
$myhashmap['ipay_out__ipg_txn_id'] = $ipg_txn_id;
}
}
else
{
$myhashmap = array(
'status' => 0,
'message' => 'ERROR'
);
}
}
else
{
$myhashmap = array(
'status' => 0,
'message' => 'ERROR'
);
}
return $myhashmap;
}
function saleTxnVerify($TxnUUID, $MerRefID)
{
$Action = 'saleTxnVerify';
$LanguageCode = 'eng';
//Parameters for SetExpressCheckout, which will be sent to ipay
$PTInvoice = "";
$PTInvoice .= "<req>".
"<mer_id>" . $this->MerchantId . "</mer_id>".
"<mer_txn_id>" .$MerRefID. "</mer_txn_id>".
"<txn_uuid>" . $TxnUUID . "</txn_uuid>".
"<action>" . $Action . "</action>" .
"</req>";
//adding hash val
$sHashverify = hash('sha256', $PTInvoice.$this->Key,false);
$ServerParam = 'VERSION='.$this->Version.'&PWD='.$this->Password.'&MERCHANTID='.$this->MerchantId.'&KEY='.$this->Key.'&HASH='.$sHashverify;
$url = $this->API_URL;
//$myXML = $ServerParam.'&PTINVOICE='.$PTInvoice;
$myXML = $ServerParam.'&PTINVOICE='.bin2hex($PTInvoice);
$options = array
(
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $myXML,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false
);
$curl = curl_init();
curl_setopt_array($curl, $options);
$response = curl_exec($curl);
curl_close($curl);
$myhashmap = array();
// Extract the response details.
$httpResponseAr = explode("&", $response);
foreach ($httpResponseAr as $i => $value) {
$tmpAr = explode("=", $value);
if(count($tmpAr) > 1) {
$myhashmap[strtoupper($tmpAr[0])] = $tmpAr[1];
}
}
//echo '<pre>', htmlspecialchars('ERROR_CODE='. $myhashmap['ERROR_CODE']. '&PTRECEIPT='. $this->hextostr($myhashmap['PTRECEIPT'])), '</pre>';
if (strcasecmp($myhashmap['ERROR_CODE'], '000') == 0)
{
$sHash1 = hash('sha256', $myhashmap['PTRECEIPT'].$this->Key,false);
if (true) //(strcasecmp($myhashmap['HASH'], $sHash1) == 0)
{
$xml = new SimpleXMLElement($this->hextostr( $myhashmap['PTRECEIPT'] ));
$BankRefID = (string)$xml->bank_ref_id;
$CurrencyCode = (string)$xml->cur;
$IPGTransactionID = (string)$xml->ipg_txn_id;
$MerRefID = (string)$xml->mer_txn_id;
$TxnAmount = (string)$xml->txn_amt;
$TxnStatus = (string)$xml->txn_status;
$ServerTime = (string)$xml->server_time;
$MaskedAccNo = (string)$xml->acc_no;
$LanguageCode = (string)$xml->lang;
$MerVar1 = (string)$xml->mer_var1;
$MerVar2 = (string)$xml->mer_var2;
$MerVar3 = (string)$xml->mer_var3;
$MerVar4 = (string)$xml->mer_var4;
$CustomerName = (string)$xml->name;
$FailReason = (string)$xml->reason;
$AuthCode = (string)$xml->auth_code;
$myhashmap = array();
$myhashmap['status'] = 1;
if($BankRefID)
{
$myhashmap['ipay_out__bank_ref_id'] = $BankRefID;
}
if($CurrencyCode)
{
$myhashmap['ipay_out__cur_code'] = $CurrencyCode;
}
if($IPGTransactionID)
{
$myhashmap['ipay_out__ipg_txn_id'] = $IPGTransactionID;
}
if($MerRefID)
{
$myhashmap['ipay_out__mer_ref_id'] = $MerRefID;
}
if($TxnAmount)
{
$myhashmap['ipay_out__txn_amt'] = $TxnAmount;
}
if($TxnStatus)
{
$myhashmap['ipay_out__txn_status'] = $TxnStatus;
}
if($ServerTime)
{
$myhashmap['ipay_out__server_time'] = $ServerTime;
}
if($MaskedAccNo)
{
$myhashmap['ipay_out__masked_acc_number'] = $MaskedAccNo;
}
if($LanguageCode)
{
$myhashmap['ipay_out__lang'] = $LanguageCode;
}
if($MerVar1)
{
$myhashmap['ipay_out__mer_var_1'] = $MerVar1;
}
if($MerVar2)
{
$myhashmap['ipay_out__mer_var_2'] = $MerVar2;
}
if($MerVar3)
{
$myhashmap['ipay_out__mer_var_3'] = $MerVar3;
}
if($MerVar4)
{
$myhashmap['ipay_out__mer_var_4'] = $MerVar4;
}
if($CustomerName)
{
$myhashmap['ipay_out__card_holder_name'] = $CustomerName;
}
if($FailReason)
{
$myhashmap['ipay_out__fail_reason'] = $FailReason;
}
if($AuthCode)
{
$myhashmap['ipay_out__auth_code'] = $AuthCode;
}
}
else
{
$myhashmap['status'] = 0;
$myhashmap['message'] = 'Hash Verification Failed';
}
}
else
{
$myhashmap['status'] = 0;
$myhashmap['message'] = 'Verification Failed';
}
return $myhashmap;
}
function hextostr($hex)
{
$str='';
for ($i=0; $i < strlen($hex)-1; $i+=2)
{
$str .= chr(hexdec($hex[$i].$hex[$i+1]));
}
//echo '<pre>', htmlspecialchars($str), '</pre>';
return $str;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment