/
signature.js
153 lines (129 loc) · 4.2 KB
/
signature.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
const https = require('https');
const crypto = require('crypto');
const accessKey = "4A20E11B9E9C6E60A075";
const secretKey = "509be94b9b26449f41c7254e7ae802ddbdf2eabf84bb7ec72158410d9ee995592477caf141f06c7e";
const log = false;
async function getRapyPayCheckoutId(req,res) {
var body = {
amount: req.body.cartvalue,
complete_payment_url: "https://static-web-app-01.herokuapp.com/",
country: "IN",
currency: req.body.currency,
error_payment_url: "https://static-web-app-01.herokuapp.com/",
merchant_reference_id: req.body.merchantId,
cardholder_preferred_currency: true,
language: "en",
metadata: {
merchant_defined: true,
},
payment_method_types_include: [
"in_visa_credit_card",
"in_dhanlaxmi_bank",
"in_paytm_ewallet",
],
expiration: (Math.round(new Date().getTime() / 1000) + 86400),
payment_method_types_exclude: [],
};
try {
httpMethod = 'post';
httpBaseURL = "sandboxapi.rapyd.net";
httpURLPath = '/v1/checkout';
salt = generateRandomString(8);
// idempotency = new Date().getTime().toString();
timestamp = Math.round(new Date().getTime() / 1000);
signature = sign(httpMethod, httpURLPath, salt, timestamp, body)
const options = {
hostname: httpBaseURL,
port: 443,
path: httpURLPath,
method: httpMethod,
headers: {
'Content-Type': 'application/json',
salt: salt,
timestamp: timestamp,
signature: signature,
access_key: accessKey
}
}
return await httpRequest(options, body, log)
// console.log(id);
// return id;
}
catch (error) {
console.error("Error generating request options");
throw error;
}
}
function sign(method, urlPath, salt, timestamp, body) {
try {
let bodyString = "";
if (body) {
bodyString = JSON.stringify(body);
bodyString = bodyString == "{}" ? "" : bodyString;
}
let toSign = method.toLowerCase() + urlPath + salt + timestamp + accessKey + secretKey + bodyString;
log && console.log(`toSign: ${toSign}`);
let hash = crypto.createHmac('sha256', secretKey);
hash.update(toSign);
const signature = Buffer.from(hash.digest("hex")).toString("base64")
log && console.log(`signature: ${signature}`);
console.log(signature);
return signature;
}
catch (error) {
console.error("Error generating signature");
throw error;
}
}
function generateRandomString(size) {
try {
return crypto.randomBytes(size).toString('hex');
}
catch (error) {
console.error("Error generating salt");
throw error;
}
}
async function httpRequest(options, body) {
return new Promise((resolve, reject) => {
try {
let bodyString = "";
if (body) {
bodyString = JSON.stringify(body);
bodyString = bodyString == "{}" ? "" : bodyString;
}
log && console.log(`httpRequest options: ${JSON.stringify(options)}`);
const req = https.request(options, (res) => {
let response = {
statusCode: res.statusCode,
headers: res.headers,
body: ''
};
res.on('data', (data) => {
response.body += data;
});
res.on('end', () => {
response.body = response.body ? JSON.parse(response.body) : {}
log && console.log(`httpRequest response: ${JSON.stringify(response)}`);
console.log("Response : ",response)
if (response.statusCode !== 200) {
return reject(response);
}
console.log(response.body.data.redirect_url);
return resolve(response.body.data.redirect_url);
});
})
req.on('error', (error) => {
return reject(error);
})
req.write(bodyString)
req.end();
}
catch(err) {
return reject(err);
}
})
}
module.exports = {
getRapyPayCheckoutId: getRapyPayCheckoutId
}