ensure a response is always sent

This commit is contained in:
Peter Rounce 2022-08-24 15:23:24 +00:00
parent 8c1150468d
commit 8d1d686355
4 changed files with 51 additions and 25 deletions

View file

@ -17,6 +17,7 @@ func lnurlw_callback(w http.ResponseWriter, req *http.Request) {
if !ok || len(params_k1[0]) < 1 {
log.WithFields(log.Fields{"url": url}).Debug("k1 not found")
write_error(w)
return
}
@ -25,12 +26,14 @@ func lnurlw_callback(w http.ResponseWriter, req *http.Request) {
p, err := db_get_payment_k1(param_k1)
if err != nil {
log.WithFields(log.Fields{"url": url, "k1": param_k1}).Warn(err)
write_error(w)
return
}
// check that payment has not been made
if p.paid_flag != "N" {
log.WithFields(log.Fields{"card_payment_id": p.card_payment_id}).Info("payment already made")
write_error(w)
return
}
@ -38,16 +41,19 @@ func lnurlw_callback(w http.ResponseWriter, req *http.Request) {
lnurlw_timeout, err := db_check_lnurlw_timeout(p.card_payment_id)
if err != nil {
log.WithFields(log.Fields{"card_payment_id": p.card_payment_id}).Warn(err)
write_error(w)
return
}
if lnurlw_timeout == true {
log.WithFields(log.Fields{"card_payment_id": p.card_payment_id}).Info("lnurlw request has timed out")
write_error(w)
return
}
params_pr, ok := req.URL.Query()["pr"]
if !ok || len(params_pr[0]) < 1 {
log.WithFields(log.Fields{"card_payment_id": p.card_payment_id}).Warn("pr field not found")
write_error(w)
return
}
@ -58,6 +64,7 @@ func lnurlw_callback(w http.ResponseWriter, req *http.Request) {
err = db_update_payment_invoice(p.card_payment_id, param_pr, bolt11.MSatoshi)
if err != nil {
log.WithFields(log.Fields{"card_payment_id": p.card_payment_id}).Warn(err)
write_error(w)
return
}
@ -67,6 +74,7 @@ func lnurlw_callback(w http.ResponseWriter, req *http.Request) {
testnode := os.Getenv("LN_TESTNODE")
if testnode != "" && bolt11.Payee != testnode {
log.WithFields(log.Fields{"card_payment_id": p.card_payment_id}).Info("rejected as not the defined test node")
write_error(w)
return
}
@ -77,12 +85,14 @@ func lnurlw_callback(w http.ResponseWriter, req *http.Request) {
day_total_sats, err := db_get_card_totals(p.card_id)
if err != nil {
log.WithFields(log.Fields{"card_payment_id": p.card_payment_id}).Warn(err)
write_error(w)
return
}
c, err := db_get_card_from_card_id(p.card_id)
if err != nil {
log.WithFields(log.Fields{"card_payment_id": p.card_payment_id}).Warn(err)
write_error(w)
return
}
@ -90,6 +100,7 @@ func lnurlw_callback(w http.ResponseWriter, req *http.Request) {
log.WithFields(log.Fields{"card_payment_id": p.card_payment_id}).Info("invoice_sats: ", invoice_sats)
log.WithFields(log.Fields{"card_payment_id": p.card_payment_id}).Info("tx_limit_sats: ", c.tx_limit_sats)
log.WithFields(log.Fields{"card_payment_id": p.card_payment_id}).Info("over tx_limit_sats!")
write_error(w)
return
}
@ -98,6 +109,7 @@ func lnurlw_callback(w http.ResponseWriter, req *http.Request) {
log.WithFields(log.Fields{"card_payment_id": p.card_payment_id}).Info("day_total_sats: ", day_total_sats)
log.WithFields(log.Fields{"card_payment_id": p.card_payment_id}).Info("day_limit_sats: ", c.day_limit_sats)
log.WithFields(log.Fields{"card_payment_id": p.card_payment_id}).Info("over day_limit_sats!")
write_error(w)
return
}
@ -107,6 +119,29 @@ func lnurlw_callback(w http.ResponseWriter, req *http.Request) {
err = db_update_payment_paid(p.card_payment_id)
if err != nil {
log.WithFields(log.Fields{"card_payment_id": p.card_payment_id}).Warn(err)
write_error(w)
return
}
payment_status, failure_reason, err := pay_invoice(param_pr)
if err != nil {
log.WithFields(log.Fields{"card_payment_id": p.card_payment_id}).Warn(err)
write_error(w)
return
}
if failure_reason != "FAILURE_REASON_NONE" {
log.WithFields(log.Fields{"card_payment_id": p.card_payment_id}).Info("payment failure reason : ", failure_reason)
write_error(w)
}
log.WithFields(log.Fields{"card_payment_id": p.card_payment_id}).Info("payment status : ", payment_status)
// store result in database
err = db_update_payment_status(p.card_payment_id, payment_status, failure_reason)
if err != nil {
log.WithFields(log.Fields{"card_payment_id": p.card_payment_id}).Warn(err)
write_error(w)
return
}
@ -120,23 +155,4 @@ func lnurlw_callback(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusOK)
jsonData := []byte(`{"status":"OK"}`)
w.Write(jsonData)
payment_status, failure_reason, err := pay_invoice(param_pr)
if err != nil {
log.WithFields(log.Fields{"card_payment_id": p.card_payment_id}).Warn(err)
return
}
if failure_reason != "FAILURE_REASON_NONE" {
log.WithFields(log.Fields{"card_payment_id": p.card_payment_id}).Info("payment failure reason : ", failure_reason)
}
log.WithFields(log.Fields{"card_payment_id": p.card_payment_id}).Info("payment status : ", payment_status)
// store result in database
err = db_update_payment_status(p.card_payment_id, payment_status, failure_reason)
if err != nil {
log.WithFields(log.Fields{"card_payment_id": p.card_payment_id}).Warn(err)
return
}
}

View file

@ -246,12 +246,7 @@ func lnurlw_response(w http.ResponseWriter, req *http.Request) {
if err != nil {
log.Debug(err.Error())
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
jsonData := []byte(`{"status":"ERROR","reason":"bad request"}`)
w.Write(jsonData)
write_error(w)
return
}
@ -259,6 +254,7 @@ func lnurlw_response(w http.ResponseWriter, req *http.Request) {
if err != nil {
log.Warn(err.Error())
write_error(w)
return
}
@ -268,6 +264,7 @@ func lnurlw_response(w http.ResponseWriter, req *http.Request) {
if err != nil {
log.Warn(err.Error())
write_error(w)
return
}
@ -279,6 +276,7 @@ func lnurlw_response(w http.ResponseWriter, req *http.Request) {
if err != nil {
log.Warn(err.Error())
write_error(w)
return
}
@ -287,6 +285,7 @@ func lnurlw_response(w http.ResponseWriter, req *http.Request) {
if err != nil {
log.Warn(err.Error())
write_error(w)
return
}
@ -302,6 +301,7 @@ func lnurlw_response(w http.ResponseWriter, req *http.Request) {
if err != nil {
log.Warn(err)
write_error(w)
return
}

View file

@ -6,6 +6,13 @@ import (
"os"
)
func write_error(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
jsonData := []byte(`{"status":"ERROR","reason":"bad request"}`)
w.Write(jsonData)
}
func main() {
log_level := os.Getenv("LOG_LEVEL")

View file

@ -45,6 +45,7 @@ func new_card_request(w http.ResponseWriter, req *http.Request) {
params_a, ok := req.URL.Query()["a"]
if !ok || len(params_a[0]) < 1 {
log.Debug("a not found")
write_error(w)
return
}
@ -56,6 +57,7 @@ func new_card_request(w http.ResponseWriter, req *http.Request) {
c, err := db_get_new_card(a)
if err != nil {
log.Warn(err)
write_error(w)
return
}
@ -78,6 +80,7 @@ func new_card_request(w http.ResponseWriter, req *http.Request) {
jsonData, err := json.Marshal(response)
if err != nil {
log.Warn(err)
write_error(w)
return
}