-
Notifications
You must be signed in to change notification settings - Fork 1
/
customers_controller.go
265 lines (244 loc) · 12.7 KB
/
customers_controller.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package advancedbilling
import (
"context"
"fmt"
"github.com/apimatic/go-core-runtime/https"
"github.com/apimatic/go-core-runtime/utilities"
"github.com/maxio-com/ab-golang-sdk/errors"
"github.com/maxio-com/ab-golang-sdk/models"
"net/http"
)
// CustomersController represents a controller struct.
type CustomersController struct {
baseController
}
// NewCustomersController creates a new instance of CustomersController.
// It takes a baseController as a parameter and returns a pointer to the CustomersController.
func NewCustomersController(baseController baseController) *CustomersController {
customersController := CustomersController{baseController: baseController}
return &customersController
}
// CreateCustomer takes context, body as parameters and
// returns an models.ApiResponse with models.CustomerResponse data and
// an error if there was an issue with the request or response.
// You may create a new Customer at any time, or you may create a Customer at the same time you create a Subscription. The only validation restriction is that you may only create one customer for a given reference value.
// If provided, the `reference` value must be unique. It represents a unique identifier for the customer from your own app, i.e. the customer’s ID. This allows you to retrieve a given customer via a piece of shared information. Alternatively, you may choose to leave `reference` blank, and store Chargify’s unique ID for the customer, which is in the `id` attribute.
// Full documentation on how to locate, create and edit Customers in the Chargify UI can be located [here](https://chargify.zendesk.com/hc/en-us/articles/4407659914267).
// ## Required Country Format
// Chargify requires that you use the ISO Standard Country codes when formatting country attribute of the customer.
// Countries should be formatted as 2 characters. For more information, please see the following wikipedia article on [ISO_3166-1.](http://en.wikipedia.org/wiki/ISO_3166-1#Current_codes)
// ## Required State Format
// Chargify requires that you use the ISO Standard State codes when formatting state attribute of the customer.
// + US States (2 characters): [ISO_3166-2](https://en.wikipedia.org/wiki/ISO_3166-2:US)
// + States Outside the US (2-3 characters): To find the correct state codes outside of the US, please go to [ISO_3166-1](http://en.wikipedia.org/wiki/ISO_3166-1#Current_codes) and click on the link in the “ISO 3166-2 codes” column next to country you wish to populate.
// ## Locale
// Chargify allows you to attribute a language/region to your customer to deliver invoices in any required language.
// For more: [Customer Locale](https://chargify.zendesk.com/hc/en-us/articles/4407870384283#customer-locale)
func (c *CustomersController) CreateCustomer(
ctx context.Context,
body *models.CreateCustomerRequest) (
models.ApiResponse[models.CustomerResponse],
error) {
req := c.prepareRequest(ctx, "POST", "/customers.json")
req.Authenticate(NewAuth("BasicAuth"))
req.AppendErrors(map[string]https.ErrorBuilder[error]{
"422": {TemplatedMessage: "HTTP Response Not OK. Status code: {$statusCode}. Response: '{$response.body}'.", Unmarshaller: errors.NewCustomerErrorResponse},
})
req.Header("Content-Type", "application/json")
if body != nil {
req.Json(body)
}
var result models.CustomerResponse
decoder, resp, err := req.CallAsJson()
if err != nil {
return models.NewApiResponse(result, resp), err
}
result, err = utilities.DecodeResults[models.CustomerResponse](decoder)
return models.NewApiResponse(result, resp), err
}
// ListCustomersInput represents the input of the ListCustomers endpoint.
type ListCustomersInput struct {
// Direction to sort customers by time of creation
Direction *models.SortingDirection
// Result records are organized in pages. By default, the first page of results is displayed. The page parameter specifies a page number of results to fetch. You can start navigating through the pages to consume the results. You do this by passing in a page parameter. Retrieve the next page by adding ?page=2 to the query string. If there are no results to return, then an empty result set will be returned.
// Use in query `page=1`.
Page *int
// This parameter indicates how many records to fetch in each request. Default value is 50. The maximum allowed values is 200; any per_page value over 200 will be changed to 200.
// Use in query `per_page=200`.
PerPage *int
// The type of filter you would like to apply to your search.
// Use in query: `date_field=created_at`.
DateField *models.BasicDateField
// The start date (format YYYY-MM-DD) with which to filter the date_field. Returns subscriptions with a timestamp at or after midnight (12:00:00 AM) in your site’s time zone on the date specified.
StartDate *string
// The end date (format YYYY-MM-DD) with which to filter the date_field. Returns subscriptions with a timestamp up to and including 11:59:59PM in your site’s time zone on the date specified.
EndDate *string
// The start date and time (format YYYY-MM-DD HH:MM:SS) with which to filter the date_field. Returns subscriptions with a timestamp at or after exact time provided in query. You can specify timezone in query - otherwise your site's time zone will be used. If provided, this parameter will be used instead of start_date.
StartDatetime *string
// The end date and time (format YYYY-MM-DD HH:MM:SS) with which to filter the date_field. Returns subscriptions with a timestamp at or before exact time provided in query. You can specify timezone in query - otherwise your site's time zone will be used. If provided, this parameter will be used instead of end_date.
EndDatetime *string
// A search query by which to filter customers (can be an email, an ID, a reference, organization)
Q *string
}
// ListCustomers takes context, direction, page, perPage, dateField, startDate, endDate, startDatetime, endDatetime, q as parameters and
// returns an models.ApiResponse with []models.CustomerResponse data and
// an error if there was an issue with the request or response.
// This request will by default list all customers associated with your Site.
// ## Find Customer
// Use the search feature with the `q` query parameter to retrieve an array of customers that matches the search query.
// Common use cases are:
// + Search by an email
// + Search by a Chargify ID
// + Search by an organization
// + Search by a reference value from your application
// + Search by a first or last name
// To retrieve a single, exact match by reference, please use the [lookup endpoint](https://developers.chargify.com/docs/api-docs/b710d8fbef104-read-customer-by-reference).
func (c *CustomersController) ListCustomers(
ctx context.Context,
input ListCustomersInput) (
models.ApiResponse[[]models.CustomerResponse],
error) {
req := c.prepareRequest(ctx, "GET", "/customers.json")
req.Authenticate(NewAuth("BasicAuth"))
if input.Direction != nil {
req.QueryParam("direction", *input.Direction)
}
if input.Page != nil {
req.QueryParam("page", *input.Page)
}
if input.PerPage != nil {
req.QueryParam("per_page", *input.PerPage)
}
if input.DateField != nil {
req.QueryParam("date_field", *input.DateField)
}
if input.StartDate != nil {
req.QueryParam("start_date", *input.StartDate)
}
if input.EndDate != nil {
req.QueryParam("end_date", *input.EndDate)
}
if input.StartDatetime != nil {
req.QueryParam("start_datetime", *input.StartDatetime)
}
if input.EndDatetime != nil {
req.QueryParam("end_datetime", *input.EndDatetime)
}
if input.Q != nil {
req.QueryParam("q", *input.Q)
}
var result []models.CustomerResponse
decoder, resp, err := req.CallAsJson()
if err != nil {
return models.NewApiResponse(result, resp), err
}
result, err = utilities.DecodeResults[[]models.CustomerResponse](decoder)
return models.NewApiResponse(result, resp), err
}
// ReadCustomer takes context, id as parameters and
// returns an models.ApiResponse with models.CustomerResponse data and
// an error if there was an issue with the request or response.
// This method allows to retrieve the Customer properties by Chargify-generated Customer ID.
func (c *CustomersController) ReadCustomer(
ctx context.Context,
id int) (
models.ApiResponse[models.CustomerResponse],
error) {
req := c.prepareRequest(ctx, "GET", fmt.Sprintf("/customers/%v.json", id))
req.Authenticate(NewAuth("BasicAuth"))
var result models.CustomerResponse
decoder, resp, err := req.CallAsJson()
if err != nil {
return models.NewApiResponse(result, resp), err
}
result, err = utilities.DecodeResults[models.CustomerResponse](decoder)
return models.NewApiResponse(result, resp), err
}
// UpdateCustomer takes context, id, body as parameters and
// returns an models.ApiResponse with models.CustomerResponse data and
// an error if there was an issue with the request or response.
// This method allows to update the Customer.
func (c *CustomersController) UpdateCustomer(
ctx context.Context,
id int,
body *models.UpdateCustomerRequest) (
models.ApiResponse[models.CustomerResponse],
error) {
req := c.prepareRequest(ctx, "PUT", fmt.Sprintf("/customers/%v.json", id))
req.Authenticate(NewAuth("BasicAuth"))
req.AppendErrors(map[string]https.ErrorBuilder[error]{
"404": {TemplatedMessage: "Not Found:'{$response.body}'"},
"422": {TemplatedMessage: "HTTP Response Not OK. Status code: {$statusCode}. Response: '{$response.body}'.", Unmarshaller: errors.NewCustomerErrorResponse},
})
req.Header("Content-Type", "application/json")
if body != nil {
req.Json(body)
}
var result models.CustomerResponse
decoder, resp, err := req.CallAsJson()
if err != nil {
return models.NewApiResponse(result, resp), err
}
result, err = utilities.DecodeResults[models.CustomerResponse](decoder)
return models.NewApiResponse(result, resp), err
}
// DeleteCustomer takes context, id as parameters and
// returns an models.ApiResponse with data and
// an error if there was an issue with the request or response.
// This method allows you to delete the Customer.
func (c *CustomersController) DeleteCustomer(
ctx context.Context,
id int) (
*http.Response,
error) {
req := c.prepareRequest(ctx, "DELETE", fmt.Sprintf("/customers/%v.json", id))
req.Authenticate(NewAuth("BasicAuth"))
context, err := req.Call()
if err != nil {
return context.Response, err
}
return context.Response, err
}
// ReadCustomerByReference takes context, reference as parameters and
// returns an models.ApiResponse with models.CustomerResponse data and
// an error if there was an issue with the request or response.
// Use this method to return the customer object if you have the unique **Reference ID (Your App)** value handy. It will return a single match.
func (c *CustomersController) ReadCustomerByReference(
ctx context.Context,
reference string) (
models.ApiResponse[models.CustomerResponse],
error) {
req := c.prepareRequest(ctx, "GET", "/customers/lookup.json")
req.Authenticate(NewAuth("BasicAuth"))
req.QueryParam("reference", reference)
var result models.CustomerResponse
decoder, resp, err := req.CallAsJson()
if err != nil {
return models.NewApiResponse(result, resp), err
}
result, err = utilities.DecodeResults[models.CustomerResponse](decoder)
return models.NewApiResponse(result, resp), err
}
// ListCustomerSubscriptions takes context, customerId as parameters and
// returns an models.ApiResponse with []models.SubscriptionResponse data and
// an error if there was an issue with the request or response.
// This method lists all subscriptions that belong to a customer.
func (c *CustomersController) ListCustomerSubscriptions(
ctx context.Context,
customerId int) (
models.ApiResponse[[]models.SubscriptionResponse],
error) {
req := c.prepareRequest(
ctx,
"GET",
fmt.Sprintf("/customers/%v/subscriptions.json", customerId),
)
req.Authenticate(NewAuth("BasicAuth"))
var result []models.SubscriptionResponse
decoder, resp, err := req.CallAsJson()
if err != nil {
return models.NewApiResponse(result, resp), err
}
result, err = utilities.DecodeResults[[]models.SubscriptionResponse](decoder)
return models.NewApiResponse(result, resp), err
}