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