import {z} from 'zod' export type GetApiApiOrgIdBillingsHostedSubscriptionUrlArgs = {org_id: string, plan: string}; export type GetApiApiOrgIdBillingsHostedSubscriptionUrlOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const stripeCheckoutSession = z.object({amount_subtotal: z.number().int().nullable().optional(), amount_total: z.number().int().nullable().optional(), cancel_url: z.string().nullable().optional(), client_reference_id: z.string().nullable().optional(), created: z.number().int(), currency: z.string().nullable().optional(), customer_email: z.string().nullable().optional(), expires_at: z.number().int(), id: z.string(), livemode: z.boolean(), metadata: z.record(z.string(), z.string()).nullable().optional(), mode: z.string(), payment_method_types: z.array(z.string()), payment_status: z.string(), status: z.string().nullable().optional(), success_url: z.string().nullable().optional(), url: z.string().nullable().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Get subscription URL for checkout session * * GET /api/{org_id}/billings/hosted_subscription_url */ export const getApiApiOrgIdBillingsHostedSubscriptionUrl = async (args: GetApiApiOrgIdBillingsHostedSubscriptionUrlArgs, options: GetApiApiOrgIdBillingsHostedSubscriptionUrlOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.plan != null) query.append("plan", String(args.plan)) if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/billings/hosted_subscription_url${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(stripeCheckoutSession, await res.json()) };