-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcustomers.go
226 lines (196 loc) · 6.85 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package conekta
import (
"encoding/json"
"net/http"
"path"
)
// Defines the public interface required to access available 'customers' methods
type CustomersAPI interface {
// Creates a new customer
// https://developers.conekta.com/api?language=bash#create-customer
Create(customer *Customer) error
// Updates a existing customer
// https://developers.conekta.com/api?language=bash#update-customer
Update(customer *Customer) error
// Deletes a existing customer
// https://developers.conekta.com/api?language=bash#capture-order
Delete(customerID string) error
// Creates new payment source
// https://developers.conekta.com/api?language=bash#payment-source
CreatePaymentSource(customerID, tokenID string) error
// Updates existing payment source
// https://developers.conekta.com/api?language=bash#update-payment-source
UpdatePaymentSource(customerID string, update *PaymentSourceUpdate) error
// Deletes existing payment source
// https://developers.conekta.com/api?language=bash#delete-payment-source
DeletePaymentSource(customerID, sourceID string) error
// Creates a new Shipping Contact for an existing customer
// https://developers.conekta.com/api?language=bash#create-shipping-contact-customer
CreateShippingContact(customerID string, contact *ShippingContact) error
// Updates an existing Shipping Contact
// https://developers.conekta.com/api?language=bash#update-shipping-contact
UpdateShippingContact(customerID string, contact *ShippingContact) error
// Deletes an existing Shipping Contact
// https://developers.conekta.com/api?language=bash#update-shipping-contact
DeleteShippingContact(customerID, contactID string) error
// Creates a new subscription using tokenized data
// https://developers.conekta.com/api?language=bash#create-subscription
CreateSubscription(customer *Customer, planID, cardID string) error
// Updates a subscription with a different card or plan
// https://developers.conekta.com/api?language=bash#update-subscription
UpdateSubscription(customer *Customer, planID, cardID string) error
// Pauses a subscription
// https://developers.conekta.com/api?language=bash#pause-subscription
PauseSubscription(customerID, subscriptionID string) error
// Resume a subscription
// https://developers.conekta.com/api?language=bash#resume-subscription
ResumeSubscription(customerID, subscriptionID string) error
// Cancel a subscription
// https://developers.conekta.com/api?language=bash#resume-subscription
CancelSubscription(customerID, subscriptionID string) error
}
type customersClient struct {
c *Client
}
func (cc *customersClient) Create(customer *Customer) error {
b, err := cc.c.request(&requestOptions{
endpoint: baseUrl + "customers",
method: http.MethodPost,
data: customer,
})
if err != nil {
return err
}
json.Unmarshal(b, customer)
return nil
}
func (cc *customersClient) Update(customer *Customer) error {
b, err := cc.c.request(&requestOptions{
endpoint: baseUrl + path.Join("customers", customer.ID),
method: http.MethodPut,
data: customer,
})
if err != nil {
return err
}
json.Unmarshal(b, customer)
return nil
}
func (cc *customersClient) Delete(customerID string) error {
_, err := cc.c.request(&requestOptions{
endpoint: baseUrl + path.Join("customers", customerID),
method: http.MethodDelete,
data: customerID,
})
return err
}
func (cc *customersClient) CreatePaymentSource(customerID, tokenID string) error {
data := map[string]string{
"type": "card",
"token_id": tokenID,
}
_, err := cc.c.request(&requestOptions{
endpoint: baseUrl + path.Join("customers", customerID, "payment_sources"),
method: http.MethodPost,
data: data,
})
if err != nil {
return err
}
return nil
}
func (cc *customersClient) UpdatePaymentSource(customerID string, update *PaymentSourceUpdate) error {
_, err := cc.c.request(&requestOptions{
endpoint: baseUrl + path.Join("customers", customerID, "payment_sources", update.ID),
method: http.MethodPut,
data: update,
})
return err
}
func (cc *customersClient) DeletePaymentSource(customerID, sourceID string) error {
_, err := cc.c.request(&requestOptions{
endpoint: baseUrl + path.Join("customers", customerID, "payment_sources", sourceID),
method: http.MethodDelete,
data: customerID,
})
return err
}
func (cc *customersClient) CreateShippingContact(customerID string, contact *ShippingContact) error {
b, err := cc.c.request(&requestOptions{
endpoint: baseUrl + path.Join("customers", customerID, "shipping_contacts"),
method: http.MethodPost,
data: contact,
})
if err != nil {
return err
}
json.Unmarshal(b, contact)
return nil
}
func (cc *customersClient) UpdateShippingContact(customerID string, contact *ShippingContact) error {
_, err := cc.c.request(&requestOptions{
endpoint: baseUrl + path.Join("customers", customerID, "shipping_contacts", contact.ID),
method: http.MethodPut,
data: contact,
})
if err != nil {
return err
}
return nil
}
func (cc *customersClient) DeleteShippingContact(customerID, contactID string) error {
_, err := cc.c.request(&requestOptions{
endpoint: baseUrl + path.Join("customers", customerID, "shipping_contacts", contactID),
method: http.MethodDelete,
data: customerID,
})
return err
}
func (cc *customersClient) CreateSubscription(customer *Customer, planID, cardID string) error {
data := map[string]string{"plan": planID}
if cardID != "" {
data["card"] = cardID
}
_, err := cc.c.request(&requestOptions{
endpoint: baseUrl + path.Join("customers", customer.ID, "subscription"),
method: http.MethodPost,
data: data,
})
return err
}
func (cc *customersClient) UpdateSubscription(customer *Customer, planID, cardID string) error {
data := map[string]string{"plan": planID}
if cardID != "" {
data["card"] = cardID
}
_, err := cc.c.request(&requestOptions{
endpoint: baseUrl + path.Join("customers", customer.ID, "subscription"),
method: http.MethodPut,
data: data,
})
return err
}
func (cc *customersClient) PauseSubscription(customerID, subscriptionID string) error {
_, err := cc.c.request(&requestOptions{
endpoint: baseUrl + path.Join("customers", customerID, "subscription", "pause"),
method: http.MethodPost,
data: map[string]string{"id": subscriptionID},
})
return err
}
func (cc *customersClient) ResumeSubscription(customerID, subscriptionID string) error {
_, err := cc.c.request(&requestOptions{
endpoint: baseUrl + path.Join("customers", customerID, "subscription", "resume"),
method: http.MethodPost,
data: map[string]string{"id": subscriptionID},
})
return err
}
func (cc *customersClient) CancelSubscription(customerID, subscriptionID string) error {
_, err := cc.c.request(&requestOptions{
endpoint: baseUrl + path.Join("customers", customerID, "subscription", "cancel"),
method: http.MethodPost,
data: map[string]string{"id": subscriptionID},
})
return err
}