Skip to content

Commit d0b0dd5

Browse files
authored
Merge pull request #99 from dendd1/master
support /customer-interaction/site/cart
2 parents 53e2ab5 + cc83657 commit d0b0dd5

File tree

6 files changed

+438
-0
lines changed

6 files changed

+438
-0
lines changed

client.go

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,6 +1957,173 @@ func (c *Client) CorporateCustomerEdit(customer CorporateCustomer, by string, si
19571957
return resp, status, nil
19581958
}
19591959

1960+
// ClearCart clears the current customer's shopping cart
1961+
//
1962+
// For more information see https://docs.retailcrm.ru/Developers/API/APIVersions/APIv5#post--api-v5-customer-interaction-site-cart-clear
1963+
//
1964+
// Example:
1965+
//
1966+
// var client = retailcrm.New("https://demo.url", "09jIJ")
1967+
//
1968+
// data, status, err := client.ClearCart("site_id", SiteFilter{SiteBy: "id"},
1969+
// ClearCartRequest{
1970+
// CreatedAt: time.Now().String(),
1971+
// Customer: CartCustomer{
1972+
// ID: 1,
1973+
// ExternalID: "ext_id",
1974+
// Site: "site",
1975+
// BrowserID: "browser_id",
1976+
// GaClientID: "ga_client_id",
1977+
// },
1978+
// Order: ClearCartOrder{
1979+
// ID: 1,
1980+
// ExternalID: "ext_id",
1981+
// Number: "abc123",
1982+
// },
1983+
// },
1984+
// )
1985+
//
1986+
// if err != nil {
1987+
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
1988+
// log.Fatalf("http status: %d, %s", status, apiErr.String())
1989+
// }
1990+
//
1991+
// log.Fatalf("http status: %d, error: %s", status, err)
1992+
// }
1993+
func (c *Client) ClearCart(site string, filter SiteFilter, req ClearCartRequest) (
1994+
SuccessfulResponse, int, error,
1995+
) {
1996+
var resp SuccessfulResponse
1997+
1998+
updateJSON, err := json.Marshal(&req)
1999+
if err != nil {
2000+
return SuccessfulResponse{}, 0, err
2001+
}
2002+
2003+
p := url.Values{
2004+
"cart": {string(updateJSON)},
2005+
}
2006+
2007+
params, _ := query.Values(filter)
2008+
2009+
data, status, err := c.PostRequest(fmt.Sprintf("/customer-interaction/%s/cart/clear?%s", site, params.Encode()), p)
2010+
if err != nil {
2011+
return resp, status, err
2012+
}
2013+
2014+
err = json.Unmarshal(data, &resp)
2015+
if err != nil {
2016+
return resp, status, err
2017+
}
2018+
2019+
return resp, status, nil
2020+
}
2021+
2022+
// SetCart creates or overwrites shopping cart data
2023+
//
2024+
// For more information see https://docs.retailcrm.ru/Developers/API/APIVersions/APIv5#post--api-v5-customer-interaction-site-cart-set
2025+
//
2026+
// Example:
2027+
//
2028+
// var client = retailcrm.New("https://demo.url", "09jIJ")
2029+
//
2030+
// data, status, err := client.SetCart("site_id", SiteFilter{SiteBy: "id"},
2031+
// SetCartRequest{
2032+
// ExternalID: "ext_id",
2033+
// DroppedAt: time.Now().String(),
2034+
// Link: "link",
2035+
// Customer: CartCustomer{
2036+
// ID: 1,
2037+
// ExternalID: "ext_id",
2038+
// Site: "site",
2039+
// BrowserID: "browser_id",
2040+
// GaClientID: "ga_client_id",
2041+
// },
2042+
// Items: []SetCartItem{
2043+
// {
2044+
// Quantity: 1,
2045+
// Price: 1.0,
2046+
// Offer: SetCartOffer{
2047+
// ID: 1,
2048+
// ExternalID: "ext_id",
2049+
// XMLID: "xml_id",
2050+
// },
2051+
// },
2052+
// },
2053+
// },
2054+
// )
2055+
//
2056+
// if err != nil {
2057+
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
2058+
// log.Fatalf("http status: %d, %s", status, apiErr.String())
2059+
// }
2060+
//
2061+
// log.Fatalf("http status: %d, error: %s", status, err)
2062+
// }
2063+
func (c *Client) SetCart(site string, filter SiteFilter, req SetCartRequest) (
2064+
SuccessfulResponse, int, error,
2065+
) {
2066+
var resp SuccessfulResponse
2067+
2068+
updateJSON, err := json.Marshal(&req)
2069+
if err != nil {
2070+
return SuccessfulResponse{}, 0, err
2071+
}
2072+
2073+
p := url.Values{
2074+
"cart": {string(updateJSON)},
2075+
}
2076+
2077+
params, _ := query.Values(filter)
2078+
2079+
data, status, err := c.PostRequest(fmt.Sprintf("/customer-interaction/%s/cart/set?%s", site, params.Encode()), p)
2080+
if err != nil {
2081+
return resp, status, err
2082+
}
2083+
2084+
err = json.Unmarshal(data, &resp)
2085+
if err != nil {
2086+
return resp, status, err
2087+
}
2088+
2089+
return resp, status, nil
2090+
}
2091+
2092+
// GetCart returns the current customer's shopping cart
2093+
//
2094+
// For more information see https://docs.retailcrm.ru/Developers/API/APIVersions/APIv5#get--api-v5-customer-interaction-site-cart-customerId
2095+
//
2096+
// Example:
2097+
//
2098+
// var client = retailcrm.New("https://demo.url", "09jIJ")
2099+
//
2100+
// data, status, err := client.GetCart("site_id","customer_id", GetCartFilter{ SiteBy: "code", By: "externalId"})
2101+
//
2102+
// if err != nil {
2103+
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
2104+
// log.Fatalf("http status: %d, %s", status, apiErr.String())
2105+
// }
2106+
//
2107+
// log.Fatalf("http status: %d, error: %s", status, err)
2108+
// }
2109+
func (c *Client) GetCart(site, customer string, filter GetCartFilter) (CartResponse, int, error) {
2110+
var resp CartResponse
2111+
2112+
params, _ := query.Values(filter)
2113+
2114+
data, status, err := c.GetRequest(fmt.Sprintf("/customer-interaction/%s/cart/%s?%s", site, customer, params.Encode()))
2115+
if err != nil {
2116+
return resp, status, err
2117+
}
2118+
2119+
err = json.Unmarshal(data, &resp)
2120+
if err != nil {
2121+
return resp, status, err
2122+
}
2123+
2124+
return resp, status, nil
2125+
}
2126+
19602127
// DeliveryTracking updates tracking data
19612128
//
19622129
// For more information see http://www.simla.com/docs/Developers/API/APIVersions/APIv5#post--api-v5-delivery-generic-subcode-tracking

client_test.go

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1877,6 +1877,171 @@ func TestClient_CorporateCustomerEdit(t *testing.T) {
18771877
}
18781878
}
18791879

1880+
func TestClient_ClearCart(t *testing.T) {
1881+
c := client()
1882+
1883+
site := "site_id"
1884+
filter := SiteFilter{SiteBy: "id"}
1885+
request := ClearCartRequest{
1886+
CreatedAt: time.Now().String(),
1887+
Customer: CartCustomer{
1888+
ID: 1,
1889+
ExternalID: "ext_id",
1890+
Site: "site",
1891+
BrowserID: "browser_id",
1892+
GaClientID: "ga_client_id",
1893+
},
1894+
Order: ClearCartOrder{
1895+
ID: 1,
1896+
ExternalID: "ext_id",
1897+
Number: "abc123",
1898+
},
1899+
}
1900+
1901+
defer gock.Off()
1902+
gock.New(crmURL).
1903+
Post(fmt.Sprintf("/customer-interaction/%s/cart/clear", site)).
1904+
MatchParam("siteBy", filter.SiteBy).
1905+
Reply(200).
1906+
BodyString(`{"success":true}`)
1907+
1908+
data, status, err := c.ClearCart(site, filter, request)
1909+
if err != nil {
1910+
t.Errorf("%v", err)
1911+
}
1912+
1913+
if status >= http.StatusBadRequest {
1914+
t.Errorf("(%d) %v", status, err)
1915+
}
1916+
1917+
if data.Success != true {
1918+
t.Errorf("%v", err)
1919+
}
1920+
}
1921+
1922+
func TestClient_SetCart(t *testing.T) {
1923+
c := client()
1924+
1925+
site := "site_id"
1926+
filter := SiteFilter{SiteBy: "id"}
1927+
request := SetCartRequest{
1928+
ExternalID: "ext_id",
1929+
DroppedAt: time.Now().String(),
1930+
Link: "link",
1931+
Customer: CartCustomer{
1932+
ID: 1,
1933+
ExternalID: "ext_id",
1934+
Site: "site",
1935+
BrowserID: "browser_id",
1936+
GaClientID: "ga_client_id",
1937+
},
1938+
Items: []SetCartItem{
1939+
{
1940+
Quantity: 1,
1941+
Price: 1.0,
1942+
Offer: SetCartOffer{
1943+
ID: 1,
1944+
ExternalID: "ext_id",
1945+
XMLID: "xml_id",
1946+
},
1947+
},
1948+
},
1949+
}
1950+
1951+
defer gock.Off()
1952+
gock.New(crmURL).
1953+
Post(fmt.Sprintf("/customer-interaction/%s/cart/set", site)).
1954+
MatchParam("siteBy", filter.SiteBy).
1955+
Reply(200).
1956+
BodyString(`{"success":true}`)
1957+
1958+
data, status, err := c.SetCart(site, filter, request)
1959+
if err != nil {
1960+
t.Errorf("%v", err)
1961+
}
1962+
1963+
if status >= http.StatusBadRequest {
1964+
t.Errorf("(%d) %v", status, err)
1965+
}
1966+
1967+
if data.Success != true {
1968+
t.Errorf("%v", err)
1969+
}
1970+
}
1971+
1972+
func TestClient_GetCart(t *testing.T) {
1973+
c := client()
1974+
1975+
site := "site_id"
1976+
customer := "customer_id"
1977+
filter := GetCartFilter{
1978+
SiteBy: "code",
1979+
By: "externalId",
1980+
}
1981+
1982+
expCart := Cart{
1983+
Currency: "currency",
1984+
ExternalID: "ext_id",
1985+
DroppedAt: time.Now().String(),
1986+
ClearedAt: time.Now().String(),
1987+
Link: "link",
1988+
Items: []CartItem{
1989+
{
1990+
ID: 1,
1991+
Quantity: 2,
1992+
Price: 3.0,
1993+
Offer: CartOffer{
1994+
DisplayName: "name",
1995+
ID: 1,
1996+
ExternalID: "ext_id",
1997+
XMLID: "xml_id",
1998+
Name: "name",
1999+
Article: "article",
2000+
VatRate: "vat_rate",
2001+
Properties: StringMap{
2002+
"a": "b",
2003+
"c": "d",
2004+
},
2005+
Barcode: "barcode",
2006+
},
2007+
},
2008+
},
2009+
}
2010+
2011+
cartResp := CartResponse{
2012+
SuccessfulResponse: SuccessfulResponse{Success: true},
2013+
Cart: expCart,
2014+
}
2015+
2016+
defer gock.Off()
2017+
gock.New(crmURL).
2018+
Get(fmt.Sprintf("/customer-interaction/%s/cart/%s", site, customer)).
2019+
MatchParams(map[string]string{
2020+
"siteBy": filter.SiteBy,
2021+
"by": filter.By,
2022+
}).
2023+
Reply(200).
2024+
JSON(cartResp)
2025+
2026+
data, status, err := c.GetCart(site, customer, filter)
2027+
if err != nil {
2028+
t.Errorf("%v", err)
2029+
}
2030+
2031+
if status >= http.StatusBadRequest {
2032+
t.Errorf("(%d) %v", status, err)
2033+
}
2034+
2035+
if data.Success != true {
2036+
t.Errorf("%v", err)
2037+
}
2038+
2039+
if !reflect.DeepEqual(expCart, data.Cart) {
2040+
t.Errorf("%v", err)
2041+
}
2042+
2043+
}
2044+
18802045
func TestClient_NotesNotes(t *testing.T) {
18812046
c := client()
18822047

filters.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,3 +480,18 @@ type OffersFilter struct {
480480
Ids []int `url:"ids,omitempty,brackets"`
481481
Active *int `url:"active,omitempty"`
482482
}
483+
484+
type SiteFilter struct {
485+
// SiteBy contains information about what is betrayed site id or site code.
486+
// id|code, default is code.
487+
SiteBy string `url:"siteBy,omitempty"`
488+
}
489+
490+
type GetCartFilter struct {
491+
// SiteBy contains information about what is betrayed site id or site code.
492+
// id|code, default is code.
493+
SiteBy string `url:"siteBy,omitempty"`
494+
// By contains information about what is betrayed: customer id or customer externalId.
495+
// id|externalId, default is externalId.
496+
By string `url:"by,omitempty"`
497+
}

request.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,22 @@ type DeliveryShipmentsRequest struct {
194194
Page int `url:"page,omitempty"`
195195
}
196196

197+
// ClearCartRequest type.
198+
type ClearCartRequest struct {
199+
CreatedAt string `url:"createdAt,omitempty"`
200+
Customer CartCustomer `url:"customer,omitempty"`
201+
Order ClearCartOrder `url:"order,omitempty"`
202+
}
203+
204+
// SetCartRequest type.
205+
type SetCartRequest struct {
206+
ExternalID string `url:"externalId,omitempty"`
207+
DroppedAt string `url:"droppedAt,omitempty"`
208+
Link string `url:"link,omitempty"`
209+
Customer CartCustomer `url:"customer,omitempty"`
210+
Items []SetCartItem `url:"items,omitempty"`
211+
}
212+
197213
// CostsRequest type.
198214
type CostsRequest struct {
199215
Filter CostsFilter `url:"filter,omitempty"`

0 commit comments

Comments
 (0)