|
|
|
@ -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)
|
|
|
|
|