Skip to content

Commit 2d4e86e

Browse files
committed
added better error handling for shipping_rates.json api, added shop public URL handling for shipping_rates.json api
1 parent 0879f3c commit 2d4e86e

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

shopify/shopify.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"strconv"
2929
"strings"
3030
"time"
31+
3132
jww "github.com/spf13/jwalterweatherman"
3233
)
3334

@@ -39,13 +40,15 @@ const (
3940
type Shopify struct {
4041
shopifyDomain string
4142
shopifySecretToken string
43+
// we need the public Store URL because with Redirect shipping_rates.json doesn't work
44+
shopifyPublicURL string
4245

4346
Products []Product
4447
}
4548

4649
// NewClient inits shopify client
47-
func NewClient(domain string, secrettoken string) Shopify {
48-
shop := Shopify{shopifyDomain: domain, shopifySecretToken: secrettoken}
50+
func NewClient(domain string, secrettoken string, publicURL string) Shopify {
51+
shop := Shopify{shopifyDomain: domain, shopifySecretToken: secrettoken, shopifyPublicURL: publicURL}
4952
return shop
5053
}
5154

@@ -191,11 +194,10 @@ func (shopifyClient *Shopify) ShippingOptions(order Order) ([]ShippingRate, erro
191194

192195
urlStr = urlStr + v.Encode()
193196

194-
completeURL = fmt.Sprintf("https://%s%s%s", shopifyClient.shopifyDomain, baseURLString, urlStr)
197+
completeURL = fmt.Sprintf("%s%s", shopifyClient.shopifyPublicURL, urlStr)
195198
jww.INFO.Printf("[ShippingOptions] - Request URL: %s", completeURL)
196199

197200
r, err = http.NewRequest("GET", completeURL, nil)
198-
199201
resp, err = client.Do(r)
200202

201203
defer resp.Body.Close()
@@ -213,6 +215,11 @@ func (shopifyClient *Shopify) ShippingOptions(order Order) ([]ShippingRate, erro
213215
return shopifyResponse.ShippingRates, err
214216
}
215217

218+
if shopifyResponse.Error != nil {
219+
genericError := errors.New(strings.Join(shopifyResponse.Error, ", "))
220+
return shopifyResponse.ShippingRates, genericError
221+
}
222+
216223
// Address not supported error handling
217224
if shopifyResponse.Country != nil || shopifyResponse.Zip != nil || shopifyResponse.Province != nil {
218225
var errorsArray []string

shopify/shopifyorder.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,4 +308,5 @@ type ShippingRatesResponse struct {
308308
Zip []string `json:"zip,omitempty"`
309309
Province []string `json:"province,omitempty"`
310310
Country []string `json:"country,omitempty"`
311+
Error []string `json:"error,omitempty"`
311312
}

0 commit comments

Comments
 (0)