@@ -3,7 +3,7 @@ import { datePivot } from '@/lib/time'
3
3
import { PAID_ACTION_PAYMENT_METHODS , USER_ID } from '@/lib/constants'
4
4
import { createHmac } from '@/api/resolvers/wallet'
5
5
import { Prisma } from '@prisma/client'
6
- import { createWrappedInvoice , createInvoice as createUserInvoice } from '@/wallets/server'
6
+ import { createWrappedInvoice , createUserInvoice } from '@/wallets/server'
7
7
import { assertBelowMaxPendingInvoices , assertBelowMaxPendingDirectPayments } from './lib/assert'
8
8
9
9
import * as ITEM_CREATE from './itemCreate'
@@ -264,42 +264,51 @@ async function performDirectAction (actionType, args, incomingContext) {
264
264
throw new NonInvoiceablePeerError ( )
265
265
}
266
266
267
- let invoiceObject
268
-
269
267
try {
270
268
await assertBelowMaxPendingDirectPayments ( userId , incomingContext )
271
269
272
270
const description = actionDescription ?? await paidActions [ actionType ] . describe ( args , incomingContext )
273
- invoiceObject = await createUserInvoice ( userId , {
271
+
272
+ for await ( const { invoice, logger, wallet } of createUserInvoice ( userId , {
274
273
msats : cost ,
275
274
description,
276
275
expiry : INVOICE_EXPIRE_SECS
277
- } , { models, lnd } )
278
- } catch ( e ) {
279
- console . error ( 'failed to create outside invoice' , e )
280
- throw new NonInvoiceablePeerError ( )
281
- }
282
-
283
- const { invoice, wallet } = invoiceObject
284
- const hash = parsePaymentRequest ( { request : invoice } ) . id
276
+ } , { models, lnd } ) ) {
277
+ let hash
278
+ try {
279
+ hash = parsePaymentRequest ( { request : invoice } ) . id
280
+ } catch ( e ) {
281
+ console . error ( 'failed to parse invoice' , e )
282
+ logger ?. error ( 'failed to parse invoice: ' + e . message , { bolt11 : invoice } )
283
+ continue
284
+ }
285
285
286
- const payment = await models . directPayment . create ( {
287
- data : {
288
- comment,
289
- lud18Data,
290
- desc : noteStr ,
291
- bolt11 : invoice ,
292
- msats : cost ,
293
- hash,
294
- walletId : wallet . id ,
295
- receiverId : userId
286
+ try {
287
+ return {
288
+ invoice : await models . directPayment . create ( {
289
+ data : {
290
+ comment,
291
+ lud18Data,
292
+ desc : noteStr ,
293
+ bolt11 : invoice ,
294
+ msats : cost ,
295
+ hash,
296
+ walletId : wallet . id ,
297
+ receiverId : userId
298
+ }
299
+ } ) ,
300
+ paymentMethod : 'DIRECT'
301
+ }
302
+ } catch ( e ) {
303
+ console . error ( 'failed to create direct payment' , e )
304
+ logger ?. error ( 'failed to create direct payment: ' + e . message , { bolt11 : invoice } )
305
+ }
296
306
}
297
- } )
298
-
299
- return {
300
- invoice : payment ,
301
- paymentMethod : 'DIRECT'
307
+ } catch ( e ) {
308
+ console . error ( 'failed to create user invoice' , e )
302
309
}
310
+
311
+ throw new NonInvoiceablePeerError ( )
303
312
}
304
313
305
314
export async function retryPaidAction ( actionType , args , incomingContext ) {
@@ -419,7 +428,7 @@ async function createSNInvoice (actionType, args, context) {
419
428
}
420
429
421
430
async function createDbInvoice ( actionType , args , context ) {
422
- const { me, models, tx, cost, optimistic, actionId, invoiceArgs, predecessorId } = context
431
+ const { me, models, tx, cost, optimistic, actionId, invoiceArgs, paymentAttempt , predecessorId } = context
423
432
const { bolt11, wrappedBolt11, preimage, wallet, maxFee } = invoiceArgs
424
433
425
434
const db = tx ?? models
@@ -445,6 +454,7 @@ async function createDbInvoice (actionType, args, context) {
445
454
actionArgs : args ,
446
455
expiresAt,
447
456
actionId,
457
+ paymentAttempt,
448
458
predecessorId
449
459
}
450
460
0 commit comments