This repository has been archived by the owner on Mar 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
77 lines (70 loc) · 2.47 KB
/
client.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
"use strict";
const assert = require("assert");
const crypto = require("crypto");
const extend = require("extend");
const querystring = require("querystring");
class SagepayForm {
/*
Form(options)
@options Required. Contains connetion options.
@options.password
Required. See Sage Pay Form Integration and Protocol Guidelines 3.00.
@options.vendor
Required. See Sage Pay Form Integration and Protocol Guidelines 3.00,
A1. Form Fields, "Vendor".
*/
constructor(options) {
assert(options, "options is required");
assert(options.password, "options.password is required");
assert(options.vendor, "options.vendor is required");
this._options = options;
}
/*
Form.createHiddenFields(transaction)
@transaction
Required. See Sage Pay Form Integration and Protocol Guidelines 3.00,
A1.3 Request Crypt Fields for properties.
Returns an object containing the hidden fields required to post the
transaction to the gateway.
*/
createHiddenFields(transaction) {
// NOTE: Sage Pay does not accept URL encoding, it needs this nasty
// hacky encoding instead...
transaction = Object.keys(transaction).map(key => [key, transaction[key]].join("=")).join("&");
var cipher = crypto.createCipheriv(
'aes-128-cbc',
this._options.password,
this._options.password
);
var crypt = "@";
crypt += cipher.update(transaction, "utf8", "hex");
crypt += cipher.final("hex");
var ret = {
"VPSProtocol": "3.00",
"TxType": "PAYMENT",
"Vendor": this._options.vendor,
"Crypt": crypt
};
return ret;
}
/*
Form.decodeResponse
@crypt
Required. See Sage Pay Form Integration and Protocol Guidelines 3.00,
Appendix B.
Returns an Object containing the decrypted data, one property for each
field.
*/
decodeResponse(crypt) {
if (crypt[0] != '@') {
throw new Error("Encrypted response must begin with the @ symbol.");
}
crypt = crypt.substr(1);
var decipher = crypto.createDecipheriv('aes-128-cbc', this._options.password, this._options.password);
var decrypted = decipher.update(crypt, "hex", "utf8");
decrypted += decipher.final("utf8");
var response = querystring.parse(decrypted);
return response;
}
}
module.exports = SagepayForm;