-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
160 lines (149 loc) · 4.65 KB
/
app.js
File metadata and controls
160 lines (149 loc) · 4.65 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
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
import express from 'express';
import { serve, setup } from 'swagger-ui-express';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
import { readFileSync } from 'fs';
import { validateLightningAddress, LIGHTNING_ADDRESS_INVALID, getRestOptionsGot, getLightningAddressUri, getGot, getQrCode, postGot } from './util.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const lnpayapihtml = 'ln-pay-api.html';
const lnpayapijson = 'ln-pay-api.json';
const PORT = process.env.PORT || 3000;
const url = `http://localhost:${PORT}`
const app = express()
app.set("view engine", "ejs");
app.use(express.static('public'));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.listen(PORT, () => {
console.log(`App listening on ${url}`)
})
// api redocly
app.get('/redocly', (req, res) => {
res.sendFile(join(__dirname, lnpayapihtml));
})
// api swagger
app.use('/swagger', serve, setup(JSON.parse(readFileSync(join(__dirname, lnpayapijson)))));
// api ping
app.get('/api/ping', (req, res) => {
res.send({ "response": "pong" })
})
// api lightning address
app.get('/api/lightning-address/:address', async (req, res) => {
try {
const lightningAddress = req.params.address;
if (!validateLightningAddress(lightningAddress)) {
throw new Error(LIGHTNING_ADDRESS_INVALID);
}
const options = getRestOptionsGot(getLightningAddressUri(lightningAddress));
const response = await getGot(options);
if (200 === response.statusCode) {
const body = response.body;
res.send({
payRequest: body.tag === 'payRequest',
minSendable: body.minSendable,
maxSendable: body.maxSendable,
commentAllowed: body.commentAllowed,
callback: body.callback
});
} else {
throw new Error(JSON.stringify(response));
}
} catch (err) {
console.error(err);
res.status(400).send(err.message);
}
})
// api pay request
app.post('/api/pay-request', async (req, res) => {
try {
const callback = req.body.callback;
const amount = req.body.amount;
const comment = req.body.comment;
const options = getRestOptionsGot(callback + '?amount=' + amount + '&comment=' + comment);
const response = await getGot(options);
if (200 === response.statusCode) {
const body = response.body;
if ("ERROR" === body.status) {
throw new Error(JSON.stringify(response));
} else {
res.send({
payRequest: body.pr,
qrCode: await getQrCode(body.pr)
});
}
} else {
throw new Error(JSON.stringify(response));
}
} catch (err) {
console.error(err);
res.status(400).send(err.message);
}
})
// app home
let errorMessage = undefined;
app.get('/', (req, res) => {
res.render("index", { errorMessage: errorMessage });
errorMessage = undefined;
})
// app pay with lightning address
let payData = {};
app.get('/pay/address', (req, res) => {
payData = {};
res.render("pay-address", { payData: payData });
})
app.post('/pay-address-response', async function (req, res) {
try {
payData.address = req.body.lightningAddress;
if (req.body.button === 'submit') {
const options = getRestOptionsGot(url + '/api/lightning-address/' + payData.address);
const response = await getGot(options);
if (200 === response.statusCode) {
const body = response.body;
payData.addressResponse = body;
if (payData.addressResponse.payRequest) {
res.redirect("/pay/request");
return;
}
} else {
throw new Error(JSON.stringify(response));
}
}
} catch (err) {
console.error(err);
errorMessage = err.message;
}
res.redirect("/");
});
app.get('/pay/request', (req, res) => {
res.render("pay-request", { payData: payData });
})
app.post('/pay-request-response', async function (req, res) {
try {
payData.amount = req.body.amount;
payData.comment = req.body.comment;
if (req.body.button === 'submit') {
const options = getRestOptionsGot(url + '/api/pay-request', {
callback: payData.addressResponse.callback,
amount: payData.amount,
comment: encodeURIComponent(payData.comment)
});
const response = await postGot(options);
if (200 === response.statusCode) {
payData.payResponse = response.body;
res.redirect("/pay/invoice");
return;
} else {
throw new Error(JSON.stringify(response));
}
}
} catch (err) {
console.error(err);
errorMessage = err.message;
}
res.redirect("/");
});
app.get('/pay/invoice', (req, res) => {
res.render("pay-invoice", { payData: payData });
payData = {};
})