forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
242 lines (206 loc) · 8.02 KB
/
app.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
// This loads the environment variables from the .env file
require('dotenv-extended').load();
var util = require('util');
var builder = require('botbuilder');
var restify = require('restify');
var payments = require('./payments');
var checkout = require('./checkout');
var catalog = require('./services/catalog');
var connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log('%s listening to %s', server.name, server.url);
});
server.post('/api/messages', connector.listen());
var CartIdKey = 'CardId';
var bot = new builder.UniversalBot(connector, (session) => {
catalog.getPromotedItem().then(product => {
// Store userId for later, when reading relatedTo to resume dialog with the receipt
var cartId = product.id;
session.conversationData[CartIdKey] = cartId;
session.conversationData[cartId] = session.message.address.user.id;
// Create PaymentRequest obj based on product information
var paymentRequest = createPaymentRequest(cartId, product);
var buyCard = new builder.HeroCard(session)
.title(product.name)
.subtitle(util.format('%s %s', product.currency, product.price))
.text(product.description)
.images([
new builder.CardImage(session).url(product.imageUrl)
])
.buttons([
new builder.CardAction(session)
.title('Buy')
.type(payments.PaymentActionType)
.value(paymentRequest)
]);
session.send(new builder.Message(session)
.addAttachment(buyCard));
});
});
bot.set('persistConversationData', true);
connector.onInvoke((invoke, callback) => {
console.log('onInvoke', invoke);
// This is a temporary workaround for the issue that the channelId for "webchat" is mapped to "directline" in the incoming RelatesTo object
invoke.relatesTo.channelId = invoke.relatesTo.channelId === 'directline' ? 'webchat' : invoke.relatesTo.channelId;
var storageCtx = {
address: invoke.relatesTo,
persistConversationData: true,
conversationId: invoke.relatesTo.conversation.id
};
connector.getData(storageCtx, (err, data) => {
var cartId = data.conversationData[CartIdKey];
if (!invoke.relatesTo.user && cartId) {
// Bot keeps the userId in context.ConversationData[cartId]
var userId = data.conversationData[cartId];
invoke.relatesTo.useAuth = true;
invoke.relatesTo.user = { id: userId };
}
// Continue based on PaymentRequest event
var paymentRequest = null;
switch (invoke.name) {
case payments.Operations.UpdateShippingAddressOperation:
case payments.Operations.UpdateShippingOptionOperation:
paymentRequest = invoke.value;
// Validate address AND shipping method (if selected)
checkout
.validateAndCalculateDetails(paymentRequest, paymentRequest.shippingAddress, paymentRequest.shippingOption)
.then(updatedPaymentRequest => {
// return new paymentRequest with updated details
callback(null, updatedPaymentRequest, 200);
}).catch(err => {
// return error to onInvoke handler
callback(err);
// send error message back to user
bot.beginDialog(invoke.relatesTo, 'checkout_failed', {
errorMessage: err.message
});
});
break;
case payments.Operations.PaymentCompleteOperation:
var paymentRequestComplete = invoke.value;
paymentRequest = paymentRequestComplete.paymentRequest;
var paymentResponse = paymentRequestComplete.paymentResponse;
// Validate address AND shipping method
checkout
.validateAndCalculateDetails(paymentRequest, paymentResponse.shippingAddress, paymentResponse.shippingOption)
.then(updatedPaymentRequest =>
// Process Payment
checkout
.processPayment(updatedPaymentRequest, paymentResponse)
.then(chargeResult => {
// return success
callback(null, { result: "success" }, 200);
// send receipt to user
bot.beginDialog(invoke.relatesTo, 'checkout_receipt', {
paymentRequest: updatedPaymentRequest,
chargeResult: chargeResult
});
})
).catch(err => {
// return error to onInvoke handler
callback(err);
// send error message back to user
bot.beginDialog(invoke.relatesTo, 'checkout_failed', {
errorMessage: err.message
});
});
break;
}
});
});
bot.dialog('checkout_receipt', function (session, args) {
console.log('checkout_receipt', args);
cleanupConversationData(session);
var paymentRequest = args.paymentRequest;
var chargeResult = args.chargeResult;
var shippingAddress = chargeResult.shippingAddress;
var shippingOption = chargeResult.shippingOption;
var orderId = chargeResult.orderId;
// send receipt card
var items = paymentRequest.details.displayItems
.map(o => builder.ReceiptItem.create(session, o.amount.currency + ' ' + o.amount.value, o.label));
var receiptCard = new builder.ReceiptCard(session)
.title('Contoso Order Receipt')
.facts([
builder.Fact.create(session, orderId, 'Order ID'),
builder.Fact.create(session, chargeResult.methodName, 'Payment Method'),
builder.Fact.create(session, [shippingAddress.addressLine, shippingAddress.city, shippingAddress.region, shippingAddress.country].join(', '), 'Shipping Address'),
builder.Fact.create(session, shippingOption, 'Shipping Option')
])
.items(items)
.total(paymentRequest.details.total.amount.currency + ' ' + paymentRequest.details.total.amount.value);
session.endDialog(
new builder.Message(session)
.addAttachment(receiptCard));
});
bot.dialog('checkout_failed', function (session, args) {
cleanupConversationData(session);
session.endDialog('Could not process your payment: %s', args.errorMessage);
});
// PaymentRequest with default options
function createPaymentRequest(cartId, product) {
if (!cartId) {
throw new Error('cartId is missing');
}
if (!product) {
throw new Error('product is missing');
}
// PaymentMethodData[]
var paymentMethods = [{
supportedMethods: [payments.MicrosoftPayMethodName],
data: {
mode: process.env.PAYMENTS_LIVEMODE === 'true' ? null : 'TEST',
merchantId: process.env.PAYMENTS_MERCHANT_ID,
supportedNetworks: ['visa', 'mastercard'],
supportedTypes: ['credit']
}
}];
// PaymentDetails
var paymentDetails = {
total: {
label: 'Total',
amount: { currency: product.currency, value: product.price.toFixed(2) },
pending: true
},
displayItems: [
{
label: product.name,
amount: { currency: product.currency, value: product.price.toFixed(2) }
}, {
label: 'Shipping',
amount: { currency: product.currency, value: '0.00' },
pending: true
}, {
label: 'Sales Tax',
amount: { currency: product.currency, value: '0.00' },
pending: true
}],
// until a shipping address is selected, we can't offer shipping options or calculate taxes or shipping costs
shippingOptions: []
};
// PaymentOptions
var paymentOptions = {
requestPayerName: true,
requestPayerEmail: true,
requestPayerPhone: true,
requestShipping: true,
shippingType: 'shipping'
};
// PaymentRequest
return {
id: cartId,
expires: '1.00:00:00', // 1 day
methodData: paymentMethods, // paymethodMethods: paymentMethods,
details: paymentDetails, // paymentDetails: paymentDetails,
options: paymentOptions // paymentOptions: paymentOptions
};
}
function cleanupConversationData(session) {
var cartId = session.conversationData[CartIdKey];
delete session.conversationData[CartIdKey];
delete session.conversationData[cartId];
}