|
|
|
|
@@ -5,6 +5,7 @@ import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
helper "github.com/debyltech/go-helpers/json"
|
|
|
|
|
@@ -12,13 +13,17 @@ import (
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
defaultLimit = 50
|
|
|
|
|
apiUri = "https://app.snipcart.com"
|
|
|
|
|
ordersPath = "/api/orders"
|
|
|
|
|
defaultLimit = 50
|
|
|
|
|
apiUri = "https://app.snipcart.com"
|
|
|
|
|
ordersPath = "/api/orders"
|
|
|
|
|
productsPath = "/api/products"
|
|
|
|
|
validationPath = "/api/requestvalidation/"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
orderUri = apiUri + ordersPath
|
|
|
|
|
orderUri = apiUri + ordersPath
|
|
|
|
|
productsUri = apiUri + productsPath
|
|
|
|
|
validationUri = apiUri + validationPath
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
|
@@ -83,6 +88,7 @@ type SnipcartOrderUpdate struct {
|
|
|
|
|
PaymentStatus string `json:"paymentStatus,omitempty"`
|
|
|
|
|
TrackingNumber string `json:"trackingNumber,omitempty"`
|
|
|
|
|
TrackingUrl string `json:"trackingUrl,omitempty"`
|
|
|
|
|
ShippingRateId string `json:"shippingRateUserDefinedId,omitempty"`
|
|
|
|
|
Metadata any `json:"metadata,omitempty"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -115,8 +121,52 @@ type SnipcartNotificationResponse struct {
|
|
|
|
|
SentOn time.Time `json:"sentOn"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewClient(snipcartApiKey string) Client {
|
|
|
|
|
return Client{
|
|
|
|
|
type SnipcartProductVariant struct {
|
|
|
|
|
Stock int `json:"stock"`
|
|
|
|
|
Variation []any `json:"variation"`
|
|
|
|
|
AllowBackorder bool `json:"allowOutOfStockPurchases"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type SnipcartProductCustomField struct {
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
Placeholder string `json:"placeholder"`
|
|
|
|
|
DisplayValue string `json:"displayValue"`
|
|
|
|
|
Type string `json:"type"`
|
|
|
|
|
Options string `json:"options"`
|
|
|
|
|
Required bool `json:"required"`
|
|
|
|
|
Value string `json:"value"`
|
|
|
|
|
Operation float64 `json:"operation"`
|
|
|
|
|
OptionsArray []string `json:"optionsArray"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type SnipcartProduct struct {
|
|
|
|
|
Token string `json:"id"`
|
|
|
|
|
Id string `json:"userDefinedId"`
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
Stock int `json:"stock"`
|
|
|
|
|
TotalStock int `json:"totalStock"`
|
|
|
|
|
AllowBackorder bool `json:"allowOutOfStockPurchases"`
|
|
|
|
|
CustomFields []SnipcartProductCustomField `json:"customFields"`
|
|
|
|
|
Variants []SnipcartProductVariant `json:"variants"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type SnipcartProductsResponse struct {
|
|
|
|
|
Keywords string `json:"keywords"`
|
|
|
|
|
UserDefinedId string `json:"userDefinedId"`
|
|
|
|
|
Archived bool `json:"archived"`
|
|
|
|
|
From time.Time `json:"from"`
|
|
|
|
|
To time.Time `json:"to"`
|
|
|
|
|
OrderBy string `json:"orderBy"`
|
|
|
|
|
Paginated bool `json:"hasMoreResults"`
|
|
|
|
|
TotalItems int `json:"totalItems"`
|
|
|
|
|
Offset int `json:"offset"`
|
|
|
|
|
Limit int `json:"limit"`
|
|
|
|
|
Sort []any `json:"sort"`
|
|
|
|
|
Items []SnipcartProduct `json:"items"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewClient(snipcartApiKey string) *Client {
|
|
|
|
|
return &Client{
|
|
|
|
|
SnipcartKey: snipcartApiKey,
|
|
|
|
|
AuthBase64: base64.StdEncoding.EncodeToString([]byte(snipcartApiKey + ":")),
|
|
|
|
|
}
|
|
|
|
|
@@ -215,3 +265,71 @@ func (s *Client) SendNotification(token string, notification *SnipcartNotificati
|
|
|
|
|
|
|
|
|
|
return &responseNotification, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Client) ValidateWebhook(token string) error {
|
|
|
|
|
validateRequest, err := http.NewRequest("GET", validationUri+token, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
|
|
|
|
|
|
auth := base64.StdEncoding.EncodeToString([]byte(s.SnipcartKey + ":"))
|
|
|
|
|
validateRequest.Header.Set("Authorization", fmt.Sprintf("Basic %s", auth))
|
|
|
|
|
validateRequest.Header.Set("Accept", "application/json")
|
|
|
|
|
|
|
|
|
|
validateResponse, err := client.Do(validateRequest)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("error validating webhook: %s", err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if validateResponse.StatusCode < 200 || validateResponse.StatusCode >= 300 {
|
|
|
|
|
return fmt.Errorf("non-2XX status code for validating webhook: %d", validateResponse.StatusCode)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Client) GetProducts(queries map[string]string) (*SnipcartProductsResponse, error) {
|
|
|
|
|
response, err := helper.Get(productsUri, "Basic", s.AuthBase64, queries)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if response.StatusCode < 200 && response.StatusCode >= 300 {
|
|
|
|
|
return nil, fmt.Errorf("unexpected response received: %s", response.Status)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
|
|
var products SnipcartProductsResponse
|
|
|
|
|
err = json.NewDecoder(response.Body).Decode(&products)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &products, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Client) GetProductById(id string) (*SnipcartProduct, error) {
|
|
|
|
|
response, err := helper.Get(productsUri, "Basic", s.AuthBase64, map[string]string{"userDefinedId": id})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if response.StatusCode < 200 && response.StatusCode >= 300 {
|
|
|
|
|
return nil, fmt.Errorf("unexpected response received: %s", response.Status)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
|
|
var products SnipcartProductsResponse
|
|
|
|
|
err = json.NewDecoder(response.Body).Decode(&products)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(products.Items) < 1 {
|
|
|
|
|
return nil, fmt.Errorf("no products with id '%s'", id)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &products.Items[0], nil
|
|
|
|
|
}
|
|
|
|
|
|