From 36ca8d6f8334c2018ba2aa7d2c60c5c074503859 Mon Sep 17 00:00:00 2001 From: Bastian de Byl Date: Sun, 26 Feb 2023 20:36:12 -0500 Subject: [PATCH] added initial webhook main function --- go.mod | 28 +++++++ snipcart/snipcart.go | 6 +- snipcart/webhook/config.go | 33 ++++++++ webhook.go | 162 +++++++++++++++++++++++++++++++++++++ 4 files changed, 227 insertions(+), 2 deletions(-) create mode 100644 snipcart/webhook/config.go create mode 100644 webhook.go diff --git a/go.mod b/go.mod index 4433bd0..38d1a69 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,33 @@ go 1.20 require ( github.com/debyltech/go-helpers v1.1.0 + github.com/debyltech/go-shippr v0.1.0 + github.com/gin-gonic/gin v1.9.0 github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e ) + +require ( + github.com/bytedance/sonic v1.8.0 // indirect + github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.11.2 // indirect + github.com/goccy/go-json v0.10.0 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/cpuid/v2 v2.0.9 // indirect + github.com/leodido/go-urn v1.2.1 // indirect + github.com/mattn/go-isatty v0.0.17 // indirect + github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pelletier/go-toml/v2 v2.0.6 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.2.9 // indirect + golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect + golang.org/x/crypto v0.5.0 // indirect + golang.org/x/net v0.7.0 // indirect + golang.org/x/sys v0.5.0 // indirect + golang.org/x/text v0.7.0 // indirect + google.golang.org/protobuf v1.28.1 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/snipcart/snipcart.go b/snipcart/snipcart.go index 677d1fd..e7bc1a8 100644 --- a/snipcart/snipcart.go +++ b/snipcart/snipcart.go @@ -50,18 +50,20 @@ type SnipcartOrder struct { Token string `json:"token"` Invoice string `json:"invoiceNumber"` Subtotal float64 `json:"subtotal,omitempty"` + Currency string `json:"currency,omitempty"` Total float64 `json:"grandTotal,omitempty"` Status string `json:"status"` TotalWeight float64 `json:"totalWeight"` - Email string `json:"email"` Name string `json:"shippingAddressName"` + Company string `json:"shippingAddressCompanyName"` 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"` + Phone string `json:"shippingAddressPhone,omitempty"` + Email string `json:"email,omitempty"` TrackingNumber string `json:"trackingNumber"` TrackingUrl string `json:"trackingUrl"` ShippingCost float64 `json:"shippingFees"` diff --git a/snipcart/webhook/config.go b/snipcart/webhook/config.go new file mode 100644 index 0000000..976b9f0 --- /dev/null +++ b/snipcart/webhook/config.go @@ -0,0 +1,33 @@ +package webhook + +import ( + "bytes" + "encoding/json" + "io/ioutil" + + "github.com/debyltech/go-shippr/shippo" +) + +type Config struct { + ShippoApiKey string `json:"shippo_api_key"` + WeightUnit string `json:"weight_unit"` + DimensionUnit string `json:"dimension_unit"` + ManufactureCountry string `json:"manufacture_country"` + SenderAddress shippo.Address `json:"sender_address"` + DefaultParcel shippo.Parcel `json:"default_parcel"` +} + +func NewConfigFromFile(filePath string) (*Config, error) { + configBytes, err := ioutil.ReadFile(filePath) + if err != nil { + return nil, err + } + + var config Config + err = json.NewDecoder(bytes.NewBuffer(configBytes)).Decode(&config) + if err != nil { + return nil, err + } + + return &config, nil +} diff --git a/webhook.go b/webhook.go new file mode 100644 index 0000000..cd6371b --- /dev/null +++ b/webhook.go @@ -0,0 +1,162 @@ +package main + +import ( + "encoding/json" + "errors" + "flag" + "fmt" + "log" + "net/http" + "strconv" + "time" + + "github.com/debyltech/go-shippr/shippo" + "github.com/debyltech/go-snipcart/snipcart" + "github.com/debyltech/go-snipcart/snipcart/webhook" + "github.com/gin-gonic/gin" +) + +const ( + ValidateUrl string = "https://app.snipcart.com/api/requestvalidation/" +) + +type ShippingWebhookEvent struct { + EventName string `json:"eventName"` + CreatedOn time.Time `json:"createdOn"` + Order snipcart.SnipcartOrder `json:"content"` +} + +type ShippingRatesResponse struct { + Cost float64 `json:"cost"` + Description string `json:"description"` + DeliveryDays int `json:"guaranteedDaysToDelivery"` +} + +func ValidateWebhook(token string) error { + validateRequest, err := http.Get(ValidateUrl + token) + if err != nil { + return err + } + + if validateRequest.StatusCode < 200 || validateRequest.StatusCode >= 300 { + return errors.New("non-2XX response received") + } + + return nil +} + +func HandleShippingRates(config *webhook.Config, shippoClient *shippo.Client) gin.HandlerFunc { + fn := func(c *gin.Context) { + err := ValidateWebhook(c.GetHeader("X-Snipcart-RequestToken")) + if err != nil { + c.AbortWithError(http.StatusBadRequest, err) + return + } + + var event ShippingWebhookEvent + err = json.NewDecoder(c.Request.Body).Decode(&event) + if err != nil { + c.AbortWithError(http.StatusInternalServerError, err) + return + } + + defer c.Request.Body.Close() + + var lineItems []shippo.LineItem + for _, v := range event.Order.Items { + lineItems = append(lineItems, shippo.LineItem{ + Quantity: v.Quantity, + TotalPrice: fmt.Sprintf("%.2f", v.TotalPrice), + Currency: event.Order.Currency, + Weight: fmt.Sprintf("%.2f", v.Weight), + WeightUnit: config.WeightUnit, + Title: v.Name, + ManufactureCountry: config.ManufactureCountry, + Sku: v.ID, + }) + } + + parcel := config.DefaultParcel + parcel.WeightUnit = config.WeightUnit + parcel.DistanceUnit = config.DimensionUnit + parcel.Weight = fmt.Sprintf("%.2f", event.Order.TotalWeight) + + rateRequest := shippo.RateRequest{ + AddressFrom: shippo.Address{ + Name: config.SenderAddress.Name, + Address1: config.SenderAddress.Address1, + Address2: config.SenderAddress.Address2, + City: config.SenderAddress.City, + State: config.SenderAddress.State, + Country: config.SenderAddress.Country, + PostalCode: config.SenderAddress.PostalCode, + }, + AddressTo: shippo.Address{ + Name: event.Order.Name, + Company: event.Order.Company, + Address1: event.Order.Address1, + Address2: event.Order.Address2, + City: event.Order.City, + Country: event.Order.Country, + State: event.Order.Province, + PostalCode: event.Order.PostalCode, + Phone: event.Order.Phone, + Email: event.Order.Email, + }, + LineItems: lineItems, + Parcel: parcel, + } + + rateResponse, err := shippoClient.GenerateRates(rateRequest) + if err != nil { + c.AbortWithError(http.StatusBadRequest, err) + return + } + + var rates []ShippingRatesResponse + for _, v := range rateResponse.Rates { + cost, err := strconv.ParseFloat(v.Amount, 64) + if err != nil { + c.AbortWithError(http.StatusInternalServerError, err) + return + } + rates = append(rates, ShippingRatesResponse{ + Cost: cost, + Description: v.Title, + DeliveryDays: v.EstimatedDays, + }) + } + + c.JSON(http.StatusOK, rates) + } + + return fn +} + +func main() { + configPath := flag.String("config", "", "path to config.json") + flag.Parse() + + if *configPath == "" { + log.Fatal("config path not defined") + } + + config, err := webhook.NewConfigFromFile(*configPath) + if err != nil { + log.Fatal(err) + } + + shippoClient := shippo.NewClient(config.ShippoApiKey) + + r := gin.Default() + + api := r.Group("/api") + { + v1 := api.Group("/v1") + { + v1.POST("/shipping", HandleShippingRates(config, &shippoClient)) + } + } + + r.Run("localhost:8081") +}