diff --git a/pkg/invoice.go b/pkg/invoice.go index 1035863..00540e2 100644 --- a/pkg/invoice.go +++ b/pkg/invoice.go @@ -117,15 +117,20 @@ func (i *Invoice) ToPublic() PublicInvoice { Estimate: 0, } - /* TODO: calculate status booleans and estimate - if i.PaidHeight > 0 { - pub.Paid = true + if i.LastIncomingAmount.IsPositive() { + pub.PartDetected = true } - if i.BlockID != "" { - pub.Confirmed = true + if i.LastIncomingAmount.GreaterThanOrEqual(i.Total) { + pub.TotalDetected = true } - */ + + //if i.LastPaidAmount.GreaterThanOrEqual(i.Total) { + if i.PaidHeight > 1 { + pub.TotalConfirmed = true + } + + // TODO: we still need a way to handle Unconfirmed? return pub } diff --git a/pkg/webapi/qrcode.go b/pkg/webapi/qrcode.go index f8e8928..bd4dca2 100644 --- a/pkg/webapi/qrcode.go +++ b/pkg/webapi/qrcode.go @@ -1,12 +1,42 @@ package webapi import ( + "encoding/hex" + "image/color" + qrcode "github.com/skip2/go-qrcode" ) -func GenerateQRCodePNG(content string, size int) ([]byte, error) { - // Generate the QR code as a PNG image - pngBytes, err := qrcode.Encode(content, qrcode.Medium, size) +func GenerateQRCodePNG(content string, size int, fg string, bg string) ([]byte, error) { + q, err := qrcode.New(content, qrcode.Medium) + q.ForegroundColor = color.White + + DefaultFG := []byte{0, 0, 0, 254} + DefaultBG := []byte{255, 255, 255, 255} + + // decode passed in fg/bg if sent + f, err := hex.DecodeString(fg) + if err == nil && len(f) >= 3 { + DefaultFG = f + } + b, err := hex.DecodeString(bg) + if err == nil && len(b) >= 3 { + DefaultBG = b + } + + // add default alpha + if len(DefaultFG) == 3 { + DefaultFG = append(DefaultFG, 255) + } + + if len(DefaultBG) == 3 { + DefaultBG = append(DefaultBG, 255) + } + + q.BackgroundColor = color.RGBA{DefaultBG[0], DefaultBG[1], DefaultBG[2], DefaultBG[3]} + q.ForegroundColor = color.RGBA{DefaultFG[0], DefaultFG[1], DefaultFG[2], DefaultFG[3]} + + pngBytes, err := q.PNG(size) if err != nil { return []byte{}, err } diff --git a/pkg/webapi/webapi.go b/pkg/webapi/webapi.go index f5b4e3a..c54d7ab 100644 --- a/pkg/webapi/webapi.go +++ b/pkg/webapi/webapi.go @@ -241,8 +241,11 @@ func (t WebAPI) getInvoiceQR(w http.ResponseWriter, r *http.Request, p httproute return } + qs := r.URL.Query() + fg := qs.Get("fg") + bg := qs.Get("bg") connectURL := fmt.Sprintf("%s/invoice/%s/connect", t.config.WebAPI.PubAPIRootURL, id) - qr, _ := GenerateQRCodePNG(fmt.Sprintf("dogecoin:%s?amount=%v&cxt=%s", string(invoice.ID), invoice.CalcTotal().String(), url.QueryEscape(connectURL)), 256) + qr, _ := GenerateQRCodePNG(fmt.Sprintf("dogecoin:%s?amount=%v&cxt=%s", string(invoice.ID), invoice.CalcTotal().String(), url.QueryEscape(connectURL)), 512, fg, bg) w.Header().Set("Content-Type", "image/png") // Maxage 900 (15 minutes) is because this image should not // change at all for a given invoice and we expect most invoices