Skip to content

Commit d8b5c00

Browse files
borosrtothandras
andauthored
Feat: Customer create charges (#4503)
Co-authored-by: Andras Toth <4157749+tothandras@users.noreply.github.com>
1 parent 097ee63 commit d8b5c00

16 files changed

Lines changed: 1845 additions & 738 deletions

File tree

api/spec/packages/aip-client-javascript/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ The full call path, HTTP route, and a short description are listed below.
152152
| `client.customers.credits.grants.updateExternalSettlement` | `POST /openmeter/customers/{customerId}/credits/grants/{creditGrantId}/settlement/external` | Update the payment settlement status of an externally funded credit grant. Use this endpoint to synchronize the payment state of an external payment with the system so that revenue recognition and credit availability work as expected. |
153153
| `client.customers.credits.transactions.list` | `GET /openmeter/customers/{customerId}/credits/transactions` | List credit transactions for a customer. Returns an immutable, chronological record of credit movements: funded credits and consumed credits. Transactions are returned in reverse chronological order by default. |
154154
| `client.customers.charges.list` | `GET /openmeter/customers/{customerId}/charges` | List customer charges. Returns the customer's charges that are represented as either flat fee or usage-based charges. |
155+
| `client.customers.charges.create` | `POST /openmeter/customers/{customerId}/charges` | Create customer charge. |
155156

156157
### Entitlements
157158

api/spec/packages/aip-client-javascript/src/funcs/customers.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ import type {
3939
ListCreditTransactionsResponse,
4040
ListCustomerChargesRequest,
4141
ListCustomerChargesResponse,
42+
CreateCustomerChargesRequest,
43+
CreateCustomerChargesResponse,
4244
} from '../models/operations/customers.js'
4345

4446
export function createCustomer(
@@ -325,3 +327,18 @@ export function listCustomerCharges(
325327
.json<ListCustomerChargesResponse>(),
326328
)
327329
}
330+
331+
export function createCustomerCharges(
332+
client: Client,
333+
req: CreateCustomerChargesRequest,
334+
options?: RequestOptions,
335+
): Promise<Result<CreateCustomerChargesResponse>> {
336+
const path = encodePath('openmeter/customers/{customerId}/charges', {
337+
customerId: req.customerId,
338+
})
339+
return request(() =>
340+
http(client)
341+
.post(path, { ...options, json: req.body })
342+
.json<CreateCustomerChargesResponse>(),
343+
)
344+
}

api/spec/packages/aip-client-javascript/src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ export type {
6060
AppStripeCheckoutSessionCustomTextParams,
6161
AppStripeCreateCustomerPortalSessionOptions,
6262
CreateLabels,
63-
PriceFree,
6463
TaxConfigStripe,
6564
TaxConfigExternalInvoicing,
6665
FlatFeeDiscounts,
66+
PriceFree,
6767
WorkflowCollectionAlignmentSubscription,
6868
WorkflowInvoicingSettings,
6969
WorkflowPaymentChargeAutomaticallySettings,
@@ -87,9 +87,9 @@ export type {
8787
AppCustomerDataStripe,
8888
AppCustomerDataExternalInvoicing,
8989
ListCostBasesParamsFilter,
90+
CurrencyAmount,
9091
PriceFlat,
9192
PriceUnit,
92-
CurrencyAmount,
9393
RateCardDiscounts,
9494
Totals,
9595
FeatureManualUnitCost,
@@ -244,6 +244,7 @@ export type {
244244
PricePagePaginatedResponse,
245245
CreateCreditGrantRequest,
246246
CreditGrant,
247+
CreateFlatFeeChargeRequest,
247248
WorkflowTaxSettings,
248249
SubscriptionAddonPagePaginatedResponse,
249250
PlanAddonPagePaginatedResponse,
@@ -268,6 +269,7 @@ export type {
268269
GovernanceQueryResponse,
269270
FlatFeeCharge,
270271
UsageBasedCharge,
272+
CreateUsageBasedChargeRequest,
271273
RateCard,
272274
FeaturePagePaginatedResponse,
273275
Workflow,

api/spec/packages/aip-client-javascript/src/models/operations/customers.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import type {
88
CreateCreditAdjustmentRequest as CreateCreditAdjustmentRequestBody,
99
CreateCreditGrantRequestInput,
1010
CreateCustomerRequest as CreateCustomerRequestBody,
11+
CreateFlatFeeChargeRequest,
12+
CreateUsageBasedChargeRequest,
1113
CreditAdjustment,
1214
CreditBalances,
1315
CreditGrant,
@@ -167,3 +169,11 @@ export type ListCustomerChargesRequest = ListCustomerChargesQuery & {
167169
customerId: string
168170
}
169171
export type ListCustomerChargesResponse = ChargePagePaginatedResponse
172+
173+
export type CreateCustomerChargesRequest = {
174+
customerId: string
175+
body: CreateFlatFeeChargeRequest | CreateUsageBasedChargeRequest
176+
}
177+
export type CreateCustomerChargesResponse = z.output<
178+
typeof schemas.createCustomerChargesResponse
179+
>

api/spec/packages/aip-client-javascript/src/models/schemas.ts

Lines changed: 104 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -598,12 +598,6 @@ export const chargeStatus = z
598598
'Lifecycle status of a charge. Values: - `created`: The charge has been created but is not active yet. - `active`: The charge is active. - `final`: The charge is fully finalized and no further changes are expected. - `deleted`: The charge has been deleted.',
599599
)
600600

601-
export const priceFree = z
602-
.object({
603-
type: z.literal('free').describe('The type of the price.'),
604-
})
605-
.describe('Free price.')
606-
607601
export const settlementMode = z
608602
.enum(['credit_then_invoice', 'credit_only'])
609603

@@ -657,6 +651,12 @@ export const rateCardProrationMode = z
657651
'The proration mode of the rate card. Values: - `no_proration`: No proration. - `prorate_prices`: Prorate the price based on the time remaining in the billing period.',
658652
)
659653

654+
export const priceFree = z
655+
.object({
656+
type: z.literal('free').describe('The type of the price.'),
657+
})
658+
.describe('Free price.')
659+
660660
export const subscriptionStatus = z
661661
.enum(['active', 'inactive', 'canceled', 'scheduled'])
662662
.describe('Subscription status.')
@@ -1241,6 +1241,13 @@ export const listCostBasesParamsFilter = z
12411241
})
12421242
.describe('Filter options for listing cost bases.')
12431243

1244+
export const currencyAmount = z
1245+
.object({
1246+
amount: numeric,
1247+
currency: currencyCode,
1248+
})
1249+
.describe('Monetary amount in a specific currency.')
1250+
12441251
export const priceFlat = z
12451252
.object({
12461253
type: z.literal('flat').describe('The type of the price.'),
@@ -1258,13 +1265,6 @@ export const priceUnit = z
12581265
'Unit price. Charges a fixed rate per billing unit. When UnitConfig is present on the rate card, billing units are the converted quantities (e.g. GB instead of bytes).',
12591266
)
12601267

1261-
export const currencyAmount = z
1262-
.object({
1263-
amount: numeric,
1264-
currency: currencyCode,
1265-
})
1266-
.describe('Monetary amount in a specific currency.')
1267-
12681268
export const rateCardDiscounts = z
12691269
.object({
12701270
percentage: z
@@ -3759,6 +3759,45 @@ export const creditGrant = z
37593759
'A credit grant allocates credits to a customer. Credits are drawn down against charges according to the settlement mode configured on the rate card.',
37603760
)
37613761

3762+
export const createFlatFeeChargeRequest = z
3763+
.object({
3764+
name: z
3765+
.string()
3766+
.min(1)
3767+
.max(256)
3768+
.describe('Display name of the resource. Between 1 and 256 characters.'),
3769+
description: z
3770+
.string()
3771+
.max(1024)
3772+
.optional()
3773+
3774+
.describe(
3775+
'Optional description of the resource. Maximum 1024 characters.',
3776+
),
3777+
labels: labels.optional(),
3778+
type: z.literal('flat_fee').describe('The type of the charge.'),
3779+
currency: currencyCode,
3780+
invoice_at: dateTime,
3781+
service_period: closedPeriod,
3782+
unique_reference_id: z
3783+
.string()
3784+
.optional()
3785+
.describe('Unique reference ID of the charge.'),
3786+
settlement_mode: settlementMode,
3787+
tax_config: taxConfig.optional(),
3788+
payment_term: pricePaymentTerm,
3789+
discounts: flatFeeDiscounts.optional(),
3790+
feature_key: z
3791+
.string()
3792+
.optional()
3793+
.describe('The feature associated with the charge, when applicable.'),
3794+
proration_configuration: rateCardProrationConfiguration,
3795+
amount_before_proration: currencyAmount,
3796+
full_service_period: closedPeriod.optional(),
3797+
billing_period: closedPeriod.optional(),
3798+
})
3799+
.describe('Flat fee charge create request.')
3800+
37623801
export const workflowTaxSettings = z
37633802
.object({
37643803
enabled: z
@@ -4157,7 +4196,6 @@ export const flatFeeCharge = z
41574196
full_service_period: closedPeriod,
41584197
billing_period: closedPeriod,
41594198
advance_after: dateTime.optional(),
4160-
price: price,
41614199
unique_reference_id: z
41624200
.string()
41634201
.optional()
@@ -4171,7 +4209,9 @@ export const flatFeeCharge = z
41714209
.optional()
41724210
.describe('The feature associated with the charge, when applicable.'),
41734211
proration_configuration: rateCardProrationConfiguration,
4212+
amount_before_proration: currencyAmount,
41744213
amount_after_proration: currencyAmount,
4214+
price: price,
41754215
})
41764216
.describe('A flat fee charge for a customer.')
41774217

@@ -4206,7 +4246,6 @@ export const usageBasedCharge = z
42064246
full_service_period: closedPeriod,
42074247
billing_period: closedPeriod,
42084248
advance_after: dateTime.optional(),
4209-
price: price,
42104249
unique_reference_id: z
42114250
.string()
42124251
.optional()
@@ -4216,9 +4255,44 @@ export const usageBasedCharge = z
42164255
discounts: rateCardDiscounts.optional(),
42174256
feature_key: z.string().describe('The feature associated with the charge.'),
42184257
totals: chargeTotals,
4258+
price: price,
42194259
})
42204260
.describe('A usage-based charge for a customer.')
42214261

4262+
export const createUsageBasedChargeRequest = z
4263+
.object({
4264+
name: z
4265+
.string()
4266+
.min(1)
4267+
.max(256)
4268+
.describe('Display name of the resource. Between 1 and 256 characters.'),
4269+
description: z
4270+
.string()
4271+
.max(1024)
4272+
.optional()
4273+
4274+
.describe(
4275+
'Optional description of the resource. Maximum 1024 characters.',
4276+
),
4277+
labels: labels.optional(),
4278+
type: z.literal('usage_based').describe('The type of the charge.'),
4279+
currency: currencyCode,
4280+
invoice_at: dateTime,
4281+
service_period: closedPeriod,
4282+
unique_reference_id: z
4283+
.string()
4284+
.optional()
4285+
.describe('Unique reference ID of the charge.'),
4286+
settlement_mode: settlementMode,
4287+
tax_config: taxConfig.optional(),
4288+
discounts: rateCardDiscounts.optional(),
4289+
feature_key: z.string().describe('The feature associated with the charge.'),
4290+
price: price,
4291+
full_service_period: closedPeriod.optional(),
4292+
billing_period: closedPeriod.optional(),
4293+
})
4294+
.describe('Usage-based charge create request.')
4295+
42224296
export const rateCard = z
42234297
.object({
42244298
name: z
@@ -4270,6 +4344,13 @@ export const charge = z
42704344
.discriminatedUnion('type', [flatFeeCharge, usageBasedCharge])
42714345
.describe('Customer charge.')
42724346

4347+
export const createChargeRequest = z
4348+
.discriminatedUnion('type', [
4349+
createFlatFeeChargeRequest,
4350+
createUsageBasedChargeRequest,
4351+
])
4352+
.describe('Customer charge.')
4353+
42734354
export const planPhase = z
42744355
.object({
42754356
name: z
@@ -4887,6 +4968,14 @@ export const listCustomerChargesResponse = z.object({
48874968
meta: paginatedMeta,
48884969
})
48894970

4971+
export const createCustomerChargesPathParams = z.object({
4972+
customerId: ulid,
4973+
})
4974+
4975+
export const createCustomerChargesBody = createChargeRequest
4976+
4977+
export const createCustomerChargesResponse = charge
4978+
48904979
export const createSubscriptionBody = subscriptionCreate
48914980

48924981
export const createSubscriptionResponse = subscription

0 commit comments

Comments
 (0)