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
|
|
|
// Mandates allow you to charge a customer’s credit card or bank account recurrently. |
13
|
|
|
type Mandate struct { |
14
|
|
|
ID string `json:"id,omitempty"` |
15
|
|
|
Resource string `json:"resource,omitempty"` |
16
|
|
|
Method PaymentMethod `json:"method,omitempty"` |
17
|
|
|
ConsumerName string `json:"consumerName,omitempty"` |
18
|
|
|
ConsumerAccount string `json:"consumerAccount,omitempty"` |
19
|
|
|
ConsumerBic string `json:"consumerBic,omitempty"` |
20
|
|
|
SignatureDate *ShortDate `json:"signatureDate,omitempty"` |
21
|
|
|
MandateReference string `json:"mandateReference,omitempty"` |
22
|
|
|
Mode Mode `json:"mode,omitempty"` |
23
|
|
|
Status MandateStatus `json:"status,omitempty"` |
24
|
|
|
CreatedAt *time.Time `json:"createdAt,omitempty"` |
25
|
|
|
Details MandateDetails `json:"details,omitempty"` |
26
|
|
|
Links MandateLinks `json:"_links,omitempty"` |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
// MandateDetails are possible values inside the mandate.details field |
30
|
|
|
type MandateDetails struct { |
31
|
|
|
ConsumerName string `json:"consumerName,omitempty"` |
32
|
|
|
ConsumerAccount string `json:"consumerAccount,omitempty"` |
33
|
|
|
ConsumerBic string `json:"consumerBic,omitempty"` |
34
|
|
|
CardHolder string `json:"cardHolder,omitempty"` |
35
|
|
|
CardNumber string `json:"cardNumber,omitempty"` |
36
|
|
|
CardLabel CardLabel `json:"cardLabel,omitempty"` |
37
|
|
|
CardFingerprint string `json:"cardFingerprint,omitempty"` |
38
|
|
|
CardExpiryDate *ShortDate `json:"cardExpiryDate,omitempty"` |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// MandateStatus for the Mandate object |
42
|
|
|
type MandateStatus string |
43
|
|
|
|
44
|
|
|
// Valid mandate statuses |
45
|
|
|
const ( |
46
|
|
|
PendingMandate MandateStatus = "pending" |
47
|
|
|
ValidMandate MandateStatus = "valid" |
48
|
|
|
InvalidMandate MandateStatus = "invalid" |
49
|
|
|
) |
50
|
|
|
|
51
|
|
|
// CardLabel. Note that not all labels can be processed through Mollie. |
52
|
|
|
type CardLabel string |
53
|
|
|
|
54
|
|
|
// Available card labels |
55
|
|
|
const ( |
56
|
|
|
AmericaExpress CardLabel = "American Express" |
57
|
|
|
CartaSi CardLabel = "Carta Si" |
58
|
|
|
CarteBleue CardLabel = "Carte Bleue" |
59
|
|
|
Dankort CardLabel = "Dankort" |
60
|
|
|
DinersClub CardLabel = "Diners Club" |
61
|
|
|
Discover CardLabel = "Discover" |
62
|
|
|
JCB CardLabel = "JCB" |
63
|
|
|
Laser CardLabel = "Laser" |
64
|
|
|
Maestro CardLabel = "Maestro" |
65
|
|
|
Mastercard CardLabel = "Mastercard" |
66
|
|
|
Unionpay CardLabel = "Unionpay" |
67
|
|
|
Visa CardLabel = "Visa" |
68
|
|
|
Empty CardLabel = "null" |
69
|
|
|
) |
70
|
|
|
|
71
|
|
|
// MandatesService operates over customer mandates endpoints. |
72
|
|
|
type MandatesService service |
73
|
|
|
|
74
|
|
|
// MandateLinks response objects |
75
|
|
|
type MandateLinks struct { |
76
|
|
|
Self *URL `json:"self,omitempty"` |
77
|
|
|
Customer *URL `json:"customer,omitempty"` |
78
|
|
|
Documentation *URL `json:"documentation,omitempty"` |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// ListMandatesOptions contains valid query parameters |
82
|
|
|
// to filter the List mandates actions. |
83
|
|
|
// From is a mandate id to offset from (inclusive) |
84
|
|
|
// Limit is the max number of mandates to retrieve |
85
|
|
|
type ListMandatesOptions struct { |
86
|
|
|
From string `url:"from,omitempty"` |
87
|
|
|
Limit int `url:"limit,omitempty"` |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// MandateList describes how a list of mandates will be retrieved by Mollie. |
91
|
|
|
type MandateList struct { |
92
|
|
|
Count int `json:"count,omitempty"` |
93
|
|
|
Embedded struct { |
94
|
|
|
Mandates []Mandate |
95
|
|
|
} `json:"_embedded,omitempty"` |
96
|
|
|
Links PaginationLinks `json:"_links,omitempty"` |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// Create a mandate for a specific customer. |
100
|
|
|
// Mandates allow you to charge a customer’s credit card or bank account recurrently. |
101
|
|
|
// |
102
|
|
|
// See: https://docs.mollie.com/reference/v2/mandates-api/create-mandate |
103
|
|
|
func (ms *MandatesService) Create(cID string, mandate Mandate) (mr *Mandate, err error) { |
104
|
1 |
|
u := fmt.Sprintf("v2/customers/%s/mandates", cID) |
105
|
1 |
|
req, err := ms.client.NewAPIRequest(http.MethodPost, u, mandate) |
106
|
1 |
|
if err != nil { |
107
|
1 |
|
return |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
res, err := ms.client.Do(req) |
111
|
1 |
|
if err != nil { |
112
|
1 |
|
return |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
if err = json.Unmarshal(res.content, &mr); err != nil { |
116
|
1 |
|
return |
117
|
|
|
} |
118
|
|
|
|
119
|
1 |
|
return |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// Get retrieves a mandate by its ID and its customer’s ID. |
123
|
|
|
// The mandate will either contain IBAN or credit card details, |
124
|
|
|
// depending on the type of mandate. |
125
|
|
|
// |
126
|
|
|
// See: https://docs.mollie.com/reference/v2/mandates-api/get-mandate |
127
|
|
|
func (ms *MandatesService) Get(cID, mID string) (mr *Mandate, err error) { |
128
|
1 |
|
u := fmt.Sprintf("v2/customers/%s/mandates/%s", cID, mID) |
129
|
1 |
|
req, err := ms.client.NewAPIRequest(http.MethodGet, u, nil) |
130
|
1 |
|
if err != nil { |
131
|
1 |
|
return |
132
|
|
|
} |
133
|
|
|
|
134
|
1 |
|
res, err := ms.client.Do(req) |
135
|
1 |
|
if err != nil { |
136
|
1 |
|
return |
137
|
|
|
} |
138
|
|
|
|
139
|
1 |
|
if err = json.Unmarshal(res.content, &mr); err != nil { |
140
|
1 |
|
return |
141
|
|
|
} |
142
|
|
|
|
143
|
1 |
|
return |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
// Revoke a customer’s mandate. |
147
|
|
|
// You will no longer be able to charge the consumer’s bank account or credit card with this mandate and all connected subscriptions will be canceled. |
148
|
|
|
// |
149
|
|
|
// See: https://docs.mollie.com/reference/v2/mandates-api/revoke-mandate |
150
|
|
|
func (ms *MandatesService) Revoke(cID, mID string) (err error) { |
151
|
1 |
|
u := fmt.Sprintf("v2/customers/%s/mandates/%s", cID, mID) |
152
|
1 |
|
req, err := ms.client.NewAPIRequest(http.MethodDelete, u, nil) |
153
|
1 |
|
if err != nil { |
154
|
1 |
|
return |
155
|
|
|
} |
156
|
|
|
|
157
|
1 |
|
_, err = ms.client.Do(req) |
158
|
1 |
|
if err != nil { |
159
|
1 |
|
return |
160
|
|
|
} |
161
|
|
|
|
162
|
1 |
|
return |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
// ListMandates retrieves all mandates for the given customerId, |
166
|
|
|
// ordered from newest to oldest. |
167
|
|
|
// |
168
|
|
|
// See: https://docs.mollie.com/reference/v2/mandates-api/list-mandates |
169
|
|
|
func (ms *MandatesService) List(cID string, opt *ListMandatesOptions) (ml MandateList, err error) { |
170
|
1 |
|
u := fmt.Sprintf("v2/customers/%s/mandates", cID) |
171
|
1 |
|
if opt != nil { |
172
|
1 |
|
v, _ := query.Values(opt) |
173
|
1 |
|
u = fmt.Sprintf("%s?%s", u, v.Encode()) |
174
|
|
|
} |
175
|
|
|
|
176
|
1 |
|
req, err := ms.client.NewAPIRequest(http.MethodGet, u, nil) |
177
|
1 |
|
if err != nil { |
178
|
1 |
|
return |
179
|
|
|
} |
180
|
|
|
|
181
|
1 |
|
res, err := ms.client.Do(req) |
182
|
1 |
|
if err != nil { |
183
|
1 |
|
return |
184
|
|
|
} |
185
|
|
|
|
186
|
1 |
|
if err = json.Unmarshal(res.content, &ml); err != nil { |
187
|
1 |
|
return |
188
|
|
|
} |
189
|
|
|
|
190
|
1 |
|
return |
191
|
|
|
} |
192
|
|
|
|