import {z} from 'zod' export type AddressInput = {line1?: (string | null) | undefined, line2?: (string | null) | undefined, postal_code?: (string | null) | undefined, city?: (string | null) | undefined, state?: (string | null) | undefined, country: 'AD' | 'AE' | 'AF' | 'AG' | 'AI' | 'AL' | 'AM' | 'AO' | 'AQ' | 'AR' | 'AS' | 'AT' | 'AU' | 'AW' | 'AX' | 'AZ' | 'BA' | 'BB' | 'BD' | 'BE' | 'BF' | 'BG' | 'BH' | 'BI' | 'BJ' | 'BL' | 'BM' | 'BN' | 'BO' | 'BQ' | 'BR' | 'BS' | 'BT' | 'BV' | 'BW' | 'BY' | 'BZ' | 'CA' | 'CC' | 'CD' | 'CF' | 'CG' | 'CH' | 'CI' | 'CK' | 'CL' | 'CM' | 'CN' | 'CO' | 'CR' | 'CV' | 'CW' | 'CX' | 'CY' | 'CZ' | 'DE' | 'DJ' | 'DK' | 'DM' | 'DO' | 'DZ' | 'EC' | 'EE' | 'EG' | 'EH' | 'ER' | 'ES' | 'ET' | 'FI' | 'FJ' | 'FK' | 'FM' | 'FO' | 'FR' | 'GA' | 'GB' | 'GD' | 'GE' | 'GF' | 'GG' | 'GH' | 'GI' | 'GL' | 'GM' | 'GN' | 'GP' | 'GQ' | 'GR' | 'GS' | 'GT' | 'GU' | 'GW' | 'GY' | 'HK' | 'HM' | 'HN' | 'HR' | 'HT' | 'HU' | 'ID' | 'IE' | 'IL' | 'IM' | 'IN' | 'IO' | 'IQ' | 'IS' | 'IT' | 'JE' | 'JM' | 'JO' | 'JP' | 'KE' | 'KG' | 'KH' | 'KI' | 'KM' | 'KN' | 'KR' | 'KW' | 'KY' | 'KZ' | 'LA' | 'LB' | 'LC' | 'LI' | 'LK' | 'LR' | 'LS' | 'LT' | 'LU' | 'LV' | 'LY' | 'MA' | 'MC' | 'MD' | 'ME' | 'MF' | 'MG' | 'MH' | 'MK' | 'ML' | 'MM' | 'MN' | 'MO' | 'MP' | 'MQ' | 'MR' | 'MS' | 'MT' | 'MU' | 'MV' | 'MW' | 'MX' | 'MY' | 'MZ' | 'NA' | 'NC' | 'NE' | 'NF' | 'NG' | 'NI' | 'NL' | 'NO' | 'NP' | 'NR' | 'NU' | 'NZ' | 'OM' | 'PA' | 'PE' | 'PF' | 'PG' | 'PH' | 'PK' | 'PL' | 'PM' | 'PN' | 'PR' | 'PS' | 'PT' | 'PW' | 'PY' | 'QA' | 'RE' | 'RO' | 'RS' | 'RW' | 'SA' | 'SB' | 'SC' | 'SD' | 'SE' | 'SG' | 'SH' | 'SI' | 'SJ' | 'SK' | 'SL' | 'SM' | 'SN' | 'SO' | 'SR' | 'SS' | 'ST' | 'SV' | 'SX' | 'SZ' | 'TC' | 'TD' | 'TF' | 'TG' | 'TH' | 'TJ' | 'TK' | 'TL' | 'TM' | 'TN' | 'TO' | 'TR' | 'TT' | 'TV' | 'TW' | 'TZ' | 'UA' | 'UG' | 'UM' | 'US' | 'UY' | 'UZ' | 'VA' | 'VC' | 'VE' | 'VG' | 'VI' | 'VN' | 'VU' | 'WF' | 'WS' | 'YE' | 'YT' | 'ZA' | 'ZM' | 'ZW'}; export type CheckoutConfirmStripe = {custom_field_data?: ({[key: string]: string | number | boolean | string | null}) | undefined, product_id?: (unknown | null) | undefined, product_price_id?: (unknown | null) | undefined, amount?: (number | null) | undefined, seats?: (number | null) | undefined, is_business_customer?: (boolean | null) | undefined, customer_name?: (string | null) | undefined, customer_email?: (string | null) | undefined, customer_billing_name?: (string | null) | undefined, customer_billing_address?: (AddressInput | null) | undefined, customer_tax_id?: (string | null) | undefined, locale?: (string | null) | undefined, payment_method_type?: (string | null) | undefined, discount_code?: (string | null) | undefined, allow_trial?: (false | null) | undefined, confirmation_token_id?: (string | null) | undefined}; export type CreateApiV1CheckoutsClientClientSecretConfirmArgs = {client_secret: string, body: CheckoutConfirmStripe}; export type CreateApiV1CheckoutsClientClientSecretConfirmOptions = {baseUrl?: string | undefined, oidc?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentProcessor = z.literal("stripe"); export const taxBehavior = z.enum(["inclusive", "exclusive"]); export const trialInterval = z.enum(["day", "week", "month", "year"]); export const address = z.object({line1: z.string().nullable().optional(), line2: z.string().nullable().optional(), postal_code: z.string().nullable().optional(), city: z.string().nullable().optional(), state: z.string().nullable().optional(), country: z.enum(["AD", "AE", "AF", "AG", "AI", "AL", "AM", "AO", "AQ", "AR", "AS", "AT", "AU", "AW", "AX", "AZ", "BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BL", "BM", "BN", "BO", "BQ", "BR", "BS", "BT", "BV", "BW", "BY", "BZ", "CA", "CC", "CD", "CF", "CG", "CH", "CI", "CK", "CL", "CM", "CN", "CO", "CR", "CU", "CV", "CW", "CX", "CY", "CZ", "DE", "DJ", "DK", "DM", "DO", "DZ", "EC", "EE", "EG", "EH", "ER", "ES", "ET", "FI", "FJ", "FK", "FM", "FO", "FR", "GA", "GB", "GD", "GE", "GF", "GG", "GH", "GI", "GL", "GM", "GN", "GP", "GQ", "GR", "GS", "GT", "GU", "GW", "GY", "HK", "HM", "HN", "HR", "HT", "HU", "ID", "IE", "IL", "IM", "IN", "IO", "IQ", "IR", "IS", "IT", "JE", "JM", "JO", "JP", "KE", "KG", "KH", "KI", "KM", "KN", "KP", "KR", "KW", "KY", "KZ", "LA", "LB", "LC", "LI", "LK", "LR", "LS", "LT", "LU", "LV", "LY", "MA", "MC", "MD", "ME", "MF", "MG", "MH", "MK", "ML", "MM", "MN", "MO", "MP", "MQ", "MR", "MS", "MT", "MU", "MV", "MW", "MX", "MY", "MZ", "NA", "NC", "NE", "NF", "NG", "NI", "NL", "NO", "NP", "NR", "NU", "NZ", "OM", "PA", "PE", "PF", "PG", "PH", "PK", "PL", "PM", "PN", "PR", "PS", "PT", "PW", "PY", "QA", "RE", "RO", "RS", "RU", "RW", "SA", "SB", "SC", "SD", "SE", "SG", "SH", "SI", "SJ", "SK", "SL", "SM", "SN", "SO", "SR", "SS", "ST", "SV", "SX", "SY", "SZ", "TC", "TD", "TF", "TG", "TH", "TJ", "TK", "TL", "TM", "TN", "TO", "TR", "TT", "TV", "TW", "TZ", "UA", "UG", "UM", "US", "UY", "UZ", "VA", "VC", "VE", "VG", "VI", "VN", "VU", "WF", "WS", "YE", "YT", "ZA", "ZM", "ZW"])}); export const billingAddressFieldMode = z.enum(["required", "optional", "disabled"]); export const checkoutBillingAddressFields = z.object({country: billingAddressFieldMode, state: billingAddressFieldMode, city: billingAddressFieldMode, postal_code: billingAddressFieldMode, line1: billingAddressFieldMode, line2: billingAddressFieldMode}); export const productVisibility = z.enum(["draft", "private", "public"]); export const recurringInterval = z.enum(["day", "week", "month", "year"]); export const productPriceSource = z.enum(["catalog", "ad_hoc"]); export const taxBehaviorOption = z.enum(["location", "inclusive", "exclusive"]); export const legacyRecurringProductPriceFixed = z.object({created_at: z.string(), modified_at: z.string().nullable(), id: z.string(), source: productPriceSource, amount_type: z.literal("fixed"), price_currency: z.string(), tax_behavior: taxBehaviorOption.nullable(), is_archived: z.boolean(), product_id: z.string(), type: z.literal("recurring"), recurring_interval: recurringInterval, price_amount: z.number().int(), legacy: z.literal(true)}); export const legacyRecurringProductPriceCustom = z.object({created_at: z.string(), modified_at: z.string().nullable(), id: z.string(), source: productPriceSource, amount_type: z.literal("custom"), price_currency: z.string(), tax_behavior: taxBehaviorOption.nullable(), is_archived: z.boolean(), product_id: z.string(), type: z.literal("recurring"), recurring_interval: recurringInterval, minimum_amount: z.number().int(), maximum_amount: z.number().int().nullable(), preset_amount: z.number().int().nullable(), legacy: z.literal(true)}); export const legacyRecurringProductPrice = z.discriminatedUnion("amount_type", [legacyRecurringProductPriceFixed, legacyRecurringProductPriceCustom]); export const productPriceFixed = z.object({created_at: z.string(), modified_at: z.string().nullable(), id: z.string(), source: productPriceSource, amount_type: z.literal("fixed"), price_currency: z.string(), tax_behavior: taxBehaviorOption.nullable(), is_archived: z.boolean(), product_id: z.string(), price_amount: z.number().int()}); export const productPriceCustom = z.object({created_at: z.string(), modified_at: z.string().nullable(), id: z.string(), source: productPriceSource, amount_type: z.literal("custom"), price_currency: z.string(), tax_behavior: taxBehaviorOption.nullable(), is_archived: z.boolean(), product_id: z.string(), minimum_amount: z.number().int(), maximum_amount: z.number().int().nullable(), preset_amount: z.number().int().nullable()}); export const seatTierType = z.enum(["volume", "graduated"]); export const productPriceSeatTier = z.object({min_seats: z.number().int().gte(1), max_seats: z.number().int().gte(1).nullable().optional(), price_per_seat: z.number().int().gte(0)}); export const productPriceSeatTiersOutput = z.object({seat_tier_type: seatTierType.optional(), tiers: z.array(productPriceSeatTier), minimum_seats: z.number().int(), maximum_seats: z.number().int().nullable()}); export const productPriceSeatBased = z.object({created_at: z.string(), modified_at: z.string().nullable(), id: z.string(), source: productPriceSource, amount_type: z.literal("seat_based"), price_currency: z.string(), tax_behavior: taxBehaviorOption.nullable(), is_archived: z.boolean(), product_id: z.string(), seat_tiers: productPriceSeatTiersOutput}); export const meterUnit = z.enum(["scalar", "token", "custom"]); export const productPriceMeter = z.object({id: z.string(), name: z.string(), unit: meterUnit, custom_label: z.string().nullable(), custom_multiplier: z.number().int().nullable()}); export const productPriceMeteredUnit = z.object({created_at: z.string(), modified_at: z.string().nullable(), id: z.string(), source: productPriceSource, amount_type: z.literal("metered_unit"), price_currency: z.string(), tax_behavior: taxBehaviorOption.nullable(), is_archived: z.boolean(), product_id: z.string(), unit_amount: z.string(), cap_amount: z.number().int().nullable(), meter_id: z.string(), meter: productPriceMeter}); export const productPrice = z.discriminatedUnion("amount_type", [productPriceFixed, productPriceCustom, productPriceSeatBased, productPriceMeteredUnit]); export const benefitType = z.enum(["custom", "discord", "github_repository", "downloadables", "license_keys", "meter_credit", "feature_flag", "slack_shared_channel"]); export const benefitPublic = z.object({id: z.string(), created_at: z.string(), modified_at: z.string().nullable(), type: benefitType, description: z.string(), selectable: z.boolean(), deletable: z.boolean(), is_deleted: z.boolean(), organization_id: z.string()}); export const productMediaFileRead = z.object({id: z.string(), organization_id: z.string(), name: z.string(), path: z.string(), mime_type: z.string(), size: z.number().int(), storage_version: z.string().nullable(), checksum_etag: z.string().nullable(), checksum_sha256_base64: z.string().nullable(), checksum_sha256_hex: z.string().nullable(), last_modified_at: z.string().nullable(), version: z.string().nullable(), service: z.literal("product_media"), is_uploaded: z.boolean(), created_at: z.string(), size_readable: z.string(), public_url: z.string()}); export const checkoutProduct = z.object({id: z.string(), created_at: z.string(), modified_at: z.string().nullable(), trial_interval: trialInterval.nullable(), trial_interval_count: z.number().int().nullable(), name: z.string(), description: z.string().nullable(), visibility: productVisibility, recurring_interval: recurringInterval.nullable(), recurring_interval_count: z.number().int().nullable(), meter_interval: recurringInterval.nullable(), meter_interval_count: z.number().int().nullable(), is_recurring: z.boolean(), is_archived: z.boolean(), organization_id: z.string(), prices: z.array(z.union([legacyRecurringProductPrice, productPrice])), benefits: z.array(benefitPublic), medias: z.array(productMediaFileRead)}); export const discountDuration = z.enum(["once", "forever", "repeating"]); export const discountType = z.enum(["fixed", "percentage"]); export const checkoutDiscountFixedOnceForeverDuration = z.object({duration: discountDuration, type: discountType, amount: z.number().int(), currency: z.string(), amounts: z.record(z.string(), z.number().int()), id: z.string(), name: z.string(), code: z.string().nullable()}); export const checkoutDiscountFixedRepeatDuration = z.object({duration: discountDuration, duration_in_months: z.number().int(), type: discountType, amount: z.number().int(), currency: z.string(), amounts: z.record(z.string(), z.number().int()), id: z.string(), name: z.string(), code: z.string().nullable()}); export const checkoutDiscountPercentageOnceForeverDuration = z.object({duration: discountDuration, type: discountType, basis_points: z.number().int(), id: z.string(), name: z.string(), code: z.string().nullable()}); export const checkoutDiscountPercentageRepeatDuration = z.object({duration: discountDuration, duration_in_months: z.number().int(), type: discountType, basis_points: z.number().int(), id: z.string(), name: z.string(), code: z.string().nullable()}); export const subscriptionProrationBehavior = z.enum(["invoice", "prorate", "next_period", "reset"]); export const checkoutOrganization = z.object({created_at: z.string(), modified_at: z.string().nullable(), id: z.string(), name: z.string(), slug: z.string(), avatar_url: z.string().nullable(), proration_behavior: subscriptionProrationBehavior, allow_customer_updates: z.boolean()}); export const metadataOutputType = z.record(z.string(), z.union([z.string(), z.number().int(), z.number(), z.boolean()])); export const customFieldTextProperties = z.object({form_label: z.string().min(1).optional(), form_help_text: z.string().min(1).optional(), form_placeholder: z.string().min(1).optional(), textarea: z.boolean().optional(), min_length: z.number().int().gte(0).lte(2147483647).optional(), max_length: z.number().int().gte(0).lte(2147483647).optional()}); export const customFieldText = z.object({created_at: z.string(), modified_at: z.string().nullable(), id: z.string(), metadata: metadataOutputType, type: z.literal("text"), slug: z.string(), name: z.string(), organization_id: z.string(), properties: customFieldTextProperties}); export const customFieldNumberProperties = z.object({form_label: z.string().min(1).optional(), form_help_text: z.string().min(1).optional(), form_placeholder: z.string().min(1).optional(), ge: z.number().int().gte(-2147483648).lte(2147483647).optional(), le: z.number().int().gte(-2147483648).lte(2147483647).optional()}); export const customFieldNumber = z.object({created_at: z.string(), modified_at: z.string().nullable(), id: z.string(), metadata: metadataOutputType, type: z.literal("number"), slug: z.string(), name: z.string(), organization_id: z.string(), properties: customFieldNumberProperties}); export const customFieldDateProperties = z.object({form_label: z.string().min(1).optional(), form_help_text: z.string().min(1).optional(), form_placeholder: z.string().min(1).optional(), ge: z.number().int().gte(-2147483648).lte(2147483647).optional(), le: z.number().int().gte(-2147483648).lte(2147483647).optional()}); export const customFieldDate = z.object({created_at: z.string(), modified_at: z.string().nullable(), id: z.string(), metadata: metadataOutputType, type: z.literal("date"), slug: z.string(), name: z.string(), organization_id: z.string(), properties: customFieldDateProperties}); export const customFieldCheckboxProperties = z.object({form_label: z.string().min(1).optional(), form_help_text: z.string().min(1).optional(), form_placeholder: z.string().min(1).optional()}); export const customFieldCheckbox = z.object({created_at: z.string(), modified_at: z.string().nullable(), id: z.string(), metadata: metadataOutputType, type: z.literal("checkbox"), slug: z.string(), name: z.string(), organization_id: z.string(), properties: customFieldCheckboxProperties}); export const customFieldSelectOption = z.object({value: z.string().min(1), label: z.string().min(1)}); export const customFieldSelectProperties = z.object({form_label: z.string().min(1).optional(), form_help_text: z.string().min(1).optional(), form_placeholder: z.string().min(1).optional(), options: z.array(customFieldSelectOption)}); export const customFieldSelect = z.object({created_at: z.string(), modified_at: z.string().nullable(), id: z.string(), metadata: metadataOutputType, type: z.literal("select"), slug: z.string(), name: z.string(), organization_id: z.string(), properties: customFieldSelectProperties}); export const customField = z.discriminatedUnion("type", [customFieldText, customFieldNumber, customFieldDate, customFieldCheckbox, customFieldSelect]); export const attachedCustomField = z.object({custom_field_id: z.string(), custom_field: customField, order: z.number().int(), required: z.boolean()}); export const checkoutPublicConfirmed = z.object({id: z.string(), created_at: z.string(), modified_at: z.string().nullable(), custom_field_data: z.record(z.string(), z.union([z.string(), z.number().int(), z.boolean(), z.string()]).nullable()).optional(), payment_processor: paymentProcessor, status: z.literal("confirmed"), client_secret: z.string(), url: z.string(), expires_at: z.string(), success_url: z.string(), return_url: z.string().nullable(), embed_origin: z.string().nullable(), amount: z.number().int(), seats: z.number().int().nullable().optional(), min_seats: z.number().int().nullable().optional(), max_seats: z.number().int().nullable().optional(), discount_amount: z.number().int(), net_amount: z.number().int(), tax_amount: z.number().int().nullable(), tax_behavior: taxBehavior.nullable(), total_amount: z.number().int(), currency: z.string(), allow_trial: z.boolean().nullable(), active_trial_interval: trialInterval.nullable(), active_trial_interval_count: z.number().int().nullable(), trial_end: z.string().nullable(), organization_id: z.string(), product_id: z.string().nullable(), product_price_id: z.string().nullable(), discount_id: z.string().nullable(), allow_discount_codes: z.boolean(), require_billing_address: z.boolean(), is_discount_applicable: z.boolean(), is_free_product_price: z.boolean(), is_payment_required: z.boolean(), is_payment_setup_required: z.boolean(), is_payment_form_required: z.boolean(), customer_id: z.string().nullable(), is_business_customer: z.boolean(), customer_name: z.string().nullable(), customer_email: z.string().nullable(), customer_ip_address: z.string().nullable(), customer_billing_name: z.string().nullable(), customer_billing_address: address.nullable(), customer_tax_id: z.string().nullable(), locale: z.string().nullable().optional(), payment_method_type: z.string().nullable(), payment_processor_metadata: z.record(z.string(), z.string()), billing_address_fields: checkoutBillingAddressFields, products: z.array(checkoutProduct), product: checkoutProduct.nullable(), product_price: z.union([legacyRecurringProductPrice, productPrice]).nullable(), prices: z.record(z.string(), z.array(z.union([legacyRecurringProductPrice, productPrice]))).nullable(), discount: z.union([checkoutDiscountFixedOnceForeverDuration, checkoutDiscountFixedRepeatDuration, checkoutDiscountPercentageOnceForeverDuration, checkoutDiscountPercentageRepeatDuration]).nullable(), organization: checkoutOrganization, attached_custom_fields: z.array(attachedCustomField).nullable(), customer_session_token: z.string().nullable()}); 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 }; /** * Confirm Checkout Session from Client * * POST /v1/checkouts/client/{client_secret}/confirm * * Also accepts auth: pat */ export const createApiV1CheckoutsClientClientSecretConfirm = async (args: CreateApiV1CheckoutsClientClientSecretConfirmArgs, options?: CreateApiV1CheckoutsClientClientSecretConfirmOptions) => { const {baseUrl = "https://api.polar.sh", fetch: fetchFn = fetch, oidc} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oidc !== undefined) headers.set('authorization', `Bearer ${oidc}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/checkouts/client/${encodeURIComponent(String(args.client_secret))}/confirm${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(checkoutPublicConfirmed, await res.json()) };