mirror of
https://github.com/retailcrm/api-client-go.git
synced 2025-04-08 03:22:03 +00:00
Compare commits
4 commits
Author | SHA1 | Date | |
---|---|---|---|
c62a02aa1f | |||
|
871459c8b7 | ||
d0b0dd59d6 | |||
|
cc83657f32 |
6 changed files with 439 additions and 0 deletions
168
client.go
168
client.go
|
@ -1957,6 +1957,174 @@ func (c *Client) CorporateCustomerEdit(customer CorporateCustomer, by string, si
|
||||||
return resp, status, nil
|
return resp, status, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ClearCart clears the current customer's shopping cart
|
||||||
|
//
|
||||||
|
// For more information see https://docs.retailcrm.ru/Developers/API/APIVersions/APIv5#post--api-v5-customer-interaction-site-cart-clear
|
||||||
|
//
|
||||||
|
// Example:
|
||||||
|
//
|
||||||
|
// var client = retailcrm.New("https://demo.url", "09jIJ")
|
||||||
|
//
|
||||||
|
// data, status, err := client.ClearCart("site_id", retailcrm.SiteFilter{SiteBy: "id"},
|
||||||
|
// retailcrm.ClearCartRequest{
|
||||||
|
// ClearedAt: time.Now().String(),
|
||||||
|
// Customer: retailcrm.CartCustomer{
|
||||||
|
// ID: 1,
|
||||||
|
// ExternalID: "ext_id",
|
||||||
|
// Site: "site",
|
||||||
|
// BrowserID: "browser_id",
|
||||||
|
// GaClientID: "ga_client_id",
|
||||||
|
// },
|
||||||
|
// Order: retailcrm.ClearCartOrder{
|
||||||
|
// ID: 1,
|
||||||
|
// ExternalID: "ext_id",
|
||||||
|
// Number: "abc123",
|
||||||
|
// },
|
||||||
|
// },
|
||||||
|
// )
|
||||||
|
//
|
||||||
|
// if err != nil {
|
||||||
|
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
|
||||||
|
// log.Fatalf("http status: %d, %s", status, apiErr.String())
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// log.Fatalf("http status: %d, error: %s", status, err)
|
||||||
|
// }
|
||||||
|
func (c *Client) ClearCart(site string, filter SiteFilter, req ClearCartRequest) (
|
||||||
|
SuccessfulResponse, int, error,
|
||||||
|
) {
|
||||||
|
var resp SuccessfulResponse
|
||||||
|
|
||||||
|
updateJSON, err := json.Marshal(&req)
|
||||||
|
if err != nil {
|
||||||
|
return SuccessfulResponse{}, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
p := url.Values{
|
||||||
|
"cart": {string(updateJSON)},
|
||||||
|
}
|
||||||
|
|
||||||
|
params, _ := query.Values(filter)
|
||||||
|
|
||||||
|
data, status, err := c.PostRequest(fmt.Sprintf("/customer-interaction/%s/cart/clear?%s", site, params.Encode()), p)
|
||||||
|
if err != nil {
|
||||||
|
return resp, status, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.Unmarshal(data, &resp)
|
||||||
|
if err != nil {
|
||||||
|
return resp, status, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp, status, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetCart creates or overwrites shopping cart data
|
||||||
|
//
|
||||||
|
// For more information see https://docs.retailcrm.ru/Developers/API/APIVersions/APIv5#post--api-v5-customer-interaction-site-cart-set
|
||||||
|
//
|
||||||
|
// Example:
|
||||||
|
//
|
||||||
|
// var client = retailcrm.New("https://demo.url", "09jIJ")
|
||||||
|
//
|
||||||
|
// data, status, err := client.SetCart("site_id", retailcrm.SiteFilter{SiteBy: "id"},
|
||||||
|
// retailcrm.SetCartRequest{
|
||||||
|
// ExternalID: "ext_id",
|
||||||
|
// DroppedAt: time.Now().String(),
|
||||||
|
// Link: "link",
|
||||||
|
// Customer: retailcrm.CartCustomer{
|
||||||
|
// ID: 1,
|
||||||
|
// ExternalID: "ext_id",
|
||||||
|
// Site: "site",
|
||||||
|
// BrowserID: "browser_id",
|
||||||
|
// GaClientID: "ga_client_id",
|
||||||
|
// },
|
||||||
|
// Items: []retailcrm.SetCartItem{
|
||||||
|
// {
|
||||||
|
// Quantity: 1,
|
||||||
|
// Price: 1.0,
|
||||||
|
// Offer: retailcrm.SetCartOffer{
|
||||||
|
// ID: 1,
|
||||||
|
// ExternalID: "ext_id",
|
||||||
|
// XMLID: "xml_id",
|
||||||
|
// },
|
||||||
|
// },
|
||||||
|
// },
|
||||||
|
// },
|
||||||
|
// )
|
||||||
|
//
|
||||||
|
// if err != nil {
|
||||||
|
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
|
||||||
|
// log.Fatalf("http status: %d, %s", status, apiErr.String())
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// log.Fatalf("http status: %d, error: %s", status, err)
|
||||||
|
// }
|
||||||
|
func (c *Client) SetCart(site string, filter SiteFilter, req SetCartRequest) (
|
||||||
|
SuccessfulResponse, int, error,
|
||||||
|
) {
|
||||||
|
var resp SuccessfulResponse
|
||||||
|
|
||||||
|
updateJSON, err := json.Marshal(&req)
|
||||||
|
if err != nil {
|
||||||
|
return SuccessfulResponse{}, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
p := url.Values{
|
||||||
|
"cart": {string(updateJSON)},
|
||||||
|
}
|
||||||
|
|
||||||
|
params, _ := query.Values(filter)
|
||||||
|
|
||||||
|
data, status, err := c.PostRequest(fmt.Sprintf("/customer-interaction/%s/cart/set?%s", site, params.Encode()), p)
|
||||||
|
if err != nil {
|
||||||
|
return resp, status, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.Unmarshal(data, &resp)
|
||||||
|
if err != nil {
|
||||||
|
return resp, status, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp, status, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetCart returns the current customer's shopping cart
|
||||||
|
//
|
||||||
|
// For more information see https://docs.retailcrm.ru/Developers/API/APIVersions/APIv5#get--api-v5-customer-interaction-site-cart-customerId
|
||||||
|
//
|
||||||
|
// Example:
|
||||||
|
//
|
||||||
|
// var client = retailcrm.New("https://demo.url", "09jIJ")
|
||||||
|
//
|
||||||
|
// data, status, err := client.GetCart("site_id","customer_id",
|
||||||
|
// retailcrm.GetCartFilter{ SiteBy: "code", By: "externalId"})
|
||||||
|
//
|
||||||
|
// if err != nil {
|
||||||
|
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
|
||||||
|
// log.Fatalf("http status: %d, %s", status, apiErr.String())
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// log.Fatalf("http status: %d, error: %s", status, err)
|
||||||
|
// }
|
||||||
|
func (c *Client) GetCart(site, customer string, filter GetCartFilter) (CartResponse, int, error) {
|
||||||
|
var resp CartResponse
|
||||||
|
|
||||||
|
params, _ := query.Values(filter)
|
||||||
|
|
||||||
|
data, status, err := c.GetRequest(fmt.Sprintf("/customer-interaction/%s/cart/%s?%s", site, customer, params.Encode()))
|
||||||
|
if err != nil {
|
||||||
|
return resp, status, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.Unmarshal(data, &resp)
|
||||||
|
if err != nil {
|
||||||
|
return resp, status, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp, status, nil
|
||||||
|
}
|
||||||
|
|
||||||
// DeliveryTracking updates tracking data
|
// DeliveryTracking updates tracking data
|
||||||
//
|
//
|
||||||
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-delivery-generic-subcode-tracking
|
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-delivery-generic-subcode-tracking
|
||||||
|
|
165
client_test.go
165
client_test.go
|
@ -1877,6 +1877,171 @@ func TestClient_CorporateCustomerEdit(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestClient_ClearCart(t *testing.T) {
|
||||||
|
c := client()
|
||||||
|
|
||||||
|
site := "site_id"
|
||||||
|
filter := SiteFilter{SiteBy: "id"}
|
||||||
|
request := ClearCartRequest{
|
||||||
|
ClearedAt: time.Now().String(),
|
||||||
|
Customer: CartCustomer{
|
||||||
|
ID: 1,
|
||||||
|
ExternalID: "ext_id",
|
||||||
|
Site: "site",
|
||||||
|
BrowserID: "browser_id",
|
||||||
|
GaClientID: "ga_client_id",
|
||||||
|
},
|
||||||
|
Order: ClearCartOrder{
|
||||||
|
ID: 1,
|
||||||
|
ExternalID: "ext_id",
|
||||||
|
Number: "abc123",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
defer gock.Off()
|
||||||
|
gock.New(crmURL).
|
||||||
|
Post(fmt.Sprintf("/customer-interaction/%s/cart/clear", site)).
|
||||||
|
MatchParam("siteBy", filter.SiteBy).
|
||||||
|
Reply(200).
|
||||||
|
BodyString(`{"success":true}`)
|
||||||
|
|
||||||
|
data, status, err := c.ClearCart(site, filter, request)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("%v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if status >= http.StatusBadRequest {
|
||||||
|
t.Errorf("(%d) %v", status, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if data.Success != true {
|
||||||
|
t.Errorf("%v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestClient_SetCart(t *testing.T) {
|
||||||
|
c := client()
|
||||||
|
|
||||||
|
site := "site_id"
|
||||||
|
filter := SiteFilter{SiteBy: "id"}
|
||||||
|
request := SetCartRequest{
|
||||||
|
ExternalID: "ext_id",
|
||||||
|
DroppedAt: time.Now().String(),
|
||||||
|
Link: "link",
|
||||||
|
Customer: CartCustomer{
|
||||||
|
ID: 1,
|
||||||
|
ExternalID: "ext_id",
|
||||||
|
Site: "site",
|
||||||
|
BrowserID: "browser_id",
|
||||||
|
GaClientID: "ga_client_id",
|
||||||
|
},
|
||||||
|
Items: []SetCartItem{
|
||||||
|
{
|
||||||
|
Quantity: 1,
|
||||||
|
Price: 1.0,
|
||||||
|
Offer: SetCartOffer{
|
||||||
|
ID: 1,
|
||||||
|
ExternalID: "ext_id",
|
||||||
|
XMLID: "xml_id",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
defer gock.Off()
|
||||||
|
gock.New(crmURL).
|
||||||
|
Post(fmt.Sprintf("/customer-interaction/%s/cart/set", site)).
|
||||||
|
MatchParam("siteBy", filter.SiteBy).
|
||||||
|
Reply(200).
|
||||||
|
BodyString(`{"success":true}`)
|
||||||
|
|
||||||
|
data, status, err := c.SetCart(site, filter, request)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("%v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if status >= http.StatusBadRequest {
|
||||||
|
t.Errorf("(%d) %v", status, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if data.Success != true {
|
||||||
|
t.Errorf("%v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestClient_GetCart(t *testing.T) {
|
||||||
|
c := client()
|
||||||
|
|
||||||
|
site := "site_id"
|
||||||
|
customer := "customer_id"
|
||||||
|
filter := GetCartFilter{
|
||||||
|
SiteBy: "code",
|
||||||
|
By: "externalId",
|
||||||
|
}
|
||||||
|
|
||||||
|
expCart := Cart{
|
||||||
|
Currency: "currency",
|
||||||
|
ExternalID: "ext_id",
|
||||||
|
DroppedAt: time.Now().String(),
|
||||||
|
ClearedAt: time.Now().String(),
|
||||||
|
Link: "link",
|
||||||
|
Items: []CartItem{
|
||||||
|
{
|
||||||
|
ID: 1,
|
||||||
|
Quantity: 2,
|
||||||
|
Price: 3.0,
|
||||||
|
Offer: CartOffer{
|
||||||
|
DisplayName: "name",
|
||||||
|
ID: 1,
|
||||||
|
ExternalID: "ext_id",
|
||||||
|
XMLID: "xml_id",
|
||||||
|
Name: "name",
|
||||||
|
Article: "article",
|
||||||
|
VatRate: "vat_rate",
|
||||||
|
Properties: StringMap{
|
||||||
|
"a": "b",
|
||||||
|
"c": "d",
|
||||||
|
},
|
||||||
|
Barcode: "barcode",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
cartResp := CartResponse{
|
||||||
|
SuccessfulResponse: SuccessfulResponse{Success: true},
|
||||||
|
Cart: expCart,
|
||||||
|
}
|
||||||
|
|
||||||
|
defer gock.Off()
|
||||||
|
gock.New(crmURL).
|
||||||
|
Get(fmt.Sprintf("/customer-interaction/%s/cart/%s", site, customer)).
|
||||||
|
MatchParams(map[string]string{
|
||||||
|
"siteBy": filter.SiteBy,
|
||||||
|
"by": filter.By,
|
||||||
|
}).
|
||||||
|
Reply(200).
|
||||||
|
JSON(cartResp)
|
||||||
|
|
||||||
|
data, status, err := c.GetCart(site, customer, filter)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("%v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if status >= http.StatusBadRequest {
|
||||||
|
t.Errorf("(%d) %v", status, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if data.Success != true {
|
||||||
|
t.Errorf("%v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(expCart, data.Cart) {
|
||||||
|
t.Errorf("%v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func TestClient_NotesNotes(t *testing.T) {
|
func TestClient_NotesNotes(t *testing.T) {
|
||||||
c := client()
|
c := client()
|
||||||
|
|
||||||
|
|
15
filters.go
15
filters.go
|
@ -480,3 +480,18 @@ type OffersFilter struct {
|
||||||
Ids []int `url:"ids,omitempty,brackets"`
|
Ids []int `url:"ids,omitempty,brackets"`
|
||||||
Active *int `url:"active,omitempty"`
|
Active *int `url:"active,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SiteFilter struct {
|
||||||
|
// SiteBy contains information about what is betrayed site id or site code.
|
||||||
|
// id|code, default is code.
|
||||||
|
SiteBy string `url:"siteBy,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetCartFilter struct {
|
||||||
|
// SiteBy contains information about what is betrayed site id or site code.
|
||||||
|
// id|code, default is code.
|
||||||
|
SiteBy string `url:"siteBy,omitempty"`
|
||||||
|
// By contains information about what is betrayed: customer id or customer externalId.
|
||||||
|
// id|externalId, default is externalId.
|
||||||
|
By string `url:"by,omitempty"`
|
||||||
|
}
|
||||||
|
|
16
request.go
16
request.go
|
@ -194,6 +194,22 @@ type DeliveryShipmentsRequest struct {
|
||||||
Page int `url:"page,omitempty"`
|
Page int `url:"page,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ClearCartRequest type.
|
||||||
|
type ClearCartRequest struct {
|
||||||
|
ClearedAt string `url:"clearedAt,omitempty"`
|
||||||
|
Customer CartCustomer `url:"customer,omitempty"`
|
||||||
|
Order ClearCartOrder `url:"order,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetCartRequest type.
|
||||||
|
type SetCartRequest struct {
|
||||||
|
ExternalID string `url:"externalId,omitempty"`
|
||||||
|
DroppedAt string `url:"droppedAt,omitempty"`
|
||||||
|
Link string `url:"link,omitempty"`
|
||||||
|
Customer CartCustomer `url:"customer,omitempty"`
|
||||||
|
Items []SetCartItem `url:"items,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
// CostsRequest type.
|
// CostsRequest type.
|
||||||
type CostsRequest struct {
|
type CostsRequest struct {
|
||||||
Filter CostsFilter `url:"filter,omitempty"`
|
Filter CostsFilter `url:"filter,omitempty"`
|
||||||
|
|
|
@ -407,6 +407,12 @@ type ProductsPropertiesResponse struct {
|
||||||
Properties []Property `json:"properties,omitempty"`
|
Properties []Property `json:"properties,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CartResponse type.
|
||||||
|
type CartResponse struct {
|
||||||
|
SuccessfulResponse
|
||||||
|
Cart Cart `json:"cart"`
|
||||||
|
}
|
||||||
|
|
||||||
// DeliveryShipmentsResponse type.
|
// DeliveryShipmentsResponse type.
|
||||||
type DeliveryShipmentsResponse struct {
|
type DeliveryShipmentsResponse struct {
|
||||||
Success bool `json:"success"`
|
Success bool `json:"success"`
|
||||||
|
|
69
types.go
69
types.go
|
@ -247,6 +247,15 @@ type CorporateCustomerContactCustomer struct {
|
||||||
Site string `json:"site,omitempty"`
|
Site string `json:"site,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CartCustomer type.
|
||||||
|
type CartCustomer struct {
|
||||||
|
ID int `json:"id,omitempty"`
|
||||||
|
ExternalID string `json:"externalId,omitempty"`
|
||||||
|
Site string `json:"site,omitempty"`
|
||||||
|
BrowserID string `json:"browserId,omitempty"`
|
||||||
|
GaClientID string `json:"gaClientId,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
type Company struct {
|
type Company struct {
|
||||||
ID int `json:"id,omitempty"`
|
ID int `json:"id,omitempty"`
|
||||||
IsMain bool `json:"isMain,omitempty"`
|
IsMain bool `json:"isMain,omitempty"`
|
||||||
|
@ -399,6 +408,13 @@ type SerializedOrderLink struct {
|
||||||
Orders []LinkedOrder `json:"orders,omitempty"`
|
Orders []LinkedOrder `json:"orders,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ClearCartOrder type.
|
||||||
|
type ClearCartOrder struct {
|
||||||
|
ID int `json:"id,omitempty"`
|
||||||
|
ExternalID string `json:"externalID,omitempty"`
|
||||||
|
Number string `json:"number,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
// ClientID type.
|
// ClientID type.
|
||||||
type ClientID struct {
|
type ClientID struct {
|
||||||
Value string `json:"value"`
|
Value string `json:"value"`
|
||||||
|
@ -469,6 +485,59 @@ type OrderDeliveryData struct {
|
||||||
AdditionalFields map[string]interface{}
|
AdditionalFields map[string]interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetCartItem type.
|
||||||
|
type SetCartItem struct {
|
||||||
|
Quantity float64 `json:"quantity,omitempty"`
|
||||||
|
Price float64 `json:"price,omitempty"`
|
||||||
|
Offer SetCartOffer `json:"offer,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetCartOffer type.
|
||||||
|
type SetCartOffer struct {
|
||||||
|
ID int `json:"id,omitempty"`
|
||||||
|
ExternalID string `json:"externalID,omitempty"`
|
||||||
|
XMLID string `json:"xmlId,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cart type.
|
||||||
|
type Cart struct {
|
||||||
|
Currency string `json:"currency,omitempty"`
|
||||||
|
ExternalID string `json:"externalId,omitempty"`
|
||||||
|
DroppedAt string `json:"droppedAt,omitempty"`
|
||||||
|
ClearedAt string `json:"clearedAt,omitempty"`
|
||||||
|
Link string `json:"link,omitempty"`
|
||||||
|
Items []CartItem `json:"items,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// CartItem type.
|
||||||
|
type CartItem struct {
|
||||||
|
ID int `json:"id,omitempty"`
|
||||||
|
Quantity float64 `json:"quantity,omitempty"`
|
||||||
|
Price float64 `json:"price,omitempty"`
|
||||||
|
Offer CartOffer `json:"offer,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// CartOffer type.
|
||||||
|
type CartOffer struct {
|
||||||
|
DisplayName string `json:"displayName,omitempty"`
|
||||||
|
ID int `json:"id,omitempty"`
|
||||||
|
ExternalID string `json:"externalId,omitempty"`
|
||||||
|
XMLID string `json:"xmlId,omitempty"`
|
||||||
|
Name string `json:"name,omitempty"`
|
||||||
|
Article string `json:"article,omitempty"`
|
||||||
|
VatRate string `json:"vatRate,omitempty"`
|
||||||
|
Properties StringMap `json:"properties,omitempty"`
|
||||||
|
Unit CartUnit `json:"unit,omitempty"`
|
||||||
|
Barcode string `json:"barcode,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// CartUnit type.
|
||||||
|
type CartUnit struct {
|
||||||
|
Code string `json:"code"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Sym string `json:"sym"`
|
||||||
|
}
|
||||||
|
|
||||||
// UnmarshalJSON method.
|
// UnmarshalJSON method.
|
||||||
func (v *OrderDeliveryData) UnmarshalJSON(b []byte) error {
|
func (v *OrderDeliveryData) UnmarshalJSON(b []byte) error {
|
||||||
var additionalData map[string]interface{}
|
var additionalData map[string]interface{}
|
||||||
|
|
Loading…
Add table
Reference in a new issue