|
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
|
|
|
// ProfileStatus determines whether the profile is able to receive live payments |
|
13
|
|
|
type ProfileStatus string |
|
14
|
|
|
|
|
15
|
|
|
// Possible profile statuses |
|
16
|
|
|
const ( |
|
17
|
|
|
StatusUnverified ProfileStatus = "unverified" |
|
18
|
|
|
StatusVerified ProfileStatus = "verified" |
|
19
|
|
|
StatusBlocked ProfileStatus = "blocked" |
|
20
|
|
|
) |
|
21
|
|
|
|
|
22
|
|
|
// Profile will usually reflect the trademark or brand name |
|
23
|
|
|
// of the profile’s website or application. |
|
24
|
|
|
type Profile struct { |
|
25
|
|
|
ID string `json:"id,omitempty"` |
|
26
|
|
|
CategoryCode CategoryCode `json:"categoryCode,omitempty"` |
|
27
|
|
|
CreatedAt *time.Time `json:"createdAt,omitempty"` |
|
28
|
|
|
Email string `json:"email,omitempty"` |
|
29
|
|
|
Mode Mode `json:"mode,omitempty"` |
|
30
|
|
|
Name string `json:"name,omitempty"` |
|
31
|
|
|
Phone PhoneNumber `json:"phone,omitempty"` |
|
32
|
|
|
Resource string `json:"resource,omitempty"` |
|
33
|
|
|
Review struct { |
|
34
|
|
|
Status string `json:"status,omitempty"` |
|
35
|
|
|
} `json:"review,omitempty"` |
|
36
|
|
|
Status ProfileStatus `json:"status,omitempty"` |
|
37
|
|
|
Website string `json:"website,omitempty"` |
|
38
|
|
|
Links ProfileLinks `json:"_links,omitempty"` |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
// ProfileLinks contains URL's to relevant information related to |
|
42
|
|
|
// a profile. |
|
43
|
|
|
type ProfileLinks struct { |
|
44
|
|
|
Self *URL `json:"self,omitempty"` |
|
45
|
|
|
Chargebacks *URL `json:"chargebacks,omitempty"` |
|
46
|
|
|
Methods *URL `json:"methods,omitempty"` |
|
47
|
|
|
Refunds *URL `json:"refunds,omitempty"` |
|
48
|
|
|
CheckoutPreviewURL *URL `json:"checkoutPreviewUrl,omitempty"` |
|
49
|
|
|
Documentation *URL `json:"documentation,omitempty"` |
|
50
|
|
|
Dashboard *URL `json:"dashboard,omitempty"` |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
// ProfileListOptions are optional query string parameters for the list profiles request |
|
54
|
|
|
type ProfileListOptions struct { |
|
55
|
|
|
From string `url:"from,omitempty"` |
|
56
|
|
|
Limit uint `url:"limit,omitempty"` |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
// ProfileList contains a list of profiles for your account. |
|
60
|
|
|
type ProfileList struct { |
|
61
|
|
|
Count int `json:"count,omitempty"` |
|
62
|
|
|
Embedded struct { |
|
63
|
|
|
Profiles []Profile `json:"profiles,omitempty"` |
|
64
|
|
|
} `json:"_embedded,omitempty"` |
|
65
|
|
|
Links PaginationLinks `json:"_links,omitempty"` |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
// ProfilesService operates over profile resource |
|
69
|
|
|
type ProfilesService service |
|
70
|
|
|
|
|
71
|
|
|
// List returns all the profiles for the authenticated account |
|
72
|
|
|
func (ps *ProfilesService) List(options *ProfileListOptions) (pl *ProfileList, err error) { |
|
73
|
1 |
|
u := "v2/profiles" |
|
74
|
1 |
|
if options != nil { |
|
75
|
1 |
|
v, _ := query.Values(options) |
|
76
|
1 |
|
u = fmt.Sprintf("%s?%s", u, v.Encode()) |
|
77
|
|
|
} |
|
78
|
1 |
|
req, err := ps.client.NewAPIRequest(http.MethodGet, u, nil) |
|
79
|
1 |
|
if err != nil { |
|
80
|
1 |
|
return |
|
81
|
|
|
} |
|
82
|
1 |
|
res, err := ps.client.Do(req) |
|
83
|
1 |
|
if err != nil { |
|
84
|
1 |
|
return |
|
85
|
|
|
} |
|
86
|
1 |
|
if err = json.Unmarshal(res.content, &pl); err != nil { |
|
87
|
1 |
|
return |
|
88
|
|
|
} |
|
89
|
1 |
|
return |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
// Get retrieves the a profile by ID. |
|
93
|
|
|
func (ps *ProfilesService) Get(id string) (p *Profile, err error) { |
|
94
|
1 |
|
return ps.get(id) |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
// Current returns the profile belonging to the API key. |
|
98
|
|
|
// This method only works when using API keys. |
|
99
|
|
|
func (ps *ProfilesService) Current() (p *Profile, err error) { |
|
100
|
1 |
|
return ps.get("me") |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
func (ps *ProfilesService) get(id string) (p *Profile, err error) { |
|
104
|
1 |
|
u := fmt.Sprintf("v2/profiles/%s", id) |
|
105
|
1 |
|
req, err := ps.client.NewAPIRequest(http.MethodGet, u, nil) |
|
106
|
1 |
|
if err != nil { |
|
107
|
1 |
|
return |
|
108
|
|
|
} |
|
109
|
1 |
|
res, err := ps.client.Do(req) |
|
110
|
1 |
|
if err != nil { |
|
111
|
1 |
|
return |
|
112
|
|
|
} |
|
113
|
1 |
|
if err = json.Unmarshal(res.content, &p); err != nil { |
|
114
|
1 |
|
return |
|
115
|
|
|
} |
|
116
|
1 |
|
return |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
// Create stores a new profile in your Mollie account. |
|
120
|
|
|
func (ps *ProfilesService) Create(np *Profile) (p *Profile, err error) { |
|
121
|
1 |
|
req, err := ps.client.NewAPIRequest(http.MethodPost, "v2/profiles", np) |
|
122
|
1 |
|
if err != nil { |
|
123
|
1 |
|
return |
|
124
|
|
|
} |
|
125
|
1 |
|
res, err := ps.client.Do(req) |
|
126
|
1 |
|
if err != nil { |
|
127
|
1 |
|
return |
|
128
|
|
|
} |
|
129
|
1 |
|
if err = json.Unmarshal(res.content, &p); err != nil { |
|
130
|
1 |
|
return |
|
131
|
|
|
} |
|
132
|
1 |
|
return |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
// Update allows you to perform mutations on a profile |
|
136
|
|
|
func (ps *ProfilesService) Update(id string, up *Profile) (p *Profile, err error) { |
|
137
|
1 |
|
u := fmt.Sprintf("v2/profiles/%s", id) |
|
138
|
1 |
|
req, err := ps.client.NewAPIRequest(http.MethodPatch, u, up) |
|
139
|
1 |
|
if err != nil { |
|
140
|
1 |
|
return |
|
141
|
|
|
} |
|
142
|
1 |
|
res, err := ps.client.Do(req) |
|
143
|
1 |
|
if err != nil { |
|
144
|
1 |
|
return |
|
145
|
|
|
} |
|
146
|
1 |
|
if err = json.Unmarshal(res.content, &p); err != nil { |
|
147
|
1 |
|
return |
|
148
|
|
|
} |
|
149
|
1 |
|
return |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
// Delete enables profile deletions, rendering the profile unavailable |
|
153
|
|
|
// for further API calls and transactions. |
|
154
|
|
|
func (ps *ProfilesService) Delete(id string) (err error) { |
|
155
|
1 |
|
u := fmt.Sprintf("v2/profiles/%s", id) |
|
156
|
1 |
|
req, err := ps.client.NewAPIRequest(http.MethodDelete, u, nil) |
|
157
|
1 |
|
if err != nil { |
|
158
|
1 |
|
return |
|
159
|
|
|
} |
|
160
|
1 |
|
_, err = ps.client.Do(req) |
|
161
|
1 |
|
if err != nil { |
|
162
|
1 |
|
return |
|
163
|
|
|
} |
|
164
|
1 |
|
return |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
// EnablePaymentMethod enables a payment method on a specific or authenticated profile. |
|
168
|
|
|
// If you're using API tokens for authentication, pass "me" as id. |
|
169
|
|
|
func (ps *ProfilesService) EnablePaymentMethod(id string, pm PaymentMethod) (pmi *PaymentMethodInfo, err error) { |
|
170
|
1 |
|
u := fmt.Sprintf("v2/profiles/%s/methods/%s", id, pm) |
|
171
|
1 |
|
req, err := ps.client.NewAPIRequest(http.MethodPost, u, nil) |
|
172
|
1 |
|
if err != nil { |
|
173
|
1 |
|
return |
|
174
|
|
|
} |
|
175
|
1 |
|
res, err := ps.client.Do(req) |
|
176
|
1 |
|
if err != nil { |
|
177
|
1 |
|
return |
|
178
|
|
|
} |
|
179
|
1 |
|
if err = json.Unmarshal(res.content, &pmi); err != nil { |
|
180
|
1 |
|
return |
|
181
|
|
|
} |
|
182
|
1 |
|
return |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
// DisablePaymentMethod disables a payment method on a specific or authenticated profile. |
|
186
|
|
|
// If you're using API tokens for authentication, pass "me" as id. |
|
187
|
|
|
func (ps *ProfilesService) DisablePaymentMethod(id string, pm PaymentMethod) (err error) { |
|
188
|
1 |
|
u := fmt.Sprintf("v2/profiles/%s/methods/%s", id, pm) |
|
189
|
1 |
|
req, err := ps.client.NewAPIRequest(http.MethodDelete, u, nil) |
|
190
|
1 |
|
if err != nil { |
|
191
|
1 |
|
return |
|
192
|
|
|
} |
|
193
|
1 |
|
_, err = ps.client.Do(req) |
|
194
|
1 |
|
if err != nil { |
|
195
|
1 |
|
return |
|
196
|
|
|
} |
|
197
|
1 |
|
return |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
// EnableGiftCardIssuer activates the requested giftcard issuer for the provided |
|
201
|
|
|
// profile id. |
|
202
|
|
|
// |
|
203
|
|
|
// See: https://docs.mollie.com/reference/v2/profiles-api/enable-gift-card-issuer |
|
204
|
|
|
func (ps *ProfilesService) EnableGiftCardIssuer(profileID string, vendor GiftCardIssuer) (gc *GiftCardEnabled, err error) { |
|
205
|
|
|
res, err := ps.toggleGiftCardIssuerStatus(profileID, http.MethodPost, vendor) |
|
206
|
|
|
if err = json.Unmarshal(res.content, &gc); err != nil { |
|
207
|
|
|
return |
|
208
|
|
|
} |
|
209
|
|
|
return |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
// DisableGiftCardIssuer deactivates the requested giftcard issuer for the provided |
|
213
|
|
|
// profile id. |
|
214
|
|
|
// |
|
215
|
|
|
// See: https://docs.mollie.com/reference/v2/profiles-api/disable-gift-card-issuer |
|
216
|
|
|
func (ps *ProfilesService) DisableGiftCardIssuer(profileID string, vendor GiftCardIssuer) (err error) { |
|
217
|
|
|
_, err = ps.toggleGiftCardIssuerStatus(profileID, http.MethodDelete, vendor) |
|
218
|
|
|
if err != nil { |
|
219
|
|
|
return |
|
220
|
|
|
} |
|
221
|
|
|
return |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
func (ps *ProfilesService) toggleGiftCardIssuerStatus(profileID string, method string, vendor GiftCardIssuer) (r *Response, err error) { |
|
225
|
|
|
u := fmt.Sprintf("v2/profiles/%s/giftcards/issuer/%s", profileID, vendor) |
|
226
|
|
|
req, err := ps.client.NewAPIRequest(method, u, nil) |
|
227
|
|
|
if err != nil { |
|
228
|
|
|
return |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
r, err = ps.client.Do(req) |
|
232
|
|
|
if err != nil { |
|
233
|
|
|
return |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
return |
|
237
|
|
|
} |
|
238
|
|
|
|