-
Notifications
You must be signed in to change notification settings - Fork 3
/
wayforpay.js
555 lines (498 loc) · 15.6 KB
/
wayforpay.js
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
'use strict';
/**
WayForPay api for JavaScript
Copyright (c) 2016 Dmytro Fedorchuk
URL: https://github.com/fducom
Licensed under the MIT license:
http://www.opensource.org/licenses/mit-license.php
WayForPay payment system aggregator https://wayforpay.com/
*/
var request = require('request');
var crypto = require('crypto');
var _ = require('lodash');
var utf8 = require('utf8');
/**
* Constructor.
*
* @param string $merchant_account
* @param string $merchant_password
*
* @throws InvalidArgumentException
*/
module.exports = function (merchant_account, merchant_password) {
// API host
const PURCHASE_URL = 'https://secure.wayforpay.com/pay';
const API_URL = 'https://api.wayforpay.com/api';
const WIDGET_URL = 'https://secure.wayforpay.com/server/pay-widget.js';
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';
const MODE_P2P_CREDIT = 'P2P_CREDIT';
const MODE_CREATE_INVOICE = 'CREATE_INVOICE';
const MODE_P2_PHONE = 'P2_PHONE';
const COMPLETE_3DS = 'COMPLETE_3DS';
this._action;
this._fields;
this._charset;
/**
* Call API
*
* @param Object fields
*
* @return Object
*/
this._query = function (call) {
console.log('_query');
let data = JSON.stringify(this._fields);
request.put({
url: API_URL,
body: data,
headers: {'Content-Type': 'application/json; charset=utf-8'}
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
call(body);
} else {
call(body);
}
}
);
};
/**
* Return signature hash
*
* @param action
* @param fields
* @return mixed
*/
this.createSignature = function (action, fields) {
this._prepare(action, fields);
return this._buildSignature();
};
this._prepare = function (action, fields) {
console.log('_prepare');
this._action = action;
if (_.isEmpty(fields)) {
throw new Error('Arguments must be not empty');
}
this._fields = fields;
this._fields.transactionType = action;
this._fields.merchantAccount = merchant_account;
this._fields.merchantSignature = this._buildSignature();
if (this._action !== MODE_PURCHASE) this._fields.apiVersion = API_VERSION;
this._checkFields();
};
/**
* MODE_SETTLE
*
* @param fields
* @return mixed
*/
this.settle = function (fields, cb) {
this._prepare(MODE_SETTLE, fields);
return this._query(cb);
};
/**
* MODE_CHARGE
*
* @param fields
* @return mixed
*/
this.charge = function (fields, cb) {
this._prepare(MODE_CHARGE, fields);
return this._query(cb);
};
/**
* MODE_REFUND
*
* @param fields
* @return mixed
*/
this.refund = function (fields, cb) {
this._prepare(MODE_REFUND, fields);
return this._query(cb);
};
/** "wayforpay": "0.0.1"
* MODE_CHECK_STATUS
*
* @param fields
* @return mixed
*/
this.checkStatus = function (fields, cb) {
this._prepare(MODE_CHECK_STATUS, fields);
return this._query(cb);
};
/**
* COMPLETE_3DS
*
* @param fields
* @return mixed
*/
this.complete3ds = function (fields, cb) {
this._prepare(COMPLETE_3DS, fields);
return this._query(cb);
};
/**
* MODE_P2P_CREDIT
*
* @param fields
* @return mixed
*/
this.account2card = function (fields, cb) {
this._prepare(MODE_P2P_CREDIT, fields);
return this._query(cb);
};
/**
* MODE_P2P_CREDIT
*
* @param fields
* @return mixed
*/
this.createInvoice = function (fields, cb) {
this._prepare(MODE_CREATE_INVOICE, fields);
return this._query(cb);
};
/**
* MODE_P2P_CREDIT
*
* @param fields
* @return mixed
*/
this.account2phone = function (fields, cb) {
this._prepare(MODE_P2_PHONE, fields);
return this._query(cb);
};
/**
* MODE_PURCHASE
* Generate html form
*
* @param fields
* @return string
*/
this.buildForm = function (fields) {
this._prepare(MODE_PURCHASE, fields);
var form = '<form method="POST" action="' + PURCHASE_URL + '" accept-charset="utf-8">';
_.each(fields, function (value, key) {
if (_.isArray(key)) {
_.each(key, function (fild) {
form += '<input type="hidden" name="' + key + '[]" value="' + fild + '" />';
});
} else {
form += '<input type="hidden" name="' + key + '" value="' + value + '" />';
}
});
form += '<input type="submit" value="Submit purchase form"></form>';
return form;
};
/**
* MODE_PURCHASE
* If GET redirect is used to redirect to purchase form, i.e.
* https://secure.wayforpay.com/pay/get?merchantAccount=test_merch_n1&merchantDomainName=domain.ua&merchantSignature=c6d08855677ec6beca68e292b2c3c6ae&orderReference=RG3656-1430373125&orderDate=1430373125&amount=0.16¤cy=UAH&productName=Saturn%20BUE%201.2&productPrice=0.16&productCount=1&language=RU
*
* @param fields
* @return string
*/
this.generatePurchaseUrl = function (fields) {
this._prepare(MODE_PURCHASE, fields);
return PURCHASE_URL + '/get?' + serialize(fields);
};
this._getFieldsNameForSignature = function () {
console.log('_getFieldsNameForSignature');
const purchaseFieldsAlias = [
'merchantAccount',
'merchantDomainName',
'orderReference',
'orderDate',
'amount',
'currency',
'productName',
'productCount',
'productPrice'
];
switch (this._action) {
case 'COMPLETE_3DS':
return [
'transactionType',
'authorization_ticket',
'd3ds_pares'
];
break;
case 'ACCEPT':
return [
'orderReference',
'status',
'time',
];
break;
case 'PURCHASE':
return purchaseFieldsAlias;
break;
case 'REFUND':
return [
'merchantAccount',
'orderReference',
'amount',
'currency'
];
case 'CHECK_STATUS':
return [
'merchantAccount',
'orderReference'
];
break;
case 'CHARGE':
return purchaseFieldsAlias;
break;
case 'SETTLE':
return [
'merchantAccount',
'orderReference',
'amount',
'currency'
];
break;
case MODE_P2P_CREDIT:
return [
'merchantAccount',
'orderReference',
'amount',
'currency',
'cardBeneficiary',
'rec2Token',
];
break;
case MODE_CREATE_INVOICE:
return purchaseFieldsAlias;
break;
case MODE_P2_PHONE:
return [
'merchantAccount',
'orderReference',
'amount',
'currency',
'phone',
];
break;
default:
throw new Error('Unknown transaction type: ' + this._action);
}
};
/**
* _checkFields
*
* @param Object fields
*
* @return status
*
* @throws InvalidArgumentException
*/
this._checkFields = function () {
console.log('_checkFields');
let required = this._getRequiredFields;
let error = [];
let parameters = this._fields;
_(required).forEach(function (item) {
if (array_key_exists(item, parameters)) {
if (!(parameters[item])) {
error.push(item);
}
} else {
error.push(item);
}
});
if (!_.isEmpty(error)) { //!_.isEmpty
throw new Error('Missed required field(s): ' + JSON.stringify(error));
}
return true;
};
/**
* _buildSignature
*
* @param Object fields
*
* @return string
*/
this._buildSignature = function () {
console.log('_buildSignature');
let signFields = this._getFieldsNameForSignature();
let data = [];
let error = [];
let parameters = this._fields;
_(signFields).forEach(function (item) {
if (array_key_exists(item, parameters)) {
let value = parameters[item];
if (_.isArray(value)) {
let arrParam = _.values(value);
const str = arrParam.join(FIELDS_DELIMITER);
data.push(str + "");
} else {
data.push(value + "");
}
} else {
error.push(item);
}
});
if (!_.isEmpty(error)) {
throw new Error('Missed signature field(s): ' + JSON.stringify(error));
}
let arrParam = _.values(data);
let secret = arrParam.join(FIELDS_DELIMITER);
const hash = crypto.createHmac('md5', merchant_password)
.update(secret)
.digest('hex');
return hash;
};
this._getRequiredFields = function () {
switch (this._action) {
case 'PURCHASE':
return [
'merchantAccount',
'merchantDomainName',
'merchantTransactionSecureType',
'orderReference',
'orderDate',
'amount',
'currency',
'productName',
'productCount',
'productPrice'
];
case 'SETTLE':
return [
'transactionType',
'merchantAccount',
'orderReference',
'amount',
'currency',
'apiVersion'
];
case 'ACCEPT':
return [
'orderReference',
'status',
'time',
];
case 'CHARGE':
var required = [
'transactionType',
'merchantAccount',
'merchantDomainName',
'orderReference',
'apiVersion',
'orderDate',
'amount',
'currency',
'productName',
'productCount',
'productPrice'
];
let additional = (this._fields['recToken']) ?
['recToken'] :
['card', 'expMonth', 'expYear', 'cardCvv', 'cardHolder'];
return required.concat(additional).unique();
case 'REFUND':
return [
'transactionType',
'merchantAccount',
'orderReference',
'amount',
'currency',
'comment',
'apiVersion'
];
case 'CHECK_STATUS':
return [
'transactionType',
'merchantAccount',
'orderReference',
'apiVersion'
];
case 'COMPLETE_3DS':
return [
'transactionType',
'authorization_ticket',
'd3ds_pares'
];
case MODE_P2P_CREDIT:
return [
'transactionType',
'merchantAccount',
'orderReference',
'amount',
'currency',
'cardBeneficiary',
'merchantSignature'
];
case MODE_CREATE_INVOICE:
return [
'transactionType',
'merchantAccount',
'merchantDomainName',
'orderReference',
'amount',
'currency',
'productName',
'productCount',
'productPrice'
];
case MODE_P2_PHONE:
return [
'merchantAccount',
'orderReference',
'orderDate',
'currency',
'amount',
'phone',
'apiVersion'
];
break;
default:
throw new Error('Unknown transaction type');
}
};
this.buildWidgetButton = function (fields, callback = null) {
this._prepare(MODE_PURCHASE, fields);
let button = '<script id="widget-wfp-script" language="javascript" type="text/javascript" src="' + WIDGET_URL + '"></script>';
button += '<script type="text/javascript">';
button += 'var wayforpay = new Wayforpay();';
button += 'var pay = function () {';
button += ' wayforpay.run(' + JSON.stringify(this._fields) + ');';
button += '}';
button += 'window.addEventListener("message", ' + (callback ? callback : "receiveMessage") + ');';
button += 'function receiveMessage(event)';
button += '{';
button += ' if(';
button += ' event.data == "WfpWidgetEventClose" || //при закрытии виджета пользователем';
button += ' event.data == "WfpWidgetEventApproved" || //при успешном завершении операции';
button += ' event.data == "WfpWidgetEventDeclined" || //при неуспешном завершении';
button += ' event.data == "WfpWidgetEventPending") // транзакция на обработке';
button += ' {';
button += ' console.log(event.data);';
button += ' }';
button += '}';
button += '</script>';
button += '<button type="button" onclick="pay();">Оплатить</button>';
return button;
};
////refact
var serialize = function (obj) {
let str = [];
for (var p in obj)
if (obj.hasOwnProperty(p)) {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
}
return str.join("&");
};
var array_key_exists = function (key, search) {
if (!search || (search.constructor !== Array && search.constructor !== Object)) {
return false;
}
return search[key] !== undefined;
};
////refactend
return this;
};