-
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 opennode payments processor (#315)
- Loading branch information
Showing
39 changed files
with
646 additions
and
405 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { Request, Response } from 'express' | ||
|
||
import { Invoice, InvoiceStatus } from '../../@types/invoice' | ||
import { createLogger } from '../../factories/logger-factory' | ||
import { fromOpenNodeInvoice } from '../../utils/transform' | ||
import { IController } from '../../@types/controllers' | ||
import { IPaymentsService } from '../../@types/services' | ||
|
||
const debug = createLogger('opennode-callback-controller') | ||
|
||
export class OpenNodeCallbackController implements IController { | ||
public constructor( | ||
private readonly paymentsService: IPaymentsService, | ||
) {} | ||
|
||
// TODO: Validate | ||
public async handleRequest( | ||
request: Request, | ||
response: Response, | ||
) { | ||
debug('request headers: %o', request.headers) | ||
debug('request body: %O', request.body) | ||
|
||
const invoice = fromOpenNodeInvoice(request.body) | ||
|
||
debug('invoice', invoice) | ||
|
||
let updatedInvoice: Invoice | ||
try { | ||
updatedInvoice = await this.paymentsService.updateInvoiceStatus(invoice) | ||
} 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({ | ||
id: invoice.id, | ||
amountPaid: updatedInvoice.amountRequested, | ||
confirmedAt: updatedInvoice.confirmedAt, | ||
}) | ||
await this.paymentsService.sendInvoiceUpdateNotification(updatedInvoice) | ||
} catch (error) { | ||
console.error(`Unable to confirm invoice ${invoice.id}`, error) | ||
|
||
throw error | ||
} | ||
|
||
response | ||
.status(200) | ||
.setHeader('content-type', 'text/plain; charset=utf8') | ||
.send('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,34 @@ | ||
import { path, pathEq } from 'ramda' | ||
import { Request, Response } from 'express' | ||
import { readFileSync } from 'fs' | ||
|
||
import { createSettings } from '../../factories/settings-factory' | ||
import { FeeSchedule } from '../../@types/settings' | ||
import { IController } from '../../@types/controllers' | ||
|
||
let pageCache: string | ||
|
||
export class GetInvoiceController implements IController { | ||
public async handleRequest( | ||
_req: Request, | ||
res: Response, | ||
): Promise<void> { | ||
const settings = createSettings() | ||
|
||
if (pathEq(['payments', 'enabled'], true, settings) | ||
&& pathEq(['payments', 'feeSchedules', 'admission', '0', 'enabled'], true, settings)) { | ||
if (!pageCache) { | ||
const name = path<string>(['info', 'name'])(settings) | ||
const feeSchedule = path<FeeSchedule>(['payments', 'feeSchedules', 'admission', '0'], settings) | ||
pageCache = readFileSync('./resources/index.html', 'utf8') | ||
.replaceAll('{{name}}', name) | ||
.replaceAll('{{processor}}', settings.payments.processor) | ||
.replaceAll('{{amount}}', (BigInt(feeSchedule.amount) / 1000n).toString()) | ||
} | ||
|
||
res.status(200).setHeader('content-type', 'text/html; charset=utf8').send(pageCache) | ||
} else { | ||
res.status(404).send() | ||
} | ||
} | ||
} |
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,3 @@ | ||
import { GetInvoiceController } from '../../controllers/invoices/get-invoice-controller' | ||
|
||
export const createGetInvoiceController = () => new GetInvoiceController() |
11 changes: 11 additions & 0 deletions
11
src/factories/controllers/get-invoice-status-controller-factory.ts
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,11 @@ | ||
import { GetInvoiceStatusController } from '../../controllers/invoices/get-invoice-status-controller' | ||
import { getReadReplicaDbClient } from '../../database/client' | ||
import { InvoiceRepository } from '../../repositories/invoice-repository' | ||
|
||
export const createGetInvoiceStatusController = () => { | ||
const rrDbClient = getReadReplicaDbClient() | ||
|
||
const invoiceRepository = new InvoiceRepository(rrDbClient) | ||
|
||
return new GetInvoiceStatusController(invoiceRepository) | ||
} |
Oops, something went wrong.