Skip to content

Commit

Permalink
Merge pull request #104 from dogecoinfoundation/qrcode-enhancement
Browse files Browse the repository at this point in the history
enhance qrcode api
  • Loading branch information
tjstebbing authored Sep 1, 2023
2 parents b80f406 + 41e89b3 commit ef637ca
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
17 changes: 11 additions & 6 deletions pkg/invoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
36 changes: 33 additions & 3 deletions pkg/webapi/qrcode.go
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/webapi/webapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ef637ca

Please sign in to comment.