@@ -12,7 +12,12 @@ function getDefaultHandler() {
1212 return defaultHandlerPromise ;
1313}
1414
15- // Custom webhook handler with proper sync
15+ // Helper to sleep for a given number of milliseconds
16+ function sleep ( ms : number ) : Promise < void > {
17+ return new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
18+ }
19+
20+ // Custom webhook handler with proper sync and retry logic
1621async function handleWebhookWithSync ( request : NextRequest ) : Promise < Response > {
1722 const body = await request . json ( ) ;
1823
@@ -28,40 +33,60 @@ async function handleWebhookWithSync(request: NextRequest): Promise<Response> {
2833
2934 const providedSecret = request . headers . get ( WEBHOOK_SECRET_HEADER ) ;
3035 if ( ! providedSecret || providedSecret !== expectedSecret ) {
31- console . error ( '[webhook] Unauthorized webhook request' ) ;
36+ console . error ( '[webhook] Unauthorized webhook request. Expected:' , expectedSecret . substring ( 0 , 8 ) + '..., Got:' , providedSecret ?. substring ( 0 , 8 ) + '... ') ;
3237 return new Response ( JSON . stringify ( { error : 'Unauthorized' } ) , {
3338 status : 401 ,
3439 headers : { 'Content-Type' : 'application/json' } ,
3540 } ) ;
3641 }
3742
3843 if ( body . event !== 'incoming-payment' ) {
39- console . error ( '[webhook] Unknown event type:' , body . event ) ;
44+ console . log ( '[webhook] Unknown event type:' , body . event ) ;
4045 return new Response ( 'OK' , { status : 200 } ) ;
4146 }
4247
43- console . log ( '[webhook] Processing incoming-payment event with node sync' ) ;
48+ console . log ( '[webhook] Processing incoming-payment event with node sync and retry ' ) ;
4449
4550 try {
4651 // Dynamically import to avoid bundling issues
4752 const { createMoneyDevKitNode, createMoneyDevKitClient, markPaymentReceived } = await import ( "@moneydevkit/core" ) ;
4853
49- const node = createMoneyDevKitNode ( ) ;
5054 const client = createMoneyDevKitClient ( ) ;
5155
52- // CRITICAL: Sync wallets BEFORE checking for payments
53- // This ensures the node has the latest blockchain state
54- console . log ( '[webhook] Syncing wallets...' ) ;
55- node . syncWallets ( ) ;
56- console . log ( '[webhook] Wallet sync complete' ) ;
56+ // Retry logic: try up to 5 times with increasing delays
57+ const maxRetries = 5 ;
58+ const delays = [ 1000 , 2000 , 3000 , 5000 , 8000 ] ; // Total: up to 19 seconds of waiting
59+
60+ let payments : Array < { paymentHash : string ; amount : number } > = [ ] ;
61+
62+ for ( let attempt = 0 ; attempt < maxRetries ; attempt ++ ) {
63+ // Create a fresh node instance for each attempt
64+ const node = createMoneyDevKitNode ( ) ;
5765
58- // Now receive payments with the synced state
59- console . log ( '[webhook] Checking for received payments...' ) ;
60- const payments = node . receivePayments ( ) ;
61- console . log ( `[webhook] Found ${ payments . length } payment(s)` ) ;
66+ // CRITICAL: Sync wallets BEFORE checking for payments
67+ console . log ( `[webhook] Attempt ${ attempt + 1 } /${ maxRetries } : Syncing wallets...` ) ;
68+ node . syncWallets ( ) ;
69+ console . log ( `[webhook] Attempt ${ attempt + 1 } /${ maxRetries } : Wallet sync complete` ) ;
70+
71+ // Now receive payments with the synced state
72+ console . log ( `[webhook] Attempt ${ attempt + 1 } /${ maxRetries } : Checking for received payments...` ) ;
73+ payments = node . receivePayments ( ) ;
74+ console . log ( `[webhook] Attempt ${ attempt + 1 } /${ maxRetries } : Found ${ payments . length } payment(s)` ) ;
75+
76+ if ( payments . length > 0 ) {
77+ break ; // Found payments, exit retry loop
78+ }
79+
80+ // If no payments found and we have more retries, wait before trying again
81+ if ( attempt < maxRetries - 1 ) {
82+ const delayMs = delays [ attempt ] ;
83+ console . log ( `[webhook] No payments found, waiting ${ delayMs } ms before retry...` ) ;
84+ await sleep ( delayMs ) ;
85+ }
86+ }
6287
6388 if ( payments . length === 0 ) {
64- console . log ( '[webhook] No payments to process ' ) ;
89+ console . log ( '[webhook] No payments found after all retries ' ) ;
6590 return new Response ( 'OK' , { status : 200 } ) ;
6691 }
6792
0 commit comments