-
Notifications
You must be signed in to change notification settings - Fork 51
/
index.js
481 lines (384 loc) · 14.3 KB
/
index.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
const express = require("express");
const path = require("path");
const hbs = require("express-handlebars");
const dotenv = require("dotenv");
const morgan = require("morgan");
const { uuid } = require("uuidv4");
const { hmacValidator } = require('@adyen/api-library');
const { Client, Config, CheckoutAPI } = require("@adyen/api-library");
const { PaymentModel, PaymentDetailsModel, getAll, getByMerchantReference, addToHistory, updatePayment, put} = require('./storage.js')
// init app
const app = express();
// setup request logging
app.use(morgan("dev"));
// Parse JSON bodies
app.use(express.json());
// Parse URL-encoded bodies
app.use(express.urlencoded({ extended: true }));
// Serve client from build folder
app.use(express.static(path.join(__dirname, "/public")));
// enables environment variables by
// parsing the .env file and assigning it to process.env
dotenv.config({
path: "./.env",
});
// Adyen Node.js API library boilerplate (configuration, etc.)
const config = new Config();
config.apiKey = process.env.ADYEN_API_KEY;
const client = new Client({ config });
client.setEnvironment("TEST"); // change to LIVE for production
const checkout = new CheckoutAPI(client);
app.engine(
"handlebars",
hbs.engine({
defaultLayout: "main",
layoutsDir: __dirname + "/views/layouts",
helpers: require("./util/helpers"),
})
);
app.set("view engine", "handlebars");
/* ################# API ENDPOINTS ###################### */
// Get payment methods
app.post("/api/getPaymentMethods", async (req, res) => {
try {
const response = await checkout.PaymentsApi.paymentMethods({
channel: "Web",
merchantAccount: process.env.ADYEN_MERCHANT_ACCOUNT,
});
res.json(response);
} catch (err) {
console.error(`Error: ${err.message}, error code: ${err.errorCode}`);
res.status(err.statusCode).json(err.message);
}
});
// submit additional payment details to complete the payment
app.post("/api/submitAdditionalDetails", async (req, res) => {
// Create the payload for submitting payment details
const payload = {
details: req.body.details,
paymentData: req.body.paymentData,
};
try {
// Return the response back to client
// (for further action handling or presenting result to shopper)
const response = await checkout.PaymentsApi.paymentsDetails(payload);
res.json(response);
} catch (err) {
console.error(`Error: ${err.message}, error code: ${err.errorCode}`);
res.status(err.statusCode).json(err.message);
}
});
// Handle all redirects from payment type
app.all("/api/handleShopperRedirect", async (req, res) => {
// Create the payload for submitting payment details
const redirect = req.method === "GET" ? req.query : req.body;
const details = {};
if (redirect.redirectResult) {
details.redirectResult = redirect.redirectResult;
} else if (redirect.payload) {
details.payload = redirect.payload;
}
try {
const response = await checkout.PaymentsApi.paymentsDetails({ details });
// Conditionally handle different result codes for the shopper
switch (response.resultCode) {
case "Authorised":
res.redirect("/result/success");
break;
case "Pending":
case "Received":
res.redirect("/result/pending");
break;
case "Refused":
res.redirect("/result/failed");
break;
default:
res.redirect("/result/error");
break;
}
} catch (err) {
console.error(`Error: ${err.message}, error code: ${err.errorCode}`);
res.redirect("/result/error");
}
});
// Pre-authorisation using the Payments API
app.post("/api/pre-authorisation", async (req, res) => {
// find shopper IP from request
const shopperIP = req.headers["x-forwarded-for"] || req.connection.remoteAddress;
try {
// unique ref for the transaction
const orderRef = uuid();
// Get host and protocol
const localhost = req.get('host');
const protocol = req.socket.encrypted? 'https' : 'http';
// Ideally the data passed here should be computed based on business logic
const response = await checkout.PaymentsApi.payments({
amount: { currency: "EUR", value: 24999 }, // value is 249.99€ in minor units
reference: orderRef, // required
merchantAccount: process.env.ADYEN_MERCHANT_ACCOUNT, // required
channel: "Web",
paymentMethod: req.body.paymentMethod,
additionalData: {
// required for 3ds2 native flow
allow3DS2: true,
// Set `authorisationType` to `preAuth`
authorisationType: "PreAuth",
},
origin: `${protocol}://${localhost}`, // required for 3ds2 native flow
browserInfo: req.body.browserInfo, // required for 3ds2
shopperIP, // required by some issuers for 3ds2
returnUrl: `${protocol}://${localhost}/api/handleShopperRedirect?orderRef=${orderRef}`, // required for 3ds2 redirect flow
});
console.log(response);
let expiryDate = new Date();
// add 28 days for define pre-authorisation expiry date
// The value of '28' varies per scheme, see https://docs.adyen.com/online-payments/adjust-authorisation/#validity.
expiryDate.setDate(expiryDate.getDate() + 28);
if(response.resultCode == 'Authorised') {
// save payment into storage
put(
new PaymentModel(
response.merchantReference,
response.pspReference,
response.amount.value,
response.amount.currency,
new Date(), // booking date
expiryDate,
response.paymentMethod.brand,
[] // initialise history
));
}
res.json(response);
} catch (err) {
console.error(`Error: ${err.message}, error code: ${err.errorCode}`);
res.status(err.statusCode).json(err.message);
}
});
/* ################# end API ENDPOINTS ###################### */
/* ################# CLIENT SIDE ENDPOINTS ###################### */
// Index (select a demo)
app.get("/", (req, res) =>
res.render("index", {
title: "Adyen Booking View"
})
);
// View cart (continue to checkout)
app.get("/preview", (req, res) =>
res.render("preview", {
title: "Adyen Booking Preview"
})
);
// Booking page (make a booking)
app.get("/booking", (req, res) =>
res.render("booking", {
title: "Adyen Booking Cart",
clientKey: process.env.ADYEN_CLIENT_KEY
})
);
// Result page
app.get("/result/:type", (req, res) =>
res.render("result", {
type: req.params.type,
})
);
// Admin Panel screens
// Admin Panel page
app.get("/admin", (req, res) =>
res.render("admin/index", {
title: "Adyen Admin Panel",
data: getAll()
})
);
// Admin Panel result (after adjustment/capture)
app.get("/admin/result/:status/:reference", (req, res) => {
if(req.params.status == "received") {
result = "success"
} else {
result = "error"
}
res.render("admin/result", {
title: "Adyen Admin Result",
type: result,
reference: req.params.reference,
refusalReason: req.query.refusalReason
})
}
);
// Admin Panel details
app.get("/admin/details/:reference", (req, res) =>
res.render("admin/details", {
title: "Adyen Admin Payment History",
data: getByMerchantReference(req.params.reference)
})
);
// Admin Panel adjust (amount value or extend validity)
app.post("/admin/update-payment-amount", async (req, res) => {
try {
const paymentModel = getByMerchantReference(req.body.reference);
if(paymentModel == null) {
throw Error("Payment not found in storage - Reference: " + req.body.reference);
}
const response = await checkout.ModificationsApi.updateAuthorisedAmount(
paymentModel.pspReference,
{
merchantAccount: process.env.ADYEN_MERCHANT_ACCOUNT,
amount: { currency: "EUR", value: req.body.amount },
reference: req.body.reference,
industryUsage: "delayedCharge"
}
)
console.log(response);
res.json(response);
} catch (err) {
console.error(`Error: ${err.message}, error code: ${err.errorCode}`);
res.status(err.statusCode).json(err.message);
}
});
// Admin Panel capture (capture payment)
app.post("/admin/capture-payment", async (req, res) => {
try {
const paymentModel = getByMerchantReference(req.body.reference);
if(paymentModel == null) {
throw Error("Payment not found in storage - Reference: " + req.body.reference);
}
const response = await checkout.ModificationsApi.captureAuthorisedPayment(
paymentModel.pspReference,
{
merchantAccount: process.env.ADYEN_MERCHANT_ACCOUNT,
amount: { currency: paymentModel.currency, value: paymentModel.amount },
reference: paymentModel.merchantReference
}
)
console.log(response);
res.json(response);
} catch (err) {
console.error(`Error: ${err.message}, error code: ${err.errorCode}`);
res.status(err.statusCode).json(err.message);
}
});
// Admin Panel reversal (refund or cancel payment)
app.post("/admin/reversal-payment", async (req, res) => {
try {
const paymentModel = getByMerchantReference(req.body.reference);
if(paymentModel == null) {
throw Error("Payment not found in storage - Reference: " + req.body.reference);
}
const response = await checkout.ModificationsApi.refundOrCancelPayment(
paymentModel.pspReference,
{
merchantAccount: process.env.ADYEN_MERCHANT_ACCOUNT,
reference: paymentModel.merchantReference
}
)
console.log(response);
res.json(response);
} catch (err) {
console.error(`Error: ${err.message}, error code: ${err.errorCode}`);
res.status(err.statusCode).json(err.message);
}
});
/* ################# end CLIENT SIDE ENDPOINTS ###################### */
/* ################# WEBHOOK ###################### */
// Process incoming Webhook: get NotificationRequestItem, validate HMAC signature,
// consume the event asynchronously, send response status code 202
app.post("/api/webhooks/notifications", async (req, res) => {
// YOUR_HMAC_KEY from the Customer Area
const hmacKey = process.env.ADYEN_HMAC_KEY;
const validator = new hmacValidator()
// Notification Request JSON
const notificationRequest = req.body;
const notificationRequestItems = notificationRequest.notificationItems
// fetch first (and only) NotificationRequestItem
const notification = notificationRequestItems[0].NotificationRequestItem
console.log(notification)
// Handle the notification
if( validator.validateHMAC(notification, hmacKey) ) {
// valid hmac: process event
const merchantReference = notification.merchantReference;
const eventCode = notification.eventCode;
console.log("merchantReference:" + merchantReference + " eventCode:" + eventCode);
// consume event asynchronously
consumeEvent(notification);
// acknowledge event has been consumed
res.status(202).send(); // Send a 202 response with an empty body
} else {
// invalid hmac
console.log("Invalid HMAC signature: " + notification);
res.status(401).send('Invalid HMAC signature');
}
});
// process payload asynchronously
function consumeEvent(notification) {
// add item to DB, queue or different thread
if (notification.eventCode == "AUTHORISATION") {
// webhook with payment authorisation
console.log("Payment authorized - pspReference:" + notification.pspReference + " eventCode:" + notification.eventCode);
// save payment
savePayment(notification);
} else if (notification.eventCode == "AUTHORISATION_ADJUSTMENT") {
// webhook with authorisation adjustment
console.log("Authorisation adjustment - pspReference:" + notification.pspReference + " eventCode:" + notification.eventCode);
// save payment
savePayment(notification);
if(notification.success) {
// update amount and expiry date of the pre-auth payment
let expiryDate = new Date();
// add 28 days for define pre-authorisation expiry date
// The value of '28' varies per scheme, see https://docs.adyen.com/online-payments/adjust-authorisation/#validity.
expiryDate.setDate(expiryDate.getDate() + 28);
updatePayment(notification.merchantReference, notification.amount.value, expiryDate);
}
} else if (notification.eventCode == "CAPTURE") {
// webhook with payment capture
console.log("Payment capture - pspReference:" + notification.pspReference + " eventCode:" + notification.eventCode);
// save payment
savePayment(notification);
} else if (notification.eventCode == "CAPTURE_FAILED") {
// webhook with payment capture failure
console.log("Payment capture failed - pspReference:" + notification.pspReference + " eventCode:" + notification.eventCode);
// save payment
savePayment(notification);
} else if (notification.eventCode == "CANCEL_OR_REFUND") {
// webhook with payment CANCEL_OR_REFUND
console.log("Payment cancel_or_refund - pspReference:" + notification.pspReference + " eventCode:" + notification.eventCode);
// save payment
savePayment(notification);
} else if (notification.eventCode == "REFUND_FAILED") {
// webhook with payment refund failure
console.log("Payment refund failed - pspReference:" + notification.pspReference + " eventCode:" + notification.eventCode);
// save payment
savePayment(notification);
} else if (notification.eventCode == "REFUNDED_REVERSED") {
// webhook with payment refund reversed
console.log("Payment refund reversed - pspReference:" + notification.pspReference + " eventCode:" + notification.eventCode);
// save payment
savePayment(notification);
} else {
console.log("Unexpected eventCode: " + notification.eventCode);
}
}
// add payment in storage
function savePayment(notification) {
// save payment
addToHistory(
new PaymentDetailsModel(
notification.merchantReference,
notification.pspReference,
notification.originalReference,
notification.amount.value,
notification.amount.currency,
new Date(),
notification.eventCode,
notification.reason,
notification.paymentMethod,
notification.success
));
}
/* ################# end WEBHOOK ###################### */
/* ################# UTILS ###################### */
function getPort() {
return process.env.PORT || 8080;
}
/* ################# end UTILS ###################### */
// Start server
app.listen(getPort(), () => console.log(`Server started -> http://localhost:${getPort()}`));