215 lines
6.0 KiB
PHP
215 lines
6.0 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace integration;
|
||
|
|
|
||
|
|
//*
|
||
|
|
// Показывать ли все ошибки
|
||
|
|
error_reporting(E_ALL);
|
||
|
|
ini_set('display_errors',true);
|
||
|
|
/*/
|
||
|
|
ini_set('display_errors','Off');
|
||
|
|
/**/
|
||
|
|
class EPay{
|
||
|
|
|
||
|
|
private $private_key;
|
||
|
|
private $invert;
|
||
|
|
|
||
|
|
var $cert_id;
|
||
|
|
var $merchant_name;
|
||
|
|
var $merchant_id;
|
||
|
|
|
||
|
|
public function __construct($cert_id,$merchant_name,$merchant_id) {
|
||
|
|
$this->cert_id = $cert_id;
|
||
|
|
$this->merchant_name = $merchant_name;
|
||
|
|
$this->merchant_id = $merchant_id;
|
||
|
|
}
|
||
|
|
|
||
|
|
function createLink($order_id,$currency="398"){
|
||
|
|
|
||
|
|
$amount = 50;
|
||
|
|
|
||
|
|
|
||
|
|
// -----------------------------------------------------------------------------------------------------------------
|
||
|
|
// Переворачиваем ключ
|
||
|
|
|
||
|
|
$this->invert();
|
||
|
|
|
||
|
|
// -----------------------------------------------------------------------------------------------------------------
|
||
|
|
// Открываем публичный ключ. "ключ", "пароль"
|
||
|
|
|
||
|
|
$this->load_private_key(__DIR__."/test.pem", "WDfUveEf9i3");
|
||
|
|
|
||
|
|
// -----------------------------------------------------------------------------------------------------------------
|
||
|
|
// Шаблон заказа
|
||
|
|
|
||
|
|
$merchant = '<merchant cert_id="%certificate%" name="%merchant_name%"><order order_id="%order_id%" amount="%amount%" currency="%currency%"><department merchant_id="%merchant_id%" amount="%amount%"/></order></merchant>';
|
||
|
|
|
||
|
|
// -----------------------------------------------------------------------------------------------------------------
|
||
|
|
// Подготовка данных заказа, обработка шаблона
|
||
|
|
|
||
|
|
$merchant = preg_replace('/\%certificate\%/', $this->cert_id , $merchant);
|
||
|
|
$merchant = preg_replace('/\%merchant_name\%/', $this->merchant_name , $merchant);
|
||
|
|
$merchant = preg_replace('/\%order_id\%/', $order_id, $merchant);
|
||
|
|
$merchant = preg_replace('/\%currency\%/', $currency, $merchant);
|
||
|
|
$merchant = preg_replace('/\%merchant_id\%/', $this->merchant_id, $merchant);
|
||
|
|
$merchant = preg_replace('/\%amount\%/', $amount, $merchant);
|
||
|
|
|
||
|
|
// -----------------------------------------------------------------------------------------------------------------
|
||
|
|
// Подготавливаем подпись
|
||
|
|
|
||
|
|
$merchant_sign = '<merchant_sign type="RSA">'.$this->sign64($merchant).'</merchant_sign>';
|
||
|
|
|
||
|
|
|
||
|
|
// -----------------------------------------------------------------------------------------------------------------
|
||
|
|
// Составляем пакет
|
||
|
|
|
||
|
|
$xml = "<document>".$merchant.$merchant_sign."</document>";
|
||
|
|
|
||
|
|
|
||
|
|
// -----------------------------------------------------------------------------------------------------------------
|
||
|
|
// Выводим пакет
|
||
|
|
|
||
|
|
//echo $xml;
|
||
|
|
|
||
|
|
/*
|
||
|
|
$url = 'https://testpay.kkb.kz/jsp/process/logon.jsp';
|
||
|
|
//$url = 'https://epay.kkb.kz/jsp/process/logon.jsp';
|
||
|
|
if ($curl = curl_init()){
|
||
|
|
curl_setopt($curl, CURLOPT_URL, $url);
|
||
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
|
||
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
|
||
|
|
curl_setopt($curl, CURLOPT_POST,1);
|
||
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS,[
|
||
|
|
'Signed_Order_B64'=>$xml,
|
||
|
|
'email'=>'starosta-91@ya.ru',
|
||
|
|
'BackLink'=>$url,
|
||
|
|
'PostLink'=>$url,
|
||
|
|
'FailureBackLink'=>$url,
|
||
|
|
]);
|
||
|
|
$out = curl_exec($curl);
|
||
|
|
echo '<pre>';
|
||
|
|
echo var_dump($out);
|
||
|
|
echo '</pre>';
|
||
|
|
curl_close($curl);
|
||
|
|
$res = json_decode($out);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**/
|
||
|
|
$items = '<document><item number="1" name="Тест" quantity="1" amount="1"/></document>';
|
||
|
|
|
||
|
|
return [
|
||
|
|
'order'=>base64_encode($xml),
|
||
|
|
'items'=>base64_encode($items),
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
function load_private_key($filename, $password = NULL){
|
||
|
|
|
||
|
|
if(!is_file($filename))
|
||
|
|
{
|
||
|
|
|
||
|
|
echo "Key not found";
|
||
|
|
return false;
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
$c = file_get_contents($filename);
|
||
|
|
|
||
|
|
if($password)
|
||
|
|
|
||
|
|
$prvkey = openssl_get_privatekey($c, $password) or die(openssl_error_string());
|
||
|
|
|
||
|
|
else
|
||
|
|
|
||
|
|
$prvkey = openssl_get_privatekey($c) or die(openssl_error_string());
|
||
|
|
|
||
|
|
|
||
|
|
if(is_resource($prvkey)){
|
||
|
|
|
||
|
|
$this->private_key = $prvkey;
|
||
|
|
return $c;
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
return false;
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
// -----------------------------------------------------------------------------------------------
|
||
|
|
// Установка флага инверсии
|
||
|
|
|
||
|
|
function invert(){
|
||
|
|
|
||
|
|
$this->invert = 1;
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
// -----------------------------------------------------------------------------------------------
|
||
|
|
// Процесс инверсии строки
|
||
|
|
|
||
|
|
function reverse($str){
|
||
|
|
|
||
|
|
return strrev($str);
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
// -----------------------------------------------------------------------------------------------
|
||
|
|
|
||
|
|
|
||
|
|
function sign($str)
|
||
|
|
{
|
||
|
|
|
||
|
|
if($this->private_key)
|
||
|
|
{
|
||
|
|
|
||
|
|
openssl_sign($str, $out, $this->private_key);
|
||
|
|
|
||
|
|
if($this->invert == 1) $out = $this->reverse($out);
|
||
|
|
|
||
|
|
return $out;
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
// -----------------------------------------------------------------------------------------------
|
||
|
|
|
||
|
|
|
||
|
|
function sign64($str){
|
||
|
|
|
||
|
|
return base64_encode($this->sign($str));
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
// -----------------------------------------------------------------------------------------------
|
||
|
|
|
||
|
|
|
||
|
|
function check_sign($data, $str, $filename){
|
||
|
|
|
||
|
|
if($this->invert == 1) $str = $this->reverse($str);
|
||
|
|
|
||
|
|
if(!is_file($filename)) return false;
|
||
|
|
|
||
|
|
$pubkey = file_get_contents($filename);
|
||
|
|
|
||
|
|
return openssl_verify($data, $str, $pubkey);
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
// -----------------------------------------------------------------------------------------------
|
||
|
|
|
||
|
|
|
||
|
|
function check_sign64($data, $str, $filename){
|
||
|
|
|
||
|
|
return $this->check_sign($data, base64_decode($str), $filename);
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|