-
Notifications
You must be signed in to change notification settings - Fork 1
/
Api.php
377 lines (329 loc) · 11.8 KB
/
Api.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<?php
namespace Villafinder\Payum2c2p;
use Http\Message\MessageFactory;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Exception\LogicException;
use Payum\Core\HttpClientInterface;
use Payum\ISO4217\Currency;
use Payum\ISO4217\ISO4217;
class Api
{
const VERSION = '6.9';
const VERSION_ONSITE = '9.9';
const HASH_OFFSITE = 'sha1';
const HASH_ONSITE = 'sha256';
/**
* @var HttpClientInterface
*/
protected $client;
/**
* @var MessageFactory
*/
protected $messageFactory;
/**
* @var array
*/
protected $options = [];
public function __construct(array $options, HttpClientInterface $client, MessageFactory $messageFactory)
{
$options = ArrayObject::ensureArrayObject($options);
$options->defaults($this->options);
$options->validatedKeysSet(array(
'currencies',
'trust_user_request',
));
if (!is_array($options['currencies'])) {
throw new LogicException('The currencies option must be an array.');
}
$iso = new ISO4217();
foreach ($options['currencies'] as $currency => $currencyOptions) {
try {
$iso->findByCode($currency);
} catch (\RuntimeException $e) {
throw new LogicException(sprintf('Currency "%s" is not found in ISO 4217.', $currency), $e->getCode(), $e);
}
if (empty($currencyOptions['merchant_id']) || empty($currencyOptions['merchant_auth_key'])) {
throw new LogicException(sprintf('Currency %s must have its options merchant_id and merchant_auth_key defined. Got %s.', $currency, implode(', ', array_keys($currencyOptions))));
}
}
if (false == is_bool($options['sandbox'])) {
throw new LogicException('The boolean sandbox option must be set.');
}
$this->options = $options;
$this->client = $client;
$this->messageFactory = $messageFactory;
}
/**
* @param string $currencyCode
* @return array
*/
public function getCurrencyConfigByCode($currencyCode)
{
$currencyCode = strtolower($currencyCode);
if (!isset($this->options['currencies'][$currencyCode])) {
throw new LogicException(sprintf('Currency "%s" is not configured in 2C2P gateway.', $currencyCode));
}
return $this->options['currencies'][$currencyCode];
}
/**
* @return bool
*/
public function trustUserRequest()
{
return (bool) $this->options['trust_user_request'];
}
/**
* @return string
*/
public function getOffsiteUrl()
{
return $this->options['sandbox'] ?
'https://demo2.2c2p.com/2C2PFrontEnd/RedirectV3/payment' :
'https://t.2c2p.com/RedirectV3/Payment'
;
}
/**
* @return string
*/
public function getOnsiteUrl()
{
return $this->options['sandbox'] ?
'https://demo2.2c2p.com/2C2PFrontEnd/SecurePayment/PaymentAuth.aspx' :
'https://t.2c2p.com/SecurePayment/PaymentAuth.aspx'
;
}
/**
* @param array $params
* @return array
*/
public function prepareOffsitePayment(array $params)
{
$supportedParams = array(
'version' => '',
'merchant_id' => '',
'payment_description' => '',
'order_id' => '',
'invoice_no' => '',
'user_defined_1' => '',
'user_defined_2' => '',
'user_defined_3' => '',
'user_defined_4' => '',
'user_defined_5' => '',
'amount' => '',
'currency' => '',
'promotion' => '',
'customer_email' => '',
'pay_category_id' => '',
'result_url_1' => '',
'result_url_2' => '',
'payment_option' => '',
'ipp_interest_type' => '',
'payment_expiry' => '',
'default_lang' => '',
'enable_store_card' => '',
'stored_card_unique_id' => '',
'request_3ds' => '',
'recurring' => '',
'order_prefix' => '',
'recurring_amount' => '',
'allow_accumulate' => '',
'max_accumulate_amount' => '',
'recurring_interval' => '',
'recurring_count' => '',
'charge_next_date' => '',
'charge_on_date' => '',
'statement_descriptor' => '',
'hash_value' => '',
);
$params = array_merge($supportedParams, $params);
$this->addGlobalParams($params);
return $params;
}
/**
* @return array
*/
public function prepareOnsitePayment(array $model, array $creditCard, array $userDefined = [])
{
$params = [
'merchantID' => $this->getMerchantIdForCurrency($model['currency']),
'uniqueTransactionCode' => substr(uniqid(time()), 0, 20),
'desc' => $model['payment_description'],
'amt' => $model['amount'],
'currencyCode' => $model['currency'],
'panCountry' => '',
'cardholderName' => isset($creditCard['credit_card']['holder']) ? $creditCard['credit_card']['holder'] : '',
'encCardData' => $creditCard['encryptedCardInfo'],
];
$userDefined = array_filter($userDefined, function ($value, $key) {
return in_array($key, range(1, 5));
}, ARRAY_FILTER_USE_BOTH);
array_walk($userDefined, function ($value, $key) use (&$params) {
$params[sprintf('userDefined%d', $key)] = $value;
});
$paymentPayload = base64_encode($this->makeXml($params, 'PaymentRequest'));
$finalPayload = [
'version' => self::VERSION_ONSITE,
'payload' => $paymentPayload,
'signature' => $this->calculateHash($paymentPayload, $model['currency'], self::HASH_ONSITE),
];
return [
'paymentRequest' => base64_encode($this->makeXml($finalPayload, 'PaymentRequest')),
];
}
/**
* @param array $params
* @param string $currency Some responses from 2C2P do not include currency, we are then using the one from model
* @return bool
*/
public function checkOffsiteResponseHash(array $params, $currency)
{
$toHash =
$params['version'].
$params['request_timestamp'].
$params['merchant_id'].
$params['order_id'].
$this->emptyOr('invoice_no', $params).
$this->emptyOr('currency', $params).
$params['amount'].
$this->emptyOr('transaction_ref', $params).
$this->emptyOr('approval_code', $params).
$this->emptyOr('eci', $params).
$params['transaction_datetime'].
$params['payment_channel'].
$params['payment_status'].
$this->emptyOr('channel_response_code', $params).
$this->emptyOr('channel_response_desc', $params).
$this->emptyOr('masked_pan', $params).
$this->emptyOr('stored_card_unique_id', $params).
$this->emptyOr('backend_invoice', $params).
$this->emptyOr('paid_channel', $params).
$this->emptyOr('paid_agent', $params).
$this->emptyOr('recurring_unique_id', $params).
$this->emptyOr('user_defined_1', $params).
$this->emptyOr('user_defined_2', $params).
$this->emptyOr('user_defined_3', $params).
$this->emptyOr('user_defined_4', $params).
$this->emptyOr('user_defined_5', $params).
$params['browser_info'].
$this->emptyOr('ippPeriod', $params).
$this->emptyOr('ippInterestType', $params).
$this->emptyOr('ippInterestRate', $params).
$this->emptyOr('ippMerchantAbsorbRate', $params)
;
return $params['hash_value'] === $this->calculateHash($toHash, $params['currency'] ?: $currency);
}
/**
* @param string $response
* @return array
* @throws \Exception
*/
public function readOnsiteResponse($response)
{
$xmlObject = simplexml_load_string(base64_decode($response));
if (!$xmlObject) {
throw new \Exception('Cannot read XML from response');
}
$payloadXmlObject = simplexml_load_string(base64_decode($xmlObject->payload));
if (!$payloadXmlObject) {
throw new \Exception('Cannot read payload XML from response');
}
$signatureHash = $this->calculateHash($xmlObject->payload, $payloadXmlObject->currencyCode, self::HASH_ONSITE);
if((string) $xmlObject->signature !== $signatureHash) {
throw new \Exception('Signature does not match.');
}
return array_filter(
(array) $payloadXmlObject,
function ($value) {
return is_string($value);
}
);
}
/**
* @param array $params
*/
protected function addGlobalParams(array &$params)
{
$params['version'] = self::VERSION;
$params['merchant_id'] = $this->getMerchantIdForCurrency($params['currency']);
$params['hash_value'] = $this->calculateOffsiteRequestHash($params);
}
/**
* @param string $currencyNumeric
* @return string
*/
protected function getMerchantIdForCurrency($currencyNumeric)
{
return $this->getCurrencyConfigByNumeric($currencyNumeric)['merchant_id'];
}
/**
* @param string $currencyNumeric
* @return string
*/
protected function getMerchantAuthKeyForCurrency($currencyNumeric)
{
return $this->getCurrencyConfigByNumeric($currencyNumeric)['merchant_auth_key'];
}
/**
* @param string $currencyNumeric
* @return array
*/
protected function getCurrencyConfigByNumeric($currencyNumeric)
{
$iso = new ISO4217();
/** @var Currency $currency */
$currency = $iso->findByNumeric($currencyNumeric);
return $this->getCurrencyConfigByCode($currency->getAlpha3());
}
/**
* @param array $params
* @return string
*/
protected function calculateOffsiteRequestHash(array $params)
{
$toHash =
$params['version'].
$params['merchant_id'].
$params['payment_description'].
$params['order_id'].
$params['invoice_no'].
$params['currency'].
$params['amount'].
$params['customer_email'].
$params['pay_category_id'].
$params['promotion'].
$params['user_defined_1'].
$params['user_defined_2'].
$params['user_defined_3'].
$params['user_defined_4'].
$params['user_defined_5'].
$params['result_url_1'].
$params['result_url_2']
;
return $this->calculateHash($toHash, $params['currency']);
}
/**
* @param string $toHash
* @param string $currencyNumeric
* @return string
*/
private function calculateHash($toHash, $currencyNumeric, $algo = self::HASH_OFFSITE)
{
return strtoupper(hash_hmac($algo, $toHash, $this->getMerchantAuthKeyForCurrency($currencyNumeric), false));
}
private function emptyOr(string $index, array $array)
{
if (!array_key_exists($index, $array)) {
return '';
}
return $array[$index];
}
private function makeXml(array $params, $rootNode)
{
$xml = sprintf('<%s>', $rootNode);
array_walk($params, function ($value, $key) use (&$xml) {
$xml .= '<'.$key.'>'.$value.'</'.$key.'>';
});
$xml .= sprintf('</%s>', $rootNode);
return $xml;
}
}