-
Notifications
You must be signed in to change notification settings - Fork 5
/
customer.go
73 lines (62 loc) · 2.3 KB
/
customer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package zooz
import (
"context"
"encoding/json"
"fmt"
)
// CustomerClient is a client for work with Customer entity.
// https://developers.paymentsos.com/docs/api#/reference/customers
type CustomerClient struct {
Caller Caller
}
// Customer is a model of entity.
type Customer struct {
CustomerParams
ID string `json:"id"`
Created json.Number `json:"created"`
Modified json.Number `json:"modified"`
PaymentMethods []PaymentMethod `json:"payment_methods"`
}
// CustomerParams is a set of params for creating and updating entity.
type CustomerParams struct {
CustomerReference string `json:"customer_reference"`
FirstName string `json:"first_name,omitempty"`
LastName string `json:"last_name,omitempty"`
Email string `json:"email,omitempty"`
AdditionalDetails AdditionalDetails `json:"additional_details,omitempty"`
ShippingAddress *Address `json:"shipping_address,omitempty"`
}
const (
customersPath = "customers"
)
// New creates new Customer entity.
func (c *CustomerClient) New(ctx context.Context, idempotencyKey string, params *CustomerParams) (*Customer, error) {
customer := &Customer{}
if err := c.Caller.Call(ctx, "POST", customersPath, map[string]string{headerIdempotencyKey: idempotencyKey}, params, customer); err != nil {
return nil, err
}
return customer, nil
}
// Get returns Customer entity.
func (c *CustomerClient) Get(ctx context.Context, id string) (*Customer, error) {
customer := &Customer{}
if err := c.Caller.Call(ctx, "GET", c.customerPath(id), nil, nil, customer); err != nil {
return nil, err
}
return customer, nil
}
// Update updates Customer entity with given params and return updated Customer entity.
func (c *CustomerClient) Update(ctx context.Context, id string, params *CustomerParams) (*Customer, error) {
customer := &Customer{}
if err := c.Caller.Call(ctx, "PUT", c.customerPath(id), nil, params, customer); err != nil {
return nil, err
}
return customer, nil
}
// Delete deletes Customer entity.
func (c *CustomerClient) Delete(ctx context.Context, id string) error {
return c.Caller.Call(ctx, "DELETE", c.customerPath(id), nil, nil, nil)
}
func (c *CustomerClient) customerPath(id string) string {
return fmt.Sprintf("%s/%s", customersPath, id)
}