-
Notifications
You must be signed in to change notification settings - Fork 86
/
checkout.js
51 lines (38 loc) · 1.89 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
import fs from 'fs'
import path from 'path'
import express from 'express'
import qrcode from 'qrcode'
import moveDec from 'move-decimal-point'
import wrap from './lib/promise-wrap'
const rpath = name => path.join(__dirname, name)
module.exports = (app, payListen) => {
app.set('url', process.env.URL || '/')
app.set('static_url', process.env.STATIC_URL || app.settings.url + 'static/')
app.set('view engine', 'pug')
app.set('views', rpath('../views'))
app.locals.formatMsat = msat => moveDec(msat, -8) + ' mBTC'
fs.existsSync(rpath('www')) // comes pre-built in dist/www
? app.use('/static', express.static(rpath('www')))
: app.use('/static', require('stylus').middleware({ src: rpath('../www'), serve: true }))
.use('/static', express.static(rpath('../www')))
app.get('/checkout/:invoice', wrap(async (req, res) => {
const opt = req.invoice.metadata && req.invoice.metadata.checkout || {}
if (req.invoice.status == 'paid' && opt.redirect_url)
return res.redirect(opt.redirect_url)
if (req.invoice.status == 'unpaid')
res.locals.qr = await qrcode.toDataURL(`lightning:${req.invoice.payreq}`.toUpperCase(), { margin: 1 })
res.render('checkout', req.invoice)
}))
// like /invoice/:invoice/wait, but user-accessible, doesn't reveal the full invoice fields,
// and with a fixed timeout.
app.get('/checkout/:invoice/wait', wrap(async (req, res) => {
if (req.invoice.status == 'paid') return res.sendStatus(204)
if (req.invoice.status == 'expired') return res.sendStatus(410)
const paid = await payListen.register(req.invoice.id, 60000)
, expired = !paid && req.invoice.expires_at <= Date.now()/1000
res.sendStatus(paid ? 204 : expired ? 410 : 402)
}))
app.get('/checkout/:invoice/qr.png', wrap(async (req, res) => {
qrcode.toFileStream(res.type('png'), `lightning:${req.invoice.payreq}`.toUpperCase())
}))
}