/
customerbalancetransaction.go
423 lines (372 loc) · 19.2 KB
/
customerbalancetransaction.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
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package orb
import (
"context"
"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/internal/shared"
"github.com/orbcorp/orb-go/option"
)
// 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...)
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.
//
// ## Eligibility
//
// The customer balance can only be applied to invoices or adjusted manually if
// invoices are not synced to a separate invoicing provider. If a payment gateway
// such as Stripe is used, the balance will be applied to the invoice before
// forwarding payment to the gateway.
func (r *CustomerBalanceTransactionService) List(ctx context.Context, customerID string, query CustomerBalanceTransactionListParams, opts ...option.RequestOption) (res *shared.Page[CustomerBalanceTransactionListResponse], err error) {
var raw *http.Response
opts = append(r.Options, opts...)
opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...)
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.
//
// ## Eligibility
//
// The customer balance can only be applied to invoices or adjusted manually if
// invoices are not synced to a separate invoicing provider. If a payment gateway
// such as Stripe is used, the balance will be applied to the invoice before
// forwarding payment to the gateway.
func (r *CustomerBalanceTransactionService) ListAutoPaging(ctx context.Context, customerID string, query CustomerBalanceTransactionListParams, opts ...option.RequestOption) *shared.PageAutoPager[CustomerBalanceTransactionListResponse] {
return shared.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 CustomerBalanceTransactionNewResponseCreditNote `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 CustomerBalanceTransactionNewResponseInvoice `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"
)
func (r CustomerBalanceTransactionNewResponseAction) IsKnown() bool {
switch r {
case CustomerBalanceTransactionNewResponseActionAppliedToInvoice, CustomerBalanceTransactionNewResponseActionManualAdjustment, CustomerBalanceTransactionNewResponseActionProratedRefund, CustomerBalanceTransactionNewResponseActionRevertProratedRefund, CustomerBalanceTransactionNewResponseActionReturnFromVoiding, CustomerBalanceTransactionNewResponseActionCreditNoteApplied, CustomerBalanceTransactionNewResponseActionCreditNoteVoided, CustomerBalanceTransactionNewResponseActionOverpaymentRefund:
return true
}
return false
}
type CustomerBalanceTransactionNewResponseCreditNote struct {
// The id of the Credit note
ID string `json:"id,required"`
JSON customerBalanceTransactionNewResponseCreditNoteJSON `json:"-"`
}
// customerBalanceTransactionNewResponseCreditNoteJSON contains the JSON metadata
// for the struct [CustomerBalanceTransactionNewResponseCreditNote]
type customerBalanceTransactionNewResponseCreditNoteJSON struct {
ID apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *CustomerBalanceTransactionNewResponseCreditNote) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r customerBalanceTransactionNewResponseCreditNoteJSON) RawJSON() string {
return r.raw
}
type CustomerBalanceTransactionNewResponseInvoice struct {
// The Invoice id
ID string `json:"id,required"`
JSON customerBalanceTransactionNewResponseInvoiceJSON `json:"-"`
}
// customerBalanceTransactionNewResponseInvoiceJSON contains the JSON metadata for
// the struct [CustomerBalanceTransactionNewResponseInvoice]
type customerBalanceTransactionNewResponseInvoiceJSON struct {
ID apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *CustomerBalanceTransactionNewResponseInvoice) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r customerBalanceTransactionNewResponseInvoiceJSON) RawJSON() string {
return r.raw
}
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 CustomerBalanceTransactionListResponseCreditNote `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 CustomerBalanceTransactionListResponseInvoice `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"
)
func (r CustomerBalanceTransactionListResponseAction) IsKnown() bool {
switch r {
case CustomerBalanceTransactionListResponseActionAppliedToInvoice, CustomerBalanceTransactionListResponseActionManualAdjustment, CustomerBalanceTransactionListResponseActionProratedRefund, CustomerBalanceTransactionListResponseActionRevertProratedRefund, CustomerBalanceTransactionListResponseActionReturnFromVoiding, CustomerBalanceTransactionListResponseActionCreditNoteApplied, CustomerBalanceTransactionListResponseActionCreditNoteVoided, CustomerBalanceTransactionListResponseActionOverpaymentRefund:
return true
}
return false
}
type CustomerBalanceTransactionListResponseCreditNote struct {
// The id of the Credit note
ID string `json:"id,required"`
JSON customerBalanceTransactionListResponseCreditNoteJSON `json:"-"`
}
// customerBalanceTransactionListResponseCreditNoteJSON contains the JSON metadata
// for the struct [CustomerBalanceTransactionListResponseCreditNote]
type customerBalanceTransactionListResponseCreditNoteJSON struct {
ID apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *CustomerBalanceTransactionListResponseCreditNote) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r customerBalanceTransactionListResponseCreditNoteJSON) RawJSON() string {
return r.raw
}
type CustomerBalanceTransactionListResponseInvoice struct {
// The Invoice id
ID string `json:"id,required"`
JSON customerBalanceTransactionListResponseInvoiceJSON `json:"-"`
}
// customerBalanceTransactionListResponseInvoiceJSON contains the JSON metadata for
// the struct [CustomerBalanceTransactionListResponseInvoice]
type customerBalanceTransactionListResponseInvoiceJSON struct {
ID apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *CustomerBalanceTransactionListResponseInvoice) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r customerBalanceTransactionListResponseInvoiceJSON) RawJSON() string {
return r.raw
}
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,
})
}