add webhook util for onpoint
This commit is contained in:
		
							parent
							
								
									5f58965975
								
							
						
					
					
						commit
						26390c4633
					
				| 
						 | 
					@ -11,6 +11,9 @@ const (
 | 
				
			||||||
	headerXAPIKey    = "x-api-key"
 | 
						headerXAPIKey    = "x-api-key"
 | 
				
			||||||
	headerXTimestamp = "x-timestamp"
 | 
						headerXTimestamp = "x-timestamp"
 | 
				
			||||||
	headerXSignature = "x-signature"
 | 
						headerXSignature = "x-signature"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						webhookEventUpdateOrderStatus = "update_order_status"
 | 
				
			||||||
 | 
						webhookEventUpdateInventory   = "update_inventory"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var (
 | 
					var (
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,6 +1,10 @@
 | 
				
			||||||
package onpoint
 | 
					package onpoint
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import "time"
 | 
					import (
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/Selly-Modules/3pl/util/pjson"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*
 | 
					/*
 | 
				
			||||||
 * Request payload
 | 
					 * Request payload
 | 
				
			||||||
| 
						 | 
					@ -55,13 +59,6 @@ type CancelOrderRequest struct {
 | 
				
			||||||
 * WEBHOOK ONPOINT
 | 
					 * WEBHOOK ONPOINT
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// WebhookPayload ...
 | 
					 | 
				
			||||||
type WebhookPayload struct {
 | 
					 | 
				
			||||||
	Event       string      `json:"event"`
 | 
					 | 
				
			||||||
	RequestedAt time.Time   `json:"requested_at"`
 | 
					 | 
				
			||||||
	Data        interface{} `json:"data"`
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// WebhookDataUpdateInventory ...
 | 
					// WebhookDataUpdateInventory ...
 | 
				
			||||||
type WebhookDataUpdateInventory struct {
 | 
					type WebhookDataUpdateInventory struct {
 | 
				
			||||||
	Sku               string    `json:"sku"`
 | 
						Sku               string    `json:"sku"`
 | 
				
			||||||
| 
						 | 
					@ -81,3 +78,40 @@ type WebhookDataUpdateOrderStatus struct {
 | 
				
			||||||
	DeliveryStatus   string    `json:"delivery_status"`
 | 
						DeliveryStatus   string    `json:"delivery_status"`
 | 
				
			||||||
	UpdatedAt        time.Time `json:"updated_at"`
 | 
						UpdatedAt        time.Time `json:"updated_at"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// WebhookPayload ...
 | 
				
			||||||
 | 
					type WebhookPayload struct {
 | 
				
			||||||
 | 
						Event       string      `json:"event"`
 | 
				
			||||||
 | 
						RequestedAt time.Time   `json:"requested_at"`
 | 
				
			||||||
 | 
						Data        interface{} `json:"data"`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// GetDataEventUpdateOrderStatus ...
 | 
				
			||||||
 | 
					func (p WebhookPayload) GetDataEventUpdateOrderStatus() (data *WebhookDataUpdateOrderStatus, ok bool) {
 | 
				
			||||||
 | 
						if p.Event != webhookEventUpdateOrderStatus {
 | 
				
			||||||
 | 
							return nil, false
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						b, err := pjson.Marshal(p.Data)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, false
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if err = pjson.Unmarshal(b, &data); err != nil {
 | 
				
			||||||
 | 
							return nil, false
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return data, true
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// GetDataEventUpdateInventory ...
 | 
				
			||||||
 | 
					func (p WebhookPayload) GetDataEventUpdateInventory() (data *WebhookDataUpdateInventory, ok bool) {
 | 
				
			||||||
 | 
						if p.Event != webhookEventUpdateInventory {
 | 
				
			||||||
 | 
							return nil, false
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						b, err := pjson.Marshal(p.Data)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, false
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if err = pjson.Unmarshal(b, &data); err != nil {
 | 
				
			||||||
 | 
							return nil, false
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return data, true
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,15 @@
 | 
				
			||||||
 | 
					package pjson
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"encoding/json"
 | 
				
			||||||
 | 
						"log"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Marshal ...
 | 
				
			||||||
 | 
					func Marshal(data interface{}) ([]byte, error) {
 | 
				
			||||||
 | 
						b, err := json.Marshal(data)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							log.Printf("3pl/util/pjson.Marshal: err %v, payload %v", err, data)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return b, err
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -2,18 +2,14 @@ package pjson
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"encoding/json"
 | 
						"encoding/json"
 | 
				
			||||||
 | 
						"log"
 | 
				
			||||||
	"github.com/Selly-Modules/logger"
 | 
					 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Unmarshal ...
 | 
					// Unmarshal ...
 | 
				
			||||||
func Unmarshal(b []byte, resultP interface{}) error {
 | 
					func Unmarshal(b []byte, resultP interface{}) error {
 | 
				
			||||||
	err := json.Unmarshal(b, resultP)
 | 
						err := json.Unmarshal(b, resultP)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		logger.Error("pjson.Unmarshal", logger.LogData{
 | 
							log.Printf("3pl/util/pjson.Unmarshal: err %v, payload %s", err, string(b))
 | 
				
			||||||
			"raw": string(b),
 | 
					 | 
				
			||||||
			"err": err.Error(),
 | 
					 | 
				
			||||||
		})
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return err
 | 
						return err
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue