This repository was archived by the owner on Sep 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcustomers.go
121 lines (98 loc) · 3.11 KB
/
customers.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
// Copyright 2017 The Go-Mollie Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package mollie
import (
"strconv"
"time"
)
// CustomerAPI is the part of the API which handles Mollie customers
type CustomerAPI struct {
c *core
}
type Links struct {
Previous string `json:"previous,omitempty"`
Next string `json:"next,omitempty"`
First string `json:"first,omitempty"`
Last string `json:"last,omitempty"`
}
type NewCustomerData struct {
Name string `json:"name"`
Email string `json:"email"`
Locale string `json:"locale,omitempty"` // One of de_DE en_US es_ES fr_FR nl_BE fr_BE nl_NL
Metadata string `json:"metadata,omitempty"`
}
type ListCustomersResponse struct {
Totalcount uint `json:"totalcount"`
Offset uint `json:"offset"`
Count uint `json:"count"`
Data []Customer `json:"data"`
Links Links `json:"links,omitempty"`
}
type Customer struct {
Resource string `json:"resource"`
ID string `json:"id"`
Mode string `json:"mode"`
Name string `json:"name"`
Email string `json:"email"`
Locale string `json:"locale"`
Metadata string `json:"metadata,omitempty"`
RecentlyUsedMethods []string `json:"recentlyUsedMethods"`
CreatedDatetime time.Time `json:"createdDatetime"`
}
func newCustomers(co *core) *CustomerAPI {
return &CustomerAPI{co}
}
// New creates a new customer
func (api *CustomerAPI) New(data NewCustomerData) (*Customer, error) {
customer := &Customer{}
err := api.c.Post("customers", customer, data)
if err != nil {
return nil, err
}
return customer, nil
}
// Get retrieves customer information with the specified ID
func (api *CustomerAPI) Get(id string) (*Customer, error) {
customer := &Customer{}
err := api.c.Get("customers/"+id, customer)
if err != nil {
return nil, err
}
return customer, nil
}
// List returns a list of customers
func (api *CustomerAPI) List(offset, limit uint64) (*ListCustomersResponse, error) {
uri := "customers?offset="
uri += strconv.FormatUint(offset, 10)
uri += "&count="
uri += strconv.FormatUint(limit, 10)
response := &ListCustomersResponse{}
err := api.c.Get(uri, response)
if err != nil {
return nil, err
}
return response, nil
}
// NewPayment creates a new payment for a customer with the specified ID
func (api *CustomerAPI) NewPayment(id string, data PaymentData) (*PaymentReply, error) {
payment := &PaymentReply{}
err := api.c.Post("customers/"+id+"/payments", payment, data)
if err != nil {
return nil, err
}
return payment, nil
}
// Payments returns a list of payments for a customer with the specified ID
func (api *CustomerAPI) Payments(id string, offset uint64, limit uint64) ([]PaymentReply, error) {
uri := "customers/" + id + "/payments?offset="
uri += strconv.FormatUint(offset, 10)
uri += "&count="
uri += strconv.FormatUint(limit, 10)
response := &paymentReplyWrapper{}
err := api.c.Get(uri, response)
if err != nil {
return nil, err
}
return response.Data, nil
}