From de0e6f2fb8f8ce23913adbb5df324670d5c9e252 Mon Sep 17 00:00:00 2001 From: Peter Rounce Date: Fri, 24 Mar 2023 13:22:47 +0000 Subject: [PATCH] respond with ok async --- lnd/lnd.go | 2 +- lndhub/lndhub.go | 62 +++++++++++++++++++++++++++++++++++---- lnurlw/lnurlw_callback.go | 58 +++++------------------------------- 3 files changed, 66 insertions(+), 56 deletions(-) diff --git a/lnd/lnd.go b/lnd/lnd.go index a3c88ee..c5e0c1a 100644 --- a/lnd/lnd.go +++ b/lnd/lnd.go @@ -192,7 +192,7 @@ func Monitor_invoice_state(r_hash []byte) { // https://api.lightning.community/?shell#sendpaymentv2 -func Pay_invoice(card_payment_id int, invoice string) { +func PayInvoice(card_payment_id int, invoice string) { // SendPaymentV2 diff --git a/lndhub/lndhub.go b/lndhub/lndhub.go index 92780a8..0012e82 100644 --- a/lndhub/lndhub.go +++ b/lndhub/lndhub.go @@ -1,12 +1,64 @@ package lndhub import ( -// log "github.com/sirupsen/logrus" + "bytes" + "encoding/json" + "io" + "net/http" + "strconv" -// "github.com/boltcard/boltcard/db" -// "github.com/boltcard/boltcard/email" + "github.com/boltcard/boltcard/db" + log "github.com/sirupsen/logrus" ) -func Pay_invoice(card_payment_id int, invoice string) { - +type LndhubPayInvoiceRequest struct { + Invoice string `json:"invoice"` + FreeAmount string `json:"freeamount"` + LoginId string `json:"loginid"` +} + +func PayInvoice(cardPaymentId int, invoice string, amountSats int, loginId string, accessToken string) { + + lndhub_url := db.Get_setting("LNDHUB_URL") + + client := &http.Client{} + + //lndhub.payinvoice API call + var payInvoiceRequest LndhubPayInvoiceRequest + payInvoiceRequest.Invoice = invoice + payInvoiceRequest.FreeAmount = strconv.Itoa(amountSats) + payInvoiceRequest.LoginId = loginId + + req_payinvoice, err := json.Marshal(payInvoiceRequest) + log.Info(string(req_payinvoice)) + if err != nil { + log.WithFields(log.Fields{"card_payment_id": cardPaymentId}).Warn(err) + return + } + + req, err := http.NewRequest("POST", lndhub_url+"/payinvoice", bytes.NewBuffer(req_payinvoice)) + if err != nil { + log.WithFields(log.Fields{"card_payment_id": cardPaymentId}).Warn(err) + return + } + + req.Header.Add("Access-Control-Allow-Origin", "*") + req.Header.Add("Content-Type", "application/json") + req.Header.Add("Authorization", "Bearer " + accessToken) + + res2, err := client.Do(req) + if err != nil { + log.WithFields(log.Fields{"card_payment_id": cardPaymentId}).Warn(err) + return + } + + defer res2.Body.Close() + + b2, err := io.ReadAll(res2.Body) + if err != nil { + log.WithFields(log.Fields{"card_payment_id": cardPaymentId}).Warn(err) + return + } + + log.Info(string(b2)) } diff --git a/lnurlw/lnurlw_callback.go b/lnurlw/lnurlw_callback.go index 24f81fd..49c682b 100644 --- a/lnurlw/lnurlw_callback.go +++ b/lnurlw/lnurlw_callback.go @@ -5,11 +5,11 @@ import ( "encoding/json" "io" "net/http" - "strconv" "strings" "github.com/boltcard/boltcard/db" "github.com/boltcard/boltcard/lnd" + "github.com/boltcard/boltcard/lndhub" "github.com/boltcard/boltcard/resp_err" decodepay "github.com/fiatjaf/ln-decodepay" log "github.com/sirupsen/logrus" @@ -25,12 +25,6 @@ type LndhubAuthResponse struct { AccessToken string `json:"access_token"` } -type LndhubPayInvoiceRequest struct { - Invoice string `json:"invoice"` - FreeAmount string `json:"freeamount"` - LoginId string `json:"loginid"` -} - func lndhub_payment(w http.ResponseWriter, p *db.Payment, bolt11 decodepay.Bolt11, param_pr string) { //get setting for LNDHUB_URL @@ -104,48 +98,13 @@ func lndhub_payment(w http.ResponseWriter, p *db.Payment, bolt11 decodepay.Bolt1 return } - //lndhub.payinvoice API call - var payInvoiceRequest LndhubPayInvoiceRequest - payInvoiceRequest.Invoice = param_pr - payInvoiceRequest.FreeAmount = strconv.Itoa(int(bolt11.MSatoshi / 1000)) - payInvoiceRequest.LoginId = card_name_parts[0] + // https://github.com/fiatjaf/lnurl-rfc/blob/luds/03.md + // + // LN SERVICE sends a {"status": "OK"} or + // {"status": "ERROR", "reason": "error details..."} + // JSON response and then attempts to pay the invoices asynchronously. - req_payinvoice, err := json.Marshal(payInvoiceRequest) - log.Info(string(req_payinvoice)) - if err != nil { - log.WithFields(log.Fields{"card_payment_id": p.Card_payment_id}).Warn(err) - resp_err.Write(w) - return - } - - req, err := http.NewRequest("POST", lndhub_url+"/payinvoice", bytes.NewBuffer(req_payinvoice)) - if err != nil { - log.WithFields(log.Fields{"card_payment_id": p.Card_payment_id}).Warn(err) - resp_err.Write(w) - return - } - - req.Header.Add("Access-Control-Allow-Origin", "*") - req.Header.Add("Content-Type", "application/json") - req.Header.Add("Authorization", "Bearer "+auth_keys.AccessToken) - - res2, err := client.Do(req) - if err != nil { - log.WithFields(log.Fields{"card_payment_id": p.Card_payment_id}).Warn(err) - resp_err.Write(w) - return - } - - defer res2.Body.Close() - - b2, err := io.ReadAll(res2.Body) - if err != nil { - log.WithFields(log.Fields{"card_payment_id": p.Card_payment_id}).Warn(err) - resp_err.Write(w) - return - } - - log.Info(string(b2)) + go lndhub.PayInvoice(p.Card_payment_id, param_pr, int(bolt11.MSatoshi / 1000), card_name_parts[0], auth_keys.AccessToken) log.Debug("sending 'status OK' response") @@ -153,7 +112,6 @@ func lndhub_payment(w http.ResponseWriter, p *db.Payment, bolt11 decodepay.Bolt1 w.WriteHeader(http.StatusOK) jsonData := []byte(`{"status":"OK"}`) w.Write(jsonData) - } func lnd_payment(w http.ResponseWriter, p *db.Payment, bolt11 decodepay.Bolt11, param_pr string) { @@ -225,7 +183,7 @@ func lnd_payment(w http.ResponseWriter, p *db.Payment, bolt11 decodepay.Bolt11, // {"status": "ERROR", "reason": "error details..."} // JSON response and then attempts to pay the invoices asynchronously. - go lnd.Pay_invoice(p.Card_payment_id, param_pr) + go lnd.PayInvoice(p.Card_payment_id, param_pr) log.Debug("sending 'status OK' response")