This commit is contained in:
Alex Lushpai 2018-01-31 18:01:05 +03:00
parent f0d7c71d0e
commit c94dc5d8ea
3 changed files with 182 additions and 0 deletions

View file

@ -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
}

View file

@ -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"`
}

98
v5_tests/payments_test.go Normal file
View file

@ -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()
}
}