add LNURLp switch

This commit is contained in:
Peter Rounce 2022-09-19 06:40:24 +00:00
parent 4a38533cec
commit 8d01474d5f
8 changed files with 40 additions and 40 deletions

View file

@ -18,7 +18,7 @@ CREATE TABLE cards (
enable_flag CHAR(1) NOT NULL DEFAULT 'N',
tx_limit_sats INT NOT NULL,
day_limit_sats INT NOT NULL,
card_name VARCHAR(100) NOT NULL DEFAULT '',
card_name VARCHAR(100) UNIQUE NOT NULL DEFAULT '',
one_time_code CHAR(32) NOT NULL DEFAULT '',
one_time_code_expiry TIMESTAMPTZ DEFAULT NOW() + INTERVAL '1 DAY',
one_time_code_used CHAR(1) NOT NULL DEFAULT 'Y',
@ -45,13 +45,14 @@ CREATE TABLE card_receipts (
card_receipt_id INT GENERATED ALWAYS AS IDENTITY,
card_id INT NOT NULL,
ln_invoice VARCHAR(1024) NOT NULL DEFAULT '',
r_hash_hex VARCHAR(64) UNIQUE NOT NULL DEFAULT '',
r_hash_hex CHAR(64) UNIQUE NOT NULL DEFAULT '',
amount_msats BIGINT CHECK (amount_msats > 0),
receipt_status VARCHAR(100) NOT NULL DEFAULT '',
receipt_status_time TIMESTAMPTZ,
CONSTRAINT fk_card FOREIGN KEY(card_id) REFERENCES cards(card_id)
);
GRANT ALL PRIVILEGES ON TABLE cards TO cardapp;
GRANT ALL PRIVILEGES ON TABLE card_payments TO cardapp;
GRANT ALL PRIVILEGES ON TABLE card_receipts TO cardapp;

View file

@ -339,8 +339,8 @@ func db_insert_payment(card_id int, lnurlw_k1 string) error {
// insert a new record into card_payments with card_id & lnurlw_k1 set
sqlStatement := `INSERT INTO card_payments` +
` (card_id, lnurlw_k1, paid_flag, lnurlw_request_time)` +
` VALUES ($1, $2, 'N', NOW());`
` (card_id, lnurlw_k1, paid_flag, lnurlw_request_time, payment_status_time)` +
` VALUES ($1, $2, 'N', NOW(), NOW());`
res, err := db.Exec(sqlStatement, card_id, lnurlw_k1)
if err != nil {
return err

View file

@ -52,7 +52,6 @@ edit `create_db.sql` to set the cardapp password
`$ ./s_create_db`
### boltcard service install
`$ sudo cp boltcard.service /etc/systemd/system/boltcard.service`
`$ ./s_build`
`$ sudo systemctl enable boltcard`
`$ sudo systemctl status boltcard`

View file

@ -163,11 +163,7 @@ func monitor_invoice_state(r_hash []byte) () {
// https://api.lightning.community/?shell#sendpaymentv2
func pay_invoice(invoice string) (payment_status string, failure_reason string, return_err error) {
payment_status = ""
failure_reason = ""
return_err = nil
func pay_invoice(card_payment_id int, invoice string) {
// SendPaymentV2
@ -175,7 +171,7 @@ func pay_invoice(invoice string) (payment_status string, failure_reason string,
ln_port, err := strconv.Atoi(os.Getenv("LN_PORT"))
if err != nil {
return_err = err
log.WithFields(log.Fields{"card_payment_id": card_payment_id}).Warn(err)
return
}
@ -190,7 +186,7 @@ func pay_invoice(invoice string) (payment_status string, failure_reason string,
fee_limit_sat_str := os.Getenv("FEE_LIMIT_SAT")
fee_limit_sat, err := strconv.ParseInt(fee_limit_sat_str, 10, 64)
if err != nil {
return_err = err
log.WithFields(log.Fields{"card_payment_id": card_payment_id}).Warn(err)
return
}
@ -204,7 +200,7 @@ func pay_invoice(invoice string) (payment_status string, failure_reason string,
FeeLimitSat: fee_limit_sat})
if err != nil {
return_err = err
log.WithFields(log.Fields{"card_payment_id": card_payment_id}).Warn(err)
return
}
@ -216,12 +212,21 @@ func pay_invoice(invoice string) (payment_status string, failure_reason string,
}
if err != nil {
return_err = err
log.WithFields(log.Fields{"card_payment_id": card_payment_id}).Warn(err)
return
}
payment_status = lnrpc.Payment_PaymentStatus_name[int32(update.Status)]
failure_reason = lnrpc.PaymentFailureReason_name[int32(update.FailureReason)]
payment_status := lnrpc.Payment_PaymentStatus_name[int32(update.Status)]
failure_reason := lnrpc.PaymentFailureReason_name[int32(update.FailureReason)]
log.WithFields(log.Fields{"card_payment_id": card_payment_id}).Info("payment failure reason : ", failure_reason)
log.WithFields(log.Fields{"card_payment_id": card_payment_id}).Info("payment status : ", payment_status)
err = db_update_payment_status(card_payment_id, payment_status, failure_reason)
if err != nil {
log.WithFields(log.Fields{"card_payment_id": card_payment_id}).Warn(err)
return
}
}
connection.Close()

View file

@ -10,6 +10,11 @@ import (
)
func lnurlp_callback(w http.ResponseWriter, r *http.Request) {
if os.Getenv("FUNCTION_LNURLP") != "ENABLE" {
log.Debug("LNURLp function is not enabled")
return
}
name := mux.Vars(r)["name"]
amount := r.URL.Query().Get("amount")
@ -60,6 +65,10 @@ func lnurlp_callback(w http.ResponseWriter, r *http.Request) {
return
}
go monitor_invoice_state(r_hash)
log.Debug("sending 'status OK' response");
jsonData := []byte(`{` +
`"status":"OK",` +
`"routes":[],` +
@ -69,6 +78,4 @@ func lnurlp_callback(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(jsonData)
go monitor_invoice_state(r_hash)
}

View file

@ -8,6 +8,11 @@ import (
)
func lnurlp_response(w http.ResponseWriter, r *http.Request) {
if os.Getenv("FUNCTION_LNURLP") != "ENABLE" {
log.Debug("LNURLp function is not enabled")
return
}
name := mux.Vars(r)["name"]
log.WithFields(

View file

@ -130,34 +130,16 @@ func lnurlw_callback(w http.ResponseWriter, req *http.Request) {
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
}
// 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.
go pay_invoice(p.card_payment_id, param_pr)
log.Debug("sending 'status OK' response");
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
jsonData := []byte(`{"status":"OK"}`)

View file

@ -1,4 +1,5 @@
go build
sudo cp boltcard.service /etc/systemd/system/boltcard.service
sudo systemctl daemon-reload
sudo systemctl stop boltcard
sudo systemctl start boltcard