-
Notifications
You must be signed in to change notification settings - Fork 1
/
subscription_status_controller.go
474 lines (440 loc) · 23 KB
/
subscription_status_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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
Package advancedbilling
This file was automatically generated for Maxio by APIMATIC v3.0 ( https://www.apimatic.io ).
*/
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"
)
// SubscriptionStatusController represents a controller struct.
type SubscriptionStatusController struct {
baseController
}
// NewSubscriptionStatusController creates a new instance of SubscriptionStatusController.
// It takes a baseController as a parameter and returns a pointer to the SubscriptionStatusController.
func NewSubscriptionStatusController(baseController baseController) *SubscriptionStatusController {
subscriptionStatusController := SubscriptionStatusController{baseController: baseController}
return &subscriptionStatusController
}
// RetrySubscription takes context, subscriptionId as parameters and
// returns an models.ApiResponse with models.SubscriptionResponse data and
// an error if there was an issue with the request or response.
// Advanced Billing offers the ability to retry collecting the balance due on a past due Subscription without waiting for the next scheduled attempt.
// ## Successful Reactivation
// The response will be `200 OK` with the updated Subscription.
// ## Failed Reactivation
// The response will be `422 "Unprocessable Entity`.
func (s *SubscriptionStatusController) RetrySubscription(
ctx context.Context,
subscriptionId int) (
models.ApiResponse[models.SubscriptionResponse],
error) {
req := s.prepareRequest(
ctx,
"PUT",
fmt.Sprintf("/subscriptions/%v/retry.json", subscriptionId),
)
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.NewErrorListResponse},
})
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
}
// CancelSubscription takes context, subscriptionId, body as parameters and
// returns an models.ApiResponse with models.SubscriptionResponse data and
// an error if there was an issue with the request or response.
// The DELETE action causes the cancellation of the Subscription. This means, the method sets the Subscription state to "canceled".
func (s *SubscriptionStatusController) CancelSubscription(
ctx context.Context,
subscriptionId int,
body *models.CancellationRequest) (
models.ApiResponse[models.SubscriptionResponse],
error) {
req := s.prepareRequest(
ctx,
"DELETE",
fmt.Sprintf("/subscriptions/%v.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}'."},
})
req.Header("Content-Type", "application/json")
if body != nil {
req.Json(body)
}
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
}
// ResumeSubscription takes context, subscriptionId, calendarBillingResumptionCharge as parameters and
// returns an models.ApiResponse with models.SubscriptionResponse data and
// an error if there was an issue with the request or response.
// Resume a paused (on-hold) subscription. If the normal next renewal date has not passed, the subscription will return to active and will renew on that date. Otherwise, it will behave like a reactivation, setting the billing date to 'now' and charging the subscriber.
func (s *SubscriptionStatusController) ResumeSubscription(
ctx context.Context,
subscriptionId int,
calendarBillingResumptionCharge *models.ResumptionCharge) (
models.ApiResponse[models.SubscriptionResponse],
error) {
req := s.prepareRequest(
ctx,
"POST",
fmt.Sprintf("/subscriptions/%v/resume.json", subscriptionId),
)
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.NewErrorListResponse},
})
if calendarBillingResumptionCharge != nil {
req.QueryParam("calendar_billing['resumption_charge']", *calendarBillingResumptionCharge)
}
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
}
// PauseSubscription takes context, subscriptionId, body 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 will place the subscription in the on_hold state and it will not renew.
// ## Limitations
// You may not place a subscription on hold if the `next_billing` date is within 24 hours.
func (s *SubscriptionStatusController) PauseSubscription(
ctx context.Context,
subscriptionId int,
body *models.PauseRequest) (
models.ApiResponse[models.SubscriptionResponse],
error) {
req := s.prepareRequest(
ctx,
"POST",
fmt.Sprintf("/subscriptions/%v/hold.json", subscriptionId),
)
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.NewErrorListResponse},
})
req.Header("Content-Type", "application/json")
if body != nil {
req.Json(body)
}
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
}
// UpdateAutomaticSubscriptionResumption takes context, subscriptionId, body as parameters and
// returns an models.ApiResponse with models.SubscriptionResponse data and
// an error if there was an issue with the request or response.
// Once a subscription has been paused / put on hold, you can update the date which was specified to automatically resume the subscription.
// To update a subscription's resume date, use this method to change or update the `automatically_resume_at` date.
// ### Remove the resume date
// Alternately, you can change the `automatically_resume_at` to `null` if you would like the subscription to not have a resume date.
func (s *SubscriptionStatusController) UpdateAutomaticSubscriptionResumption(
ctx context.Context,
subscriptionId int,
body *models.PauseRequest) (
models.ApiResponse[models.SubscriptionResponse],
error) {
req := s.prepareRequest(
ctx,
"PUT",
fmt.Sprintf("/subscriptions/%v/hold.json", subscriptionId),
)
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.NewErrorListResponse},
})
req.Header("Content-Type", "application/json")
if body != nil {
req.Json(body)
}
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
}
// ReactivateSubscription takes context, subscriptionId, body as parameters and
// returns an models.ApiResponse with models.SubscriptionResponse data and
// an error if there was an issue with the request or response.
// Advanced Billing offers the ability to reactivate a previously canceled subscription. For details on how the reactivation works, and how to reactivate subscriptions through the application, see [reactivation](https://maxio.zendesk.com/hc/en-us/articles/24252109503629-Reactivating-and-Resuming).
// **Please note: The term
// "resume" is used also during another process in Advanced Billing. This occurs when an on-hold subscription is "resumed". This returns the subscription to an active state.**
// + The response returns the subscription object in the `active` or `trialing` state.
// + The `canceled_at` and `cancellation_message` fields do not have values.
// + The method works for "Canceled" or "Trial Ended" subscriptions.
// + It will not work for items not marked as "Canceled", "Unpaid", or "Trial Ended".
// ## Resume the current billing period for a subscription
// A subscription is considered "resumable" if you are attempting to reactivate within the billing period the subscription was canceled in.
// A resumed subscription's billing date remains the same as before it was canceled. In other words, it does not start a new billing period. Payment may or may not be collected for a resumed subscription, depending on whether or not the subscription had a balance when it was canceled (for example, if it was canceled because of dunning).
// Consider a subscription which was created on June 1st, and would renew on July 1st. The subscription is then canceled on June 15.
// If a reactivation with `resume: true` were attempted _before_ what would have been the next billing date of July 1st, then Advanced Billing would resume the subscription.
// If a reactivation with `resume: true` were attempted _after_ what would have been the next billing date of July 1st, then Advanced Billing would not resume the subscription, and instead it would be reactivated with a new billing period.
// | Canceled | Reactivation | Resumable? |
// |---|---|---|
// | Jun 15 | June 28 | Yes |
// | Jun 15 | July 2 | No |
// ## Reactivation Scenarios
// ### Reactivating Canceled Subscription While Preserving Balance
// + Given you have a product that costs $20
// + Given you have a canceled subscription to the $20 product
// + 1 charge should exist for $20
// + 1 payment should exist for $20
// + When the subscription has canceled due to dunning, it retained a negative balance of $20
// #### Results
// The resulting charges upon reactivation will be:
// + 1 charge for $20 for the new product
// + 1 charge for $20 for the balance due
// + Total charges = $40
// + The subscription will transition to active
// + The subscription balance will be zero
// ### Reactivating a Canceled Subscription With Coupon
// + Given you have a canceled subscription
// + It has no current period defined
// + You have a coupon code "EARLYBIRD"
// + The coupon is set to recur for 6 periods
// PUT request sent to:
// `https://acme.chargify.com/subscriptions/{subscription_id}/reactivate.json?coupon_code=EARLYBIRD`
// #### Results
// + The subscription will transition to active
// + The subscription should have applied a coupon with code "EARLYBIRD"
// ### Reactivating Canceled Subscription With a Trial, Without the include_trial Flag
// + Given you have a canceled subscription
// + The product associated with the subscription has a trial
// + PUT request to
// `https://acme.chargify.com/subscriptions/{subscription_id}/reactivate.json`
// #### Results
// + The subscription will transition to active
// ### Reactivating Canceled Subscription With Trial, With the include_trial Flag
// + Given you have a canceled subscription
// + The product associated with the subscription has a trial
// + Send a PUT request to `https://acme.chargify.com/subscriptions/{subscription_id}/reactivate.json?include_trial=1`
// #### Results
// + The subscription will transition to trialing
// ### Reactivating Trial Ended Subscription
// + Given you have a trial_ended subscription
// + Send a PUT request to `https://acme.chargify.com/subscriptions/{subscription_id}/reactivate.json`
// #### Results
// + The subscription will transition to active
// ### Resuming a Canceled Subscription
// + Given you have a `canceled` subscription and it is resumable
// + Send a PUT request to `https://acme.chargify.com/subscriptions/{subscription_id}/reactivate.json?resume=true`
// #### Results
// + The subscription will transition to active
// + The next billing date should not have changed
// ### Attempting to resume a subscription which is not resumable
// + Given you have a `canceled` subscription, and it is not resumable
// + Send a PUT request to `https://acme.chargify.com/subscriptions/{subscription_id}/reactivate.json?resume=true`
// #### Results
// + The subscription will transition to active, with a new billing period.
// ### Attempting to resume but not reactivate a subscription which is not resumable
// + Given you have a `canceled` subscription, and it is not resumable
// + Send a PUT request to `https://acme.chargify.com/subscriptions/{subscription_id}/reactivate.json?resume[require_resume]=true`
// + The response status should be "422 UNPROCESSABLE ENTITY"
// + The subscription should be canceled with the following response
// ```
// {
// "errors": ["Request was 'resume only', but this subscription cannot be resumed."]
// }
// ```
// #### Results
// + The subscription should remain `canceled`
// + The next billing date should not have changed
// ### Resuming Subscription Which Was Trialing
// + Given you have a `trial_ended` subscription, and it is resumable
// + And the subscription was canceled in the middle of a trial
// + And there is still time left on the trial
// + Send a PUT request to `https://acme.chargify.com/subscriptions/{subscription_id}/reactivate.json?resume=true`
// #### Results
// + The subscription will transition to trialing
// + The next billing date should not have changed
// ### Resuming Subscription Which Was trial_ended
// + Given you have a `trial_ended` subscription, and it is resumable
// + Send a PUT request to `https://acme.chargify.com/subscriptions/{subscription_id}/reactivate.json?resume=true`
// #### Results
// + The subscription will transition to active
// + The next billing date should not have changed
// + Any product-related charges should have been collected
func (s *SubscriptionStatusController) ReactivateSubscription(
ctx context.Context,
subscriptionId int,
body *models.ReactivateSubscriptionRequest) (
models.ApiResponse[models.SubscriptionResponse],
error) {
req := s.prepareRequest(
ctx,
"PUT",
fmt.Sprintf("/subscriptions/%v/reactivate.json", subscriptionId),
)
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.NewErrorListResponse},
})
req.Header("Content-Type", "application/json")
if body != nil {
req.Json(body)
}
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
}
// InitiateDelayedCancellation takes context, subscriptionId, body as parameters and
// returns an models.ApiResponse with models.DelayedCancellationResponse data and
// an error if there was an issue with the request or response.
// Advanced Billing offers the ability to cancel a subscription at the end of the current billing period. This period is set by its current product.
// Requesting to cancel the subscription at the end of the period sets the `cancel_at_end_of_period` flag to true.
// Note that you cannot set `cancel_at_end_of_period` at subscription creation, or if the subscription is past due.
func (s *SubscriptionStatusController) InitiateDelayedCancellation(
ctx context.Context,
subscriptionId int,
body *models.CancellationRequest) (
models.ApiResponse[models.DelayedCancellationResponse],
error) {
req := s.prepareRequest(
ctx,
"POST",
fmt.Sprintf("/subscriptions/%v/delayed_cancel.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},
})
req.Header("Content-Type", "application/json")
if body != nil {
req.Json(body)
}
var result models.DelayedCancellationResponse
decoder, resp, err := req.CallAsJson()
if err != nil {
return models.NewApiResponse(result, resp), err
}
result, err = utilities.DecodeResults[models.DelayedCancellationResponse](decoder)
return models.NewApiResponse(result, resp), err
}
// CancelDelayedCancellation takes context, subscriptionId as parameters and
// returns an models.ApiResponse with models.DelayedCancellationResponse data and
// an error if there was an issue with the request or response.
// Removing the delayed cancellation on a subscription will ensure that it doesn't get canceled at the end of the period that it is in. The request will reset the `cancel_at_end_of_period` flag to `false`.
// This endpoint is idempotent. If the subscription was not set to cancel in the future, removing the delayed cancellation has no effect and the call will be successful.
func (s *SubscriptionStatusController) CancelDelayedCancellation(
ctx context.Context,
subscriptionId int) (
models.ApiResponse[models.DelayedCancellationResponse],
error) {
req := s.prepareRequest(
ctx,
"DELETE",
fmt.Sprintf("/subscriptions/%v/delayed_cancel.json", subscriptionId),
)
req.Authenticate(NewAuth("BasicAuth"))
req.AppendErrors(map[string]https.ErrorBuilder[error]{
"404": {TemplatedMessage: "Not Found:'{$response.body}'"},
})
var result models.DelayedCancellationResponse
decoder, resp, err := req.CallAsJson()
if err != nil {
return models.NewApiResponse(result, resp), err
}
result, err = utilities.DecodeResults[models.DelayedCancellationResponse](decoder)
return models.NewApiResponse(result, resp), err
}
// CancelDunning takes context, subscriptionId as parameters and
// returns an models.ApiResponse with models.SubscriptionResponse data and
// an error if there was an issue with the request or response.
// If a subscription is currently in dunning, the subscription will be set to active and the active Dunner will be resolved.
func (s *SubscriptionStatusController) CancelDunning(
ctx context.Context,
subscriptionId int) (
models.ApiResponse[models.SubscriptionResponse],
error) {
req := s.prepareRequest(
ctx,
"POST",
fmt.Sprintf("/subscriptions/%v/cancel_dunning.json", subscriptionId),
)
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.NewErrorListResponse},
})
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
}
// PreviewRenewal takes context, subscriptionId, body as parameters and
// returns an models.ApiResponse with models.RenewalPreviewResponse data and
// an error if there was an issue with the request or response.
// The Chargify API allows you to preview a renewal by posting to the renewals endpoint. Renewal Preview is an object representing a subscription’s next assessment. You can retrieve it to see a snapshot of how much your customer will be charged on their next renewal.
// The "Next Billing" amount and "Next Billing" date are already represented in the UI on each Subscriber's Summary. For more information, please see our documentation [here](https://maxio.zendesk.com/hc/en-us/articles/24252493695757-Subscriber-Interface-Overview).
// ## Optional Component Fields
// This endpoint is particularly useful due to the fact that it will return the computed billing amount for the base product and the components which are in use by a subscriber.
// By default, the preview will include billing details for all components _at their **current** quantities_. This means:
// * Current `allocated_quantity` for quantity-based components
// * Current enabled/disabled status for on/off components
// * Current metered usage `unit_balance` for metered components
// * Current metric quantity value for events recorded thus far for events-based components
// In the above statements, "current" means the quantity or value as of the call to the renewal preview endpoint. We do not predict end-of-period values for components, so metered or events-based usage may be less than it will eventually be at the end of the period.
// Optionally, **you may provide your own custom quantities** for any component to see a billing preview for non-current quantities. This is accomplished by sending a request body with data under the `components` key. See the request body documentation below.
// ## Subscription Side Effects
// You can request a `POST` to obtain this data from the endpoint without any side effects. Plain and simple, this will preview data, not log any changes against a subscription.
func (s *SubscriptionStatusController) PreviewRenewal(
ctx context.Context,
subscriptionId int,
body *models.RenewalPreviewRequest) (
models.ApiResponse[models.RenewalPreviewResponse],
error) {
req := s.prepareRequest(
ctx,
"POST",
fmt.Sprintf("/subscriptions/%v/renewals/preview.json", subscriptionId),
)
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.NewErrorListResponse},
})
req.Header("Content-Type", "application/json")
if body != nil {
req.Json(body)
}
var result models.RenewalPreviewResponse
decoder, resp, err := req.CallAsJson()
if err != nil {
return models.NewApiResponse(result, resp), err
}
result, err = utilities.DecodeResults[models.RenewalPreviewResponse](decoder)
return models.NewApiResponse(result, resp), err
}