|
| 1 | +import { Request, Response } from 'express' |
| 2 | + |
| 3 | +import { createLogger } from '../../factories/logger-factory' |
| 4 | +import { IController } from '../../@types/controllers' |
| 5 | +import { IInvoiceRepository } from '../../@types/repositories' |
| 6 | +import { InvoiceStatus } from '../../@types/invoice' |
| 7 | +import { IPaymentsService } from '../../@types/services' |
| 8 | + |
| 9 | +const debug = createLogger('lnbits-callback-controller') |
| 10 | + |
| 11 | +export class LNbitsCallbackController implements IController { |
| 12 | + public constructor( |
| 13 | + private readonly paymentsService: IPaymentsService, |
| 14 | + private readonly invoiceRepository: IInvoiceRepository |
| 15 | + ) { } |
| 16 | + |
| 17 | + // TODO: Validate |
| 18 | + public async handleRequest( |
| 19 | + request: Request, |
| 20 | + response: Response, |
| 21 | + ) { |
| 22 | + debug('request headers: %o', request.headers) |
| 23 | + debug('request body: %o', request.body) |
| 24 | + |
| 25 | + const body = request.body |
| 26 | + if (!body || typeof body !== 'object' || typeof body.payment_hash !== 'string' || body.payment_hash.length !== 64) { |
| 27 | + response |
| 28 | + .status(400) |
| 29 | + .setHeader('content-type', 'text/plain; charset=utf8') |
| 30 | + .send('Malformed body') |
| 31 | + return |
| 32 | + } |
| 33 | + |
| 34 | + const invoice = await this.paymentsService.getInvoiceFromPaymentsProcessor(body.payment_hash) |
| 35 | + const storedInvoice = await this.invoiceRepository.findById(body.payment_hash) |
| 36 | + |
| 37 | + if (!storedInvoice) { |
| 38 | + response |
| 39 | + .status(404) |
| 40 | + .setHeader('content-type', 'text/plain; charset=utf8') |
| 41 | + .send('No such invoice') |
| 42 | + return |
| 43 | + } |
| 44 | + |
| 45 | + try { |
| 46 | + await this.paymentsService.updateInvoice(invoice) |
| 47 | + } catch (error) { |
| 48 | + console.error(`Unable to persist invoice ${invoice.id}`, error) |
| 49 | + |
| 50 | + throw error |
| 51 | + } |
| 52 | + |
| 53 | + if ( |
| 54 | + invoice.status !== InvoiceStatus.COMPLETED |
| 55 | + && !invoice.confirmedAt |
| 56 | + ) { |
| 57 | + response |
| 58 | + .status(200) |
| 59 | + .send() |
| 60 | + |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + if (storedInvoice.status === InvoiceStatus.COMPLETED) { |
| 65 | + response |
| 66 | + .status(409) |
| 67 | + .setHeader('content-type', 'text/plain; charset=utf8') |
| 68 | + .send('Invoice is already marked paid') |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + invoice.amountPaid = invoice.amountRequested |
| 73 | + |
| 74 | + try { |
| 75 | + await this.paymentsService.confirmInvoice(invoice) |
| 76 | + await this.paymentsService.sendInvoiceUpdateNotification(invoice) |
| 77 | + } catch (error) { |
| 78 | + console.error(`Unable to confirm invoice ${invoice.id}`, error) |
| 79 | + |
| 80 | + throw error |
| 81 | + } |
| 82 | + |
| 83 | + response |
| 84 | + .status(200) |
| 85 | + .setHeader('content-type', 'text/plain; charset=utf8') |
| 86 | + .send('OK') |
| 87 | + } |
| 88 | +} |
0 commit comments