Wip download invoice status

master
Chakib Benziane 5 years ago
parent d8c8c6a49b
commit fdd586d11c

@ -149,7 +149,6 @@ func download(c *gin.Context) {
}
//TODO: Check if invoice paid ??
//
//If paid return the files else return the invoice to pay

@ -70,7 +70,7 @@ func upSessionHandler(c *gin.Context) {
}
func pollInvoice(c *gin.Context) {
rhash := c.Params("rhash")
invoiceId := c.Param("rhash")
// First check if already paid
lnInv, err := lndrpc.LookupInvoiceRhashStr(invoiceId)
@ -81,7 +81,20 @@ func pollInvoice(c *gin.Context) {
return
}
// if not poll the status
invoice := ln.InvoiceFromLndIn(lnInv)
res := gin.H{
"invoice": invoice,
}
// If paid return ok
if lnInv.Settled {
c.JSON(http.StatusOK, res)
} else {
c.JSON(http.StatusPaymentRequired, res)
}
return
}
// Was used by ln-charge

@ -132,8 +132,8 @@ func (i *Invoice) UnmarshalBinary(b []byte) error {
// Create new Invoice from lnrpc.Invoice
func InvoiceFromLndIn(lndInvoice *lnrpc.Invoice) *Invoice {
log.Printf("new invoice form lnd with value %d", lndInvoice.Value)
log.Printf("setting msat to %d", float64(lndInvoice.Value*1000))
//log.Printf("new invoice form lnd with value %d", lndInvoice.Value)
//log.Printf("setting msat to %f", float64(lndInvoice.Value*1000))
invoice := Invoice{
AddIndex: lndInvoice.AddIndex,
Description: lndInvoice.GetMemo(),

@ -6,6 +6,7 @@ import (
"fmt"
"io"
"log"
"regexp"
"time"
"git.sp4ke.com/sp4ke/bit4sat/bus"
@ -20,6 +21,8 @@ import (
const (
LastSettledInvoiceIndexKey = "last_invoice_settled_index_cursor"
ReUploadInvoice = `bit4sat upload`
ReDownInvoice = `bit4sat download`
)
// Last watched invoice
@ -93,15 +96,22 @@ func WatchInvoice() {
//TODO
func handleExpiredInvoice(invoice *lnrpc.Invoice) {
// Update upload status to "expired"
uploadId, err := storage.GetUploadIdForInvoice(hex.EncodeToString(invoice.RHash))
matchUp, err := regexp.MatchString(ReUploadInvoice, invoice.Memo)
if err != nil {
log.Fatal(fmt.Errorf("handleExpiredInvoice: %s", err))
log.Printf("Error regex match: %s", err)
}
err = storage.SetUploadStatus(uploadId, storage.UpPayExpired)
if err != nil {
log.Fatal(fmt.Errorf("handleExpiredInvoice %s", err))
if matchUp {
// Update upload status to "expired"
uploadId, err := storage.GetUploadIdForInvoice(hex.EncodeToString(invoice.RHash))
if err != nil {
log.Fatal(fmt.Errorf("handleExpiredInvoice: %s", err))
}
err = storage.SetUploadStatus(uploadId, storage.UpPayExpired)
if err != nil {
log.Fatal(fmt.Errorf("handleExpiredInvoice %s", err))
}
}
// No need to store the invoice
@ -125,44 +135,53 @@ func handleSettledInvoice(invoice *lnrpc.Invoice) {
// Save last settled index
setLastSettledInvoiceIndex(invoice.SettleIndex)
// Update upload status to "paid"
uploadId, err := storage.GetUploadIdForInvoice(hex.EncodeToString(invoice.RHash))
matchUp, err := regexp.MatchString(ReUploadInvoice, invoice.Memo)
if err != nil {
log.Fatal(fmt.Errorf("handleSettledInvoice: %s", err))
log.Printf("Error regex match: %s", err)
}
err = storage.SetUploadStatus(uploadId, storage.UpPaid)
if err != nil {
log.Fatal(fmt.Errorf("handleSettledInvoice: %s", err))
}
// Handle upload related invoices
if matchUp {
// Get stored invoice for upload
storedInvoice, err := storage.GetUploadInvoice(uploadId)
if err != nil {
log.Fatal(fmt.Errorf("handleSettledInvoice: %s", err))
}
// Update upload status to "paid"
uploadId, err := storage.GetUploadIdForInvoice(hex.EncodeToString(invoice.RHash))
if err != nil {
log.Fatal(fmt.Errorf("handleSettledInvoice: %s", err))
}
// Update stored invoice fields
newInvoice := ln.UpdateInvoiceFromLnd(storedInvoice, invoice)
err = storage.SetUploadStatus(uploadId, storage.UpPaid)
if err != nil {
log.Fatal(fmt.Errorf("handleSettledInvoice: %s", err))
}
// Set invoice for upload
err = storage.SetUploadInvoice(uploadId, newInvoice)
if err != nil {
log.Fatal(fmt.Errorf("handleSettledInvoice: %s", err))
}
// Get stored invoice for upload
storedInvoice, err := storage.GetUploadInvoice(uploadId)
if err != nil {
log.Fatal(fmt.Errorf("handleSettledInvoice: %s", err))
}
newInvoiceJson, err := json.Marshal(newInvoice)
if err != nil {
log.Fatal(fmt.Errorf("handleSettledInvoice: %s", err))
}
// Update stored invoice fields
newInvoice := ln.UpdateInvoiceFromLnd(storedInvoice, invoice)
// publish invoice was updated to upload_id_paid channel
log.Printf("Notifying invoice paid %s", uploadId)
key := fmt.Sprintf("%s:%s", bus.InvoicePaidChannelPrefix,
newInvoice.RHash)
// Set invoice for upload
err = storage.SetUploadInvoice(uploadId, newInvoice)
if err != nil {
log.Fatal(fmt.Errorf("handleSettledInvoice: %s", err))
}
newInvoiceJson, err := json.Marshal(newInvoice)
if err != nil {
log.Fatal(fmt.Errorf("handleSettledInvoice: %s", err))
}
log.Printf("Notifying invoice paid %s", uploadId)
// publish invoice was updated to upload_id_paid channel
key := fmt.Sprintf("%s:%s", bus.InvoicePaidChannelPrefix,
newInvoice.RHash)
err = db.DB.Redis.Do(radix.FlatCmd(nil, "PUBLISH",
key, newInvoiceJson))
err = db.DB.Redis.Do(radix.FlatCmd(nil, "PUBLISH",
key, newInvoiceJson))
}
if err != nil {
panic(err)

Loading…
Cancel
Save