diff --git a/v5/client.go b/v5/client.go index ab17d56..379b96d 100644 --- a/v5/client.go +++ b/v5/client.go @@ -640,3 +640,71 @@ func (c *Client) NoteDelete(id int) (*SucessfulResponse, int, error) { return &resp, status, err } + +// PaymentCreate method +func (c *Client) PaymentCreate(payment Payment, site ...string) (*CreateResponse, int, error) { + var resp CreateResponse + + paymentJson, _ := json.Marshal(&payment) + + p := url.Values{ + "payment": {string(paymentJson[:])}, + } + + fillSite(&p, site) + + data, status, err := c.PostRequest(fmt.Sprintf("%s/orders/payments/create", versionedPrefix), p) + if err != nil { + return &resp, status, err + } + + err = json.Unmarshal(data, &resp) + + return &resp, status, err +} + +// PaymentDelete method +func (c *Client) PaymentDelete(id int) (*SucessfulResponse, int, error) { + var resp SucessfulResponse + + p := url.Values{ + "id": {string(id)}, + } + + data, status, err := c.PostRequest(fmt.Sprintf("%s/orders/payments/%d/delete", versionedPrefix, id), p) + if err != nil { + return &resp, status, err + } + + err = json.Unmarshal(data, &resp) + + return &resp, status, err +} + +// PaymentEdit method +func (c *Client) PaymentEdit(payment Payment, by string, site ...string) (*SucessfulResponse, int, error) { + var resp SucessfulResponse + var uid = strconv.Itoa(payment.Id) + var context = checkBy(by) + + if context == "externalId" { + uid = payment.ExternalId + } + + paymentJson, _ := json.Marshal(&payment) + + p := url.Values{ + "payment": {string(paymentJson[:])}, + } + + fillSite(&p, site) + + data, status, err := c.PostRequest(fmt.Sprintf("%s/orders/payments/%s/edit", versionedPrefix, uid), p) + if err != nil { + return &resp, status, err + } + + err = json.Unmarshal(data, &resp) + + return &resp, status, err +} diff --git a/v5/types.go b/v5/types.go index 673e1aa..6f7a9f3 100644 --- a/v5/types.go +++ b/v5/types.go @@ -378,3 +378,19 @@ type Note struct { CreatedAt string `json:"createdAt,omitempty"` Customer *Customer `json:"customer,omitempty"` } + +/* + Payments related types +*/ + +// Payment type +type Payment struct { + Id int `json:"id,omitempty"` + ExternalId string `json:"externalId,omitempty"` + PaidAt string `json:"paidAt,omitempty"` + Amount float32 `json:"amount,omitempty"` + Comment string `json:"comment,omitempty"` + Status string `json:"status,omitempty"` + Type string `json:"type,omitempty"` + Order *Order `json:"order,omitempty"` +} diff --git a/v5_tests/payments_test.go b/v5_tests/payments_test.go new file mode 100644 index 0000000..ad13eaf --- /dev/null +++ b/v5_tests/payments_test.go @@ -0,0 +1,98 @@ +package v5_tests + +import ( + "fmt" + "net/http" + "testing" + + "github.com/retailcrm/api-client-go/v5" +) + +func TestClient_PaymentCreateEditDelete(t *testing.T) { + c := client() + + order := v5.Order{ + FirstName: "Понтелей", + LastName: "Турбин", + Patronymic: "Аристархович", + ExternalId: RandomString(8), + Email: fmt.Sprintf("%s@example.com", RandomString(8)), + } + + createOrderResponse, status, err := c.OrderCreate(order) + if err != nil { + t.Errorf("%s", err) + t.Fail() + } + + if status != http.StatusCreated { + t.Errorf("%s", err) + t.Fail() + } + + if createOrderResponse.Success != true { + t.Errorf("%s", err) + t.Fail() + } + + f := v5.Payment{ + Order: &v5.Order{ + Id: createOrderResponse.Id, + }, + Amount: 300, + Type: "cash", + } + + paymentCreateResponse, status, err := c.PaymentCreate(f) + if err != nil { + t.Errorf("%s", err) + t.Fail() + } + + if status != http.StatusCreated { + t.Errorf("%s", err) + t.Fail() + } + + if paymentCreateResponse.Success != true { + t.Errorf("%s", err) + t.Fail() + } + + k := v5.Payment{ + Id: paymentCreateResponse.Id, + Amount: 500, + } + + paymentEditResponse, status, err := c.PaymentEdit(k, "id") + if err != nil { + t.Errorf("%s", err) + t.Fail() + } + + if status != http.StatusOK { + t.Errorf("%s", err) + t.Fail() + } + + if paymentEditResponse.Success != true { + t.Errorf("%s", err) + t.Fail() + } + + paymentDeleteResponse, status, err := c.PaymentDelete(paymentCreateResponse.Id) + if err != nil { + t.Errorf("%s", err) + t.Fail() + } + + if status != http.StatusOK { + t.Errorf("%s", err) + t.Fail() + } + + if paymentDeleteResponse.Success != true { + t.Errorf("%s", err) + t.Fail() + } +}