A snapshot of packages/ in the Weft repository; the official packages are published from these commits. Markup-Kit-Commit: dd1e6eb5270e62e0c8b6c202b3d01f0da6e00ddc
139 lines
3.3 KiB
Text
139 lines
3.3 KiB
Text
// Prices: what a product costs.
|
|
import { collect, request, segment } from "./stripe"
|
|
import type { Client, Page, RequestOptions, StripeError } from "./stripe"
|
|
|
|
/// How often a recurring price is charged.
|
|
export type Recurring = {
|
|
interval: string,
|
|
interval_count?: float,
|
|
usage_type?: string,
|
|
meter?: string,
|
|
}
|
|
|
|
/// A price. `unit_amount` is in the currency's smallest unit (cents). `product` is expandable: `idOf` gives its id.
|
|
export type Price = {
|
|
id: string,
|
|
active?: boolean,
|
|
currency?: string,
|
|
unit_amount?: float,
|
|
unit_amount_decimal?: string,
|
|
billing_scheme?: string,
|
|
lookup_key?: string,
|
|
nickname?: string,
|
|
product?: json,
|
|
recurring?: Recurring,
|
|
tax_behavior?: string,
|
|
type?: string,
|
|
metadata?: json,
|
|
created?: float,
|
|
livemode?: boolean,
|
|
}
|
|
|
|
/// A new price. `extra` holds any field this type doesn't list.
|
|
export type PriceParams = {
|
|
currency: string,
|
|
unit_amount?: float,
|
|
unit_amount_decimal?: string,
|
|
product?: string,
|
|
product_data?: { name: string, active?: boolean, metadata?: Map<string, string> },
|
|
recurring?: { interval: string, interval_count?: float },
|
|
lookup_key?: string,
|
|
transfer_lookup_key?: boolean,
|
|
nickname?: string,
|
|
active?: boolean,
|
|
tax_behavior?: string,
|
|
metadata?: Map<string, string>,
|
|
extra?: json,
|
|
}
|
|
|
|
/// The fields of a price that can change after it is made.
|
|
export type PriceUpdateParams = {
|
|
active?: boolean,
|
|
lookup_key?: string,
|
|
transfer_lookup_key?: boolean,
|
|
nickname?: string,
|
|
tax_behavior?: string,
|
|
metadata?: Map<string, string>,
|
|
extra?: json,
|
|
}
|
|
|
|
/// What to list. `lookup_keys` finds prices by the stable names you gave them.
|
|
export type PriceListParams = {
|
|
active?: boolean,
|
|
currency?: string,
|
|
product?: string,
|
|
type?: string,
|
|
lookup_keys?: string[],
|
|
recurring?: { interval?: string, usage_type?: string },
|
|
limit?: float,
|
|
starting_after?: string,
|
|
ending_before?: string,
|
|
extra?: json,
|
|
}
|
|
|
|
/// Creates a price.
|
|
export async fn create(
|
|
c: Client,
|
|
params: PriceParams,
|
|
options: RequestOptions = {},
|
|
): Price ! StripeError {
|
|
return try await request(c, "POST", "/v1/prices", params, options, j => try j as Price)
|
|
}
|
|
|
|
/// Gets a price by id.
|
|
export async fn retrieve(c: Client, id: string, options: RequestOptions = {}): Price ! StripeError {
|
|
return try await request(
|
|
c,
|
|
"GET",
|
|
`/v1/prices/${segment(id)}`,
|
|
null,
|
|
options,
|
|
j => try j as Price,
|
|
)
|
|
}
|
|
|
|
/// Changes the fields given.
|
|
export async fn update(
|
|
c: Client,
|
|
id: string,
|
|
params: PriceUpdateParams,
|
|
options: RequestOptions = {},
|
|
): Price ! StripeError {
|
|
return try await request(
|
|
c,
|
|
"POST",
|
|
`/v1/prices/${segment(id)}`,
|
|
params,
|
|
options,
|
|
j => try j as Price,
|
|
)
|
|
}
|
|
|
|
/// One page of prices.
|
|
///
|
|
/// ```weft
|
|
/// let found = try await prices.list(stripe, { lookup_keys: ["base-monthly"], active: true })
|
|
/// ```
|
|
export async fn list(c: Client, params: PriceListParams = {}, options: RequestOptions = {}): Page<
|
|
Price,
|
|
> ! StripeError {
|
|
return try await request(c, "GET", "/v1/prices", params, options, j => try j as Page<Price>)
|
|
}
|
|
|
|
/// Every price matching `params`, up to `max`.
|
|
export async fn listAll(
|
|
c: Client,
|
|
params: PriceListParams = {},
|
|
options: RequestOptions = {},
|
|
max: int = 10000,
|
|
): Price[] ! StripeError {
|
|
return try await collect(
|
|
c,
|
|
"/v1/prices",
|
|
params,
|
|
options,
|
|
j => try j as Page<Price>,
|
|
x => x.id,
|
|
max,
|
|
)
|
|
}
|