forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkout.js
77 lines (62 loc) · 1.94 KB
/
checkout.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
var util = require('util');
var express = require('express');
var router = express.Router();
var orderService = require('./services/orders');
var bot = require('./bot');
var botUtils = require('./bot/utils');
/* GET Checkout */
router.get('/', function (req, res, next) {
// orderId and user address
var orderId = req.query.orderId;
var address = botUtils.deserializeAddress(req.query.address);
console.log('user address is', address);
orderService.retrieveOrder(orderId).then((order) => {
// Check order exists
if (!order) {
throw new Error('Order ID not found');
}
// Check order if order is already processed
if (order.payed) {
// Dispatch completion dialog
bot.beginDialog(address, 'checkout:completed', { orderId: orderId });
// Show completion
return res.render('checkout/completed', {
title: 'Contoso Flowers - Order Processed',
order: order
});
}
// Payment form
return res.render('checkout/index', {
title: 'Contoso Flowers - Order Checkout',
address: req.query.address,
order: order
});
}).catch((err) => {
next(err);
});
});
/* POST Checkout */
router.post('/', function (req, res, next) {
// orderId and user address
var orderId = req.body.orderId;
var address = botUtils.deserializeAddress(req.body.address);
console.log('user address is', address);
// Payment information
var paymentDetails = {
creditcardNumber: req.body.creditcard,
creditcardHolder: req.body.fullname
};
// Complete order
orderService.confirmOrder(orderId, paymentDetails).then((processedOrder) => {
// Dispatch completion dialog
bot.beginDialog(address, 'checkout:completed', { orderId: orderId });
// Show completion
return res.render('checkout/completed', {
title: 'Contoso Flowers - Order processed',
order: processedOrder
});
}).catch((err) => {
next(err);
});
});
module.exports = router;