-
Notifications
You must be signed in to change notification settings - Fork 1
/
subscription_invoice_account_controller.go
246 lines (224 loc) · 10.3 KB
/
subscription_invoice_account_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
/*
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"
"net/http"
)
// SubscriptionInvoiceAccountController represents a controller struct.
type SubscriptionInvoiceAccountController struct {
baseController
}
// NewSubscriptionInvoiceAccountController creates a new instance of SubscriptionInvoiceAccountController.
// It takes a baseController as a parameter and returns a pointer to the SubscriptionInvoiceAccountController.
func NewSubscriptionInvoiceAccountController(baseController baseController) *SubscriptionInvoiceAccountController {
subscriptionInvoiceAccountController := SubscriptionInvoiceAccountController{baseController: baseController}
return &subscriptionInvoiceAccountController
}
// ReadAccountBalances takes context, subscriptionId as parameters and
// returns an models.ApiResponse with models.AccountBalances data and
// an error if there was an issue with the request or response.
// Returns the `balance_in_cents` of the Subscription's Pending Discount, Service Credit, and Prepayment accounts, as well as the sum of the Subscription's open, payable invoices.
func (s *SubscriptionInvoiceAccountController) ReadAccountBalances(
ctx context.Context,
subscriptionId int) (
models.ApiResponse[models.AccountBalances],
error) {
req := s.prepareRequest(
ctx,
"GET",
fmt.Sprintf("/subscriptions/%v/account_balances.json", subscriptionId),
)
req.Authenticate(NewAuth("BasicAuth"))
var result models.AccountBalances
decoder, resp, err := req.CallAsJson()
if err != nil {
return models.NewApiResponse(result, resp), err
}
result, err = utilities.DecodeResults[models.AccountBalances](decoder)
return models.NewApiResponse(result, resp), err
}
// CreatePrepayment takes context, subscriptionId, body as parameters and
// returns an models.ApiResponse with models.CreatePrepaymentResponse data and
// an error if there was an issue with the request or response.
// ## Create Prepayment
// In order to specify a prepayment made against a subscription, specify the `amount, memo, details, method`.
// When the `method` specified is `"credit_card_on_file"`, the prepayment amount will be collected using the default credit card payment profile and applied to the prepayment account balance. This is especially useful for manual replenishment of prepaid subscriptions.
// Please note that you **can't** pass `amount_in_cents`.
func (s *SubscriptionInvoiceAccountController) CreatePrepayment(
ctx context.Context,
subscriptionId int,
body *models.CreatePrepaymentRequest) (
models.ApiResponse[models.CreatePrepaymentResponse],
error) {
req := s.prepareRequest(
ctx,
"POST",
fmt.Sprintf("/subscriptions/%v/prepayments.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}'."},
})
req.Header("Content-Type", "application/json")
if body != nil {
req.Json(body)
}
var result models.CreatePrepaymentResponse
decoder, resp, err := req.CallAsJson()
if err != nil {
return models.NewApiResponse(result, resp), err
}
result, err = utilities.DecodeResults[models.CreatePrepaymentResponse](decoder)
return models.NewApiResponse(result, resp), err
}
// ListPrepaymentsInput represents the input of the ListPrepayments endpoint.
type ListPrepaymentsInput struct {
// The Chargify id of the subscription
SubscriptionId int
// 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
// Filter to use for List Prepayments operations
Filter *models.ListPrepaymentsFilter
}
// ListPrepayments takes context, subscriptionId, page, perPage, filter as parameters and
// returns an models.ApiResponse with models.PrepaymentsResponse data and
// an error if there was an issue with the request or response.
// This request will list a subscription's prepayments.
func (s *SubscriptionInvoiceAccountController) ListPrepayments(
ctx context.Context,
input ListPrepaymentsInput) (
models.ApiResponse[models.PrepaymentsResponse],
error) {
req := s.prepareRequest(
ctx,
"GET",
fmt.Sprintf("/subscriptions/%v/prepayments.json", input.SubscriptionId),
)
req.Authenticate(NewAuth("BasicAuth"))
req.AppendErrors(map[string]https.ErrorBuilder[error]{
"404": {TemplatedMessage: "Not Found:'{$response.body}'"},
})
if input.Page != nil {
req.QueryParam("page", *input.Page)
}
if input.PerPage != nil {
req.QueryParam("per_page", *input.PerPage)
}
if input.Filter != nil {
req.QueryParam("filter", *input.Filter)
}
var result models.PrepaymentsResponse
decoder, resp, err := req.CallAsJson()
if err != nil {
return models.NewApiResponse(result, resp), err
}
result, err = utilities.DecodeResults[models.PrepaymentsResponse](decoder)
return models.NewApiResponse(result, resp), err
}
// IssueServiceCredit takes context, subscriptionId, body as parameters and
// returns an models.ApiResponse with models.ServiceCredit data and
// an error if there was an issue with the request or response.
// Credit will be added to the subscription in the amount specified in the request body. The credit is subsequently applied to the next generated invoice.
func (s *SubscriptionInvoiceAccountController) IssueServiceCredit(
ctx context.Context,
subscriptionId int,
body *models.IssueServiceCreditRequest) (
models.ApiResponse[models.ServiceCredit],
error) {
req := s.prepareRequest(
ctx,
"POST",
fmt.Sprintf("/subscriptions/%v/service_credits.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}'."},
})
req.Header("Content-Type", "application/json")
if body != nil {
req.Json(body)
}
var result models.ServiceCredit
decoder, resp, err := req.CallAsJson()
if err != nil {
return models.NewApiResponse(result, resp), err
}
result, err = utilities.DecodeResults[models.ServiceCredit](decoder)
return models.NewApiResponse(result, resp), err
}
// DeductServiceCredit takes context, subscriptionId, body as parameters and
// returns an *Response and
// an error if there was an issue with the request or response.
// Credit will be removed from the subscription in the amount specified in the request body. The credit amount being deducted must be equal to or less than the current credit balance.
func (s *SubscriptionInvoiceAccountController) DeductServiceCredit(
ctx context.Context,
subscriptionId int,
body *models.DeductServiceCreditRequest) (
*http.Response,
error) {
req := s.prepareRequest(
ctx,
"POST",
fmt.Sprintf("/subscriptions/%v/service_credit_deductions.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}'."},
})
req.Header("Content-Type", "application/json")
if body != nil {
req.Json(body)
}
httpCtx, err := req.Call()
if err != nil {
return httpCtx.Response, err
}
return httpCtx.Response, err
}
// RefundPrepayment takes context, subscriptionId, prepaymentId, body as parameters and
// returns an models.ApiResponse with models.PrepaymentResponse data and
// an error if there was an issue with the request or response.
// This endpoint will refund, completely or partially, a particular prepayment applied to a subscription. The `prepayment_id` will be the account transaction ID of the original payment. The prepayment must have some amount remaining in order to be refunded.
// The amount may be passed either as a decimal, with `amount`, or an integer in cents, with `amount_in_cents`.
func (s *SubscriptionInvoiceAccountController) RefundPrepayment(
ctx context.Context,
subscriptionId int,
prepaymentId int64,
body *models.RefundPrepaymentRequest) (
models.ApiResponse[models.PrepaymentResponse],
error) {
req := s.prepareRequest(
ctx,
"POST",
fmt.Sprintf("/subscriptions/%v/prepayments/%v/refunds.json", subscriptionId, prepaymentId),
)
req.Authenticate(NewAuth("BasicAuth"))
req.AppendErrors(map[string]https.ErrorBuilder[error]{
"404": {TemplatedMessage: "Not Found:'{$response.body}'"},
"400": {TemplatedMessage: "HTTP Response Not OK. Status code: {$statusCode}. Response: '{$response.body}'.", Unmarshaller: errors.NewRefundPrepaymentBaseErrorsResponse},
"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.PrepaymentResponse
decoder, resp, err := req.CallAsJson()
if err != nil {
return models.NewApiResponse(result, resp), err
}
result, err = utilities.DecodeResults[models.PrepaymentResponse](decoder)
return models.NewApiResponse(result, resp), err
}