-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement nodeless payments processor (#305)
* chore: hide powered by zebedee if payment processor is not * chore: add nodeless as payments processor to settings * fix: bad content type on zebedee callback req handler * chore(release): 1.23.0 [skip ci] # [1.23.0](v1.22.6...v1.23.0) (2023-05-12) ### Bug Fixes * add SECRET as env variable ([#298](#298)) ([58a1254](58a1254)) * invoice auto marked as paid ([be6d6f1](be6d6f1)) * issues with invoices ([#271](#271)) ([e1561e7](e1561e7)) ### Features * add LNURL processor ([#202](#202)) ([f237400](f237400)) * allow lightning zap receipts on paid relays ([#303](#303)) ([14bc96f](14bc96f)) * feat: implement nodeless payments processor * docs: add accepting payments section * chore: validate nodeless webhook secret * chore: hide powered-by-zebedee for non-zebedee processors --------- Co-authored-by: semantic-release-bot <semantic-release-bot@martynus.net>
- Loading branch information
1 parent
62c1dbe
commit 52aac39
Showing
27 changed files
with
395 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { always, applySpec, ifElse, is, path, prop, propEq, propSatisfies } from 'ramda' | ||
import { Request, Response } from 'express' | ||
|
||
import { Invoice, InvoiceStatus } from '../../@types/invoice' | ||
import { createLogger } from '../../factories/logger-factory' | ||
import { fromNodelessInvoice } from '../../utils/transform' | ||
import { IController } from '../../@types/controllers' | ||
import { IPaymentsService } from '../../@types/services' | ||
|
||
const debug = createLogger('nodeless-callback-controller') | ||
|
||
export class NodelessCallbackController implements IController { | ||
public constructor( | ||
private readonly paymentsService: IPaymentsService, | ||
) {} | ||
|
||
// TODO: Validate | ||
public async handleRequest( | ||
request: Request, | ||
response: Response, | ||
) { | ||
debug('callback request headers: %o', request.headers) | ||
debug('callback request body: %O', request.body) | ||
|
||
const nodelessInvoice = applySpec({ | ||
id: prop('uuid'), | ||
status: prop('status'), | ||
satsAmount: prop('amount'), | ||
metadata: prop('metadata'), | ||
paidAt: ifElse( | ||
propEq('status', 'paid'), | ||
always(new Date().toISOString()), | ||
always(null), | ||
), | ||
createdAt: ifElse( | ||
propSatisfies(is(String), 'createdAt'), | ||
prop('createdAt'), | ||
path(['metadata', 'createdAt']), | ||
), | ||
})(request.body) | ||
|
||
debug('nodeless invoice: %O', nodelessInvoice) | ||
|
||
const invoice = fromNodelessInvoice(nodelessInvoice) | ||
|
||
debug('invoice: %O', invoice) | ||
|
||
let updatedInvoice: Invoice | ||
try { | ||
updatedInvoice = await this.paymentsService.updateInvoiceStatus(invoice) | ||
debug('updated invoice: %O', updatedInvoice) | ||
} catch (error) { | ||
console.error(`Unable to persist invoice ${invoice.id}`, error) | ||
|
||
throw error | ||
} | ||
|
||
if ( | ||
updatedInvoice.status !== InvoiceStatus.COMPLETED | ||
&& !updatedInvoice.confirmedAt | ||
) { | ||
response | ||
.status(200) | ||
.send() | ||
|
||
return | ||
} | ||
|
||
invoice.amountPaid = invoice.amountRequested | ||
updatedInvoice.amountPaid = invoice.amountRequested | ||
|
||
try { | ||
await this.paymentsService.confirmInvoice(invoice) | ||
await this.paymentsService.sendInvoiceUpdateNotification(updatedInvoice) | ||
} catch (error) { | ||
console.error(`Unable to confirm invoice ${invoice.id}`, error) | ||
|
||
throw error | ||
} | ||
|
||
response | ||
.status(200) | ||
.setHeader('content-type', 'application/json; charset=utf8') | ||
.send('{"status":"ok"}') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { createPaymentsService } from './payments-service-factory' | ||
import { IController } from '../@types/controllers' | ||
import { NodelessCallbackController } from '../controllers/callbacks/nodeless-callback-controller' | ||
|
||
export const createNodelessCallbackController = (): IController => new NodelessCallbackController( | ||
createPaymentsService(), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.