1
|
|
|
package mollie |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"encoding/json" |
5
|
|
|
"fmt" |
6
|
|
|
"net/http" |
7
|
|
|
"time" |
8
|
|
|
|
9
|
|
|
"github.com/google/go-querystring/query" |
10
|
|
|
) |
11
|
|
|
|
12
|
|
|
// CustomersService operates over the customer resource |
13
|
|
|
type CustomersService service |
14
|
|
|
|
15
|
|
|
// CustomerLinks contains the HAL resources for a customer response |
16
|
|
|
type CustomerLinks struct { |
17
|
|
|
Self *URL `json:"self,omitempty"` |
18
|
|
|
Mandates *URL `json:"mandates,omitempty"` |
19
|
|
|
Subscriptions *URL `json:"subscriptions,omitempty"` |
20
|
|
|
Payments *URL `json:"payments,omitempty"` |
21
|
|
|
Documentation *URL `json:"documentation,omitempty"` |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
// Customer represents buyers |
25
|
|
|
type Customer struct { |
26
|
|
|
Resource string `json:"resource,omitempty"` |
27
|
|
|
ID string `json:"id,omitempty"` |
28
|
|
|
Mode Mode `json:"mode,omitempty"` |
29
|
|
|
Name string `json:"name,omitempty"` |
30
|
|
|
Email string `json:"email,omitempty"` |
31
|
|
|
Locale Locale `json:"locale,omitempty"` |
32
|
|
|
Metadata map[string]interface{} `json:"metadata,omitempty"` |
33
|
|
|
CreatedAt *time.Time `json:"createdAt,omitempty"` |
34
|
|
|
Links CustomerLinks `json:"_links,omitempty"` |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
// ListCustomersOptions contains valid query parameters for the list customers endpoint. |
38
|
|
|
type ListCustomersOptions struct { |
39
|
|
|
From string `url:"from,omitempty"` |
40
|
|
|
Limit int `url:"limit,omitempty"` |
41
|
|
|
ProfileID string `url:"profileId,omitempty"` |
42
|
|
|
SequenceType SequenceType `url:"sequenceType,omitempty"` |
43
|
|
|
RedirectURL string `url:"redirectUrl,omitempty"` |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// CustomersList contains a embedded list of customers |
47
|
|
|
// wrapped in a standard Mollie paginated response. |
48
|
|
|
type CustomersList struct { |
49
|
|
|
Count int `json:"count,omitempty"` |
50
|
|
|
Embedded struct { |
51
|
|
|
Customers []Customer `json:"customers,omitempty"` |
52
|
|
|
} `json:"_embedded,omitempty"` |
53
|
|
|
Links PaginationLinks `json:"links,omitempty"` |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// Get finds a customer by its ID |
57
|
|
|
// |
58
|
|
|
// See: https://docs.mollie.com/reference/v2/customers-api/get-customer |
59
|
|
|
func (cs *CustomersService) Get(id string) (c *Customer, err error) { |
60
|
1 |
|
u := fmt.Sprintf("v2/customers/%s", id) |
61
|
1 |
|
req, err := cs.client.NewAPIRequest(http.MethodGet, u, nil) |
62
|
1 |
|
if err != nil { |
63
|
1 |
|
return |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
res, err := cs.client.Do(req) |
67
|
1 |
|
if err != nil { |
68
|
1 |
|
return |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
if err = json.Unmarshal(res.content, &c); err != nil { |
72
|
1 |
|
return |
73
|
|
|
} |
74
|
1 |
|
return |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// Create creates a simple minimal representation of a customer in the Mollie API |
78
|
|
|
// to use for the Mollie Checkout and Recurring features |
79
|
|
|
// |
80
|
|
|
// See: https://docs.mollie.com/reference/v2/customers-api/create-customer |
81
|
|
|
func (cs *CustomersService) Create(c Customer) (cc *Customer, err error) { |
82
|
1 |
|
req, err := cs.client.NewAPIRequest(http.MethodPost, "v2/customers", c) |
83
|
1 |
|
if err != nil { |
84
|
1 |
|
return |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
res, err := cs.client.Do(req) |
88
|
1 |
|
if err != nil { |
89
|
1 |
|
return |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
if err = json.Unmarshal(res.content, &cc); err != nil { |
93
|
1 |
|
return |
94
|
|
|
} |
95
|
1 |
|
return |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// Update mutates an existing customer |
99
|
|
|
// |
100
|
|
|
// See: https://docs.mollie.com/reference/v2/customers-api/update-customer |
101
|
|
|
func (cs *CustomersService) Update(id string, c Customer) (cc *Customer, err error) { |
102
|
1 |
|
u := fmt.Sprintf("v2/customers/%s", id) |
103
|
1 |
|
req, err := cs.client.NewAPIRequest(http.MethodPatch, u, c) |
104
|
1 |
|
if err != nil { |
105
|
1 |
|
return |
106
|
|
|
} |
107
|
|
|
|
108
|
1 |
|
res, err := cs.client.Do(req) |
109
|
1 |
|
if err != nil { |
110
|
1 |
|
return |
111
|
|
|
} |
112
|
|
|
|
113
|
1 |
|
if err = json.Unmarshal(res.content, &cc); err != nil { |
114
|
1 |
|
return |
115
|
|
|
} |
116
|
1 |
|
return |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// Delete a customer. |
120
|
|
|
// All mandates and subscriptions created for this customer will be canceled as well. |
121
|
|
|
// |
122
|
|
|
// See: https://docs.mollie.com/reference/v2/customers-api/delete-customer |
123
|
|
|
func (cs *CustomersService) Delete(id string) (err error) { |
124
|
1 |
|
u := fmt.Sprintf("v2/customers/%s", id) |
125
|
1 |
|
req, err := cs.client.NewAPIRequest(http.MethodDelete, u, nil) |
126
|
1 |
|
if err != nil { |
127
|
1 |
|
return |
128
|
|
|
} |
129
|
|
|
|
130
|
1 |
|
_, err = cs.client.Do(req) |
131
|
1 |
|
if err != nil { |
132
|
1 |
|
return |
133
|
|
|
} |
134
|
1 |
|
return |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// List retrieves all customers created. |
138
|
|
|
// |
139
|
|
|
// See: https://docs.mollie.com/reference/v2/customers-api/list-customers |
140
|
|
|
func (cs *CustomersService) List(options *ListCustomersOptions) (cl *CustomersList, err error) { |
141
|
1 |
|
u := "v2/customers" |
142
|
1 |
|
if options != nil { |
143
|
1 |
|
v, _ := query.Values(options) |
144
|
1 |
|
u = fmt.Sprintf("%s?%s", u, v.Encode()) |
145
|
|
|
} |
146
|
|
|
|
147
|
1 |
|
res, err := cs.list(u) |
148
|
1 |
|
if err != nil { |
149
|
1 |
|
return |
150
|
|
|
} |
151
|
|
|
|
152
|
1 |
|
if err = json.Unmarshal(res.content, &cl); err != nil { |
153
|
1 |
|
return |
154
|
|
|
} |
155
|
1 |
|
return |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
// GetPayments retrieves all payments linked to the customer. |
159
|
|
|
// |
160
|
|
|
// See: https://docs.mollie.com/reference/v2/customers-api/list-customer-payments |
161
|
|
|
func (cs *CustomersService) GetPayments(id string, options *ListCustomersOptions) (pl *PaymentList, err error) { |
162
|
1 |
|
u := fmt.Sprintf("v2/customers/%s/payments", id) |
163
|
1 |
|
if options != nil { |
164
|
1 |
|
v, _ := query.Values(options) |
165
|
1 |
|
u = fmt.Sprintf("%s?%s", u, v.Encode()) |
166
|
|
|
} |
167
|
|
|
|
168
|
1 |
|
res, err := cs.list(u) |
169
|
1 |
|
if err != nil { |
170
|
1 |
|
return |
171
|
|
|
} |
172
|
|
|
|
173
|
1 |
|
if err = json.Unmarshal(res.content, &pl); err != nil { |
174
|
1 |
|
return |
175
|
|
|
} |
176
|
1 |
|
return |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
// CreatePayment creates a payment for the customer. |
180
|
|
|
// |
181
|
|
|
// See: https://docs.mollie.com/reference/v2/customers-api/create-customer-payment |
182
|
|
|
func (cs *CustomersService) CreatePayment(id string, p Payment) (pp *Payment, err error) { |
183
|
1 |
|
u := fmt.Sprintf("v2/customers/%s/payments", id) |
184
|
1 |
|
req, err := cs.client.NewAPIRequest(http.MethodPost, u, p) |
185
|
1 |
|
if err != nil { |
186
|
1 |
|
return |
187
|
|
|
} |
188
|
|
|
|
189
|
1 |
|
res, err := cs.client.Do(req) |
190
|
1 |
|
if err != nil { |
191
|
1 |
|
return |
192
|
|
|
} |
193
|
|
|
|
194
|
1 |
|
if err = json.Unmarshal(res.content, &pp); err != nil { |
195
|
1 |
|
return |
196
|
|
|
} |
197
|
1 |
|
return |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
func (cs *CustomersService) list(uri string) (r *Response, err error) { |
201
|
1 |
|
req, err := cs.client.NewAPIRequest(http.MethodGet, uri, nil) |
202
|
1 |
|
if err != nil { |
203
|
1 |
|
return |
204
|
|
|
} |
205
|
|
|
|
206
|
1 |
|
r, err = cs.client.Do(req) |
207
|
1 |
|
if err != nil { |
208
|
1 |
|
return |
209
|
|
|
} |
210
|
1 |
|
return |
211
|
|
|
} |
212
|
|
|
|