Repository navigation
Expand file tree
/
Copy pathWayForPay.php
More file actions
386 lines (344 loc) · 10.4 KB
/
Copy pathWayForPay.php
File metadata and controls
386 lines (344 loc) · 10.4 KB
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
378
379
380
381
382
383
384
385
386
<?php
/**
* Class WayForPay
*/
class WayForPay
{
const PURCHASE_URL = 'https://secure.wayforpay.com/pay';
const API_URL = 'https://api.wayforpay.com/api';
const FIELDS_DELIMITER = ';';
const API_VERSION = 1;
const DEFAULT_CHARSET = 'utf8';
const MODE_PURCHASE = 'PURCHASE';
const MODE_SETTLE = 'SETTLE';
const MODE_CHARGE = 'CHARGE';
const MODE_REFUND = 'REFUND';
const MODE_CHECK_STATUS = 'CHECK_STATUS';
private $_merchant_account;
private $_merchant_password;
private $_action;
private $_params;
private $_charset;
/**
* Init
*
* @param $merchant_account
* @param $merchant_password
* @param string $charset
* @throws InvalidArgumentException
*/
public function __construct($merchant_account, $merchant_password, $charset = self::DEFAULT_CHARSET)
{
if (!is_string($merchant_account) || $merchant_account === '') {
throw new InvalidArgumentException('Merchant account must be string and not empty');
}
if (!is_string($merchant_password) || $merchant_password === '') {
throw new InvalidArgumentException('Merchant password must be string and not empty');
}
$this->_merchant_account = $merchant_account;
$this->_merchant_password = $merchant_password;
$this->_charset = $charset;
}
/**
* MODE_SETTLE
*
* @param $fields
* @return mixed
*/
public function settle($fields)
{
$this->_prepare(self::MODE_SETTLE, $fields);
return $this->_query();
}
/**
* MODE_CHARGE
*
* @param $fields
* @return mixed
*/
public function charge($fields)
{
$this->_prepare(self::MODE_CHARGE, $fields);
return $this->_query();
}
/**
* MODE_REFUND
*
* @param $fields
* @return mixed
*/
public function refund($fields)
{
$this->_prepare(self::MODE_REFUND, $fields);
return $this->_query();
}
/**
* MODE_CHECK_STATUS
*
* @param $fields
* @return mixed
*/
public function checkStatus($fields)
{
$this->_prepare(self::MODE_CHECK_STATUS, $fields);
return $this->_query();
}
/**
* MODE_PURCHASE
* Generate html form
*
* @param $fields
* @return string
*/
public function buildForm($fields)
{
$this->_prepare(self::MODE_PURCHASE, $fields);
$form = sprintf('<form method="POST" action="%s" accept-charset="utf-8">', self::PURCHASE_URL);
foreach ($this->_params as $key => $value) {
if (is_array($value)) {
foreach ($value as $field) {
$form .= sprintf('<input type="hidden" name="%s" value="%s" />', $key . '[]', htmlspecialchars($field));
}
} else {
$form .= sprintf('<input type="hidden" name="%s" value="%s" />', $key, htmlspecialchars($value));
}
}
$form .= '<input type="submit" value="Submit purchase form"></form>';
return $form;
}
/**
* Return signature hash
*
* @param $action
* @param $fields
* @return mixed
*/
public function createSignature($action, $fields)
{
$this->_prepare($action, $fields);
return $this->_buildSignature();
}
/**
* @param $action
* @param array $params
* @throws InvalidArgumentException
*/
private function _prepare($action, array $params)
{
$this->_action = $action;
if(empty($params)){
throw new InvalidArgumentException('Arguments must be not empty');
}
$this->_params = $params;
$this->_params['transactionType'] = $this->_action;
$this->_params['merchantAccount'] = $this->_merchant_account;
$this->_params['merchantSignature'] = $this->_buildSignature();
if ($this->_action !== self::MODE_PURCHASE) $this->_params['apiVersion'] = self::API_VERSION;
$this->_checkFields();
}
/**
* Check required fields
*
* @param $fields
* @return bool
* @throws InvalidArgumentException
*/
private function _checkFields()
{
$required = $this->_getRequiredFields();
$error = array();
foreach ($required as $item) {
if (array_key_exists($item, $this->_params)) {
if (empty($this->_params[$item])) {
$error[] = $item;
}
} else {
$error[] = $item;
}
}
if (!empty($error)) {
throw new InvalidArgumentException('Missed required field(s): ' . implode(', ', $error) . '.');
}
return true;
}
/**
* Generate signature hash
*
* @param $fields
* @return string
* @throws InvalidArgumentException
*/
private function _buildSignature()
{
$signFields = $this->_getFieldsNameForSignature();
$data = array();
$error = array();
foreach ($signFields as $item) {
if (array_key_exists($item, $this->_params)) {
$value = $this->_params[$item];
if (is_array($value)) {
$data[] = implode(self::FIELDS_DELIMITER, $value);
} else {
$data[] = (string) $value;
}
} else {
$error[] = $item;
}
}
if ( $this->_charset != self::DEFAULT_CHARSET) {
foreach($data as $key => $value) {
$data[$key] = iconv($this->_charset, self::DEFAULT_CHARSET, $data[$key]);
}
}
if (!empty($error)) {
throw new InvalidArgumentException('Missed signature field(s): ' . implode(', ', $error) . '.');
}
return hash_hmac('md5', implode(self::FIELDS_DELIMITER, $data), $this->_merchant_password);
}
/**
* Request method
*
* @param $fields
* @return mixed
*/
private function _query()
{
$fields = json_encode($this->_params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, self::API_URL);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=utf-8'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
/**
* Signature fields
*
* @return array
* @throws InvalidArgumentException
*/
private function _getFieldsNameForSignature()
{
$purchuseFieldsAlias = array(
'merchantAccount',
'merchantDomainName',
'orderReference',
'orderDate',
'amount',
'currency',
'productName',
'productCount',
'productPrice'
);
switch ($this->_action) {
case 'PURCHASE':
return $purchuseFieldsAlias;
break;
case 'REFUND':
return array(
'merchantAccount',
'orderReference',
'amount',
'currency'
);
case 'CHECK_STATUS':
return array(
'merchantAccount',
'orderReference'
);
break;
case 'CHARGE':
return $purchuseFieldsAlias;
break;
case 'SETTLE':
return array(
'merchantAccount',
'orderReference',
'amount',
'currency'
);
break;
default:
throw new InvalidArgumentException('Unknown transaction type');
}
}
/**
* Required fields
*
* @return array
*/
private function _getRequiredFields()
{
switch ($this->_action) {
case 'PURCHASE':
return array(
'merchantAccount',
'merchantDomainName',
'merchantTransactionSecureType',
'orderReference',
'orderDate',
'amount',
'currency',
'productName',
'productCount',
'productPrice'
);
case 'SETTLE':
return array(
'transactionType',
'merchantAccount',
'orderReference',
'amount',
'currency',
'apiVersion'
);
case 'CHARGE':
$required = array(
'transactionType',
'merchantAccount',
'merchantDomainName',
'orderReference',
'apiVersion',
'orderDate',
'amount',
'currency',
'productName',
'productCount',
'productPrice',
'clientFirstName',
'clientLastName',
'clientEmail',
'clientPhone',
'clientCountry',
'clientIpAddress'
);
$additional = !empty($this->_params['recToken']) ?
array('recToken') :
array('card', 'expMonth', 'expYear', 'cardCvv', 'cardHolder');
return array_merge($required, $additional);
case 'REFUND':
return array(
'transactionType',
'merchantAccount',
'orderReference',
'amount',
'currency',
'comment',
'apiVersion'
);
case 'CHECK_STATUS':
return array(
'transactionType',
'merchantAccount',
'orderReference',
'apiVersion'
);
default:
throw new InvalidArgumentException('Unknown transaction type');
}
}
}