Skip to content

Commit

Permalink
add ln-pay handler
Browse files Browse the repository at this point in the history
  • Loading branch information
yemmyharry committed May 13, 2024
1 parent 8d2c2e3 commit 8d2931a
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion wrappers/fedimint-go/pkg/handlers/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package handlers

import (
"fedimint-go-client/pkg/fedimint"
"fmt"
"html/template"
"net/http"
"strconv"
Expand Down Expand Up @@ -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
}
}

}

0 comments on commit 8d2931a

Please sign in to comment.