-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
692 lines (555 loc) · 23.2 KB
/
server.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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
// Add a bunch of packages that you need
const express = require('express');
const fetch = require("node-fetch");
const qs = require('qs');
const app = express();
var sha512 = require('js-sha512');
const { response } = require('express');
// Anything in the "globalpayments" folder will be served as static content in localhost
app.use(express.static('globalpayments'));
app.use(express.json()); // adds a built-in JSON parser to Express
app.use(express.urlencoded({
extended: true
}));
app.listen(8080, () =>{
console.log("Running on port 8080");
})
// GP API details
// Configure .env file
require('dotenv').config()
let gpApiVersion = process.env.GP_API_VERSION
let app_id = process.env.GP_API_APP_ID
let app_key = process.env.GP_API_APP_KEY
let nonce = new Date().toISOString();
// API requests to GP API
const createSecret = (app_key, nonce) => {
let secretKey = sha512(nonce + app_key)
return secretKey
}
const getAccessToken = async() => {
let headers ={
"X-GP-Version": gpApiVersion,
"Content-Type": "application/json"
}
let body = {
"app_id":app_id,
"secret": createSecret(app_key, nonce),
"grant_type": "client_credentials",
"nonce":nonce
}
let options = {
method:"POST",
body: JSON.stringify(body),
headers:headers
}
const response = await fetch(`${process.env.GP_API_ENVIRONMENT}/ucp/accesstoken`, options);
const json = await response.json()
return json
}
const getAuthenticationResult = async(data) => {
let auth_id = data
console.log(data)
let accessToken = await getAccessToken()
let headers ={
"X-GP-Version": gpApiVersion,
"Content-Type": "application/json",
"Accept" : "application/json",
"Authorization" : "Bearer " + accessToken.token
}
let options = {
method:"POST",
headers:headers
}
const response = await fetch(`${process.env.GP_API_ENVIRONMENT}/ucp/authentications/${auth_id}/result`, options);
const json = await response.json()
return json
}
const check3dsVersion = async(data) => {
let accessToken = await getAccessToken()
let headers ={
"X-GP-Version": gpApiVersion,
"Content-Type": "application/json",
"Accept" : "application/json",
"Authorization" : "Bearer " + accessToken.token
}
let body = {
"account_name": accessToken.scope.accounts[0].name,
"channel": "CNP",
"currency": "GBP",
"reference": "My Own Reference",
"country": "GB",
"amount": "1999",
"three_ds":{
"source": "BROWSER"
},
"payment_method": {
"id": data.paymentToken
},
"notifications": {
"challenge_return_url": data.challenge_return_url,
"three_ds_method_return_url": data.three_ds_method_return_url
}
}
let options = {
method:"POST",
body: JSON.stringify(body),
headers:headers
}
const response = await fetch(`${process.env.GP_API_ENVIRONMENT}/ucp/authentications`, options);
const json = await response.json()
return json
}
const initiate3dsecure = async (data) => {
let id = data.id
let accessToken = await getAccessToken()
let headers ={
"X-GP-Version": gpApiVersion,
"Content-Type": "application/json",
"Accept" : "application/json",
"Authorization" : "Bearer " + accessToken.token
}
let body = {
"account_name": accessToken.scope.accounts[0].name,
"channel": "CNP",
"amount": "1999",
"currency": "GBP",
"country": "GB",
"preference": "CHALLENGE_MANDATED",
"three_ds":{
"source": "BROWSER"
},
"method_url_completion_status": "YES",
"payment_method": {
"id": data.paymentToken
},
"order": {
"time_created_reference": "2019-04-26T10:19:32.552327Z",
"amount": "1001",
"currency": "GBP",
"reference": "3400dd37-101d-4940-be15-3c963b6109b3",
"address_match_indicator": "false",
"shipping_address": {
"line1": "Apartment 852",
"line2": "Complex 741",
"line3": "House 963",
"city": "Chicago",
"postal_code": "50001",
"state": "IL",
"country": "840"
},
"gift_card_count": "01",
"gift_card_currency": "EUR",
"gift_card_amount": "25000",
"delivery_email": "james.mason@example.com",
"delivery_timeframe": "ELECTRONIC_DELIVERY",
"shipping_method": "ANOTHER_VERIFIED_ADDRESS",
"shipping_name_matches_cardholder_name": "true",
"preorder_indicator": "MERCHANDISE_AVAILABLE",
"preorder_availability_date:": "2019-04-18",
"reorder_indicator": "FIRST_TIME_ORDER",
"transaction_type": "GOODS_SERVICE_PURCHASE"
},
"payer": {
"reference": "6dcb24f5-74a0-4da3-98da-4f0aa0e88db3",
"account_age": "LESS_THAN_THIRTY_DAYS",
"account_creation_date": "2019-01-10",
"account_change_date": "2019-01-28",
"account_change_indicator": "THIS_TRANSACTION",
"account_password_change_date": "2019-01-15",
"account_password_change_indicator": "LESS_THAN_THIRTY_DAYS",
"home_phone": {
"country_code": "44",
"subscriber_number": "123456789"
},
"work_phone": {
"country_code": "44",
"subscriber_number": "1801555888"
},
"payment_account_creation_date": "2019-01-01",
"payment_account_age_indicator": "LESS_THAN_THIRTY_DAYS",
"suspicious_account_activity": "NO_SUSPICIOUS_ACTIVITY",
"purchases_last_6months_count": "03",
"transactions_last_24hours_count": "01",
"transaction_last_year_count": "05",
"provision_attempt_last_24hours_count": "01",
"shipping_address_time_created_reference": "2019-01-28",
"shipping_address_creation_indicator": "THIS_TRANSACTION"
},
"payer_prior_three_ds_authentication_data": {
"authentication_method": "FRICTIONLESS_AUTHENTICATION",
"acs_transaction_reference": "26c3f619-39a4-4040-bf1f-6fd433e6d615",
"authentication_timestamp": "2020-07-28T10:26:49.712Z",
"authentication_data": "secret123"
},
"recurring_authorization_data": {
"max_number_of_instalments": "05",
"frequency": "25",
"expiry_date": "2019-08-25"
},
"payer_login_data": {
"authentication_data": "secret123",
"authentication_timestamp": "2020-07-28T10:26:49.712Z",
"authentication_type": "MERCHANT_SYSTEM_AUTHENTICATION"
},
"browser_data": {
"accept_header": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"color_depth": "TWENTY_FOUR_BITS",
"ip": "123.123.123.123",
"java_enabled": "true",
"javascript_enabled": "true",
"language": "en-US",
"screen_height": "1080",
"screen_width": "1920",
"challenge_window_size": "FULL_SCREEN",
"timezone": "0",
"user_agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36"
},
"merchant_contact_url": "https://enp4qhvjseljg.x.pipedream.net/"
}
let options = {
method:"POST",
body: JSON.stringify(body),
headers:headers
}
const response = await fetch(`${process.env.GP_API_ENVIRONMENT}/ucp/authentications/${id}/initiate`, options);
const json = await response.json()
return json
}
const createCharge = async (data) => {
data.token_id
data.auth_id
data.serverTransRef
data.authenticationValue
data.eci
data.messageVersion
let accessToken = await getAccessToken()
let headers ={
"X-GP-Version": gpApiVersion,
"Content-Type": "application/json",
"Accept" : "application/json",
"Authorization" : "Bearer " + accessToken.token
}
let body = {
"account_name": accessToken.scope.accounts[0].name,
"type": "SALE",
"channel": "CNP",
"capture_mode": "AUTO",
"amount": "1999",
"currency": "GBP",
"reference": "93459c78-f3f9-427c-84df-ca0584bb55bf",
"country": "GB",
"ip_address": "123.123.123.123",
"payment_method": {
"id": data.token_id,
"name": "James Mason",
"entry_mode": "ECOM",
"authentication": {
"id": data.auth_id
// "three_ds": {
// "server_trans_ref": data.serverTransRef,
// "value": data.authenticationValue,
// "eci": data.eci,
// "message_version": data.messageVersion
// }
}
}
}
let options = {
method:"POST",
body: JSON.stringify(body),
headers:headers
}
console.log(body)
const response = await fetch(`${process.env.GP_API_ENVIRONMENT}/ucp/transactions`, options);
const json = await response.json()
return json
}
// Routes for application requests
app.get('/api/accessToken', async (request, response) => {
// define a function to get Access Token
let accessToken = await getAccessToken()
// pull environment from config file
let environment = process.env.GP_API_CLIENTSIDE_ENVIRONMENT
console.log(environment)
// Return object to client side
response.send({
"config": accessToken,
"environment": environment
})
})
app.all('/3ds2/challengeNotificationUrl', async (request, response) => {
console.log("*********************************** /api/3DSecure/challengeNotificationUrl API REQUEST ");
console.log("challengeNotifcationURL", request.body);
const data = request.body
try {
// decode base64 response from ACS
let buff = Buffer.from(data.cres, 'base64')
let string = buff.toString('utf8')
// Convert into JSON
let stringJson = JSON.parse(string)
// Pull out the individual elements
let acsTransID = stringJson.acsTransID
let transStatus = stringJson.transStatus
let threeDSServerTransID = stringJson.threeDSServerTransID
let messageType = stringJson.messageType
let messageVersion = stringJson.messageVersion
// Check if the challenge was successful
if (transStatus == "Y"){
// send a query string to the challenge URL to notify of a successful result
console.log(stringJson)
response.redirect("/challengeNotificationUrl.html?challengeSuccessful=true");
}
else{
console.log(stringJson)
response.redirect("/challengeNotificationUrl.html?challengeSuccessful=false");
}
} catch (error) {
console.log(error)
response.redirect("/challengeNotificationUrl.html?challengeSuccessful=false");
}
})
app.post('/3ds2/methodNotificationUrl', async (request, response) => {
console.log("*********************************** /api/3DSecure/methodNotificationUrl API REQUEST ");
console.log("methodNotificationUrl", request.body);
response.redirect("/methodNotificationUrl.html");
})
app.post('/3ds2/check3dsVersion', async (request, response) => {
console.log("*********************************** /api/3DSecure/checkVersion API REQUEST ");
const data = request.body;
// console.log(data)
try {
let checkVersionResponse = await check3dsVersion({
"paymentToken": data.paymentToken,
"challenge_return_url" : data.challengeNotificationUrl,
"three_ds_method_return_url" : data.methodNotificationUrl
})
console.log("/api/3DSecure/checkVersion API RESPONSE", checkVersionResponse);
// https://github.com/globalpayments/globalpayments-js/blob/20cc8a775af8dbcaf7b36489f94626632ba919f1/packages/globalpayments-3ds/src/interfaces.ts#L44
switch(checkVersionResponse.three_ds.enrolled_status){
case "NOT_ENROLLED":
response.send({
"enrolled":checkVersionResponse.three_ds.enrolled_status,
"methodData":checkVersionResponse.three_ds.method_data.encoded_method_data,
"methodUrl":checkVersionResponse.three_ds.method_url,
"serverTransactionId":checkVersionResponse.three_ds.server_trans_ref,
"three_ds":{
"eci":checkVersionResponse.three_ds.eci,
"liability_shift":checkVersionResponse.three_ds.liability_shift
},
"versions":{
"accessControlServer":{
"end": checkVersionResponse.three_ds.acs_protocol_version_end,
"start" : checkVersionResponse.three_ds.acs_protocol_version_start
},
"directoryServer":{
"end": checkVersionResponse.three_ds.ds_protocol_version_end,
"start" : checkVersionResponse.three_ds.ds_protocol_version_start
}
},
"id":checkVersionResponse.id
});
break;
case "ENROLLED":
response.send({
"enrolled":checkVersionResponse.three_ds.enrolled_status,
"methodData":checkVersionResponse.three_ds.method_data.encoded_method_data,
"methodUrl":checkVersionResponse.three_ds.method_url,
"serverTransactionId":checkVersionResponse.three_ds.server_trans_ref,
"versions":{
"accessControlServer":{
"end": checkVersionResponse.three_ds.acs_protocol_version_end,
"start" : checkVersionResponse.three_ds.acs_protocol_version_start
},
"directoryServer":{
"end": checkVersionResponse.three_ds.ds_protocol_version_end,
"start" : checkVersionResponse.three_ds.ds_protocol_version_start
}
},
"id":checkVersionResponse.id
});
break;
}
}
catch (error) {
response.send(error)
}
})
app.post('/3ds2/initiateAuthentication', async (request, response) => {
console.log("*********************************** /api/3DSecure/initiate3DSecure API REQUEST ");
const data = request.body
console.log("Data", data)
try {
let initiate3dsecureResponse = await initiate3dsecure({
"authenticationRequestType":data.authenticationRequestType,
"authenticationSource":data.authenticationSource,
"browserData":data.browserData,
"challengeNotificationUrl":data.challengeNotificationUrl,
"challengeRequestIndicator":data.challengeRequestIndicator,
"merchantContactUrl":data.merchantContactUrl,
"messageCategory":data.messageCategory,
"methodUrlComplete":data.methodUrlComplete,
"serverTransactionId":data.serverTransactionId,
"id":data.id,
"paymentToken":data.paymentToken
})
console.log(initiate3dsecureResponse)
switch(initiate3dsecureResponse.status){
// Successful
case "SUCCESS_AUTHENTICATED":
case "SUCCESS_ATTEMPT_MADE":
response.send({
"result":initiate3dsecureResponse.status,
"mpi": {
"authenticationValue":initiate3dsecureResponse.three_ds.authentication_value,
"eci":initiate3dsecureResponse.three_ds.eci,
},
"serverTransactionId": initiate3dsecureResponse.three_ds.server_trans_ref,
"status": initiate3dsecureResponse.three_ds.status,
});
break;
// Unsuccessful
case "NOT_AUTHENTICATED":
case "FAILED":
response.send({
"result":initiate3dsecureResponse.status,
"detail": initiate3dsecureResponse
});
break;
// Challenge required
case "CHALLENGE_REQUIRED":
// https://github.com/globalpayments/globalpayments-js/blob/20cc8a775af8dbcaf7b36489f94626632ba919f1/packages/globalpayments-3ds/src/interfaces.ts#L99
response.send({
"acsTransactionId": initiate3dsecureResponse.three_ds.acs_trans_ref,
"authenticationSource": initiate3dsecureResponse.three_ds.authentication_source,
"authenticationRequestType": initiate3dsecureResponse.three_ds.message_category,
"cardholderResponseInfo": initiate3dsecureResponse.three_ds.cardholder_response_info,
"challenge": {
"encodedChallengeRequest":initiate3dsecureResponse.three_ds.challenge_value,
"requestUrl":initiate3dsecureResponse.three_ds.acs_challenge_request_url,
},
"challengeMandated": initiate3dsecureResponse.three_ds.challenge_status,
"deviceRenderOptions": initiate3dsecureResponse.three_ds.authentication_source,
"dsTransactionId": initiate3dsecureResponse.three_ds.ds_trans_ref,
"messageCategory": initiate3dsecureResponse.three_ds.message_category,
"messageExtension": "",
"messageVersion": {
"end":initiate3dsecureResponse.three_ds.message_version,
"start":initiate3dsecureResponse.three_ds.message_version,
},
"mpi": {
"authenticationValue":initiate3dsecureResponse.three_ds.authentication_value,
"eci":initiate3dsecureResponse.three_ds.eci,
},
"serverTransactionId": initiate3dsecureResponse.three_ds.server_trans_ref,
"status": initiate3dsecureResponse.three_ds.status,
"statusReason": initiate3dsecureResponse.three_ds.status_reason,
})
break;
default:
response.send({
"result":error,
"detail":initiate3dsecureResponse
})
break
}
} catch (error) {
response.send(error)
}
})
app.post('/api/createCharge', async (request, response) => {
console.log("*********************************** /api/createCharge API REQUEST ");
const data = request.body
console.log(data)
let authenticationData = await getAuthenticationResult(data.auth_id)
console.log(authenticationData)
try {
switch(authenticationData.three_ds.liability_shift){
case "NO":
response.redirect("/error.html?" + qs.stringify(authenticationData))
break;
case "YES":
// Take the payment token and auth ID from the client side and send for auth
let charge = await createCharge({
"token_id":data.payment_token,
"auth_id":data.auth_id
})
switch (charge.payment_method.result){
case "00":
// Redirect to success page with tranaction response
response.redirect("/success.html?" + qs.stringify(charge))
break;
default:
// Redirect to failure page
response.redirect("/error.html?" + qs.stringify(charge))
break;
}
break;
default:
response.redirect("/error.html?" + qs.stringify(authenticationData))
}
}
catch (error) {
response.redirect("/error.html?" + qs.stringify(authenticationData))
}
})
// Formatters!
let checkVersionFormatter = async (jsonResponse) =>{
let response = {
"enrolled":jsonResponse.three_ds.enrolled_status,
"methodData":jsonResponse.three_ds.method_data.encoded_method_data,
"methodUrl":jsonResponse.three_ds.method_url,
"serverTransactionId":jsonResponse.three_ds.server_trans_ref,
"versions":{
"accessControlServer":jsonResponse.three_ds.acs_protocol_version_start,
"directoryServer":jsonResponse.three_ds.ds_protocol_version_start
},
"id":jsonResponse.id,
"three_ds":{
"message_version": jsonResponse.three_ds.message_version,
"ds_protocol_version_start":jsonResponse.three_ds.ds_protocol_version_start,
"ds_protocol_version_end":jsonResponse.three_ds.ds_protocol_version_end,
"acs_protocol_version_start": jsonResponse.three_ds.acs_protocol_version_start,
"acs_protocol_version_end": jsonResponse.three_ds.acs_protocol_version_end,
"enrolled_status:": jsonResponse.three_ds.enrolled_status,
"eci": jsonResponse.three_ds.eci,
"liability_shift": jsonResponse.three_ds.liability_shift,
"challenge_model": jsonResponse.three_ds.challenge_model,
"challenge_status": jsonResponse.three_ds.challenge_status,
"session_data_field_name": jsonResponse.three_ds.session_data_field_name,
"message_type": jsonResponse.three_ds.message_type,
"challenge_value": jsonResponse.three_ds.challenge_value,
"redirect_url": jsonResponse.three_ds.redirect_url,
"acs_challenge_request_url": jsonResponse.three_ds.acs_challenge_request_url
},
"notifications":{
"challenge_return_url": jsonResponse.notifications.challenge_return_url
}
}
return response;
}
let initiateAuthenticationFormatter = async (jsonResponse) =>{
let response = {
"acsTransactionId": jsonResponse.three_ds.acs_trans_ref,
"authenticationSource": jsonResponse.three_ds.authentication_source,
"authenticationRequestType": jsonResponse.three_ds.message_category,
"cardholderResponseInfo": jsonResponse.three_ds.cardholder_response_info,
"challenge": {
"encodedChallengeRequest":jsonResponse.three_ds.challenge_value,
"requestUrl":jsonResponse.three_ds.acs_challenge_request_url, // or redirect_url / acs_challenge_request_url
},
"challengeMandated": jsonResponse.three_ds.challenge_status,
"deviceRenderOptions": jsonResponse.three_ds.authentication_source,
"dsTransactionId": jsonResponse.ds_trans_ref,
"messageCategory": jsonResponse.three_ds.liability_shift,
"messageExtension": jsonResponse.three_ds.challenge_model,
"messageVersion": jsonResponse.three_ds.message_version,
"mpi": {
"authenticationValue":jsonResponse.three_ds.authentication_value,
"eci":jsonResponse.three_ds.eci,
},
"serverTransactionId": jsonResponse.three_ds.server_trans_ref,
"status": jsonResponse.three_ds.status,
"statusReason": jsonResponse.three_ds.status_reason,
}
return response
}