@@ -331,12 +331,15 @@ where
331331
332332 // Check if zap receipt already exists
333333 let mut published = false ;
334- if zap. zap_event . is_some ( ) {
334+ if let Some ( zap_receipt ) = & zap. zap_event {
335335 debug ! (
336336 "Zap receipt already exists for payment hash {}" ,
337337 payment_hash
338338 ) ;
339- return Ok ( Json ( PublishZapReceiptResponse { published } ) ) ;
339+ return Ok ( Json ( PublishZapReceiptResponse {
340+ published,
341+ zap_receipt : zap_receipt. clone ( ) ,
342+ } ) ) ;
340343 }
341344
342345 // Parse the zap request to get relay info
@@ -404,13 +407,6 @@ where
404407 }
405408 } ;
406409
407- // The nostr keys are not really needed here, but we use them to create the client
408- let publish_nostr_keys = match & state. nostr_keys {
409- Some ( keys) => keys. clone ( ) ,
410- None => Keys :: generate ( ) ,
411- } ;
412- let nostr_client = nostr_sdk:: Client :: new ( publish_nostr_keys) ;
413-
414410 let relays = zap_request
415411 . tags
416412 . iter ( )
@@ -424,32 +420,39 @@ where
424420 . flatten ( )
425421 . collect :: < Vec < _ > > ( ) ;
426422
427- if relays. is_empty ( ) {
428- debug ! (
429- "No relays to publish zap receipt for payment hash {}" ,
430- payment_hash
431- ) ;
432- return Ok ( Json ( PublishZapReceiptResponse { published } ) ) ;
433- }
434-
435- for r in & relays {
436- if let Err ( e ) = nostr_client . add_relay ( r ) . await {
437- warn ! ( "Failed to add relay {r}: {e}" ) ;
423+ if ! relays. is_empty ( ) {
424+ // The nostr keys are not really needed here, but we use them to create the client
425+ let publish_nostr_keys = match & state . nostr_keys {
426+ Some ( keys ) => keys . clone ( ) ,
427+ None => Keys :: generate ( ) ,
428+ } ;
429+ let nostr_client = nostr_sdk :: Client :: new ( publish_nostr_keys ) ;
430+ for r in & relays {
431+ if let Err ( e ) = nostr_client . add_relay ( r ) . await {
432+ warn ! ( "Failed to add relay {r}: {e}" ) ;
433+ }
438434 }
439- }
440435
441- nostr_client. connect ( ) . await ;
436+ nostr_client. connect ( ) . await ;
442437
443- if let Err ( e) = nostr_client. send_event ( & zap_receipt) . await {
444- error ! ( "Failed to publish zap receipt to relays: {e}" ) ;
445- } else {
446- debug ! ( "Published zap receipt to {} relays" , relays. len( ) ) ;
447- published = true ;
448- }
438+ if let Err ( e) = nostr_client. send_event ( & zap_receipt) . await {
439+ error ! ( "Failed to publish zap receipt to relays: {e}" ) ;
440+ } else {
441+ debug ! ( "Published zap receipt to {} relays" , relays. len( ) ) ;
442+ published = !relays . is_empty ( ) ;
443+ }
449444
450- nostr_client. disconnect ( ) . await ;
445+ nostr_client. disconnect ( ) . await ;
446+ }
451447
452- zap. zap_event = Some ( payload. zap_receipt ) ;
448+ let zap_receipt_json = zap_receipt. try_as_json ( ) . map_err ( |e| {
449+ error ! ( "failed to serialize zap receipt: {}" , e) ;
450+ (
451+ StatusCode :: INTERNAL_SERVER_ERROR ,
452+ Json ( json ! ( { "error" : "internal server error" } ) ) ,
453+ )
454+ } ) ?;
455+ zap. zap_event = Some ( zap_receipt_json. clone ( ) ) ;
453456 zap. updated_at = now_millis ( ) ;
454457 state. db . upsert_zap ( & zap) . await . map_err ( |e| {
455458 error ! ( "failed to save zap receipt: {}" , e) ;
@@ -459,7 +462,10 @@ where
459462 )
460463 } ) ?;
461464
462- Ok ( Json ( PublishZapReceiptResponse { published } ) )
465+ Ok ( Json ( PublishZapReceiptResponse {
466+ published,
467+ zap_receipt : zap_receipt_json,
468+ } ) )
463469 }
464470
465471 pub async fn handle_lnurl_pay (
0 commit comments