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:
parent
08d0eee00c
commit
a070fe35a0
@ -69,17 +69,13 @@ LnTip.prototype.stopWatchingPayment = function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
LnTip.prototype.payWithWebln = function () {
|
LnTip.prototype.payWithWebln = function () {
|
||||||
console.log(this.invoice)
|
|
||||||
if (!webln.isEnabled) {
|
if (!webln.isEnabled) {
|
||||||
webln.enable().then((weblnResponse) => {
|
webln.enable().then((weblnResponse) => {
|
||||||
console.log(this.invoice.PaymentRequest)
|
|
||||||
return webln.sendPayment({ paymentRequest: this.invoice.PaymentRequest })
|
return webln.sendPayment({ paymentRequest: this.invoice.PaymentRequest })
|
||||||
}).catch((e) => {
|
}).catch((e) => {
|
||||||
console.log(e);
|
|
||||||
this.requestPayment();
|
this.requestPayment();
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
console.log(this.invoice.PaymentRequest)
|
|
||||||
return webln.sendPayment({ paymentRequest: this.invoice.PaymentRequest })
|
return webln.sendPayment({ paymentRequest: this.invoice.PaymentRequest })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -110,8 +106,16 @@ LnTip.prototype.requestPayment = function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
LnTip.prototype.getInvoice = function () {
|
LnTip.prototype.getInvoice = function () {
|
||||||
return this._request(`${this.host}/payme?memo=${this.memo}&amount=${this.amount}`)
|
var args = {
|
||||||
.then((invoice) => {
|
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.invoice = invoice;
|
||||||
this.watchPayment();
|
this.watchPayment();
|
||||||
|
|
||||||
@ -123,8 +127,8 @@ LnTip.prototype.getInvoice = function () {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
LnTip.prototype._request = function(url) {
|
LnTip.prototype._request = function(url, args) {
|
||||||
return fetch(url).then((response) => {
|
return fetch(url, args).then((response) => {
|
||||||
return response.json();
|
return response.json();
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
24
invoices.go
24
invoices.go
@ -8,15 +8,20 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var stdOutLogger = log.New(os.Stdout, "", log.LstdFlags)
|
var stdOutLogger = log.New(os.Stdout, "", log.LstdFlags)
|
||||||
|
|
||||||
|
type Invoice struct {
|
||||||
|
Amount int64 `json:"amount"`
|
||||||
|
Memo string `json:"memo"`
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
address := flag.String("address", "localhost:10009", "The host and port of the ln gRPC server")
|
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")
|
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")
|
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()
|
flag.Parse()
|
||||||
|
|
||||||
@ -25,7 +30,7 @@ func main() {
|
|||||||
e.Use(middleware.CORS())
|
e.Use(middleware.CORS())
|
||||||
e.Use(middleware.Recover())
|
e.Use(middleware.Recover())
|
||||||
|
|
||||||
lndOptions := ln.LNDoptions{
|
lndOptions := ln.LNDoptions{
|
||||||
Address: *address,
|
Address: *address,
|
||||||
CertFile: *certFile,
|
CertFile: *certFile,
|
||||||
MacaroonFile: *macaroonFile,
|
MacaroonFile: *macaroonFile,
|
||||||
@ -35,12 +40,15 @@ func main() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
e.GET("/payme", func(c echo.Context) error {
|
e.POST("/invoice", func(c echo.Context) error {
|
||||||
memo := c.FormValue("memo")
|
i := new(Invoice)
|
||||||
amount, _ := strconv.ParseInt(c.FormValue("amount"), 10, 64)
|
if err := c.Bind(i); err != nil {
|
||||||
invoice, err := lnClient.GenerateInvoice(amount, memo)
|
return c.JSON(http.StatusBadRequest, "bad request")
|
||||||
|
}
|
||||||
|
|
||||||
|
invoice, err := lnClient.GenerateInvoice(i.Amount, i.Memo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.JSON(http.StatusInternalServerError, "error")
|
return c.JSON(http.StatusInternalServerError, "invoice creation error")
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.JSON(http.StatusOK, invoice)
|
return c.JSON(http.StatusOK, invoice)
|
||||||
@ -52,5 +60,5 @@ func main() {
|
|||||||
return c.JSON(http.StatusOK, invoice)
|
return c.JSON(http.StatusOK, invoice)
|
||||||
})
|
})
|
||||||
|
|
||||||
e.Logger.Fatal(e.Start(":1323"))
|
e.Logger.Fatal(e.Start(*bind))
|
||||||
}
|
}
|
||||||
|
@ -36,12 +36,11 @@ type LNDclient struct {
|
|||||||
func (c LNDclient) GenerateInvoice(amount int64, memo string) (Invoice, error) {
|
func (c LNDclient) GenerateInvoice(amount int64, memo string) (Invoice, error) {
|
||||||
result := Invoice{}
|
result := Invoice{}
|
||||||
|
|
||||||
// Create the request and send it
|
stdOutLogger.Printf("Creating invoice: memo=%s amount=%v ", memo, amount)
|
||||||
invoice := lnrpc.Invoice{
|
invoice := lnrpc.Invoice{
|
||||||
Memo: memo,
|
Memo: memo,
|
||||||
Value: amount,
|
Value: amount,
|
||||||
}
|
}
|
||||||
stdOutLogger.Printf("Creating invoice: %s", memo)
|
|
||||||
res, err := c.lndClient.AddInvoice(c.ctx, &invoice)
|
res, err := c.lndClient.AddInvoice(c.ctx, &invoice)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return result, err
|
return result, err
|
||||||
@ -63,7 +62,7 @@ func (c LNDclient) CheckInvoice(id string) (bool, error) {
|
|||||||
return false, err
|
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
|
// Get the invoice for that hash
|
||||||
paymentHash := lnrpc.PaymentHash{
|
paymentHash := lnrpc.PaymentHash{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user