From d2d4c26f44e46df549f7ed9d5d648e3da9abb597 Mon Sep 17 00:00:00 2001 From: yemmyharry Date: Mon, 13 May 2024 22:54:08 +0100 Subject: [PATCH] feat: add ln-pay handler --- wrappers/fedimint-go/pkg/handlers/handler.go | 41 +++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/wrappers/fedimint-go/pkg/handlers/handler.go b/wrappers/fedimint-go/pkg/handlers/handler.go index 24beb04..ddb74a0 100644 --- a/wrappers/fedimint-go/pkg/handlers/handler.go +++ b/wrappers/fedimint-go/pkg/handlers/handler.go @@ -2,7 +2,6 @@ package handlers import ( "fedimint-go-client/pkg/fedimint" - "fmt" "html/template" "net/http" "strconv" @@ -108,3 +107,43 @@ func (h *Handler) CreateInvoiceHandler(w http.ResponseWriter, r *http.Request) { } } + +func (h *Handler) LnPayHandler(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodPost { + + gwIDStr := r.FormValue("gatewayId") + fedIDStr := r.FormValue("federationId") + lnurlComment := r.FormValue("lnurlComment") + paymentInfo := r.FormValue("paymentInfo") + amountMsatStr := r.FormValue("amountMsat") + amountMsat, err := strconv.ParseUint(amountMsatStr, 10, 64) + if err != nil { + http.Error(w, "Invalid amountMsat: "+err.Error(), http.StatusBadRequest) + return + } + + lnPayResponse, err := h.Fc.Ln.Pay(paymentInfo, &gwIDStr, &amountMsat, &lnurlComment, &fedIDStr) + if err != nil { + // Check if the error message contains "malformed public key" indicating a problem with gatewayId + if strings.Contains(err.Error(), "malformed public key") { + http.Error(w, "Invalid gatewayId provided", http.StatusBadRequest) + return + } + http.Error(w, "Error paying: "+err.Error(), http.StatusInternalServerError) + return + } + + err = h.Tmpl.ExecuteTemplate(w, "ln_pay.gohtml", lnPayResponse) + if err != nil { + http.Error(w, "Error executing template: "+err.Error(), http.StatusInternalServerError) + return + } + } else { + err := h.Tmpl.ExecuteTemplate(w, "ln_pay.gohtml", nil) + if err != nil { + http.Error(w, "Error executing template: "+err.Error(), http.StatusInternalServerError) + return + } + } + +}