-
Notifications
You must be signed in to change notification settings - Fork 1
/
subscription_groups_controller.go
317 lines (291 loc) · 15.7 KB
/
subscription_groups_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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
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"
)
// SubscriptionGroupsController represents a controller struct.
type SubscriptionGroupsController struct {
baseController
}
// NewSubscriptionGroupsController creates a new instance of SubscriptionGroupsController.
// It takes a baseController as a parameter and returns a pointer to the SubscriptionGroupsController.
func NewSubscriptionGroupsController(baseController baseController) *SubscriptionGroupsController {
subscriptionGroupsController := SubscriptionGroupsController{baseController: baseController}
return &subscriptionGroupsController
}
// SignupWithSubscriptionGroup takes context, body as parameters and
// returns an models.ApiResponse with models.SubscriptionGroupSignupResponse data and
// an error if there was an issue with the request or response.
// Create multiple subscriptions at once under the same customer and consolidate them into a subscription group.
// You must provide one and only one of the `payer_id`/`payer_reference`/`payer_attributes` for the customer attached to the group.
// You must provide one and only one of the `payment_profile_id`/`credit_card_attributes`/`bank_account_attributes` for the payment profile attached to the group.
// Only one of the `subscriptions` can have `"primary": true` attribute set.
// When passing product to a subscription you can use either `product_id` or `product_handle` or `offer_id`. You can also use `custom_price` instead.
func (s *SubscriptionGroupsController) SignupWithSubscriptionGroup(
ctx context.Context,
body *models.SubscriptionGroupSignupRequest) (
models.ApiResponse[models.SubscriptionGroupSignupResponse],
error) {
req := s.prepareRequest(ctx, "POST", "/subscription_groups/signup.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.NewSubscriptionGroupSignupErrorResponse},
})
req.Header("Content-Type", "application/json")
if body != nil {
req.Json(body)
}
var result models.SubscriptionGroupSignupResponse
decoder, resp, err := req.CallAsJson()
if err != nil {
return models.NewApiResponse(result, resp), err
}
result, err = utilities.DecodeResults[models.SubscriptionGroupSignupResponse](decoder)
return models.NewApiResponse(result, resp), err
}
// CreateSubscriptionGroup takes context, body as parameters and
// returns an models.ApiResponse with models.SubscriptionGroupResponse data and
// an error if there was an issue with the request or response.
// Creates a subscription group with given members.
func (s *SubscriptionGroupsController) CreateSubscriptionGroup(
ctx context.Context,
body *models.CreateSubscriptionGroupRequest) (
models.ApiResponse[models.SubscriptionGroupResponse],
error) {
req := s.prepareRequest(ctx, "POST", "/subscription_groups.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.NewSubscriptionGroupCreateErrorResponse},
})
req.Header("Content-Type", "application/json")
if body != nil {
req.Json(body)
}
var result models.SubscriptionGroupResponse
decoder, resp, err := req.CallAsJson()
if err != nil {
return models.NewApiResponse(result, resp), err
}
result, err = utilities.DecodeResults[models.SubscriptionGroupResponse](decoder)
return models.NewApiResponse(result, resp), err
}
// ListSubscriptionGroupsInput represents the input of the ListSubscriptionGroups endpoint.
type ListSubscriptionGroupsInput struct {
// 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 20. 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
// A list of additional information to include in the response. The following values are supported:
// - `account_balances`: Account balance information for the subscription groups. Use in query: `include[]=account_balances`
Include []models.SubscriptionGroupsListInclude
}
// ListSubscriptionGroups takes context, page, perPage, include as parameters and
// returns an models.ApiResponse with models.ListSubscriptionGroupsResponse data and
// an error if there was an issue with the request or response.
// Returns an array of subscription groups for the site. The response is paginated and will return a `meta` key with pagination information.
// #### Account Balance Information
// Account balance information for the subscription groups is not returned by default. If this information is desired, the `include[]=account_balances` parameter must be provided with the request.
func (s *SubscriptionGroupsController) ListSubscriptionGroups(
ctx context.Context,
input ListSubscriptionGroupsInput) (
models.ApiResponse[models.ListSubscriptionGroupsResponse],
error) {
req := s.prepareRequest(ctx, "GET", "/subscription_groups.json")
req.Authenticate(NewAuth("BasicAuth"))
if input.Page != nil {
req.QueryParamWithArraySerializationOption("page", *input.Page, https.UnIndexed)
}
if input.PerPage != nil {
req.QueryParamWithArraySerializationOption("per_page", *input.PerPage, https.UnIndexed)
}
if input.Include != nil {
req.QueryParamWithArraySerializationOption("include", input.Include, https.UnIndexed)
}
var result models.ListSubscriptionGroupsResponse
decoder, resp, err := req.CallAsJson()
if err != nil {
return models.NewApiResponse(result, resp), err
}
result, err = utilities.DecodeResults[models.ListSubscriptionGroupsResponse](decoder)
return models.NewApiResponse(result, resp), err
}
// ReadSubscriptionGroup takes context, uid, include as parameters and
// returns an models.ApiResponse with models.FullSubscriptionGroupResponse data and
// an error if there was an issue with the request or response.
// Use this endpoint to find subscription group details.
// #### Current Billing Amount in Cents
// Current billing amount for the subscription group is not returned by default. If this information is desired, the `include[]=current_billing_amount_in_cents` parameter must be provided with the request.
func (s *SubscriptionGroupsController) ReadSubscriptionGroup(
ctx context.Context,
uid string,
include []models.SubscriptionGroupInclude) (
models.ApiResponse[models.FullSubscriptionGroupResponse],
error) {
req := s.prepareRequest(
ctx,
"GET",
fmt.Sprintf("/subscription_groups/%v.json", uid),
)
req.Authenticate(NewAuth("BasicAuth"))
if include != nil {
req.QueryParamWithArraySerializationOption("include", include, https.UnIndexed)
}
var result models.FullSubscriptionGroupResponse
decoder, resp, err := req.CallAsJson()
if err != nil {
return models.NewApiResponse(result, resp), err
}
result, err = utilities.DecodeResults[models.FullSubscriptionGroupResponse](decoder)
return models.NewApiResponse(result, resp), err
}
// UpdateSubscriptionGroupMembers takes context, uid, body as parameters and
// returns an models.ApiResponse with models.SubscriptionGroupResponse data and
// an error if there was an issue with the request or response.
// Use this endpoint to update subscription group members.
// `"member_ids"` should contain an array of both subscription IDs to set as group members and subscription IDs already present in the groups. Not including them will result in removing them from subscription group. To clean up members, just leave the array empty.
func (s *SubscriptionGroupsController) UpdateSubscriptionGroupMembers(
ctx context.Context,
uid string,
body *models.UpdateSubscriptionGroupRequest) (
models.ApiResponse[models.SubscriptionGroupResponse],
error) {
req := s.prepareRequest(
ctx,
"PUT",
fmt.Sprintf("/subscription_groups/%v.json", uid),
)
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.NewSubscriptionGroupUpdateErrorResponse},
})
req.Header("Content-Type", "application/json")
if body != nil {
req.Json(body)
}
var result models.SubscriptionGroupResponse
decoder, resp, err := req.CallAsJson()
if err != nil {
return models.NewApiResponse(result, resp), err
}
result, err = utilities.DecodeResults[models.SubscriptionGroupResponse](decoder)
return models.NewApiResponse(result, resp), err
}
// DeleteSubscriptionGroup takes context, uid as parameters and
// returns an models.ApiResponse with models.DeleteSubscriptionGroupResponse data and
// an error if there was an issue with the request or response.
// Use this endpoint to delete subscription group.
// Only groups without members can be deleted
func (s *SubscriptionGroupsController) DeleteSubscriptionGroup(
ctx context.Context,
uid string) (
models.ApiResponse[models.DeleteSubscriptionGroupResponse],
error) {
req := s.prepareRequest(
ctx,
"DELETE",
fmt.Sprintf("/subscription_groups/%v.json", uid),
)
req.Authenticate(NewAuth("BasicAuth"))
req.AppendErrors(map[string]https.ErrorBuilder[error]{
"404": {TemplatedMessage: "Not Found:'{$response.body}'"},
})
var result models.DeleteSubscriptionGroupResponse
decoder, resp, err := req.CallAsJson()
if err != nil {
return models.NewApiResponse(result, resp), err
}
result, err = utilities.DecodeResults[models.DeleteSubscriptionGroupResponse](decoder)
return models.NewApiResponse(result, resp), err
}
// FindSubscriptionGroup takes context, subscriptionId as parameters and
// returns an models.ApiResponse with models.FullSubscriptionGroupResponse data and
// an error if there was an issue with the request or response.
// Use this endpoint to find subscription group associated with subscription.
// If the subscription is not in a group endpoint will return 404 code.
func (s *SubscriptionGroupsController) FindSubscriptionGroup(
ctx context.Context,
subscriptionId string) (
models.ApiResponse[models.FullSubscriptionGroupResponse],
error) {
req := s.prepareRequest(ctx, "GET", "/subscription_groups/lookup.json")
req.Authenticate(NewAuth("BasicAuth"))
req.AppendErrors(map[string]https.ErrorBuilder[error]{
"404": {TemplatedMessage: "Not Found:'{$response.body}'"},
})
req.QueryParam("subscription_id", subscriptionId)
var result models.FullSubscriptionGroupResponse
decoder, resp, err := req.CallAsJson()
if err != nil {
return models.NewApiResponse(result, resp), err
}
result, err = utilities.DecodeResults[models.FullSubscriptionGroupResponse](decoder)
return models.NewApiResponse(result, resp), err
}
// AddSubscriptionToGroup takes context, subscriptionId, body as parameters and
// returns an models.ApiResponse with models.SubscriptionGroupResponse data and
// an error if there was an issue with the request or response.
// For sites making use of the [Relationship Billing](https://chargify.zendesk.com/hc/en-us/articles/4407737494171) and [Customer Hierarchy](https://chargify.zendesk.com/hc/en-us/articles/4407746683291) features, it is possible to add existing subscriptions to subscription groups.
// Passing `group` parameters with a `target` containing a `type` and optional `id` is all that's needed. When the `target` parameter specifies a `"customer"` or `"subscription"` that is already part of a hierarchy, the subscription will become a member of the customer's subscription group. If the target customer or subscription is not part of a subscription group, a new group will be created and the subscription will become part of the group with the specified target customer set as the responsible payer for the group's subscriptions.
// **Please Note:** In order to add an existing subscription to a subscription group, it must belong to either the same customer record as the target, or be within the same customer hierarchy.
// Rather than specifying a customer, the `target` parameter could instead simply have a value of
// * `"self"` which indicates the subscription will be paid for not by some other customer, but by the subscribing customer,
// * `"parent"` which indicates the subscription will be paid for by the subscribing customer's parent within a customer hierarchy, or
// * `"eldest"` which indicates the subscription will be paid for by the root-level customer in the subscribing customer's hierarchy.
// To create a new subscription into a subscription group, please reference the following:
// [Create Subscription in a Subscription Group](https://developers.chargify.com/docs/api-docs/d571659cf0f24-create-subscription#subscription-in-a-subscription-group)
func (s *SubscriptionGroupsController) AddSubscriptionToGroup(
ctx context.Context,
subscriptionId int,
body *models.AddSubscriptionToAGroup) (
models.ApiResponse[models.SubscriptionGroupResponse],
error) {
req := s.prepareRequest(
ctx,
"POST",
fmt.Sprintf("/subscriptions/%v/group.json", subscriptionId),
)
req.Authenticate(NewAuth("BasicAuth"))
req.Header("Content-Type", "application/json")
if body != nil {
req.Json(body)
}
var result models.SubscriptionGroupResponse
decoder, resp, err := req.CallAsJson()
if err != nil {
return models.NewApiResponse(result, resp), err
}
result, err = utilities.DecodeResults[models.SubscriptionGroupResponse](decoder)
return models.NewApiResponse(result, resp), err
}
// RemoveSubscriptionFromGroup takes context, subscriptionId as parameters and
// returns an models.ApiResponse with data and
// an error if there was an issue with the request or response.
// For sites making use of the [Relationship Billing](https://chargify.zendesk.com/hc/en-us/articles/4407737494171) and [Customer Hierarchy](https://chargify.zendesk.com/hc/en-us/articles/4407746683291) features, it is possible to remove existing subscription from subscription group.
func (s *SubscriptionGroupsController) RemoveSubscriptionFromGroup(
ctx context.Context,
subscriptionId int) (
*http.Response,
error) {
req := s.prepareRequest(
ctx,
"DELETE",
fmt.Sprintf("/subscriptions/%v/group.json", subscriptionId),
)
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.NewErrorListResponse},
})
context, err := req.Call()
if err != nil {
return context.Response, err
}
return context.Response, err
}