A snapshot of packages/ in the Weft repository; the official packages are published from these commits. Markup-Kit-Commit: dd1e6eb5270e62e0c8b6c202b3d01f0da6e00ddc
213 lines
5.3 KiB
Text
213 lines
5.3 KiB
Text
// Checkout: Stripe's hosted payment page.
|
|
import { collect, request, segment } from "./stripe"
|
|
import type { Client, Page, RequestOptions, StripeError } from "./stripe"
|
|
|
|
/// A Checkout session. `customer`, `subscription` and `payment_intent` are expandable: `idOf` gives their ids.
|
|
export type Session = {
|
|
id: string,
|
|
url?: string,
|
|
status?: string,
|
|
payment_status?: string,
|
|
mode?: string,
|
|
amount_subtotal?: float,
|
|
amount_total?: float,
|
|
currency?: string,
|
|
client_reference_id?: string,
|
|
client_secret?: string,
|
|
customer?: json,
|
|
customer_email?: string,
|
|
customer_details?: json,
|
|
invoice?: json,
|
|
line_items?: json,
|
|
locale?: string,
|
|
metadata?: json,
|
|
payment_intent?: json,
|
|
subscription?: json,
|
|
success_url?: string,
|
|
cancel_url?: string,
|
|
expires_at?: float,
|
|
total_details?: json,
|
|
created?: float,
|
|
livemode?: boolean,
|
|
}
|
|
|
|
/// What a line is sold for, when it isn't an existing price.
|
|
export type PriceData = {
|
|
currency: string,
|
|
unit_amount?: float,
|
|
unit_amount_decimal?: string,
|
|
product?: string,
|
|
product_data?: {
|
|
name: string,
|
|
description?: string,
|
|
images?: string[],
|
|
metadata?: Map<string, string>,
|
|
},
|
|
recurring?: { interval: string, interval_count?: float },
|
|
tax_behavior?: string,
|
|
}
|
|
|
|
/// One line of what is being bought: an existing `price`, or `price_data`.
|
|
export type LineItem = { price?: string, price_data?: PriceData, quantity?: float, extra?: json }
|
|
|
|
/// A new Checkout session. `extra` holds any field this type doesn't list.
|
|
export type SessionParams = {
|
|
mode: "payment" | "subscription" | "setup",
|
|
success_url?: string,
|
|
cancel_url?: string,
|
|
return_url?: string,
|
|
ui_mode?: string,
|
|
line_items?: LineItem[],
|
|
customer?: string,
|
|
customer_email?: string,
|
|
customer_creation?: string,
|
|
client_reference_id?: string,
|
|
locale?: string,
|
|
metadata?: Map<string, string>,
|
|
expires_at?: float,
|
|
allow_promotion_codes?: boolean,
|
|
discounts?: { coupon?: string, promotion_code?: string }[],
|
|
payment_method_types?: string[],
|
|
billing_address_collection?: string,
|
|
automatic_tax?: { enabled: boolean },
|
|
payment_intent_data?: {
|
|
application_fee_amount?: float,
|
|
description?: string,
|
|
metadata?: Map<string, string>,
|
|
capture_method?: string,
|
|
receipt_email?: string,
|
|
transfer_data?: { destination: string, amount?: float },
|
|
extra?: json,
|
|
},
|
|
subscription_data?: {
|
|
application_fee_percent?: float,
|
|
description?: string,
|
|
metadata?: Map<string, string>,
|
|
trial_end?: float,
|
|
trial_period_days?: float,
|
|
extra?: json,
|
|
},
|
|
extra?: json,
|
|
}
|
|
|
|
/// What to list.
|
|
export type SessionListParams = {
|
|
customer?: string,
|
|
payment_intent?: string,
|
|
subscription?: string,
|
|
status?: string,
|
|
limit?: float,
|
|
starting_after?: string,
|
|
ending_before?: string,
|
|
extra?: json,
|
|
}
|
|
|
|
/// A line of a finished session.
|
|
export type SessionLineItem = {
|
|
id: string,
|
|
amount_subtotal?: float,
|
|
amount_total?: float,
|
|
currency?: string,
|
|
description?: string,
|
|
price?: json,
|
|
quantity?: float,
|
|
}
|
|
|
|
/// Creates a session: send the customer to its `url`.
|
|
///
|
|
/// ```weft
|
|
/// let session = try await checkout.create(stripe, {
|
|
/// mode: "payment",
|
|
/// success_url: "https://example.com/thanks?s={CHECKOUT_SESSION_ID}",
|
|
/// cancel_url: "https://example.com/cart",
|
|
/// line_items: [{ quantity: 1, price_data: { currency: "eur", unit_amount: 1999, product_data: { name: "Ticket" } } }],
|
|
/// }, { idempotencyKey: `sale-${sale.id}`, stripeAccount: accountId })
|
|
/// ```
|
|
export async fn create(
|
|
c: Client,
|
|
params: SessionParams,
|
|
options: RequestOptions = {},
|
|
): Session ! StripeError {
|
|
return try await request(
|
|
c,
|
|
"POST",
|
|
"/v1/checkout/sessions",
|
|
params,
|
|
options,
|
|
j => try j as Session,
|
|
)
|
|
}
|
|
|
|
/// Gets a session by id. `{ expand: ["subscription"] }` includes what it refers to.
|
|
export async fn retrieve(
|
|
c: Client,
|
|
id: string,
|
|
options: RequestOptions = {},
|
|
): Session ! StripeError {
|
|
return try await request(
|
|
c,
|
|
"GET",
|
|
`/v1/checkout/sessions/${segment(id)}`,
|
|
null,
|
|
options,
|
|
j => try j as Session,
|
|
)
|
|
}
|
|
|
|
/// Ends a session that hasn't been paid, so it can't be.
|
|
export async fn expire(c: Client, id: string, options: RequestOptions = {}): Session ! StripeError {
|
|
return try await request(
|
|
c,
|
|
"POST",
|
|
`/v1/checkout/sessions/${segment(id)}/expire`,
|
|
null,
|
|
options,
|
|
j => try j as Session,
|
|
)
|
|
}
|
|
|
|
/// One page of sessions, newest first.
|
|
export async fn list(c: Client, params: SessionListParams = {}, options: RequestOptions = {}): Page<
|
|
Session,
|
|
> ! StripeError {
|
|
return try await request(
|
|
c,
|
|
"GET",
|
|
"/v1/checkout/sessions",
|
|
params,
|
|
options,
|
|
j => try j as Page<Session>,
|
|
)
|
|
}
|
|
|
|
/// Every session matching `params`, up to `max`.
|
|
export async fn listAll(
|
|
c: Client,
|
|
params: SessionListParams = {},
|
|
options: RequestOptions = {},
|
|
max: int = 10000,
|
|
): Session[] ! StripeError {
|
|
return try await collect(
|
|
c,
|
|
"/v1/checkout/sessions",
|
|
params,
|
|
options,
|
|
j => try j as Page<Session>,
|
|
x => x.id,
|
|
max,
|
|
)
|
|
}
|
|
|
|
/// One page of what a session sold.
|
|
export async fn lineItems(c: Client, id: string, options: RequestOptions = {}): Page<
|
|
SessionLineItem,
|
|
> ! StripeError {
|
|
return try await request(
|
|
c,
|
|
"GET",
|
|
`/v1/checkout/sessions/${segment(id)}/line_items`,
|
|
null,
|
|
options,
|
|
j => try j as Page<SessionLineItem>,
|
|
)
|
|
}
|