forked from orbcorp/orb-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustomerbalancetransaction.go
More file actions
336 lines (301 loc) · 16.5 KB
/
customerbalancetransaction.go
File metadata and controls
336 lines (301 loc) · 16.5 KB
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
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package orb
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"time"
"github.com/orbcorp/orb-go/internal/apijson"
"github.com/orbcorp/orb-go/internal/apiquery"
"github.com/orbcorp/orb-go/internal/param"
"github.com/orbcorp/orb-go/internal/requestconfig"
"github.com/orbcorp/orb-go/option"
"github.com/orbcorp/orb-go/packages/pagination"
"github.com/orbcorp/orb-go/shared"
)
// CustomerBalanceTransactionService contains methods and other services that help
// with interacting with the orb API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
// the [NewCustomerBalanceTransactionService] method instead.
type CustomerBalanceTransactionService struct {
Options []option.RequestOption
}
// NewCustomerBalanceTransactionService generates a new service that applies the
// given options to each request. These options are applied after the parent
// client's options (if there is one), and before any request-specific options.
func NewCustomerBalanceTransactionService(opts ...option.RequestOption) (r *CustomerBalanceTransactionService) {
r = &CustomerBalanceTransactionService{}
r.Options = opts
return
}
// Creates an immutable balance transaction that updates the customer's balance and
// returns back the newly created transaction.
func (r *CustomerBalanceTransactionService) New(ctx context.Context, customerID string, body CustomerBalanceTransactionNewParams, opts ...option.RequestOption) (res *CustomerBalanceTransactionNewResponse, err error) {
opts = append(r.Options[:], opts...)
if customerID == "" {
err = errors.New("missing required customer_id parameter")
return
}
path := fmt.Sprintf("customers/%s/balance_transactions", customerID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return
}
// ## The customer balance
//
// The customer balance is an amount in the customer's currency, which Orb
// automatically applies to subsequent invoices. This balance can be adjusted
// manually via Orb's webapp on the customer details page. You can use this balance
// to provide a fixed mid-period credit to the customer. Commonly, this is done due
// to system downtime/SLA violation, or an adhoc adjustment discussed with the
// customer.
//
// If the balance is a positive value at the time of invoicing, it represents that
// the customer has credit that should be used to offset the amount due on the next
// issued invoice. In this case, Orb will automatically reduce the next invoice by
// the balance amount, and roll over any remaining balance if the invoice is fully
// discounted.
//
// If the balance is a negative value at the time of invoicing, Orb will increase
// the invoice's amount due with a positive adjustment, and reset the balance to 0.
//
// This endpoint retrieves all customer balance transactions in reverse
// chronological order for a single customer, providing a complete audit trail of
// all adjustments and invoice applications.
func (r *CustomerBalanceTransactionService) List(ctx context.Context, customerID string, query CustomerBalanceTransactionListParams, opts ...option.RequestOption) (res *pagination.Page[CustomerBalanceTransactionListResponse], err error) {
var raw *http.Response
opts = append(r.Options[:], opts...)
opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...)
if customerID == "" {
err = errors.New("missing required customer_id parameter")
return
}
path := fmt.Sprintf("customers/%s/balance_transactions", customerID)
cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, query, &res, opts...)
if err != nil {
return nil, err
}
err = cfg.Execute()
if err != nil {
return nil, err
}
res.SetPageConfig(cfg, raw)
return res, nil
}
// ## The customer balance
//
// The customer balance is an amount in the customer's currency, which Orb
// automatically applies to subsequent invoices. This balance can be adjusted
// manually via Orb's webapp on the customer details page. You can use this balance
// to provide a fixed mid-period credit to the customer. Commonly, this is done due
// to system downtime/SLA violation, or an adhoc adjustment discussed with the
// customer.
//
// If the balance is a positive value at the time of invoicing, it represents that
// the customer has credit that should be used to offset the amount due on the next
// issued invoice. In this case, Orb will automatically reduce the next invoice by
// the balance amount, and roll over any remaining balance if the invoice is fully
// discounted.
//
// If the balance is a negative value at the time of invoicing, Orb will increase
// the invoice's amount due with a positive adjustment, and reset the balance to 0.
//
// This endpoint retrieves all customer balance transactions in reverse
// chronological order for a single customer, providing a complete audit trail of
// all adjustments and invoice applications.
func (r *CustomerBalanceTransactionService) ListAutoPaging(ctx context.Context, customerID string, query CustomerBalanceTransactionListParams, opts ...option.RequestOption) *pagination.PageAutoPager[CustomerBalanceTransactionListResponse] {
return pagination.NewPageAutoPager(r.List(ctx, customerID, query, opts...))
}
type CustomerBalanceTransactionNewResponse struct {
// A unique id for this transaction.
ID string `json:"id,required"`
Action CustomerBalanceTransactionNewResponseAction `json:"action,required"`
// The value of the amount changed in the transaction.
Amount string `json:"amount,required"`
// The creation time of this transaction.
CreatedAt time.Time `json:"created_at,required" format:"date-time"`
CreditNote shared.CreditNoteTiny `json:"credit_note,required,nullable"`
// An optional description provided for manual customer balance adjustments.
Description string `json:"description,required,nullable"`
// The new value of the customer's balance prior to the transaction, in the
// customer's currency.
EndingBalance string `json:"ending_balance,required"`
Invoice shared.InvoiceTiny `json:"invoice,required,nullable"`
// The original value of the customer's balance prior to the transaction, in the
// customer's currency.
StartingBalance string `json:"starting_balance,required"`
Type CustomerBalanceTransactionNewResponseType `json:"type,required"`
JSON customerBalanceTransactionNewResponseJSON `json:"-"`
}
// customerBalanceTransactionNewResponseJSON contains the JSON metadata for the
// struct [CustomerBalanceTransactionNewResponse]
type customerBalanceTransactionNewResponseJSON struct {
ID apijson.Field
Action apijson.Field
Amount apijson.Field
CreatedAt apijson.Field
CreditNote apijson.Field
Description apijson.Field
EndingBalance apijson.Field
Invoice apijson.Field
StartingBalance apijson.Field
Type apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *CustomerBalanceTransactionNewResponse) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r customerBalanceTransactionNewResponseJSON) RawJSON() string {
return r.raw
}
type CustomerBalanceTransactionNewResponseAction string
const (
CustomerBalanceTransactionNewResponseActionAppliedToInvoice CustomerBalanceTransactionNewResponseAction = "applied_to_invoice"
CustomerBalanceTransactionNewResponseActionManualAdjustment CustomerBalanceTransactionNewResponseAction = "manual_adjustment"
CustomerBalanceTransactionNewResponseActionProratedRefund CustomerBalanceTransactionNewResponseAction = "prorated_refund"
CustomerBalanceTransactionNewResponseActionRevertProratedRefund CustomerBalanceTransactionNewResponseAction = "revert_prorated_refund"
CustomerBalanceTransactionNewResponseActionReturnFromVoiding CustomerBalanceTransactionNewResponseAction = "return_from_voiding"
CustomerBalanceTransactionNewResponseActionCreditNoteApplied CustomerBalanceTransactionNewResponseAction = "credit_note_applied"
CustomerBalanceTransactionNewResponseActionCreditNoteVoided CustomerBalanceTransactionNewResponseAction = "credit_note_voided"
CustomerBalanceTransactionNewResponseActionOverpaymentRefund CustomerBalanceTransactionNewResponseAction = "overpayment_refund"
CustomerBalanceTransactionNewResponseActionExternalPayment CustomerBalanceTransactionNewResponseAction = "external_payment"
CustomerBalanceTransactionNewResponseActionSmallInvoiceCarryover CustomerBalanceTransactionNewResponseAction = "small_invoice_carryover"
)
func (r CustomerBalanceTransactionNewResponseAction) IsKnown() bool {
switch r {
case CustomerBalanceTransactionNewResponseActionAppliedToInvoice, CustomerBalanceTransactionNewResponseActionManualAdjustment, CustomerBalanceTransactionNewResponseActionProratedRefund, CustomerBalanceTransactionNewResponseActionRevertProratedRefund, CustomerBalanceTransactionNewResponseActionReturnFromVoiding, CustomerBalanceTransactionNewResponseActionCreditNoteApplied, CustomerBalanceTransactionNewResponseActionCreditNoteVoided, CustomerBalanceTransactionNewResponseActionOverpaymentRefund, CustomerBalanceTransactionNewResponseActionExternalPayment, CustomerBalanceTransactionNewResponseActionSmallInvoiceCarryover:
return true
}
return false
}
type CustomerBalanceTransactionNewResponseType string
const (
CustomerBalanceTransactionNewResponseTypeIncrement CustomerBalanceTransactionNewResponseType = "increment"
CustomerBalanceTransactionNewResponseTypeDecrement CustomerBalanceTransactionNewResponseType = "decrement"
)
func (r CustomerBalanceTransactionNewResponseType) IsKnown() bool {
switch r {
case CustomerBalanceTransactionNewResponseTypeIncrement, CustomerBalanceTransactionNewResponseTypeDecrement:
return true
}
return false
}
type CustomerBalanceTransactionListResponse struct {
// A unique id for this transaction.
ID string `json:"id,required"`
Action CustomerBalanceTransactionListResponseAction `json:"action,required"`
// The value of the amount changed in the transaction.
Amount string `json:"amount,required"`
// The creation time of this transaction.
CreatedAt time.Time `json:"created_at,required" format:"date-time"`
CreditNote shared.CreditNoteTiny `json:"credit_note,required,nullable"`
// An optional description provided for manual customer balance adjustments.
Description string `json:"description,required,nullable"`
// The new value of the customer's balance prior to the transaction, in the
// customer's currency.
EndingBalance string `json:"ending_balance,required"`
Invoice shared.InvoiceTiny `json:"invoice,required,nullable"`
// The original value of the customer's balance prior to the transaction, in the
// customer's currency.
StartingBalance string `json:"starting_balance,required"`
Type CustomerBalanceTransactionListResponseType `json:"type,required"`
JSON customerBalanceTransactionListResponseJSON `json:"-"`
}
// customerBalanceTransactionListResponseJSON contains the JSON metadata for the
// struct [CustomerBalanceTransactionListResponse]
type customerBalanceTransactionListResponseJSON struct {
ID apijson.Field
Action apijson.Field
Amount apijson.Field
CreatedAt apijson.Field
CreditNote apijson.Field
Description apijson.Field
EndingBalance apijson.Field
Invoice apijson.Field
StartingBalance apijson.Field
Type apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *CustomerBalanceTransactionListResponse) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r customerBalanceTransactionListResponseJSON) RawJSON() string {
return r.raw
}
type CustomerBalanceTransactionListResponseAction string
const (
CustomerBalanceTransactionListResponseActionAppliedToInvoice CustomerBalanceTransactionListResponseAction = "applied_to_invoice"
CustomerBalanceTransactionListResponseActionManualAdjustment CustomerBalanceTransactionListResponseAction = "manual_adjustment"
CustomerBalanceTransactionListResponseActionProratedRefund CustomerBalanceTransactionListResponseAction = "prorated_refund"
CustomerBalanceTransactionListResponseActionRevertProratedRefund CustomerBalanceTransactionListResponseAction = "revert_prorated_refund"
CustomerBalanceTransactionListResponseActionReturnFromVoiding CustomerBalanceTransactionListResponseAction = "return_from_voiding"
CustomerBalanceTransactionListResponseActionCreditNoteApplied CustomerBalanceTransactionListResponseAction = "credit_note_applied"
CustomerBalanceTransactionListResponseActionCreditNoteVoided CustomerBalanceTransactionListResponseAction = "credit_note_voided"
CustomerBalanceTransactionListResponseActionOverpaymentRefund CustomerBalanceTransactionListResponseAction = "overpayment_refund"
CustomerBalanceTransactionListResponseActionExternalPayment CustomerBalanceTransactionListResponseAction = "external_payment"
CustomerBalanceTransactionListResponseActionSmallInvoiceCarryover CustomerBalanceTransactionListResponseAction = "small_invoice_carryover"
)
func (r CustomerBalanceTransactionListResponseAction) IsKnown() bool {
switch r {
case CustomerBalanceTransactionListResponseActionAppliedToInvoice, CustomerBalanceTransactionListResponseActionManualAdjustment, CustomerBalanceTransactionListResponseActionProratedRefund, CustomerBalanceTransactionListResponseActionRevertProratedRefund, CustomerBalanceTransactionListResponseActionReturnFromVoiding, CustomerBalanceTransactionListResponseActionCreditNoteApplied, CustomerBalanceTransactionListResponseActionCreditNoteVoided, CustomerBalanceTransactionListResponseActionOverpaymentRefund, CustomerBalanceTransactionListResponseActionExternalPayment, CustomerBalanceTransactionListResponseActionSmallInvoiceCarryover:
return true
}
return false
}
type CustomerBalanceTransactionListResponseType string
const (
CustomerBalanceTransactionListResponseTypeIncrement CustomerBalanceTransactionListResponseType = "increment"
CustomerBalanceTransactionListResponseTypeDecrement CustomerBalanceTransactionListResponseType = "decrement"
)
func (r CustomerBalanceTransactionListResponseType) IsKnown() bool {
switch r {
case CustomerBalanceTransactionListResponseTypeIncrement, CustomerBalanceTransactionListResponseTypeDecrement:
return true
}
return false
}
type CustomerBalanceTransactionNewParams struct {
Amount param.Field[string] `json:"amount,required"`
Type param.Field[CustomerBalanceTransactionNewParamsType] `json:"type,required"`
// An optional description that can be specified around this entry.
Description param.Field[string] `json:"description"`
}
func (r CustomerBalanceTransactionNewParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type CustomerBalanceTransactionNewParamsType string
const (
CustomerBalanceTransactionNewParamsTypeIncrement CustomerBalanceTransactionNewParamsType = "increment"
CustomerBalanceTransactionNewParamsTypeDecrement CustomerBalanceTransactionNewParamsType = "decrement"
)
func (r CustomerBalanceTransactionNewParamsType) IsKnown() bool {
switch r {
case CustomerBalanceTransactionNewParamsTypeIncrement, CustomerBalanceTransactionNewParamsTypeDecrement:
return true
}
return false
}
type CustomerBalanceTransactionListParams struct {
// Cursor for pagination. This can be populated by the `next_cursor` value returned
// from the initial request.
Cursor param.Field[string] `query:"cursor"`
// The number of items to fetch. Defaults to 20.
Limit param.Field[int64] `query:"limit"`
OperationTimeGt param.Field[time.Time] `query:"operation_time[gt]" format:"date-time"`
OperationTimeGte param.Field[time.Time] `query:"operation_time[gte]" format:"date-time"`
OperationTimeLt param.Field[time.Time] `query:"operation_time[lt]" format:"date-time"`
OperationTimeLte param.Field[time.Time] `query:"operation_time[lte]" format:"date-time"`
}
// URLQuery serializes [CustomerBalanceTransactionListParams]'s query parameters as
// `url.Values`.
func (r CustomerBalanceTransactionListParams) URLQuery() (v url.Values) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatBrackets,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}