-
Notifications
You must be signed in to change notification settings - Fork 5
/
KKBPaymentResult.php
224 lines (195 loc) · 7.68 KB
/
KKBPaymentResult.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/**
* Created by PhpStorm.
* User: naffiq
* Date: 9/30/2016
* Time: 4:13 PM
*/
namespace naffiq\kkb;
use LaLit\Array2XML;
use yii\base\Model;
/**
* Class KKBPaymentResult
* @package app\modules\payment\components
*/
class KKBPaymentResult extends Model
{
/**
* @var string Bank name
*/
public $bankName;
/**
* @var string
*/
public $customerName;
/**
* @var string
*/
public $customerEmail;
/**
* @var string
*/
public $customerPhone;
/**
* @var string Серийный номер сертификата
*/
public $merchantCertId;
/**
* @var string имя магазина(сайта)
*/
public $merchantName;
/**
* @var int Номер заказа(должен состоять не менее чем из 6 ЧИСЛОВЫХ знаков, максимально -15)
*/
public $orderId;
/**
* @var int сумма заказа
*/
public $orderAmount;
/**
* @var int код валюты оплаты [ 398 - тенге ]
*/
public $orderCurrency;
/**
* @var string время проведения платежа
*/
public $paymentDate;
/**
* @var int ID продавца в платежной системе
*/
public $merchantId;
/**
* @var string номер обращения к платежной системе
*/
public $paymentReference;
/**
* @var string код авторизации
*/
public $paymentApprovalCode;
/**
* @var string код результата авторизации. Должен иметь значение "00" (два нуля),
* в противном случае свяжитесь, пожалуйста, с администратором системы авторизации
*/
public $paymentResponseCode;
/**
* @var bool Yes/No признак, что транзакция была 3DSecure или нет
*/
public $paymentSecure;
/**
* @var string Страна эмитент карты
*/
public $paymentCardBin;
/**
* @var string Хэш карты
*/
public $paymentCHash;
/**
* @var bool
*/
public $paymentSuccessful;
/**
* @var string
*/
public $paymentErrorType;
/**
* @var bool
*/
public $signErrors = false;
/**
* @param array $response
* @param string $originalResponse
* @param $kkbComponent bool|KKBPayment
* @return KKBPaymentResult
*/
public static function parseSuccessData($response, $originalResponse, KKBPayment $kkbComponent = null)
{
$object = new static();
$object->paymentSuccessful = true;
$object->checkSignErrors($response, $originalResponse, $kkbComponent);
$data = $response['document']['bank'];
$object->bankName = $data['@attributes']['name'];
$object->paymentDate = $data['results']['@attributes']['timestamp'];
$object->setCustomerAttributes($data['customer']['@attributes']);
$object->setMerchantAttributes($data['customer']['merchant']['@attributes']);
$orderData = $data['customer']['merchant']['order'];
$object->merchantId = $orderData['department']['@attributes']['merchant_id'];
$object->setOrderAttributes($orderData['@attributes']);
$object->setPaymentAttributes($data['results']['payment']['@attributes']);
return $object;
}
public function checkSignErrors($response, $originalResponse, KKBPayment $kkbComponent = null)
{
$bankXmlStart = strpos($originalResponse, '<bank');
$bankXmlLength = strpos($originalResponse, '</bank>') - $bankXmlStart + strlen('</bank>');
$bankXml = substr($originalResponse, $bankXmlStart, $bankXmlLength);
// $bankXml = str_replace(' ', '', $bankXml);
// $bankXml = '<bank name="Kazkommertsbank JSC"><customer name="TSET TEST" mail="[email protected]" phone=""><merchant cert_id="00C182B189" name="Test shop"><order order_id="400124" amount="1000" currency="398"><department merchant_id="92061101" amount="1000"/></order></merchant><merchant_sign type="RSA"/></customer><customer_sign type="RSA"/><results timestamp="2017-11-24 19:08:56"><payment merchant_id="92061101" card="440564-XX-XXXX-6150" amount="1000" reference="171124190855" approval_code="190855" response_code="00" Secure="No" card_bin="" c_hash="13988BBF7C6649F799F36A4808490A3E"/></results></bank>';
// <bank name="Kazkommertsbank JSC"><customer name="TSET TEST" mail="[email protected]" phone=""><merchant cert_id="00C182B189" name="Test shop"><order order_id="400124" amount="1000" currency="398"><department merchant_id="92061101" amount="1000"></department></order></merchant><merchant_sign type="RSA"></merchant_sign></customer><customer_sign type="RSA"></customer_sign><results timestamp="2017-11-24 19:08:56"><payment merchant_id="92061101" card="440564-XX-XXXX-6150" amount="1000" reference="171124190855" approval_code="190855" response_code="00" Secure="No" card_bin="" c_hash="13988BBF7C6649F799F36A4808490A3E"></payment></results></bank>
$bankSign = $response['document']['bank_sign']['@value'];
$kkb = $kkbComponent ?: \Yii::$app->get('kkbPayment');
$kkbSign = new KKBSign();
$kkbSign->invert();
$this->signErrors = $kkbSign->checkSign64($bankXml, $bankSign, \Yii::getAlias($kkb->publicKeyPath)) !== 1;
}
/**
* Обработка результата, в случае неудачной оплаты
*
* @param $response array Массив с данными об ошибке
* @return KKBPaymentResult
*/
public static function parseErrorData($response)
{
$object = new static();
$object->paymentSuccessful = false;
$object->orderId = $response['response']['@attributes']['order_id'];
$errorAttributes = $response['response']['error']['@attributes'];
$object->paymentErrorType = $errorAttributes['type'];
$object->paymentDate = $errorAttributes['time'];
$object->paymentResponseCode = $errorAttributes['code'];
return $object;
}
/**
* Метод для задачи аттрибутов покупателя
*
* @param $attributes
*/
public function setCustomerAttributes($attributes)
{
$this->customerName = $attributes['name'];
$this->customerEmail = $attributes['mail'];
$this->customerPhone = $attributes['phone'];
}
/**
* Метод для задачи аттрибутов продавца
*
* @param $merchantAttributes
*/
public function setMerchantAttributes($merchantAttributes)
{
$this->merchantCertId = $merchantAttributes['cert_id'];
$this->merchantName = $merchantAttributes['name'];
}
/**
* Метод для задачи аттрибутов заказа
*
* @param $orderAttributes
*/
public function setOrderAttributes($orderAttributes)
{
$this->orderId = $orderAttributes['order_id'];
$this->orderAmount = $orderAttributes['amount'];
$this->orderCurrency = $orderAttributes['currency'];
}
/**
* @param $paymentResultData
*/
public function setPaymentAttributes($paymentResultData)
{
$this->paymentReference = $paymentResultData['reference'];
$this->paymentApprovalCode = $paymentResultData['approval_code'];
$this->paymentResponseCode = $paymentResultData['response_code'];
$this->paymentSecure = $paymentResultData['Secure'];
$this->paymentCardBin = $paymentResultData['card_bin'];
$this->paymentCHash = $paymentResultData['c_hash'];
}
}