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
|
|
|
type CustomersService service |
13
|
|
|
|
14
|
|
|
type CustomerReqOptions struct { |
15
|
|
|
Testmode bool `json:"testmode,omitempty"` |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
type Customer struct { |
19
|
|
|
Resource string `json:"resource,omitempty"` |
20
|
|
|
ID string `json:"id,omitempty"` |
21
|
|
|
Mode Mode `json:"mode,omitempty"` |
22
|
|
|
Name string `json:"name,omitempty"` |
23
|
|
|
Email string `json:"email,omitempty"` |
24
|
|
|
Metadata interface{} `json:"metadata,omitempty"` |
25
|
|
|
Locale *Locale `json:"locale,omitempty"` |
26
|
|
|
CreatedAt *time.Time `json:"createdAt,omitempty"` |
27
|
|
|
Links *PaymentLinks `json:"_links,omitempty"` |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
// Get retrieve a single customer by its ID. |
31
|
|
|
func (sv *CustomersService) Get(ID string) (c Customer, err error) { |
32
|
|
|
path := fmt.Sprintf("/v2/customers/%s", ID) |
33
|
|
|
|
34
|
|
|
req, err := sv.client.NewAPIRequest(http.MethodGet, path) |
35
|
|
|
if err != nil { |
36
|
|
|
return |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
res, err := sv.client.Do(req) |
40
|
|
|
if err != nil { |
41
|
|
|
return |
42
|
|
|
} |
43
|
|
|
if err = json.Unmarshal(res.content, &c); err != nil { |
44
|
|
|
return |
45
|
|
|
} |
46
|
|
|
return |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// CreateCustomerReq represents the body of CreateCustomer endpoint |
50
|
|
|
// All attributes are optional |
51
|
|
|
type CreateCustomerReq struct { |
52
|
|
|
Name string `json:"name"` |
53
|
|
|
Email string `json:"email"` |
54
|
|
|
Locale *Locale `json:"locale"` |
55
|
|
|
Metadata interface{} `json:"metadata,omitempty"` |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// Create creates a simple minimal representation of a customer |
59
|
|
|
// in the Mollie API to use for the Mollie Checkout and Recurring features. |
60
|
|
|
// These customers will appear in your Mollie Dashboard where you can manage |
61
|
|
|
// their details, and also see their payments and subscriptions. |
62
|
|
|
func (sv *CustomersService) Create(reqBody CreateCustomerReq) (c Customer, err error) { |
63
|
|
|
path := "/v2/customers" |
64
|
|
|
|
65
|
|
|
req, err := sv.client.NewAPIRequest(http.MethodPost, path, reqBody) |
66
|
|
|
if err != nil { |
67
|
|
|
return |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
res, err := ps.client.Do(req) |
71
|
|
|
if err != nil { |
72
|
|
|
return |
73
|
|
|
} |
74
|
|
|
if err = json.Unmarshal(res.content, &c); err != nil { |
75
|
|
|
return |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// UpdateCutomerReq represents the body of the update request. |
82
|
|
|
// Its structure is currently identical to CreateCustomerReq |
83
|
|
|
type UpdateCustomerReq CreateCustomerReq |
84
|
|
|
|
85
|
|
|
// Update updates an existing customer |
86
|
|
|
func (sv *CustomersService) Update(ID string, reqBody UpdateCustomerReq) (c Customer, err error) { |
87
|
|
|
path := fmt.Sprintf("/v2/customers/%s", ID) |
88
|
|
|
|
89
|
|
|
req, err := sv.client.NewAPIRequest(http.MethodPatch, path, reqBody) |
90
|
|
|
if err != nil { |
91
|
|
|
return |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
res, err := ps.client.Do(req) |
95
|
|
|
if err != nil { |
96
|
|
|
return |
97
|
|
|
} |
98
|
|
|
if err = json.Unmarshal(res.content, &c); err != nil { |
99
|
|
|
return |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// Delete retrieve a single customer by its ID. |
106
|
|
|
func (sv *CustomersService) Delete(ID string) (err error) { |
107
|
|
|
path := fmt.Sprintf("/v2/customers/%s", ID) |
108
|
|
|
|
109
|
|
|
req, err := sv.client.NewAPIRequest(http.MethodDelete, path) |
110
|
|
|
if err != nil { |
111
|
|
|
return |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
_, err = sv.client.Do(req) |
115
|
|
|
|
116
|
|
|
return |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// ListCustomerOptions represents the available options |
120
|
|
|
// From offsets the result set to the customer with this ID. |
121
|
|
|
// The customer with this ID is included in the result set as well. |
122
|
|
|
// Limit limits the number of customers to return (with a maximum of 250). |
123
|
|
|
type ListCustomerOptions struct { |
124
|
|
|
From string `json:"list,omitempty"` |
125
|
|
|
Limit int `json:"limit,omitempty"` |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
// |
129
|
|
|
type CustomersList struct { |
130
|
|
|
Count int `json:"count,omitempty"` |
131
|
|
|
Embedded struct { |
132
|
|
|
Customers []Customer |
133
|
|
|
} `json:"_embedded,omitempty"` |
134
|
|
|
Links PaginationLinks `json:"_links,omitempty"` |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// List retrieves all customers created. |
138
|
|
|
func (sv *CustomersService) List(options *ListCustomerOptions) (cl CustomersList, err error) { |
139
|
|
|
path := "v2/customers" |
140
|
|
|
if options != nil { |
141
|
|
|
v, _ := query.Values(options) |
142
|
|
|
path = fmt.Sprintf("%s?%s", path, v.Encode()) |
143
|
|
|
} |
144
|
|
|
req, err := ps.client.NewAPIRequest(http.MethodGet, path, nil) |
145
|
|
|
if err != nil { |
146
|
|
|
return |
147
|
|
|
} |
148
|
|
|
res, err := ps.client.Do(req) |
149
|
|
|
if err != nil { |
150
|
|
|
return |
151
|
|
|
} |
152
|
|
|
if err = json.Unmarshal(res.content, &pl); err != nil { |
153
|
|
|
return |
154
|
|
|
} |
155
|
|
|
return |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
type CreateCustomerPaymentReq struct { |
159
|
|
|
Payment |
160
|
|
|
SequenceType string `json:"sequenceType,omitempty"` |
161
|
|
|
RedirectUrl string `json:"redirectUrl, omitempty"` |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
type CustomerPaymentOptions struct { |
165
|
|
|
ProfileID string `json:"profileID"` |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
var ( |
169
|
|
|
requiredCreateParam = "parameter required for creating a payment: %+v" |
170
|
|
|
) |
171
|
|
|
|
172
|
|
|
// CreatePayment creates a payment for the customer |
173
|
|
|
func (sv *CustomersService) CreatePayment(reqBody *CreateCustomerPaymentReq, options *CustomerPaymentOptions) (p Payment, err error) { |
174
|
|
|
path := fmt.Sprintf("v2/customers/%s/payments") |
175
|
|
|
if options.ProfileID == "" { |
176
|
|
|
return p, fmt.Errorf(requiredCreateParam, options.ProfileID) |
177
|
|
|
} |
178
|
|
|
if options != nil { |
179
|
|
|
v, _ := query.Values(options) |
180
|
|
|
path = fmt.Sprintf("%s?%s", path, v.Encode()) |
181
|
|
|
} |
182
|
|
|
if reqBody.Payment.Amount == nil { |
183
|
|
|
return p, fmt.Errorf(requiredCreateParam, reqBody.Payment.Amount) |
184
|
|
|
} |
185
|
|
|
if reqBody.Payment.Description == "" { |
186
|
|
|
return p, fmt.Errorf(requiredCreateParam, reqBody.Payment.Description) |
187
|
|
|
} |
188
|
|
|
req, err := ps.client.NewAPIRequest(http.MethodPost, path, reqBody) |
189
|
|
|
if err != nil { |
190
|
|
|
return |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
res, err := ps.client.Do(req) |
194
|
|
|
if err != nil { |
195
|
|
|
return |
196
|
|
|
} |
197
|
|
|
if err = json.Unmarshal(res.content, &p); err != nil { |
198
|
|
|
return |
199
|
|
|
} |
200
|
|
|
return |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
func (sv *CustomersService) ListPayments(ID string, options *CustomerPaymentOptions) (pl PaymentsList, err error) { |
204
|
|
|
path := fmt.Sprintf("v2/customers/%s/payments") |
205
|
|
|
if options.ProfileID == "" { |
206
|
|
|
return lp, fmt.Errorf(requiredCreateParam, options.ProfileID) |
207
|
|
|
} |
208
|
|
|
if options != nil { |
209
|
|
|
v, _ := query.Values(options) |
210
|
|
|
path = fmt.Sprintf("%s?%s", path, v.Encode()) |
211
|
|
|
} |
212
|
|
|
req, err := sv.client.NewAPIRequest(http.MethodGet, path, nil) |
213
|
|
|
if err != nil { |
214
|
|
|
return |
215
|
|
|
} |
216
|
|
|
res, err := sv.client.Do(req) |
217
|
|
|
if err != nil { |
218
|
|
|
return |
219
|
|
|
} |
220
|
|
|
if err = json.Unmarshal(res.content, &pl); err != nil { |
221
|
|
|
return |
222
|
|
|
} |
223
|
|
|
return |
224
|
|
|
} |
225
|
|
|
|