MCPcopy Create free account
hub / github.com/cameri/nostream / confirmInvoice

Method confirmInvoice

src/services/payments-service.ts:130–185  ·  view source on GitHub ↗
(invoice: Invoice)

Source from the content-addressed store, hash-verified

128 }
129
130 public async confirmInvoice(invoice: Invoice): Promise<void> {
131 logger('confirm invoice %s: %O', invoice.id, invoice)
132
133 const transaction = new Transaction(this.dbClient)
134
135 try {
136 if (!invoice.confirmedAt) {
137 throw new Error('Invoice confirmation date is not set')
138 }
139 if (invoice.status !== InvoiceStatus.COMPLETED) {
140 throw new Error(`Invoice is not complete: ${invoice.status}`)
141 }
142
143 if (typeof invoice.amountPaid !== 'bigint') {
144 throw new Error(`Invoice paid amount is not set: ${invoice.amountPaid}`)
145 }
146
147 await transaction.begin()
148
149 await this.invoiceRepository.confirmInvoice(
150 invoice.id,
151 invoice.amountPaid,
152 invoice.confirmedAt,
153 transaction.transaction,
154 )
155
156 const currentSettings = this.settings()
157
158 let amountPaidMsat = invoice.amountPaid
159
160 if (invoice.unit === InvoiceUnit.SATS) {
161 amountPaidMsat *= 1000n
162 } else if (invoice.unit === InvoiceUnit.BTC) {
163 amountPaidMsat *= 1000n * 100000000n
164 }
165
166 const isApplicableFee = (feeSchedule: FeeSchedule) =>
167 feeSchedule.enabled && !feeSchedule.whitelists?.pubkeys?.includes(invoice.pubkey)
168 const admissionFeeSchedules = currentSettings.payments?.feeSchedules?.admission ?? []
169 const admissionFeeAmount = admissionFeeSchedules.reduce((sum, feeSchedule) => {
170 return sum + (isApplicableFee(feeSchedule) ? BigInt(feeSchedule.amount) : 0n)
171 }, 0n)
172
173 if (admissionFeeAmount > 0n && amountPaidMsat >= admissionFeeAmount) {
174 const date = new Date()
175 await this.userRepository.admitUser(invoice.pubkey, date, transaction.transaction)
176 }
177
178 await transaction.commit()
179 } catch (error) {
180 logger.error('Unable to confirm invoice. Reason:', error)
181 await transaction.rollback()
182
183 throw error
184 }
185 }
186
187 public async sendInvoiceUpdateNotification(invoice: Invoice): Promise<void> {

Callers

nothing calls this directly

Calls 5

beginMethod · 0.95
commitMethod · 0.95
rollbackMethod · 0.95
confirmInvoiceMethod · 0.65
admitUserMethod · 0.65

Tested by

no test coverage detected