Skip to content

Commit 7eddd37

Browse files
committed
Implement DELETE /invoice/:id. Resolves #12
1 parent d82b0b4 commit 7eddd37

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,19 @@ $ curl $CHARGE/invoice/OYwwaOQAPMFvg039gj_Rb
191191
{"id":"OYwwaOQAPMFvg039gj_Rb","msatoshi":"3738106","quoted_currency":"EUR","quoted_amount":"0.5","status":"unpaid",...}
192192
```
193193

194+
### `DELETE /invoice/:id`
195+
196+
Delete the specified invoice.
197+
198+
*Body parameters:* `status`
199+
200+
The current status of the invoice needs to be specified in the request body.
201+
202+
```bash
203+
$ curl -X DELETE $CHARGE/invoice/OYwwaOQAPMFvg039gj_Rb -d status=unpaid
204+
204 No Content
205+
```
206+
194207
### `GET /invoice/:id/wait?timeout=[sec]`
195208

196209
Long-polling invoice payment notification.

src/invoicing.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const debug = require('debug')('lightning-charge')
66
const maxWait = +process.env.MAX_WAIT || 600
77

88
module.exports = (app, payListen, model, auth, lnconf) => {
9-
const { newInvoice, fetchInvoice, listInvoices, delExpired } = model
9+
const { newInvoice, fetchInvoice, listInvoices, delInvoice, delExpired } = model
1010

1111
app.on('listening', server => server.timeout = maxWait*1000 + 500)
1212

@@ -38,6 +38,11 @@ module.exports = (app, payListen, model, auth, lnconf) => {
3838
// @TODO remove listener on client disconnect
3939
}))
4040

41+
app.delete('/invoice/:invoice', auth, wrap(async (req, res) => {
42+
await delInvoice(req.params.invoice, req.body.status)
43+
res.sendStatus(204)
44+
}))
45+
4146
// Enable automatic cleanup for expired invoices if enabled on c-lightning,
4247
// using the same configurations.
4348

src/model.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ module.exports = (db, ln) => {
4545
const fetchInvoice = id =>
4646
db('invoice').where({ id }).first().then(r => r && format(r))
4747

48+
const delInvoice = async (id, status) => {
49+
await ln.delinvoice(id, status)
50+
await db('invoice').where({ id }).del()
51+
}
52+
4853
const markPaid = (id, pay_index, paid_at, msatoshi_received) =>
4954
db('invoice').where({ id, pay_index: null })
5055
.update({ pay_index, paid_at, msatoshi_received })
@@ -79,7 +84,7 @@ module.exports = (db, ln) => {
7984
!err ? { requested_at: now(), success: true, resp_code: res.status }
8085
: { requested_at: now(), success: false, resp_error: err })
8186

82-
return { newInvoice, listInvoices, fetchInvoice
87+
return { newInvoice, listInvoices, fetchInvoice, delInvoice
8388
, getLastPaid, markPaid, delExpired
8489
, addHook, getHooks, logHook }
8590
}

test/invoice.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,30 @@ describe('Invoice API', function() {
115115
})
116116
})
117117

118+
describe('DELETE /invoice/:invoice', () => {
119+
it('deletes an invoice by its id and status', async () => {
120+
const inv = await mkInvoice()
121+
charge.del(`/invoice/${ inv.id }`)
122+
.send({ status: 'unpaid' })
123+
.expect(204)
124+
})
125+
126+
it("won't delete the invoice if the status mismatches", async () => {
127+
const inv = await mkInvoice()
128+
129+
await charge.del(`/invoice/${ inv.id }`)
130+
.send({ status: 'paid' })
131+
.expect(400)
132+
133+
await lnBob.pay(inv.payreq, 1000)
134+
135+
await charge.del(`/invoice/${ inv.id }`)
136+
.send({ status: 'paid' })
137+
.expect(204)
138+
})
139+
})
140+
141+
118142
describe('GET /invoice/:invoice/wait', function() {
119143
this.slow(500)
120144

0 commit comments

Comments
 (0)