-
Notifications
You must be signed in to change notification settings - Fork 7
/
without_middleware.js
156 lines (134 loc) · 5.81 KB
/
without_middleware.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
"use strict"
require("dotenv").config();
const server = require("express")();
const cache = require("memory-cache");
const debug = require("debug")("pay-test");
// Importing LINE Pay API SDK
const linePay = require("line-pay");
const pay = new linePay({
channelId: process.env.LINE_PAY_CHANNEL_ID,
channelSecret: process.env.LINE_PAY_CHANNEL_SECRET,
hostname: process.env.LINE_PAY_HOSTNAME
});
// Importing LINE Messaging API SDK
const lineBot = require("@line/bot-sdk");
const botConfig = {
channelAccessToken: process.env.LINE_BOT_ACCESS_TOKEN,
channelSecret: process.env.LINE_BOT_CHANNEL_SECRET
}
const bot = new lineBot.Client(botConfig);
server.listen(process.env.PORT || 5000);
// Webhook for Messaging API.
server.post("/webhook", lineBot.middleware(botConfig), (req, res, next) => {
res.sendStatus(200);
req.body.events.map((event) => {
// We skip connection validation message.
if (event.replyToken == "00000000000000000000000000000000" || event.replyToken == "ffffffffffffffffffffffffffffffff") return;
// Recall the context since we save the context with userId.
let context = cache.get(event.source.userId);
if (!context){
// This should be the first message.
debug(`This should be the first message.`);
let message = {
type: "template",
altText: "You need to purchase subscription to use this Chatbot. It's 1yen/month. Do you want to puchase?",
template: {
type: "confirm",
text: "You need to purchase subscription to use this Chatbot. It's 1yen/month. Do you want to purchase?",
actions: [
{type: "postback", label: "Yes", data: "yes"},
{type: "postback", label: "No Thanks", data: "no"}
]
}
}
return bot.replyMessage(event.replyToken, message).then((response) => {
cache.put(event.source.userId, {
subscription: "inactive"
});
});
} else if (context.subscription == "inactive"){
// This should be the answer for the question if the user like to buy the subscription.
debug(`This should be the answer for the question if the user like to buy the subscription.`);
if (event.type == "postback"){
if (event.postback.data == "yes"){
let reservation = {
productName: "My product",
amount: 1,
currency: "JPY",
confirmUrl: process.env.LINE_PAY_CONFIRM_URL || `https://${req.hostname}/pay/confirm`,
confirmUrlType: "SERVER",
orderId: `${event.source.userId}-${Date.now()}`
}
// Call LINE Pay reserve API.
pay.reserve(reservation).then((response) => {
reservation.transactionId = response.info.transactionId;
reservation.userId = event.source.userId;
cache.put(reservation.transactionId, reservation);
let message = {
type: "template",
altText: "Please proceed to the payment.",
template: {
type: "buttons",
text: "Please proceed to the payment.",
actions: [
{type: "uri", label: "Pay by LINE Pay", uri: response.info.paymentUrl.web},
]
}
}
// Now we can provide payment URL.
return bot.replyMessage(event.replyToken, message);
}).then((response) => {
return;
});
} else {
// User does not purchase so say good bye.
let message = {
type: "text",
text: "OK. Have a nice day."
}
return bot.replyMessage(event.replyToken, message).then((response) => {
cache.del(event.source.userId);
return;
});
}
}
} else if (context.subscription == "active"){
// User has the active subscription.
debug(`User has the active subscription.`);
delete event.message.id;
return bot.replyMessage(event.replyToken, event.message).then((response) => {
return;
});
}
});
});
// If user approve the payment, LINE Pay server call this webhook.
server.get("/pay/confirm", (req, res, next) => {
if (!req.query.transactionId){
return res.status(400).send("Transaction Id not found.");
}
// Retrieve the reservation from database.
let reservation = cache.get(req.query.transactionId);
if (!reservation){
return res.status(400).send("Reservation not found.")
}
let confirmation = {
transactionId: req.query.transactionId,
amount: reservation.amount,
currency: reservation.currency
}
return pay.confirm(confirmation).then((response) => {
res.sendStatus(200);
let messages = [{
type: "sticker",
packageId: 2,
stickerId: 144
},{
type: "text",
text: "Congratulations! Now your chatbot is fully functional."
}]
return bot.pushMessage(reservation.userId, messages);
}).then((response) => {
cache.put(reservation.userId, {subscription: "active"});
});
});