dkforest

A forum and chat platform (onion)
git clone https://git.dasho.dev/n0tr1v/dkforest.git
Log | Files | Refs | LICENSE

tableXmrInvoices.go (2070B)


      1 package database
      2 
      3 import (
      4 	"dkforest/pkg/config"
      5 	"fmt"
      6 	"github.com/boombuler/barcode"
      7 	"github.com/boombuler/barcode/qr"
      8 	wallet1 "github.com/monero-ecosystem/go-monero-rpc-client/wallet"
      9 	"github.com/sirupsen/logrus"
     10 	"image"
     11 	"time"
     12 )
     13 
     14 type XmrInvoice struct {
     15 	ID              int64
     16 	UserID          UserID
     17 	ProductID       int64
     18 	Address         string
     19 	AmountRequested int64
     20 	AmountReceived  *int64
     21 	Confirmations   int64
     22 	CreatedAt       time.Time
     23 }
     24 
     25 func (d *DkfDB) CreateXmrInvoice(userID UserID, productID int64) (out XmrInvoice, err error) {
     26 	err = d.db.Where("user_id = ? AND product_id = ? AND amount_received IS NULL", userID, productID).First(&out).Error
     27 	if err == nil {
     28 		return
     29 	}
     30 	resp, err := config.Xmr().CreateAddress(&wallet1.RequestCreateAddress{})
     31 	if err != nil {
     32 		logrus.Error(err)
     33 		return
     34 	}
     35 	out = XmrInvoice{
     36 		UserID:          userID,
     37 		ProductID:       productID,
     38 		Address:         resp.Address,
     39 		AmountRequested: 10,
     40 	}
     41 	if err = d.db.Create(&out).Error; err != nil {
     42 		return
     43 	}
     44 	return
     45 }
     46 
     47 func (d *DkfDB) GetXmrInvoiceByAddress(address string) (out XmrInvoice, err error) {
     48 	err = d.db.Where("address = ?", address).First(&out).Error
     49 	return
     50 }
     51 
     52 // GetURL monero:599sdD99LUJ8a7ubSetcRS58zRBQ9uqcMGJED5eg1J9rd8Ktq1A93zfi9iMsKbKVPq3XvbTwKLmZ48mcWe9Kwc5AUKdbyV8?tx_amount=0.010000000000&recipient_name=n0tr1v&tx_description=desc
     53 func (i XmrInvoice) GetURL() string {
     54 	description := "membership"
     55 	recipient := "n0tr1v"
     56 	amount := 0.01
     57 	return fmt.Sprintf("monero:%s?tx_amount=%f&recipient_name=%s&tx_description=%s", i.Address, amount, recipient, description)
     58 }
     59 
     60 func (i XmrInvoice) GetImage() (image.Image, error) {
     61 	b, err := qr.Encode(i.GetURL(), qr.L, qr.Auto)
     62 	if err != nil {
     63 		return nil, err
     64 	}
     65 	b, err = barcode.Scale(b, 150, 150)
     66 	if err != nil {
     67 		return nil, err
     68 	}
     69 	return b, nil
     70 }
     71 
     72 func (i *XmrInvoice) DoSave(db *DkfDB) {
     73 	if err := db.db.Save(i).Error; err != nil {
     74 		logrus.Error(err)
     75 	}
     76 }
     77 
     78 type Product struct {
     79 	ID          int64
     80 	Name        string
     81 	Description string
     82 	Price       int64
     83 	CreatedAt   time.Time
     84 }