forked from braintree-go/braintree-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
customer_gateway.go
74 lines (67 loc) · 1.7 KB
/
customer_gateway.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
74
package braintree
import "encoding/xml"
type CustomerGateway struct {
*Braintree
}
// Create creates a new customer from the passed in customer object.
// If no Id is set, Braintree will assign one.
func (g *CustomerGateway) Create(c *Customer) (*Customer, error) {
resp, err := g.execute("POST", "customers", c)
if err != nil {
return nil, err
}
switch resp.StatusCode {
case 201:
return resp.customer()
}
return nil, &invalidResponseError{resp}
}
// Update updates any field that is set in the passed customer object.
// The Id field is mandatory.
func (g *CustomerGateway) Update(c *Customer) (*Customer, error) {
resp, err := g.execute("PUT", "customers/"+c.Id, c)
if err != nil {
return nil, err
}
switch resp.StatusCode {
case 200:
return resp.customer()
}
return nil, &invalidResponseError{resp}
}
// Find finds the customer with the given id.
func (g *CustomerGateway) Find(id string) (*Customer, error) {
resp, err := g.execute("GET", "customers/"+id, nil)
if err != nil {
return nil, err
}
switch resp.StatusCode {
case 200:
return resp.customer()
}
return nil, &invalidResponseError{resp}
}
func (g *CustomerGateway) Search(query *SearchQuery) (*CustomerSearchResult, error) {
resp, err := g.execute("POST", "customers/advanced_search", query)
if err != nil {
return nil, err
}
var v CustomerSearchResult
err = xml.Unmarshal(resp.Body, &v)
if err != nil {
return nil, err
}
return &v, err
}
// Delete deletes the customer with the given id.
func (g *CustomerGateway) Delete(id string) error {
resp, err := g.execute("DELETE", "customers/"+id, nil)
if err != nil {
return err
}
switch resp.StatusCode {
case 200:
return nil
}
return &invalidResponseError{resp}
}