Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use 302 instead of 402, and application/json instead of bolt11 #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/paypercall.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = opt => {
.digest().toString('base64').replace(/\W+/g, '')

const makeToken = (req, invid) => [ invid, hmac(req, invid) ].join('.')
const parseToken = (req, t=req.get('X-Token').split('.')) => hmac(req, t[0]) === t[1] && t[0]
const parseToken = (req, t=(req.get('X-Token') || req.query['X-Token']).split('.')) => hmac(req, t[0]) === t[1] && t[0]

// Database
const markSpent = inv => db('spent').insert({ status: 'processing', invid: inv.id, paid_at: inv.paid_at }).catch(err => false)
Expand All @@ -36,7 +36,7 @@ module.exports = opt => {

// Middleware
return (amount, currency=defCurrency) => pwrap(async (req, res, next) => {
const invid = req.get('X-Token') && parseToken(req)
const invid = (req.get('X-Token') || req.query['X-Token']) && parseToken(req)
, inv = invid && await charge.fetch(invid)
, paid = inv && inv.status === 'paid' && inv.paid_at > now() - accessExp

Expand All @@ -54,10 +54,12 @@ module.exports = opt => {
, expiry: invoiceExp
})

res.status(402) // Payment Required
.type('application/vnd.lightning.bolt11')
.set('X-Token', makeToken(req, inv.id))
.send(inv.payreq)
const xToken = makeToken(req, inv.id)
const response = {"invoice": inv.payreq, "X-Token-Header": xToken}
res.status(302) // (Found; Payment required for this route. Note: 402/Lightning MIME doesn't load in Chrome.)
.type('application/json')
.set('X-Token', xToken)
.send(response)
}
})
}