Compare commits

..

8 Commits

Author SHA1 Message Date
Bastian de Byl
f80f35f5d5 changed dimension types in SnipcartItem struct 2023-02-26 01:31:28 -05:00
Bastian de Byl
13bef02dcb changed weight strinbg to float64 2023-02-26 01:30:13 -05:00
Bastian de Byl
e6ffe724af added more fields to SnipcartItem struct 2023-02-26 01:28:02 -05:00
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
Bastian de Byl
3627712c9b added tracking response 2023-02-25 16:40:03 -05:00
2 changed files with 58 additions and 18 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 (
@@ -29,27 +30,34 @@ type SnipcartItem struct {
UUID string `json:"uniqueId"`
ID string `json:"id"`
Name string `json:"name"`
Quantity float64 `json:"quantity"`
Weight string `json:"weight"`
TotalWeight float64 `json:"totalWeight"`
Quantity int `json:"quantity"`
TotalWeight float64 `json:"totalWeight,omitempty"`
CustomFieldsJSON string `json:"customFieldsJson"`
Length float64 `json:"length,omitempty"`
Width float64 `json:"width,omitempty"`
Height float64 `json:"height,omitempty"`
Weight float64 `json:"weight,omitempty"`
Shippable bool `json:"shippable,omitempty"`
}
type SnipcartOrder struct {
Token string `json:"token"`
Invoice string `json:"invoiceNumber"`
Status string `json:"status"`
TotalWeight float64 `json:"totalWeight"`
Email string `json:"email"`
Name string `json:"shippingAddressName"`
Address1 string `json:"shippingAddressAddress1"`
Address2 string `json:"shippingAddressAddress2"`
City string `json:"shippingAddressCity"`
Province string `json:"shippingAddressProvince"`
Country string `json:"shippingAddressCountry"`
PostalCode string `json:"shippingAddressPostalCode"`
Phone string `json:"shippingAddressPhone"`
Items []SnipcartItem `json:"items"`
Token string `json:"token"`
Invoice string `json:"invoiceNumber"`
Status string `json:"status"`
TotalWeight float64 `json:"totalWeight"`
Email string `json:"email"`
Name string `json:"shippingAddressName"`
Address1 string `json:"shippingAddressAddress1"`
Address2 string `json:"shippingAddressAddress2"`
City string `json:"shippingAddressCity"`
Province string `json:"shippingAddressProvince"`
Country string `json:"shippingAddressCountry"`
PostalCode string `json:"shippingAddressPostalCode"`
Phone string `json:"shippingAddressPhone"`
TrackingNumber string `json:"trackingNumber"`
TrackingUrl string `json:"trackingUrl"`
ShippingCost string `json:"shippingFees"`
Items []SnipcartItem `json:"items"`
}
type SnipcartOrders struct {
@@ -64,6 +72,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 {
@@ -91,3 +119,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
}