کد:
class MabnaGateway
{
private
$merchant = "100110729900007" ,
$terminal = "10000046" ,
$crn = 'rnd' ,
$publicDirFile = 'pub_server.pem' ,
$privateDirFile ='prv.pem' ;
function __construct()
{
date_default_timezone_set('Asia/Tehran') ;
$this->publicDirFile = dirname(__FILE__) .'/pub_server.pem';
$this->privateDirFile = dirname(__FILE__) .'/prv.pem';
/*if( ! class_exists('nusoap_client'))
require_once dirname(__FILE__).'/nusoap.new.php';*/
}
function setPublicDirFile($_)
{
$this->publicDirFile = $_;
}
function setPrivateDirFile($_)
{
$this->publicDirFile = $_;
}
function setMerchant($_)
{
$this->merchant = $_;
}
function setTerminal($_)
{
$this->terminal = $_;
}
function setCrn($_)
{
$this->crn = $_;
}
function num2en($num)
{
$arr1 = array(0,1,2,3,4,5,6,7,8,9);
$arr2 = array('a','b','c','v','g','b','n','m','r','t');
return str_replace($arr1,$arr2,$num);
}
function getToken($amount , $orderID , $callback)
{
$client = new nusoap_client("https://mabna.shaparak.ir/TokenService?wsdl", 'wsdl');
$error = $client->getError();
if($error)
{
print_r($error);
die("<br> in file :". __FILE__ ." in line : ". __LINE__);
}
$source = $amount . $this->crn . "-{$orderID}" . $this->merchant . $callback . $this->terminal;
/*
if( ! file_exists($this->publicDirFile))
{
die(" public file not found .<br> in file :". __FILE__ ." in line : ". __LINE__);
}
*/
$fp=fopen($this->publicDirFile,"r");
$pub_key=fread($fp,8192);
fclose($fp);
$key_resource = openssl_get_publickey($pub_key);
// Amount
openssl_public_encrypt($amount, $crypttext, $key_resource );
$Amount = base64_encode($crypttext);
// CRN
openssl_public_encrypt($this->crn . "-{$orderID}", $crypttext, $key_resource );
$CRN = base64_encode($crypttext);
// MID
openssl_public_encrypt($this->merchant, $crypttext, $key_resource );
$MID = base64_encode($crypttext);
// TID
openssl_public_encrypt($this->terminal, $crypttext, $key_resource );
$TID = base64_encode($crypttext);
// TID
openssl_public_encrypt($callback, $crypttext, $key_resource );
$referal = base64_encode($crypttext);
/*
if( ! file_exists($this->privateDirFile))
{
die(" private file not found .<br> in file :". __FILE__ ." in line : ". __LINE__);
}
*/
// Sign data
$key = file_get_contents($this->privateDirFile);
$priv_key = openssl_pkey_get_private($key);
$signature = '';
if( ! openssl_sign($source, $signature, $priv_key, OPENSSL_ALGO_SHA1))
{
die(" make signature error <br> in file :". __FILE__ ." in line : ". __LINE__);
}
$inputArray = array("Token_param" => array("AMOUNT" => $Amount,
"CRN" => $CRN,
"MID" => $MID,
"REFERALADRESS" => $referal,
"SIGNATURE" => base64_encode($signature),
"TID" => $TID));
$WSResult = $client->call("reservation", $inputArray);
$error = $client->getError();
if($error)
{
print_r($error);
die("<br> in file :". __FILE__ ." in line : ". __LINE__);
}
if(empty($WSResult["return"]["token"]))
{
print_r($WSResult);
die("<br>token invalid in line ". __LINE__);
}
$signature = base64_decode($WSResult["return"]["signature"]);
// state whether signature is okay or not
$ok = openssl_verify($WSResult["return"]["token"], $signature, $key_resource);
if($ok ==0 or empty($ok) or $ok!=1)
die("bad signature");
// free the key from memory
openssl_free_key($key_resource);
return $WSResult["return"]["token"];
}
function sendConfirmation($amount , $orderID)
{
if( ! isset($_POST['AMOUNT']) or ! isset($_POST['CRN']) or ! isset($_POST['TRN']) or ! isset($_POST['RESCODE']))
{
die('input invalid!');
}
if(intval($_POST['RESCODE']) !=0)
{
return false;
}
if($_POST['CRN']!=$this->crn . "-{$orderID}")
return false;
if($_POST['AMOUNT']!=$amount)
die('invalid amount!');
$client = new nusoap_client("https://mabna.shaparak.ir/TransactionReference/TransactionReference?wsdl", 'wsdl');
$error = $client->getError();
if($error)
{
print_r($error);
die("<br> in file :". __FILE__ ." in line : ". __LINE__);
}
/*
if( ! file_exists($this->publicDirFile))
{
die(" public file not found .<br> in file :". __FILE__ ." in line : ". __LINE__);
}
*/
$fp=fopen($this->publicDirFile,"r");
$pub_key=fread($fp,8192);
fclose($fp);
$key_resource = openssl_get_publickey($pub_key);
// Amount
openssl_public_encrypt($_POST["TRN"], $crypttext, $key_resource );
$TRN = base64_encode($crypttext);
// CRN
openssl_public_encrypt($this->crn . "-{$orderID}", $crypttext, $key_resource );
$CRN = base64_encode($crypttext);
// MID
openssl_public_encrypt($this->merchant, $crypttext, $key_resource );
$MID = base64_encode($crypttext);
/*
if( ! file_exists($this->privateDirFile))
{
die(" private file not found .<br> in file :". __FILE__ ." in line : ". __LINE__);
}
*/
// Sign data
$source = $this->merchant . $_POST['TRN'] . $_POST['CRN'];
$key = file_get_contents($this->privateDirFile);
$priv_key = openssl_pkey_get_private($key);
$signature = '';
if( ! openssl_sign($source, $signature, $priv_key, OPENSSL_ALGO_SHA1))
{
die(" make signature error <br> in file :". __FILE__ ." in line : ". __LINE__);
}
$inputArray = array("SaleConf_req" => array("MID" => $MID,
"CRN" => $CRN,
"TRN" => $TRN,
"SIGNATURE" => base64_encode($signature)));
$WSResult = $client->call("sendConfirmation", $inputArray);
$error = $client->getError();
if($error)
{
print_r($error);
die("<br> in file :". __FILE__ ." in line : ". __LINE__);
}
if(empty($WSResult["return"]["SIGNATURE"]))
die('error in connect server');
$signature = base64_decode($WSResult["return"]["SIGNATURE"]);
$data = $WSResult["return"]["RESCODE"].$WSResult["return"]["REPETETIVE"].$WSResult["return"]["AMOUNT"].$WSResult["return"]["DATE"].$WSResult["return"]["TIME"].$WSResult["return"]["TRN"].$WSResult["return"]["STAN"];
$ok = openssl_verify($data, $signature, $key_resource);
if($ok == 1)
{
$out = true;
}
elseif ($ok == 0)
{
$out = false;
}
else
{
$out = false;
}
// free the key from memory
openssl_free_key($key_resource);
return $out;
}
}