| 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, |
| 75 | status: invoiceResponse.status, |
| 76 | expiresAt: invoiceResponse.expiresAt, |
| 77 | updatedAt: date, |
| 78 | createdAt: date, |
| 79 | verifyURL: invoiceResponse.verifyURL, |
| 80 | }, |
| 81 | transaction.transaction, |
| 82 | ) |
| 83 | |
| 84 | await transaction.commit() |
| 85 | |
| 86 | return { |
| 87 | id: invoiceResponse.id, |
| 88 | pubkey, |
| 89 | bolt11: invoiceResponse.bolt11, |
| 90 | amountRequested: invoiceResponse.amountRequested, |
| 91 | unit: invoiceResponse.unit, |
| 92 | status: invoiceResponse.status, |
| 93 | description, |
| 94 | expiresAt: invoiceResponse.expiresAt, |
| 95 | updatedAt: date, |
| 96 | createdAt: invoiceResponse.createdAt, |
| 97 | verifyURL: invoiceResponse.verifyURL, |
| 98 | } |
| 99 | } catch (error) { |
| 100 | await transaction.rollback() |
| 101 | logger.error('Unable to create invoice:', error) |
| 102 | |
| 103 | throw error |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | public async updateInvoice(invoice: Partial<Invoice>): Promise<void> { |