1
1
mirror of https://github.com/bumi/lntip synced 2025-06-16 17:55:35 +00:00

Use JSON POST request for invoice creation

This commit is contained in:
bumi 2019-01-07 21:29:08 +01:00
parent 08d0eee00c
commit a070fe35a0
3 changed files with 30 additions and 19 deletions

View File

@ -69,17 +69,13 @@ LnTip.prototype.stopWatchingPayment = function () {
}
LnTip.prototype.payWithWebln = function () {
console.log(this.invoice)
if (!webln.isEnabled) {
webln.enable().then((weblnResponse) => {
console.log(this.invoice.PaymentRequest)
return webln.sendPayment({ paymentRequest: this.invoice.PaymentRequest })
}).catch((e) => {
console.log(e);
this.requestPayment();
})
} else {
console.log(this.invoice.PaymentRequest)
return webln.sendPayment({ paymentRequest: this.invoice.PaymentRequest })
}
}
@ -110,8 +106,16 @@ LnTip.prototype.requestPayment = function () {
}
LnTip.prototype.getInvoice = function () {
return this._request(`${this.host}/payme?memo=${this.memo}&amount=${this.amount}`)
.then((invoice) => {
var args = {
method: 'POST',
mode: 'cors',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ memo: this.memo, amount: this.amount })
};
return this._request(
`${this.host}/invoice`,
args
).then((invoice) => {
this.invoice = invoice;
this.watchPayment();
@ -123,8 +127,8 @@ LnTip.prototype.getInvoice = function () {
})
}
LnTip.prototype._request = function(url) {
return fetch(url).then((response) => {
LnTip.prototype._request = function(url, args) {
return fetch(url, args).then((response) => {
return response.json();
})
}

View File

@ -8,15 +8,20 @@ import (
"log"
"net/http"
"os"
"strconv"
)
var stdOutLogger = log.New(os.Stdout, "", log.LstdFlags)
type Invoice struct {
Amount int64 `json:"amount"`
Memo string `json:"memo"`
}
func main() {
address := flag.String("address", "localhost:10009", "The host and port of the ln gRPC server")
certFile := flag.String("cert", "tls.cert", "Path to the lnd tls.cert file")
macaroonFile := flag.String("macaroon", "invoice.macaroon", "Path to the lnd macaroon file")
bind := flag.String("bind", ":1323", "Host and port to bind on")
flag.Parse()
@ -35,12 +40,15 @@ func main() {
panic(err)
}
e.GET("/payme", func(c echo.Context) error {
memo := c.FormValue("memo")
amount, _ := strconv.ParseInt(c.FormValue("amount"), 10, 64)
invoice, err := lnClient.GenerateInvoice(amount, memo)
e.POST("/invoice", func(c echo.Context) error {
i := new(Invoice)
if err := c.Bind(i); err != nil {
return c.JSON(http.StatusBadRequest, "bad request")
}
invoice, err := lnClient.GenerateInvoice(i.Amount, i.Memo)
if err != nil {
return c.JSON(http.StatusInternalServerError, "error")
return c.JSON(http.StatusInternalServerError, "invoice creation error")
}
return c.JSON(http.StatusOK, invoice)
@ -52,5 +60,5 @@ func main() {
return c.JSON(http.StatusOK, invoice)
})
e.Logger.Fatal(e.Start(":1323"))
e.Logger.Fatal(e.Start(*bind))
}

View File

@ -36,12 +36,11 @@ type LNDclient struct {
func (c LNDclient) GenerateInvoice(amount int64, memo string) (Invoice, error) {
result := Invoice{}
// Create the request and send it
stdOutLogger.Printf("Creating invoice: memo=%s amount=%v ", memo, amount)
invoice := lnrpc.Invoice{
Memo: memo,
Value: amount,
}
stdOutLogger.Printf("Creating invoice: %s", memo)
res, err := c.lndClient.AddInvoice(c.ctx, &invoice)
if err != nil {
return result, err
@ -63,7 +62,7 @@ func (c LNDclient) CheckInvoice(id string) (bool, error) {
return false, err
}
stdOutLogger.Printf("Lookup invoice with hash %v\n", id)
stdOutLogger.Printf("Lookup invoice: hash=%s\n", id)
// Get the invoice for that hash
paymentHash := lnrpc.PaymentHash{