forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkout.js
120 lines (104 loc) · 4.05 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
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
var util = require('util');
var builder = require('botbuilder');
var botUtils = require('../utils');
var siteUrl = require('../site-url');
var orderService = require('../../services/orders');
// Checkout flow
var RestartMessage = 'restart';
var StartOver = 'start_over';
var KeepGoing = 'continue';
var Help = 'help';
var lib = new builder.Library('checkout');
lib.dialog('/', [
function (session, args, next) {
args = args || {};
var order = args.order;
if (!order) {
// 'Changed my mind' was pressed, continue to next step and prompt for options
return next();
}
// Serialize user address
var addressSerialized = botUtils.serializeAddress(session.message.address);
// Create order (with no payment - pending)
orderService.placePendingOrder(order).then(function (order) {
// Build Checkout url using previously stored Site url
var checkoutUrl = util.format(
'%s/checkout?orderId=%s&address=%s',
siteUrl.retrieve(),
encodeURIComponent(order.id),
encodeURIComponent(addressSerialized));
var messageText = session.gettext('final_price', order.selection.price);
var card = new builder.HeroCard(session)
.text(messageText)
.buttons([
builder.CardAction.openUrl(session, checkoutUrl, 'add_credit_card'),
builder.CardAction.imBack(session, session.gettext(RestartMessage), RestartMessage)
]);
session.send(new builder.Message(session)
.addAttachment(card));
});
},
function (session, args) {
builder.Prompts.choice(session, 'select_how_to_continue', [
session.gettext(StartOver),
session.gettext(KeepGoing),
session.gettext(Help)
]);
},
function (session, args) {
switch (args.response.entity) {
case KeepGoing:
return session.reset();
case StartOver:
return session.reset('/');
case Help:
return session.beginDialog('help:/');
}
}
]);
// Checkout completed (initiated from web application. See /checkout.js in the root folder)
lib.dialog('completed', function (session, args, next) {
args = args || {};
var orderId = args.orderId;
// Retrieve order and create ReceiptCard
orderService.retrieveOrder(orderId).then(function (order) {
if (!order) {
throw new Error(session.gettext('order_not_found'));
}
var messageText = session.gettext(
'order_details',
order.id,
order.selection.name,
order.details.recipient.firstName,
order.details.recipient.lastName,
order.details.note);
var receiptCard = new builder.ReceiptCard(session)
.title(order.paymentDetails.creditcardHolder)
.facts([
builder.Fact.create(session, order.id, 'order_number'),
builder.Fact.create(session, offuscateNumber(order.paymentDetails.creditcardNumber), 'payment_method')
])
.items([
builder.ReceiptItem.create(session, order.selection.price, order.selection.name)
.image(builder.CardImage.create(session, order.selection.imageUrl))
])
.total(order.selection.price)
.buttons([
builder.CardAction.openUrl(session, 'https://dev.botframework.com/', 'more_information')
]);
var message = new builder.Message(session)
.text(messageText)
.addAttachment(receiptCard);
session.endDialog(message);
}).catch(function (err) {
session.endDialog(session.gettext('error_ocurred', err.message));
});
});
// Helpers
function offuscateNumber(cardNumber) {
return cardNumber.substring(0, 4) + ' ****';
}
// Export createLibrary() function
module.exports.createLibrary = function () {
return lib.clone();
};