forked from getAlby/nostr-wallet-connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle_payment_request.go
136 lines (123 loc) · 3.83 KB
/
handle_payment_request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/nbd-wtf/go-nostr"
decodepay "github.com/nbd-wtf/ln-decodepay"
"github.com/sirupsen/logrus"
)
func (svc *Service) HandlePayInvoiceEvent(ctx context.Context, request *Nip47Request, event *nostr.Event, app App, ss []byte) (result *nostr.Event, err error) {
nostrEvent := NostrEvent{App: app, NostrId: event.ID, Content: event.Content, State: "received"}
err = svc.db.Create(&nostrEvent).Error
if err != nil {
svc.Logger.WithFields(logrus.Fields{
"eventId": event.ID,
"eventKind": event.Kind,
"appId": app.ID,
}).Errorf("Failed to save nostr event: %v", err)
return nil, err
}
var bolt11 string
payParams := &Nip47PayParams{}
err = json.Unmarshal(request.Params, payParams)
if err != nil {
svc.Logger.WithFields(logrus.Fields{
"eventId": event.ID,
"eventKind": event.Kind,
"appId": app.ID,
}).Errorf("Failed to decode nostr event: %v", err)
return nil, err
}
bolt11 = payParams.Invoice
// Convert invoice to lowercase string
bolt11 = strings.ToLower(bolt11)
paymentRequest, err := decodepay.Decodepay(bolt11)
if err != nil {
svc.Logger.WithFields(logrus.Fields{
"eventId": event.ID,
"eventKind": event.Kind,
"appId": app.ID,
"bolt11": bolt11,
}).Errorf("Failed to decode bolt11 invoice: %v", err)
return svc.createResponse(event, Nip47Response{
ResultType: NIP_47_PAY_INVOICE_METHOD,
Error: &Nip47Error{
Code: NIP_47_ERROR_INTERNAL,
Message: fmt.Sprintf("Failed to decode bolt11 invoice: %s", err.Error()),
},
}, ss)
}
hasPermission, code, message := svc.hasPermission(&app, event, request.Method, paymentRequest.MSatoshi)
if !hasPermission {
svc.Logger.WithFields(logrus.Fields{
"eventId": event.ID,
"eventKind": event.Kind,
"appId": app.ID,
}).Errorf("App does not have permission: %s %s", code, message)
return svc.createResponse(event, Nip47Response{
ResultType: NIP_47_PAY_INVOICE_METHOD,
Error: &Nip47Error{
Code: code,
Message: message,
}}, ss)
}
payment := Payment{App: app, NostrEvent: nostrEvent, PaymentRequest: bolt11, Amount: uint(paymentRequest.MSatoshi / 1000)}
insertPaymentResult := svc.db.Create(&payment)
if insertPaymentResult.Error != nil {
return nil, insertPaymentResult.Error
}
svc.Logger.WithFields(logrus.Fields{
"eventId": event.ID,
"eventKind": event.Kind,
"appId": app.ID,
"bolt11": bolt11,
}).Info("Sending payment")
Client := svc.lnClient
if app.BackendOptions.Backend == "lnbits" {
var lnbitsClient *LNClient
var host = app.BackendOptions.LNBitsHost
if app.BackendOptions.LNBitsHost == "" {
if svc.cfg.LNBitsHost != "" {
host = svc.cfg.LNBitsHost
} else {
host = "http://" + svc.cfg.LnBitsUmbrel + ":3007"
}
}
var options = LNBitsOptions{
AdminKey: app.BackendOptions.LNBitsKey,
Host: host,
}
svc.lnClient = &LNBitsWrapper{lnbitsClient, options}
}
preimage, err := svc.lnClient.SendPaymentSync(ctx, event.PubKey, bolt11)
if err != nil {
svc.Logger.WithFields(logrus.Fields{
"eventId": event.ID,
"eventKind": event.Kind,
"appId": app.ID,
"bolt11": bolt11,
}).Infof("Failed to send payment: %v", err)
nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_ERROR
svc.db.Save(&nostrEvent)
return svc.createResponse(event, Nip47Response{
ResultType: NIP_47_PAY_INVOICE_METHOD,
Error: &Nip47Error{
Code: NIP_47_ERROR_INTERNAL,
Message: fmt.Sprintf("Something went wrong while paying invoice: %s", err.Error()),
},
}, ss)
}
payment.Preimage = &preimage
svc.lnClient = Client
nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_EXECUTED
svc.db.Save(&nostrEvent)
svc.db.Save(&payment)
return svc.createResponse(event, Nip47Response{
ResultType: NIP_47_PAY_INVOICE_METHOD,
Result: Nip47PayResponse{
Preimage: preimage,
},
}, ss)
}