-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcardtokens.js
200 lines (179 loc) · 6.8 KB
/
cardtokens.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
import { v4 as uuidv4 } from 'uuid'; // needed to generate UUIDs for each request
import request from 'request'; // needed to request Cardtokens
import crypto from 'crypto' // needed to processe RSA encryption
//
// This is the merchantid created within Cardtokens
//
const MERCHANTID = "523ca9d5eb9d4ce0a60b2a3f5eb3119d"
//
// The apikey is required from the menu "settings" in Cardtokens
//
const APIKEY = "95f734793a424ea4ae8d9dc0b8c1a4d7"
//
// This is the Cardtokens API endpoint
//
const HOST = "https://api.cardtokens.io"
//
// The account public key used to encrypt card holder data.
// The public key can be extracted from the Cardtokens backoffice from the
// menu "settings"
//
const PEMPUBLICKEY = "LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0NCk1JSUNJakFOQmdrcWhraUc5dzBCQVFFRkFBT0NBZzhBTUlJQ0NnS0NBZ0VBMW1zdE1QckZSVmQ4VE1HclkzMjQNCjJwcTQ2aFlFMFBieXcrTnB0MnRDSjBpRHkrWkxQWWJGMnVYTkg1UE9neGkzdDVIVTY2MVVTQThYOXp5N2pJTzANCjlpOGxRMkdoN1dpejlqZXpFVDBpVmNvUGovSFFrV1N1KzA5Y0RIUk5qUDJoaWtIWUEwOUlZc05vemo3eHR2ME4NCnJxbjZacWZ5amhOS1NrN2RUeUVVQ0xoaEwvTUVFRTZ0QUREVVJZb0tIVXFrVml0cFlzcE1HamlKNkFBSVlVZWENCk1DdkZ2cnhaSkFNSW5FbnY3THNhTHVBV21pdzRrOXM5M0x1MXdoM3A1bjR1a09pVWpRWEZ5Nm9NNzMwblpvb1MNCmR2U2lYUlR2UlFwMDkyZDAzbnY5Zk55cWgwM3ZoM2l5TFJja3RoVnc2ZklPN3p4cktjTXpoVmhzK3doUGVtMzkNCkRhU05oSjFrZUx4bzcyaDJIL01FMzRuQzNOSUhCUEhQZ1NBeHVDSjlCcXVVRW1idXdGMTc0eDlGOUhFYm5jRlkNClRTd1hmS3diN1cxZ0F1U1RlWmhKVXc1eDZ6a3ZUTmRTejRWaFFjT051SjJ6am1VdGdSK3FXc1NjOUh2N1RGREgNCjlQbCt5NmQxeVJ0Rmp2TmlqeGZQUmo5a1dKbVJvcnBVVExUMTh2dThlbzg1aWNLTVY1VmladDMweGxpc1RVTjANCjJOWkxjNG83TVdraHE1eGhGcXhmZDdTZXZEc1FLa0VpenlRbi9zOUpZNmsybEtQUG4wTXk1UjdURWtBZEhVREUNCklIc09qTXlrZnpwYVdoNldMK2RmRlRFVzE4MFNkRHdXbEFXaWtpYWhFT1NDRGVFMkpWTDluMjY3QzJkc0ZJZDYNCjVPczJKVjE5anl5b2VGQkhOQm11MFBjQ0F3RUFBUT09DQotLS0tLUVORCBQVUJMSUMgS0VZLS0tLS0NCg=="
//
// This is the example card holder data object filled with test data
//
var card_mastercard = {
"pan": "5555341244441115",
"expmonth": "06",
"expyear": "2029",
"securitycode": "000"
};
//
// The create token request object
//
var requestobj = {
"enccard": "",
"clientwalletaccountemailaddress": "[email protected]",
"merchantid": MERCHANTID
};
//
// The RSA public key used to encrypt the card object and inject as base64 encoded data in the enccard object
//
const publickey= Buffer.from(PEMPUBLICKEY , 'base64').toString('utf8');
//
// Encrypt the card holder data using the public key
//
const encryptedData = crypto.publicEncrypt(
{
key: publickey,
padding: crypto.constants.RSA_PKCS1_PADDING,
oaepHash: "sha256",
},
// We convert the data string to a buffer using `Buffer.from`
Buffer.from(JSON.stringify(card_mastercard))
);
//
// Now transform the encrypted data into base64
//
var b64 = Buffer.from(encryptedData).toString('base64');
//
// Set the base64 encoded data into the enccard field on the request object
//
requestobj.enccard = b64;
//
// Transform the requestobject into json - we are good to go
//
var jsonrequest = JSON.stringify(requestobj);
//
// Generate HTTP request to create the token including the JSON payload
//
var options = {
uri: HOST + '/api/token',
method: 'POST',
headers: {
'content-type': 'application/json',
'x-request-id': uuidv4(),
'x-api-key': APIKEY,
'Content-Length': Buffer.byteLength(jsonrequest),
'User-Agent': 'Cardtokens/1.0'
},
json: requestobj
};
//
// Request Cardtokens to create the token
//
request(options, function (error, response, body) {
//
// If create token success
//
if (!error && response.statusCode == 200) {
//
// On success - print the internal Cardtokens tokenid
//
console.log("Token created by Cardtokens tokenid: " + body.tokenid + " and network token: " + body.token);
//
// Now generaet a cryptogram on behalf of this token
//
var requestobjcryptogram = {
"reference": "thomask",
"unpredictablenumber": "99887766"
};
//
// Create the HTTP request including the cryptogram data
//
var options_cryptogram = {
uri: HOST + '/api/token/' + body.tokenid + "/cryptogram",
method: 'POST',
headers: {
'content-type': 'application/json',
'x-api-key': APIKEY,
'Content-Length': Buffer.byteLength(JSON.stringify(requestobjcryptogram)),
'User-Agent': 'Cardtokens/1.0'
},
json: requestobjcryptogram
};
//
// Now request for a cryptogram
//
request(options_cryptogram, function (error, response, body) {
//
// ON success
//
if (!error && response.statusCode == 200) {
//
// Print the cryptogram
//
console.log(body.cryptogram);
//
// now fetch status of the token
//
var options_status = {
uri: HOST + '/api/token/' + body.tokenid + "/status",
method: 'GET',
headers: {
'content-type': 'application/json',
'x-api-key': APIKEY,
'User-Agent': 'Cardtokens/1.0'
},
};
//
// Request Cardtokens for the status of the token
//
request(options_status, function (error, response, body) {
//
// On get status success
//
if (!error && response.statusCode == 200) {
//
// Print the status of the token
//
console.log("success got status. The token has status: " + JSON.parse(body).status);
//
// Finally delete this token - go and create the delete request
//
var options_delete = {
uri: HOST + '/api/token/' + JSON.parse(body).tokenid + "/delete",
method: 'DELETE',
headers: {
'content-type': 'application/json',
'x-api-key': APIKEY,
'User-Agent': 'Cardtokens/1.0'
},
};
//
// On success
//
request(options_delete, function (error, response, body) {
//
// On success delete request
//
if (!error && response.statusCode == 200) {
console.log("Statuscode: " + response.statusCode + " - token is deleted");
}
});
}
});
};
});
}
});