@@ -4,6 +4,7 @@ use crate::{db::Order, lightning::is_valid_invoice};
4
4
use anyhow:: Result ;
5
5
use lnurl:: lightning_address:: LightningAddress ;
6
6
use mostro_core:: message:: { Action , Message , Payload } ;
7
+ use mostro_core:: order:: Status ;
7
8
use nostr_sdk:: prelude:: * ;
8
9
use std:: str:: FromStr ;
9
10
use uuid:: Uuid ;
@@ -16,32 +17,41 @@ pub async fn execute_add_invoice(
16
17
client : & Client ,
17
18
) -> Result < ( ) > {
18
19
let pool = connect ( ) . await ?;
19
- let order = Order :: get_by_id ( & pool, & order_id. to_string ( ) )
20
+ let mut order = Order :: get_by_id ( & pool, & order_id. to_string ( ) )
20
21
. await
21
22
. unwrap ( ) ;
22
- let trade_keys = order. trade_keys . unwrap ( ) ;
23
+ let trade_keys = order. trade_keys . clone ( ) . unwrap ( ) ;
23
24
let trade_keys = Keys :: parse ( trade_keys) . unwrap ( ) ;
24
25
25
26
println ! (
26
27
"Sending a lightning invoice {} to mostro pubId {}" ,
27
28
order_id, mostro_key
28
29
) ;
29
- let mut payload = None ;
30
+ // let mut payload = None;
30
31
// Check invoice string
31
32
let ln_addr = LightningAddress :: from_str ( invoice) ;
32
- if ln_addr. is_ok ( ) {
33
- payload = Some ( Payload :: PaymentRequest ( None , invoice. to_string ( ) , None ) ) ;
33
+ let payload = if ln_addr. is_ok ( ) {
34
+ Some ( Payload :: PaymentRequest ( None , invoice. to_string ( ) , None ) )
34
35
} else {
35
36
match is_valid_invoice ( invoice) {
36
- Ok ( i) => payload = Some ( Payload :: PaymentRequest ( None , i. to_string ( ) , None ) ) ,
37
- Err ( e) => println ! ( "{}" , e) ,
37
+ Ok ( i) => Some ( Payload :: PaymentRequest ( None , i. to_string ( ) , None ) ) ,
38
+ Err ( e) => {
39
+ println ! ( "Invalid invoice: {}" , e) ;
40
+ None
41
+ }
38
42
}
39
- }
43
+ } ;
44
+ let request_id = Uuid :: new_v4 ( ) . as_u128 ( ) as u64 ;
40
45
// Create AddInvoice message
41
- let add_invoice_message =
42
- Message :: new_order ( Some ( * order_id) , None , None , Action :: AddInvoice , payload) ;
46
+ let add_invoice_message = Message :: new_order (
47
+ Some ( * order_id) ,
48
+ Some ( request_id) ,
49
+ None ,
50
+ Action :: AddInvoice ,
51
+ payload,
52
+ ) ;
43
53
44
- send_message_sync (
54
+ let dm = send_message_sync (
45
55
client,
46
56
Some ( identity_keys) ,
47
57
& trade_keys,
@@ -52,5 +62,20 @@ pub async fn execute_add_invoice(
52
62
)
53
63
. await ?;
54
64
65
+ dm. iter ( ) . for_each ( |el| {
66
+ let message = el. 0 . get_inner_message_kind ( ) ;
67
+ if message. request_id == Some ( request_id) && message. action == Action :: WaitingSellerToPay {
68
+ println ! ( "Now we should wait for the seller to pay the invoice" ) ;
69
+ }
70
+ } ) ;
71
+ match order
72
+ . set_status ( Status :: WaitingPayment . to_string ( ) )
73
+ . save ( & pool)
74
+ . await
75
+ {
76
+ Ok ( _) => println ! ( "Order status updated" ) ,
77
+ Err ( e) => println ! ( "Failed to update order status: {}" , e) ,
78
+ }
79
+
55
80
Ok ( ( ) )
56
81
}
0 commit comments