-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestBuyLinkSignature.js
82 lines (76 loc) · 2.64 KB
/
testBuyLinkSignature.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
const Twocheckout = require("../twocheckout");
const config = {
sellerId: "",
secretKey: "",
secretWord: "",
jwtExpireTime: 20, //minutes
};
let lastRefNo = null;
const tco = new Twocheckout(config);
console.log("Test buy link generate signature.");
const buyLinkParams = {
address: 'Test Address',
city: 'LA',
country: 'US',
name: 'Customer 2Checkout',
phone: '0770678987',
zip: '12345',
email: '[email protected]',
'company-name': 'Verifone',
state: 'California',
'ship-name': 'Customer 2Checkout',
'ship-address': 'Test Address',
'ship-city': 'LA',
'ship-country': 'US',
'ship-email': '[email protected]',
'ship-state': 'California',
prod: 'Buy link Dynamic product test product from API',
price: 1,
qty: 1,
type: 'PRODUCT',
tangible: 0,
src: 'NodeJsLibrary',
'return-url': 'http://tcoLib.example/paymentCallback.php',
'return-type': 'redirect',
expiration: new Date().getTime() + 3600 * 5,
'order-ext-ref': 'CustOrd100',
'item-ext-ref': '20230614145657',
'customer-ext-ref': '[email protected]',
currency: 'usd',
language: 'en',
test: 1,
merchant: config.sellerId,
dynamic: 1
};
console.log("Generate buy link signature using callback.");
tco.generateBuyLinkSignature(buyLinkParams, (err, res) => {
if (err) {
console.error("Error: %s", err.toString());
} else if ("signature" in res) {
console.log("Generated Signature: %s", res.signature);
let finalParams = JSON.parse(JSON.stringify(buyLinkParams));
finalParams.signature = res.signature;
const params = new URLSearchParams(finalParams);
const str = params.toString();
const redirectTo = "https://secure.2checkout.com/checkout/buy/?" + str;
console.log("Redirect to: %s", redirectTo);
} else {
console.error("Signature not found! Res: %s", res.toString());
}
});
console.log("Testing using async/await.");
(async () => {
try {
let result = await tco.generateBuyLinkSignature(buyLinkParams);
console.log("\r\nAsync/Await signature result: %s", result.signature);
//make a copy of the current params object which contains src
let finalParams = JSON.parse(JSON.stringify(buyLinkParams));
finalParams.signature = result.signature;
const params = new URLSearchParams(finalParams);
const str = params.toString();
const redirectTo = "https://secure.2checkout.com/checkout/buy/?" + str;
console.log("Async/Await Redirect to: %s", redirectTo);
} catch (error) {
console.error(error);
}
})();