-
Notifications
You must be signed in to change notification settings - Fork 5
/
KKBPayment.php
179 lines (156 loc) · 5 KB
/
KKBPayment.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/**
* Created by PhpStorm.
* User: naffiq
* Date: 9/13/2016
* Time: 4:27 PM
*/
namespace naffiq\kkb;
use LaLit\Array2XML;
use LaLit\XML2Array;
use yii\base\Component;
use yii\base\Exception;
use Yii;
/**
* Class KKBPayment
*
* Компонент для генерации поля Signed_Order_B64
*
* @package app\modules\payment\components
*/
class KKBPayment extends Component
{
/**
* @var string расположение приватного ключа для проверки
*/
public $publicKeyPath;
/**
* @var string расположение приватного ключа для подписи
*/
public $privateKeyPath;
/**
* @var string пароль к приватному ключу
*/
public $privateKeyPassword;
/**
* @var integer ID продавца
*/
public $merchantId;
/**
* @var integer ID сертификата продавца
*/
public $merchantCertificateId;
/**
* @var string Название интернет-магазина/портала
*/
public $merchantName;
/**
* Функция, для генерации поля Signed_Order_B64
*
* @param integer $orderId - order index - recoded to 6 digit format with leaded zero
* @param integer $amount - total payment amount
* @param bool $b64 - flag to encode result in base64 default = true
* @param int $currencyCode - preferred currency codes 840-USD, 398-Tenge
*
* @return string
* @throws Exception
*/
public function processRequest($orderId, $amount, $currencyCode = 398, $b64 = true)
{
if (is_numeric($orderId)) {
if ($orderId > 0) {
$orderId = sprintf("%06d", $orderId);
} else {
throw new Exception("Null Order ID");
};
} else {
throw new Exception("Order ID must be number");
};
if (strlen($currencyCode) == 0) {
throw new Exception("Empty Currency code");
};
if ($amount == 0) {
throw new Exception("Nothing to charge");
};
if (empty($this->privateKeyPath)) {
throw new Exception("Path for Private key not found");
};
$kkb = new KKBSign();
$kkb->invert();
$kkb->loadPrivateKey(Yii::getAlias($this->privateKeyPath), $this->privateKeyPassword);
$result = $this->generateMerchantXML($orderId, $amount, $currencyCode);
$result_sign = '<merchant_sign type="RSA">' . $kkb->sign64($result) . '</merchant_sign>';
$xml = "<document>" . $result . $result_sign . "</document>";
if ($b64) {
return base64_encode($xml);
} else {
return $xml;
}
}
/**
* Генерирует XML для данных об оплате
*
* @param integer $orderId - order index - recoded to 6 digit format with leaded zero
* @param integer $amount - total payment amount
* @param int $currencyCode - preferred currency codes 840-USD, 398-Tenge
* @return mixed
*/
private function generateMerchantXML($orderId, $amount, $currencyCode = 398)
{
$xml = Array2XML::createXML('merchant', [
'@attributes' => [
'cert_id' => $this->merchantCertificateId,
'name' => $this->merchantName,
],
'order' => [
'@attributes' => [
'order_id' => $orderId,
'amount' => $amount,
'currency' => $currencyCode,
],
'department' => [
'@attributes' => [
'merchant_id' => $this->merchantId,
'amount' => $amount,
]
],
]
])->saveXML();
return self::trimXML($xml);
}
/**
* Подготовка XML к отправке в ККБ
*
* @param $xml string
*
* @return string
*/
private static function trimXML($xml)
{
/*
* Удаляем первую строку сгенерированного файла
* (<?xml version="1.0" encoding="UTF-8"?>)
*/
$xml = preg_replace('/^.+\n/', '', $xml);
/*
* Удаляем пробелы, табы, символ обрыва строки
*/
return preg_replace('/^\s+|\n|\r|\s+$/m', '', $xml);
}
/**
* Process incoming XML to array of values with verifying digital-key
*
* @param string $response XML response from bank
* @return null|KKBPaymentResult
*/
public function processResponse($response) {
$result = XML2Array::createArray($response);
if (!empty($result["response"])){
return KKBPaymentResult::parseErrorData($result);
};
if (!empty($result['document'])){
return KKBPaymentResult::parseSuccessData($result, $response, $this);
};
return null;
}
}