respond with ok async

This commit is contained in:
Peter Rounce 2023-03-24 13:22:47 +00:00
parent 0d3216ad99
commit de0e6f2fb8
3 changed files with 66 additions and 56 deletions

View file

@ -192,7 +192,7 @@ func Monitor_invoice_state(r_hash []byte) {
// https://api.lightning.community/?shell#sendpaymentv2 // https://api.lightning.community/?shell#sendpaymentv2
func Pay_invoice(card_payment_id int, invoice string) { func PayInvoice(card_payment_id int, invoice string) {
// SendPaymentV2 // SendPaymentV2

View file

@ -1,12 +1,64 @@
package lndhub package lndhub
import ( import (
// log "github.com/sirupsen/logrus" "bytes"
"encoding/json"
"io"
"net/http"
"strconv"
// "github.com/boltcard/boltcard/db" "github.com/boltcard/boltcard/db"
// "github.com/boltcard/boltcard/email" 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))
} }

View file

@ -5,11 +5,11 @@ import (
"encoding/json" "encoding/json"
"io" "io"
"net/http" "net/http"
"strconv"
"strings" "strings"
"github.com/boltcard/boltcard/db" "github.com/boltcard/boltcard/db"
"github.com/boltcard/boltcard/lnd" "github.com/boltcard/boltcard/lnd"
"github.com/boltcard/boltcard/lndhub"
"github.com/boltcard/boltcard/resp_err" "github.com/boltcard/boltcard/resp_err"
decodepay "github.com/fiatjaf/ln-decodepay" decodepay "github.com/fiatjaf/ln-decodepay"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -25,12 +25,6 @@ type LndhubAuthResponse struct {
AccessToken string `json:"access_token"` 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) { func lndhub_payment(w http.ResponseWriter, p *db.Payment, bolt11 decodepay.Bolt11, param_pr string) {
//get setting for LNDHUB_URL //get setting for LNDHUB_URL
@ -104,48 +98,13 @@ func lndhub_payment(w http.ResponseWriter, p *db.Payment, bolt11 decodepay.Bolt1
return return
} }
//lndhub.payinvoice API call // https://github.com/fiatjaf/lnurl-rfc/blob/luds/03.md
var payInvoiceRequest LndhubPayInvoiceRequest //
payInvoiceRequest.Invoice = param_pr // LN SERVICE sends a {"status": "OK"} or
payInvoiceRequest.FreeAmount = strconv.Itoa(int(bolt11.MSatoshi / 1000)) // {"status": "ERROR", "reason": "error details..."}
payInvoiceRequest.LoginId = card_name_parts[0] // JSON response and then attempts to pay the invoices asynchronously.
req_payinvoice, err := json.Marshal(payInvoiceRequest) go lndhub.PayInvoice(p.Card_payment_id, param_pr, int(bolt11.MSatoshi / 1000), card_name_parts[0], auth_keys.AccessToken)
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))
log.Debug("sending 'status OK' response") 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) w.WriteHeader(http.StatusOK)
jsonData := []byte(`{"status":"OK"}`) jsonData := []byte(`{"status":"OK"}`)
w.Write(jsonData) w.Write(jsonData)
} }
func lnd_payment(w http.ResponseWriter, p *db.Payment, bolt11 decodepay.Bolt11, param_pr string) { 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..."} // {"status": "ERROR", "reason": "error details..."}
// JSON response and then attempts to pay the invoices asynchronously. // 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") log.Debug("sending 'status OK' response")