-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcredit-grants.ts
591 lines (489 loc) · 15.6 KB
/
credit-grants.ts
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
import { APIResource } from '../resource';
import { isRequestOptions } from '../core';
import * as Core from '../core';
import * as CreditGrantsAPI from './credit-grants';
import * as Shared from './shared';
import { CursorPage, type CursorPageParams } from '../pagination';
export class CreditGrants extends APIResource {
/**
* Create a new credit grant
*/
create(
body: CreditGrantCreateParams,
options?: Core.RequestOptions,
): Core.APIPromise<CreditGrantCreateResponse> {
return this._client.post('/credits/createGrant', { body, ...options });
}
/**
* List credit grants. This list does not included voided grants.
*/
list(
params?: CreditGrantListParams,
options?: Core.RequestOptions,
): Core.PagePromise<CreditGrantListResponsesCursorPage, CreditGrantListResponse>;
list(
options?: Core.RequestOptions,
): Core.PagePromise<CreditGrantListResponsesCursorPage, CreditGrantListResponse>;
list(
params: CreditGrantListParams | Core.RequestOptions = {},
options?: Core.RequestOptions,
): Core.PagePromise<CreditGrantListResponsesCursorPage, CreditGrantListResponse> {
if (isRequestOptions(params)) {
return this.list({}, params);
}
const { limit, next_page, ...body } = params;
return this._client.getAPIList('/credits/listGrants', CreditGrantListResponsesCursorPage, {
query: { limit, next_page },
body,
method: 'post',
...options,
});
}
/**
* Edit an existing credit grant
*/
edit(body: CreditGrantEditParams, options?: Core.RequestOptions): Core.APIPromise<CreditGrantEditResponse> {
return this._client.post('/credits/editGrant', { body, ...options });
}
/**
* Fetches a list of credit ledger entries. Returns lists of ledgers per customer.
* Ledger entries are returned in chronological order. Ledger entries associated
* with voided credit grants are not included.
*/
listEntries(
params?: CreditGrantListEntriesParams,
options?: Core.RequestOptions,
): Core.APIPromise<CreditGrantListEntriesResponse>;
listEntries(options?: Core.RequestOptions): Core.APIPromise<CreditGrantListEntriesResponse>;
listEntries(
params: CreditGrantListEntriesParams | Core.RequestOptions = {},
options?: Core.RequestOptions,
): Core.APIPromise<CreditGrantListEntriesResponse> {
if (isRequestOptions(params)) {
return this.listEntries({}, params);
}
const { next_page, ...body } = params;
return this._client.post('/credits/listEntries', { query: { next_page }, body, ...options });
}
/**
* Void a credit grant
*/
void(body: CreditGrantVoidParams, options?: Core.RequestOptions): Core.APIPromise<CreditGrantVoidResponse> {
return this._client.post('/credits/voidGrant', { body, ...options });
}
}
export class CreditGrantListResponsesCursorPage extends CursorPage<CreditGrantListResponse> {}
export interface CreditLedgerEntry {
/**
* an amount representing the change to the customer's credit balance
*/
amount: number;
created_by: string;
/**
* the credit grant this entry is related to
*/
credit_grant_id: string;
effective_at: string;
reason: string;
/**
* the running balance for this credit type at the time of the ledger entry,
* including all preceding charges
*/
running_balance: number;
/**
* if this entry is a deduction, the Metronome ID of the invoice where the credit
* deduction was consumed; if this entry is a grant, the Metronome ID of the
* invoice where the grant's paid_amount was charged
*/
invoice_id?: string | null;
}
export interface RolloverAmountMaxAmount {
/**
* Rollover up to a fixed amount of the original credit grant amount.
*/
type: 'MAX_AMOUNT';
/**
* The maximum amount to rollover.
*/
value: number;
}
export interface RolloverAmountMaxPercentage {
/**
* Rollover up to a percentage of the original credit grant amount.
*/
type: 'MAX_PERCENTAGE';
/**
* The maximum percentage (0-1) of the original credit grant to rollover.
*/
value: number;
}
export interface CreditGrantCreateResponse {
data: Shared.ID;
}
export interface CreditGrantListResponse {
/**
* the Metronome ID of the credit grant
*/
id: string;
/**
* The effective balance of the grant as of the end of the customer's current
* billing period. Expiration deductions will be included only if the grant expires
* before the end of the current billing period.
*/
balance: CreditGrantListResponse.Balance;
custom_fields: Record<string, string>;
/**
* the Metronome ID of the customer
*/
customer_id: string;
deductions: Array<CreditLedgerEntry>;
effective_at: string;
expires_at: string;
/**
* the amount of credits initially granted
*/
grant_amount: CreditGrantListResponse.GrantAmount;
name: string;
/**
* the amount paid for this credit grant
*/
paid_amount: CreditGrantListResponse.PaidAmount;
pending_deductions: Array<CreditLedgerEntry>;
priority: number;
credit_grant_type?: string | null;
/**
* the Metronome ID of the invoice with the purchase charge for this credit grant,
* if applicable
*/
invoice_id?: string | null;
/**
* The products which these credits will be applied to. (If unspecified, the
* credits will be applied to charges for all products.)
*/
products?: Array<CreditGrantListResponse.Product>;
reason?: string | null;
/**
* Prevents the creation of duplicates. If a request to create a record is made
* with a previously used uniqueness key, a new record will not be created and the
* request will fail with a 409 error.
*/
uniqueness_key?: string | null;
}
export namespace CreditGrantListResponse {
/**
* The effective balance of the grant as of the end of the customer's current
* billing period. Expiration deductions will be included only if the grant expires
* before the end of the current billing period.
*/
export interface Balance {
/**
* The end_date of the customer's current billing period.
*/
effective_at: string;
/**
* The grant's current balance including all posted deductions. If the grant has
* expired, this amount will be 0.
*/
excluding_pending: number;
/**
* The grant's current balance including all posted and pending deductions. If the
* grant expires before the end of the customer's current billing period, this
* amount will be 0.
*/
including_pending: number;
}
/**
* the amount of credits initially granted
*/
export interface GrantAmount {
amount: number;
/**
* the credit type for the amount granted
*/
credit_type: Shared.CreditTypeData;
}
/**
* the amount paid for this credit grant
*/
export interface PaidAmount {
amount: number;
/**
* the credit type for the amount paid
*/
credit_type: Shared.CreditTypeData;
}
export interface Product {
id: string;
name: string;
}
}
export interface CreditGrantEditResponse {
data: Shared.ID;
}
export interface CreditGrantListEntriesResponse {
data: Array<CreditGrantListEntriesResponse.Data>;
next_page: string | null;
}
export namespace CreditGrantListEntriesResponse {
export interface Data {
customer_id: string;
ledgers: Array<Data.Ledger>;
}
export namespace Data {
export interface Ledger {
credit_type: Shared.CreditTypeData;
/**
* the effective balances at the end of the specified time window
*/
ending_balance: Ledger.EndingBalance;
entries: Array<CreditGrantsAPI.CreditLedgerEntry>;
pending_entries: Array<CreditGrantsAPI.CreditLedgerEntry>;
starting_balance: Ledger.StartingBalance;
}
export namespace Ledger {
/**
* the effective balances at the end of the specified time window
*/
export interface EndingBalance {
/**
* the ending_before request parameter (if supplied) or the current billing
* period's end date
*/
effective_at: string;
/**
* the ending balance, including the balance of all grants that have not expired
* before the effective_at date and deductions that happened before the
* effective_at date
*/
excluding_pending: number;
/**
* the excluding_pending balance plus any pending invoice deductions and
* expirations that will happen by the effective_at date
*/
including_pending: number;
}
export interface StartingBalance {
/**
* the starting_on request parameter (if supplied) or the first credit grant's
* effective_at date
*/
effective_at: string;
/**
* the starting balance, including all posted grants, deductions, and expirations
* that happened at or before the effective_at timestamp
*/
excluding_pending: number;
/**
* the excluding_pending balance plus any pending activity that has not been posted
* at the time of the query
*/
including_pending: number;
}
}
}
}
export interface CreditGrantVoidResponse {
data: Shared.ID;
}
export interface CreditGrantCreateParams {
/**
* the Metronome ID of the customer
*/
customer_id: string;
/**
* The credit grant will only apply to usage or charges dated before this timestamp
*/
expires_at: string;
/**
* the amount of credits granted
*/
grant_amount: CreditGrantCreateParams.GrantAmount;
/**
* the name of the credit grant as it will appear on invoices
*/
name: string;
/**
* the amount paid for this credit grant
*/
paid_amount: CreditGrantCreateParams.PaidAmount;
priority: number;
credit_grant_type?: string;
/**
* Custom fields to attach to the credit grant.
*/
custom_fields?: Record<string, string>;
/**
* The credit grant will only apply to usage or charges dated on or after this
* timestamp
*/
effective_at?: string;
/**
* The date to issue an invoice for the paid_amount.
*/
invoice_date?: string;
/**
* The product(s) which these credits will be applied to. (If unspecified, the
* credits will be applied to charges for all products.). The array ordering
* specified here will be used to determine the order in which credits will be
* applied to invoice line items
*/
product_ids?: Array<string>;
reason?: string;
/**
* Configure a rollover for this credit grant so if it expires it rolls over a
* configured amount to a new credit grant. This feature is currently opt-in only.
* Contact Metronome to be added to the beta.
*/
rollover_settings?: CreditGrantCreateParams.RolloverSettings;
/**
* Prevents the creation of duplicates. If a request to create a record is made
* with a previously used uniqueness key, a new record will not be created and the
* request will fail with a 409 error.
*/
uniqueness_key?: string;
}
export namespace CreditGrantCreateParams {
/**
* the amount of credits granted
*/
export interface GrantAmount {
amount: number;
/**
* the ID of the pricing unit to be used. Defaults to USD (cents) if not passed.
*/
credit_type_id: string;
}
/**
* the amount paid for this credit grant
*/
export interface PaidAmount {
amount: number;
/**
* the ID of the pricing unit to be used. Defaults to USD (cents) if not passed.
*/
credit_type_id: string;
}
/**
* Configure a rollover for this credit grant so if it expires it rolls over a
* configured amount to a new credit grant. This feature is currently opt-in only.
* Contact Metronome to be added to the beta.
*/
export interface RolloverSettings {
/**
* The date to expire the rollover credits.
*/
expires_at: string;
/**
* The priority to give the rollover credit grant that gets created when a rollover
* happens.
*/
priority: number;
/**
* Specify how much to rollover to the rollover credit grant
*/
rollover_amount: CreditGrantsAPI.RolloverAmountMaxPercentage | CreditGrantsAPI.RolloverAmountMaxAmount;
}
}
export interface CreditGrantListParams extends CursorPageParams {
/**
* Body param: An array of credit grant IDs. If this is specified, neither
* credit_type_ids nor customer_ids may be specified.
*/
credit_grant_ids?: Array<string>;
/**
* Body param: An array of credit type IDs. This must not be specified if
* credit_grant_ids is specified.
*/
credit_type_ids?: Array<string>;
/**
* Body param: An array of Metronome customer IDs. This must not be specified if
* credit_grant_ids is specified.
*/
customer_ids?: Array<string>;
/**
* Body param: Only return credit grants that are effective before this timestamp
* (exclusive).
*/
effective_before?: string;
/**
* Body param: Only return credit grants that expire at or after this timestamp.
*/
not_expiring_before?: string;
}
export interface CreditGrantEditParams {
/**
* the ID of the credit grant
*/
id: string;
/**
* the updated credit grant type
*/
credit_grant_type?: string;
/**
* the updated expiration date for the credit grant
*/
expires_at?: string;
/**
* the updated name for the credit grant
*/
name?: string;
}
export interface CreditGrantListEntriesParams {
/**
* Query param: Cursor that indicates where the next page of results should start.
*/
next_page?: string;
/**
* Body param: A list of Metronome credit type IDs to fetch ledger entries for. If
* absent, ledger entries for all credit types will be returned.
*/
credit_type_ids?: Array<string>;
/**
* Body param: A list of Metronome customer IDs to fetch ledger entries for. If
* absent, ledger entries for all customers will be returned.
*/
customer_ids?: Array<string>;
/**
* Body param: If supplied, ledger entries will only be returned with an
* effective_at before this time. This timestamp must not be in the future. If no
* timestamp is supplied, all entries up to the start of the customer's next
* billing period will be returned.
*/
ending_before?: string;
/**
* Body param: If supplied, only ledger entries effective at or after this time
* will be returned.
*/
starting_on?: string;
}
export interface CreditGrantVoidParams {
id: string;
/**
* If true, resets the uniqueness key on this grant so it can be re-used
*/
release_uniqueness_key?: boolean;
/**
* If true, void the purchase invoice associated with the grant
*/
void_credit_purchase_invoice?: boolean;
}
CreditGrants.CreditGrantListResponsesCursorPage = CreditGrantListResponsesCursorPage;
export declare namespace CreditGrants {
export {
type CreditLedgerEntry as CreditLedgerEntry,
type RolloverAmountMaxAmount as RolloverAmountMaxAmount,
type RolloverAmountMaxPercentage as RolloverAmountMaxPercentage,
type CreditGrantCreateResponse as CreditGrantCreateResponse,
type CreditGrantListResponse as CreditGrantListResponse,
type CreditGrantEditResponse as CreditGrantEditResponse,
type CreditGrantListEntriesResponse as CreditGrantListEntriesResponse,
type CreditGrantVoidResponse as CreditGrantVoidResponse,
CreditGrantListResponsesCursorPage as CreditGrantListResponsesCursorPage,
type CreditGrantCreateParams as CreditGrantCreateParams,
type CreditGrantListParams as CreditGrantListParams,
type CreditGrantEditParams as CreditGrantEditParams,
type CreditGrantListEntriesParams as CreditGrantListEntriesParams,
type CreditGrantVoidParams as CreditGrantVoidParams,
};
}