| 15 | const logger = createLogger('payments-service') |
| 16 | |
| 17 | export class PaymentsService implements IPaymentsService { |
| 18 | public constructor( |
| 19 | private readonly dbClient: DatabaseClient, |
| 20 | private readonly paymentsProcessor: IPaymentsProcessor, |
| 21 | private readonly userRepository: IUserRepository, |
| 22 | private readonly invoiceRepository: IInvoiceRepository, |
| 23 | private readonly eventRepository: IEventRepository, |
| 24 | private readonly settings: () => Settings, |
| 25 | ) {} |
| 26 | |
| 27 | public async getPendingInvoices(): Promise<Invoice[]> { |
| 28 | logger('get pending invoices') |
| 29 | try { |
| 30 | return await this.invoiceRepository.findPendingInvoices(0, 10) |
| 31 | } catch (error) { |
| 32 | logger.error('Unable to get pending invoices.', error) |
| 33 | |
| 34 | throw error |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | public async getInvoiceFromPaymentsProcessor(invoice: Invoice | string): Promise<Partial<Invoice>> { |
| 39 | try { |
| 40 | return await this.paymentsProcessor.getInvoice( |
| 41 | typeof invoice === 'string' || invoice?.verifyURL ? invoice : invoice.id, |
| 42 | ) |
| 43 | } catch (error) { |
| 44 | logger.error('Unable to get invoice from payments processor. Reason:', error) |
| 45 | |
| 46 | throw error |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | public async createInvoice(pubkey: Pubkey, amount: bigint, description: string): Promise<Invoice> { |
| 51 | logger('create invoice for %s for %s: %s', pubkey, amount.toString(), description) |
| 52 | const transaction = new Transaction(this.dbClient) |
| 53 | |
| 54 | try { |
| 55 | await transaction.begin() |
| 56 | |
| 57 | await this.userRepository.upsert({ pubkey }, transaction.transaction) |
| 58 | |
| 59 | const invoiceResponse = await this.paymentsProcessor.createInvoice({ |
| 60 | amount, |
| 61 | description, |
| 62 | requestId: pubkey, |
| 63 | }) |
| 64 | |
| 65 | const date = new Date() |
| 66 | |
| 67 | await this.invoiceRepository.upsert( |
| 68 | { |
| 69 | id: invoiceResponse.id, |
| 70 | pubkey, |
| 71 | bolt11: invoiceResponse.bolt11, |
| 72 | amountRequested: invoiceResponse.amountRequested, |
| 73 | description: invoiceResponse.description, |
| 74 | unit: invoiceResponse.unit, |
nothing calls this directly
no outgoing calls
no test coverage detected