From 131af89f4f43c8f71f59ce35bfd864ce4daddc6c Mon Sep 17 00:00:00 2001 From: Bastian de Byl Date: Wed, 12 Apr 2023 16:56:01 -0400 Subject: [PATCH] moved enums to enums.go, added SendNotification, Notification structs & enums --- snipcart/{orderstatus.go => enums.go} | 7 ++++++ snipcart/snipcart.go | 35 ++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) rename snipcart/{orderstatus.go => enums.go} (51%) diff --git a/snipcart/orderstatus.go b/snipcart/enums.go similarity index 51% rename from snipcart/orderstatus.go rename to snipcart/enums.go index 7d1d4fb..355bd9d 100644 --- a/snipcart/orderstatus.go +++ b/snipcart/enums.go @@ -1,6 +1,7 @@ package snipcart type OrderStatus string +type NotificationType string const ( Processed OrderStatus = "Processed" @@ -10,4 +11,10 @@ const ( Pending = "Pending" Cancelled = "Cancelled" Dispatched = "Dispatched" + + Comment NotificationType = "Comment" + OrderStatusChanged = "OrderStatusChanged" + OrderShipped = "OrderShipped" + TrackingNumber = "TrackingNumber" + Invoice = "Invice" ) diff --git a/snipcart/snipcart.go b/snipcart/snipcart.go index 2bb5507..402b0a6 100644 --- a/snipcart/snipcart.go +++ b/snipcart/snipcart.go @@ -98,6 +98,23 @@ type SnipcartTax struct { Rate float64 `json:"rate"` } +type SnipcartNotification struct { + Type NotificationType `json:"type"` + DeliveryMethod string `json:"deliveryMethod"` + Message string `json:"message,omitempty"` +} + +type SnipcartNotificationResponse struct { + Id string `json:"id"` + Created time.Time `json:"creationDate"` + Type NotificationType `json:"type"` + DeliveryMethod string `json:"deliveryMethod"` + Body string `json:"body"` + Message string `json:"message"` + Subject string `json:"subject"` + SentOn time.Time `json:"sentOn"` +} + func NewClient(snipcartApiKey string) Client { return Client{ SnipcartKey: snipcartApiKey, @@ -170,7 +187,6 @@ func (s *Client) UpdateOrder(token string, orderUpdate *SnipcartOrderUpdate) (*S if response.StatusCode < 200 && response.StatusCode >= 300 { return nil, fmt.Errorf("unexpected response received: %s", response.Status) } - fmt.Println(response.Status) defer response.Body.Close() @@ -182,3 +198,20 @@ func (s *Client) UpdateOrder(token string, orderUpdate *SnipcartOrderUpdate) (*S return &responseOrder, nil } + +func (s *Client) SendNotification(token string, notification *SnipcartNotification) (*SnipcartNotificationResponse, error) { + response, err := helper.Post(orderUri+"/"+token+"/notifications", "Basic", s.AuthBase64, notification) + if err != nil { + return nil, err + } + + defer response.Body.Close() + + var responseNotification SnipcartNotificationResponse + err = json.NewDecoder(response.Body).Decode(&responseNotification) + if err != nil { + return nil, err + } + + return &responseNotification, nil +}