Compare commits

..

4 Commits

Author SHA1 Message Date
Bastian de Byl
8fee81d8a4 added ShippingCost to SnipcartOrder 2023-02-26 00:07:03 -05:00
Bastian de Byl
41c58a497b added GetOder(token) function 2023-02-25 20:21:48 -05:00
Bastian de Byl
831ad7c029 added order: prefix to TokenPNGBase64 2023-02-25 16:57:10 -05:00
Bastian de Byl
be7452ddc1 added order TokenPNGBase64 2023-02-25 16:49:56 -05:00
2 changed files with 35 additions and 1 deletions

5
go.mod
View File

@@ -2,4 +2,7 @@ module github.com/debyltech/go-snipcart
go 1.20
require github.com/debyltech/go-helpers v0.0.0-20230224002154-eb55816c71ec
require (
github.com/debyltech/go-helpers v0.0.0-20230224002154-eb55816c71ec
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
)

View File

@@ -7,6 +7,7 @@ import (
"fmt"
helper "github.com/debyltech/go-helpers"
"github.com/skip2/go-qrcode"
)
const (
@@ -51,6 +52,7 @@ type SnipcartOrder struct {
Phone string `json:"shippingAddressPhone"`
TrackingNumber string `json:"trackingNumber"`
TrackingUrl string `json:"trackingUrl"`
ShippingCost string `json:"shippingFees"`
Items []SnipcartItem `json:"items"`
}
@@ -66,6 +68,26 @@ func NewSnipcartProvider(snipcartApiKey string) SnipcartProvider {
}
}
func (s *SnipcartProvider) GetOrder(token string) (*SnipcartOrder, error) {
response, err := helper.Get(orderUri+"/"+token, "Basic", s.AuthBase64, nil)
if err != nil {
return nil, err
}
if response.Status != "200 OK" {
return nil, fmt.Errorf("unexpected response received: %s", response.Status)
}
defer response.Body.Close()
var order SnipcartOrder
err = json.NewDecoder(response.Body).Decode(&order)
if err != nil {
return nil, err
}
return &order, nil
}
func (s *SnipcartProvider) GetOrders(queries map[string]string) (*SnipcartOrders, error) {
response, err := helper.Get(orderUri, "Basic", s.AuthBase64, queries)
if err != nil {
@@ -93,3 +115,12 @@ func (s *SnipcartProvider) GetOrdersByStatus(status OrderStatus) (*SnipcartOrder
return s.GetOrders(map[string]string{"status": string(status)})
}
func (o *SnipcartOrder) TokenPNGBase64() (string, error) {
img, err := qrcode.Encode("order:"+o.Token, qrcode.Medium, 128)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(img), nil
}