-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb.js
More file actions
90 lines (71 loc) · 2.45 KB
/
Copy pathweb.js
File metadata and controls
90 lines (71 loc) · 2.45 KB
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
"use strict";
// Import packages.
const express = require("express");
const app = express();
// Launch server.
app.listen(process.env.PORT || 5000, () => {
console.log(`server is listening to ${process.env.PORT || 5000}...`);
});
// Middleware configuration to serve static file.
app.use(express.static(__dirname + "/public"));
// Set ejs as template engine.
app.set("view engine", "ejs");
// Router configuration to serve web page containing pay button.
app.get("/", (req, res) => {
res.render(__dirname + "/index");
})
// Import environment variables from .env file.
require("dotenv").config();
// Import packages.
const uuid = require("uuid/v4");
const cache = require("memory-cache");
// Instanticate LINE Pay API SDK.
const line_pay = require("line-pay");
const pay = new line_pay({
channelId: process.env.LINE_PAY_CHANNEL_ID,
channelSecret: process.env.LINE_PAY_CHANNEL_SECRET,
hostname: process.env.LINE_PAY_HOSTNAME,
isSandbox: true
})
// Router configuration to start payment.
app.use("/pay/reserve", (req, res) => {
let options = {
productName: "チョコレート",
amount: 1,
currency: "JPY",
orderId: uuid(),
confirmUrl: process.env.LINE_PAY_CONFIRM_URL
}
pay.reserve(options).then((response) => {
let reservation = options;
reservation.transactionId = response.info.transactionId;
console.log(`Reservation was made. Detail is following.`);
console.log(reservation);
// Save order information
cache.put(reservation.transactionId, reservation);
res.redirect(response.info.paymentUrl.web);
})
})
// Router configuration to recieve notification when user approves payment.
app.use("/pay/confirm", (req, res) => {
if (!req.query.transactionId){
throw new Error("Transaction Id not found.");
}
// Retrieve the reservation from database.
let reservation = cache.get(req.query.transactionId);
if (!reservation){
throw new Error("Reservation not found.");
}
console.log(`Retrieved following reservation.`);
console.log(reservation);
let confirmation = {
transactionId: req.query.transactionId,
amount: reservation.amount,
currency: reservation.currency
}
console.log(`Going to confirm payment with following options.`);
console.log(confirmation);
pay.confirm(confirmation).then((response) => {
res.send("決済が完了しました。");
});
})