diff --git a/types/stripe-js/checkout.d.ts b/types/stripe-js/checkout.d.ts index edbf0a52..07a648bb 100644 --- a/types/stripe-js/checkout.d.ts +++ b/types/stripe-js/checkout.d.ts @@ -1,153 +1,489 @@ -export interface RedirectToCheckoutServerOptions { - /** - * The ID of the [Checkout Session](https://stripe.com/docs/api/checkout/sessions) that is used in [Checkout's server integration](https://stripe.com/docs/payments/checkout/one-time). - */ - sessionId: string; +import { + LayoutObject, + Layout, + TermsOption, + StripePaymentElement, +} from './elements/payment'; +import { + AddressMode, + ContactOption, + StripeAddressElement, +} from './elements/address'; +import {Appearance, CssFontSource, CustomFontSource} from './elements-group'; +import {StripeError} from './stripe'; +import { + StripeCurrencySelectorElement, + FieldsOption, + StripeElementBase, + StripeExpressCheckoutElement, + StripeExpressCheckoutElementConfirmEvent, + StripeExpressCheckoutElementOptions, + StripeExpressCheckoutElementReadyEvent, +} from './elements'; + +/** + * Requires beta access: + * Contact [Stripe support](https://support.stripe.com/) for more information. + */ + +export interface StripeCheckoutElementsOptions { + appearance?: Appearance; + loader?: 'auto' | 'always' | 'never'; + fonts?: Array; } -export interface RedirectToCheckoutClientOptions { - /** - * The URL to which Stripe should send customers when payment is complete. - * If you’d like access to the Checkout Session for the successful payment, read more about it in our guide on [fulfilling your payments with webhooks](https://stripe.com/docs/payments/checkout/fulfillment#webhooks). - */ - successUrl: string; +export interface StripeCheckoutOptions { + clientSecret: string; + elementsOptions?: StripeCheckoutElementsOptions; +} - /** - * The URL to which Stripe should send customers when payment is canceled. - */ - cancelUrl: string; +/* Elements with CheckoutSessions API types */ +export type StripeCheckoutAddress = { + country: string; + line1?: string | null; + line2?: string | null; + city?: string | null; + postal_code?: string | null; + state?: string | null; +}; - /** - * An array of objects representing the items that your customer would like to purchase. - * These items are shown as line items in the Checkout interface and make up the total amount to be collected by Checkout. - */ - lineItems?: Array<{ - /** - * The ID of the price that the customer would like to purchase. SKU or plan IDs may also be used. - */ - price?: string; - - /** - * The quantity of units for the item. - */ - quantity?: number; - }>; +export type StripeCheckoutAdjustableQuantity = { + maximum: number; + minimum: number; +}; - /** - * An array of objects representing the items that your customer would like to purchase. - * These items are shown as line items in the Checkout interface and make up the total amount to be collected by Checkout. - * - * @deprecated - */ - items?: Array<{ - /** - * The ID of the SKU that the customer would like to purchase - */ - sku?: string; - - /** - * The ID of the plan that the customer would like to subscribe to. - */ - plan?: string; - - /** - * The quantity of units for the item. - */ - quantity?: number; - }>; +export type StripeCheckoutBillingInterval = 'day' | 'month' | 'week' | 'year'; + +export type StripeCheckoutConfirmationRequirement = + | 'phoneNumber' + | 'shippingAddress' + | 'billingAddress' + | 'paymentDetails' + | 'email'; + +export type StripeCheckoutContact = { + name?: string | null; + address: StripeCheckoutAddress; +}; + +export type StripeCheckoutDeliveryEstimate = { + maximum: StripeCheckoutEstimate | null; + minimum: StripeCheckoutEstimate | null; +}; + +export type StripeCheckoutDiscountAmount = { + amount: number; + displayName: string; + promotionCode: string | null; + recurring: + | {type: 'forever'} + | {type: 'repeating'; durationInMonths: number} + | null; +}; + +export type StripeCheckoutDueNext = { + amountSubtotal: number; + amountDiscount: number; + amountTaxInclusive: number; + amountTaxExclusive: number; + billingCycleAnchor: number | null; +}; + +export type StripeCheckoutEstimate = { + unit: 'business_day' | 'day' | 'hour' | 'week' | 'month'; + value: number; +}; + +export type StripeCheckoutLastPaymentError = { + message: string; +}; + +export type StripeCheckoutRedirectBehavior = 'always' | 'if_required'; + +export type StripeCheckoutSavedPaymentMethod = { + id: string; + type: 'card'; + card: { + brand: string; + expMonth: number; + expYear: number; + last4: string; + }; + billingDetails: { + email?: string | null; + name?: string | null; + phone?: string | null; + address?: { + line1?: string | null; + line2?: string | null; + city?: string | null; + postal_code?: string | null; + state?: string | null; + country?: string | null; + } | null; + }; +}; + +export type StripeCheckoutTaxAmount = { + amount: number; + inclusive: boolean; + displayName: string; +}; + +export type StripeCheckoutLineItem = { + id: string; + name: string; + amountDiscount: number; + amountSubtotal: number; + amountTaxExclusive: number; + amountTaxInclusive: number; + unitAmount: number; + description: string | null; + quantity: number; + discountAmounts: Array | null; + taxAmounts: Array | null; + recurring: { + interval: StripeCheckoutBillingInterval; + intervalCount: number; + isProrated: boolean; + usageType: 'metered' | 'licensed'; + } | null; + adjustableQuantity: StripeCheckoutAdjustableQuantity | null; + images: string[]; +}; + +export type StripeCheckoutRecurring = { + interval: StripeCheckoutBillingInterval; + intervalCount: number; + dueNext: StripeCheckoutDueNext; + trial: StripeCheckoutTrial | null; +}; +export type StripeCheckoutShipping = { + shippingOption: StripeCheckoutShippingOption; + taxAmounts: Array | null; +}; + +export type StripeCheckoutShippingOption = { + id: string; + amount: number; + currency: string; + displayName: string | null; + deliveryEstimate: StripeCheckoutDeliveryEstimate | null; +}; + +export type StripeCheckoutStatus = + | {type: 'open'} + | {type: 'expired'} + | { + type: 'complete'; + paymentStatus: 'paid' | 'unpaid' | 'no_payment_required'; + }; + +export type StripeCheckoutTaxStatus = + | {status: 'ready'} + | {status: 'requires_shipping_address'} + | {status: 'requires_billing_address'}; + +export type StripeCheckoutTotalSummary = { + appliedBalance: number; + balanceAppliedToNextInvoice: boolean; + discount: number; + shippingRate: number; + subtotal: number; + taxExclusive: number; + taxInclusive: number; + total: number; +}; + +export type StripeCheckoutTrial = { + trialEnd: number; + trialPeriodDays: number; +}; + +export type StripeCheckoutCurrencyOption = { + amount: number; + currency: string; + currencyConversion?: {fxRate: number; sourceCurrency: string}; +}; + +/* Custom Checkout session */ +export interface StripeCheckoutSession { + billingAddress: StripeCheckoutContact | null; + businessName: string | null; + canConfirm: boolean; + confirmationRequirements: StripeCheckoutConfirmationRequirement[]; + currency: string; + currencyOptions: Array | null; + discountAmounts: Array | null; + email: string | null; + id: string; + lastPaymentError: StripeCheckoutLastPaymentError | null; + lineItems: Array; + livemode: boolean; + phoneNumber: string | null; + recurring: StripeCheckoutRecurring | null; + savedPaymentMethods: Array | null; + shipping: StripeCheckoutShipping | null; + shippingAddress: StripeCheckoutContact | null; + shippingOptions: Array; + status: StripeCheckoutStatus; + tax: StripeCheckoutTaxStatus; + taxAmounts: Array | null; + total: StripeCheckoutTotalSummary; +} + +export type StripeCheckoutResult = + | {session: StripeCheckoutSession; error?: undefined} + | {session?: undefined; error: StripeError}; + +export type StripeCheckoutPaymentElementOptions = { + layout?: Layout | LayoutObject; + paymentMethodOrder?: Array; + readonly?: boolean; + terms?: TermsOption; + fields?: FieldsOption; +}; + +export type StripeCheckoutAddressElementOptions = { + mode: AddressMode; + contacts?: ContactOption[]; + display?: { + name?: 'full' | 'split' | 'organization'; + }; +}; + +export type StripeCheckoutExpressCheckoutElementOptions = { + buttonHeight: StripeExpressCheckoutElementOptions['buttonHeight']; + buttonTheme: StripeExpressCheckoutElementOptions['buttonTheme']; + buttonType: StripeExpressCheckoutElementOptions['buttonType']; + layout: StripeExpressCheckoutElementOptions['layout']; + paymentMethodOrder: StripeExpressCheckoutElementOptions['paymentMethodOrder']; + paymentMethods: StripeExpressCheckoutElementOptions['paymentMethods']; +}; + +export type StripeCheckoutUpdateHandler = ( + session: StripeCheckoutSession +) => void; + +export type StripeCheckoutExpressCheckoutElement = StripeElementBase & { /** - * The mode of the Checkout Session. Required if using lineItems. + * Triggered when the element is fully rendered. */ - mode?: 'payment' | 'subscription'; + on( + eventType: 'ready', + handler: (event: StripeExpressCheckoutElementReadyEvent) => any + ): StripeCheckoutExpressCheckoutElement; + once( + eventType: 'ready', + handler: (event: StripeExpressCheckoutElementReadyEvent) => any + ): StripeCheckoutExpressCheckoutElement; + off( + eventType: 'ready', + handler?: (event: StripeExpressCheckoutElementReadyEvent) => any + ): StripeCheckoutExpressCheckoutElement; /** - * A unique string to reference the Checkout session. - * This can be a customer ID, a cart ID, or similar. - * It is included in the `checkout.session.completed` webhook and can be used to fulfill the purchase. + * Triggered when the element gains focus. */ - clientReferenceId?: string; + on( + eventType: 'focus', + handler: (event: {elementType: 'expressCheckout'}) => any + ): StripeCheckoutExpressCheckoutElement; + once( + eventType: 'focus', + handler: (event: {elementType: 'expressCheckout'}) => any + ): StripeCheckoutExpressCheckoutElement; + off( + eventType: 'focus', + handler?: (event: {elementType: 'expressCheckout'}) => any + ): StripeCheckoutExpressCheckoutElement; /** - * The email address used to create the customer object. - * If you already know your customer's email address, use this attribute to prefill it on Checkout. + * Triggered when the element loses focus. */ - customerEmail?: string; + on( + eventType: 'blur', + handler: (event: {elementType: 'expressCheckout'}) => any + ): StripeCheckoutExpressCheckoutElement; + once( + eventType: 'blur', + handler: (event: {elementType: 'expressCheckout'}) => any + ): StripeCheckoutExpressCheckoutElement; + off( + eventType: 'blur', + handler?: (event: {elementType: 'expressCheckout'}) => any + ): StripeCheckoutExpressCheckoutElement; /** - * Specify whether Checkout should collect the customer’s billing address. - * If set to `required`, Checkout will attempt to collect the customer’s billing address. - * If not set or set to `auto` Checkout will only attempt to collect the billing address when necessary. + * Triggered when the escape key is pressed within the element. */ - billingAddressCollection?: 'auto' | 'required'; + on( + eventType: 'escape', + handler: (event: {elementType: 'expressCheckout'}) => any + ): StripeCheckoutExpressCheckoutElement; + once( + eventType: 'escape', + handler: (event: {elementType: 'expressCheckout'}) => any + ): StripeCheckoutExpressCheckoutElement; + off( + eventType: 'escape', + handler?: (event: {elementType: 'expressCheckout'}) => any + ): StripeCheckoutExpressCheckoutElement; /** - * Provides configuration for Checkout to collect a shipping address from a customer. + * Triggered when the element fails to load. */ - shippingAddressCollection?: { - /** - * An array of two-letter ISO country codes representing which countries - * Checkout should provide as options for shipping locations. The codes are - * expected to be uppercase. Unsupported country codes: AS, CX, CC, CU, HM, IR, KP, MH, FM, NF, MP, PW, SD, SY, UM, VI. - */ - allowedCountries: string[]; - }; + on( + eventType: 'loaderror', + handler: (event: { + elementType: 'expressCheckout'; + error: StripeError; + }) => any + ): StripeCheckoutExpressCheckoutElement; + once( + eventType: 'loaderror', + handler: (event: { + elementType: 'expressCheckout'; + error: StripeError; + }) => any + ): StripeCheckoutExpressCheckoutElement; + off( + eventType: 'loaderror', + handler?: (event: { + elementType: 'expressCheckout'; + error: StripeError; + }) => any + ): StripeCheckoutExpressCheckoutElement; /** - * The [IETF language tag](https://en.wikipedia.org/wiki/IETF_language_tag) of the locale to display Checkout in. - * Default is `auto` (Stripe detects the locale of the browser). + * Triggered when a buyer authorizes a payment within a supported payment method. */ - locale?: CheckoutLocale; + on( + eventType: 'confirm', + handler: (event: StripeExpressCheckoutElementConfirmEvent) => any + ): StripeCheckoutExpressCheckoutElement; + once( + eventType: 'confirm', + handler: (event: StripeExpressCheckoutElementConfirmEvent) => any + ): StripeCheckoutExpressCheckoutElement; + off( + eventType: 'confirm', + handler?: (event: StripeExpressCheckoutElementConfirmEvent) => any + ): StripeCheckoutExpressCheckoutElement; /** - * Describes the type of transaction being performed by Checkout in order to customize relevant text on the page, such as the **Submit** button. - * `submitType` can only be specified when using using line items or SKUs, and not subscriptions. - * The default is `auto`. + * Updates the options the `ExpressCheckoutElement` was initialized with. + * Updates are merged into the existing configuration. */ - submitType?: 'auto' | 'book' | 'donate' | 'pay'; -} + update: StripeExpressCheckoutElement['update']; +}; + +type AnyBuyerError = {message: string; code: null}; +type ApplyPromotionCodeError = + | {message: string; code: 'invalidCode'} + | AnyBuyerError; +export type StripeCheckoutApplyPromotionCodeResult = + | {type: 'success'; success: StripeCheckoutSession} + | {type: 'error'; error: ApplyPromotionCodeError}; + +export type StripeCheckoutRemovePromotionCodeResult = + | {type: 'success'; success: StripeCheckoutSession} + | {type: 'error'; error: AnyBuyerError}; + +export type StripeCheckoutUpdateAddressResult = + | {type: 'success'; success: StripeCheckoutSession} + | {type: 'error'; error: AnyBuyerError}; -export type RedirectToCheckoutOptions = - | RedirectToCheckoutServerOptions - | RedirectToCheckoutClientOptions; - -export type CheckoutLocale = - | 'auto' - | 'bg' - | 'cs' - | 'da' - | 'de' - | 'el' - | 'en' - | 'en-GB' - | 'es' - | 'es-419' - | 'et' - | 'fi' - | 'fil' - | 'fr' - | 'fr-CA' - | 'hr' - | 'hu' - | 'id' - | 'it' - | 'ja' - | 'lt' - | 'lv' - | 'ms' - | 'mt' - | 'nb' - | 'nl' - | 'pl' - | 'pt' - | 'pt-BR' - | 'ro' - | 'ru' - | 'sk' - | 'sl' - | 'sv' - | 'th' - | 'tr' - | 'zh' - | 'zh-HK' - | 'zh-TW'; +export type StripeCheckoutUpdatePhoneNumberResult = + | {type: 'success'; success: StripeCheckoutSession} + | {type: 'error'; error: never}; + +type UpdateEmailError = + | {message: string; code: 'incompleteEmail'} + | {message: string; code: 'invalidEmail'}; +export type StripeCheckoutUpdateEmailResult = + | {type: 'success'; success: StripeCheckoutSession} + | {type: 'error'; error: UpdateEmailError}; + +export type StripeCheckoutUpdateLineItemQuantityResult = + | {type: 'success'; success: StripeCheckoutSession} + | {type: 'error'; error: AnyBuyerError}; + +export type StripeCheckoutUpdateShippingOptionResult = + | {type: 'success'; success: StripeCheckoutSession} + | {type: 'error'; error: AnyBuyerError}; + +type ConfirmError = + | { + message: string; + code: 'paymentFailed'; + paymentFailed: { + declineCode: string | null; + }; + } + | AnyBuyerError; +export type StripeCheckoutConfirmResult = + | {type: 'success'; success: StripeCheckoutSession} + | {type: 'error'; error: ConfirmError}; +export interface StripeCheckout { + /* Custom Checkout methods */ + applyPromotionCode: ( + promotionCode: string + ) => Promise; + removePromotionCode: () => Promise; + updateShippingAddress: ( + shippingAddress: StripeCheckoutContact | null + ) => Promise; + updateBillingAddress: ( + billingAddress: StripeCheckoutContact | null + ) => Promise; + updatePhoneNumber: ( + phoneNumber: string + ) => Promise; + updateEmail: (email: string) => Promise; + updateLineItemQuantity: (args: { + lineItem: string; + quantity: number; + }) => Promise; + updateShippingOption: ( + shippingOption: string + ) => Promise; + confirm: (args?: { + returnUrl?: string; + redirect?: StripeCheckoutRedirectBehavior; + paymentMethod?: string; + savePaymentMethod?: boolean; + }) => Promise; + session: () => StripeCheckoutSession; + on: (event: 'change', handler: StripeCheckoutUpdateHandler) => void; + + /* Elements methods */ + changeAppearance: (appearance: Appearance) => void; + getElement(elementType: 'payment'): StripePaymentElement | null; + getElement( + elementType: 'address', + mode: AddressMode + ): StripeAddressElement | null; + getElement( + elementType: 'expressCheckout' + ): StripeCheckoutExpressCheckoutElement | null; + /* Requires beta access: Contact [Stripe support](https://support.stripe.com/) for more information. */ + getElement( + elementType: 'currencySelector' + ): StripeCurrencySelectorElement | null; + createElement( + elementType: 'payment', + options?: StripeCheckoutPaymentElementOptions + ): StripePaymentElement; + createElement( + elementType: 'address', + options: StripeCheckoutAddressElementOptions + ): StripeAddressElement; + createElement( + elementType: 'expressCheckout', + options: StripeCheckoutExpressCheckoutElementOptions + ): StripeCheckoutExpressCheckoutElement; + /* Requires beta access: Contact [Stripe support](https://support.stripe.com/) for more information. */ + createElement(elementType: 'currencySelector'): StripeCurrencySelectorElement; +} diff --git a/types/stripe-js/custom-checkout.d.ts b/types/stripe-js/custom-checkout.d.ts deleted file mode 100644 index 90993d90..00000000 --- a/types/stripe-js/custom-checkout.d.ts +++ /dev/null @@ -1,456 +0,0 @@ -import { - LayoutObject, - Layout, - TermsOption, - StripePaymentElement, -} from './elements/payment'; -import { - AddressMode, - ContactOption, - StripeAddressElement, -} from './elements/address'; -import {Appearance, CssFontSource, CustomFontSource} from './elements-group'; -import {StripeError} from './stripe'; -import { - StripeCurrencySelectorElement, - FieldsOption, - StripeElementBase, - StripeExpressCheckoutElement, - StripeExpressCheckoutElementConfirmEvent, - StripeExpressCheckoutElementOptions, - StripeExpressCheckoutElementReadyEvent, -} from './elements'; - -/** - * Requires beta access: - * Contact [Stripe support](https://support.stripe.com/) for more information. - */ - -export interface StripeCustomCheckoutElementsOptions { - appearance?: Appearance; - loader?: 'auto' | 'always' | 'never'; - fonts?: Array; -} - -export interface StripeCustomCheckoutOptions { - clientSecret: string; - elementsOptions?: StripeCustomCheckoutElementsOptions; -} - -/* Custom Checkout types */ -export type StripeCustomCheckoutAddress = { - country: string; - line1?: string | null; - line2?: string | null; - city?: string | null; - postal_code?: string | null; - state?: string | null; -}; - -export type StripeCustomCheckoutAdjustableQuantity = { - maximum: number; - minimum: number; -}; - -export type StripeCustomCheckoutBillingInterval = - | 'day' - | 'month' - | 'week' - | 'year'; - -export type StripeCustomCheckoutConfirmationRequirement = - | 'phoneNumber' - | 'shippingAddress' - | 'billingAddress' - | 'paymentDetails' - | 'email'; - -export type StripeCustomCheckoutContact = { - name?: string | null; - address: StripeCustomCheckoutAddress; -}; - -export type StripeCustomCheckoutDeliveryEstimate = { - maximum: StripeCustomCheckoutEstimate | null; - minimum: StripeCustomCheckoutEstimate | null; -}; - -export type StripeCustomCheckoutDiscountAmount = { - amount: number; - displayName: string; - promotionCode: string | null; - recurring: - | {type: 'forever'} - | {type: 'repeating'; durationInMonths: number} - | null; -}; - -export type StripeCustomCheckoutDueNext = { - amountSubtotal: number; - amountDiscount: number; - amountTaxInclusive: number; - amountTaxExclusive: number; - billingCycleAnchor: number | null; -}; - -export type StripeCustomCheckoutEstimate = { - unit: 'business_day' | 'day' | 'hour' | 'week' | 'month'; - value: number; -}; - -export type StripeCustomCheckoutLastPaymentError = { - message: string; -}; - -export type StripeCustomCheckoutRedirectBehavior = 'always' | 'if_required'; - -export type StripeCustomCheckoutSavedPaymentMethod = { - id: string; - type: 'card'; - card: { - brand: string; - expMonth: number; - expYear: number; - last4: string; - }; - billingDetails: { - email?: string | null; - name?: string | null; - phone?: string | null; - address?: { - line1?: string | null; - line2?: string | null; - city?: string | null; - postal_code?: string | null; - state?: string | null; - country?: string | null; - } | null; - }; -}; - -export type StripeCustomCheckoutTaxAmount = { - amount: number; - inclusive: boolean; - displayName: string; -}; - -export type StripeCustomCheckoutLineItem = { - id: string; - name: string; - amountDiscount: number; - amountSubtotal: number; - amountTaxExclusive: number; - amountTaxInclusive: number; - unitAmount: number; - description: string | null; - quantity: number; - discountAmounts: Array | null; - taxAmounts: Array | null; - recurring: { - interval: StripeCustomCheckoutBillingInterval; - intervalCount: number; - isProrated: boolean; - usageType: 'metered' | 'licensed'; - } | null; - adjustableQuantity: StripeCustomCheckoutAdjustableQuantity | null; - images: string[]; -}; - -export type StripeCustomCheckoutRecurring = { - interval: StripeCustomCheckoutBillingInterval; - intervalCount: number; - dueNext: StripeCustomCheckoutDueNext; - trial: StripeCustomCheckoutTrial | null; -}; - -export type StripeCustomCheckoutShipping = { - shippingOption: StripeCustomCheckoutShippingOption; - taxAmounts: Array | null; -}; - -export type StripeCustomCheckoutShippingOption = { - id: string; - amount: number; - currency: string; - displayName: string | null; - deliveryEstimate: StripeCustomCheckoutDeliveryEstimate | null; -}; - -export type StripeCustomCheckoutStatus = - | {type: 'open'} - | {type: 'expired'} - | { - type: 'complete'; - paymentStatus: 'paid' | 'unpaid' | 'no_payment_required'; - }; - -export type StripeCustomCheckoutTaxStatus = - | {status: 'ready'} - | {status: 'requires_shipping_address'} - | {status: 'requires_billing_address'}; - -export type StripeCustomCheckoutTotalSummary = { - appliedBalance: number; - balanceAppliedToNextInvoice: boolean; - discount: number; - shippingRate: number; - subtotal: number; - taxExclusive: number; - taxInclusive: number; - total: number; -}; - -export type StripeCustomCheckoutTrial = { - trialEnd: number; - trialPeriodDays: number; -}; - -export type StripeCustomCheckoutCurrencyOption = { - amount: number; - currency: string; - currencyConversion?: {fxRate: number; sourceCurrency: string}; -}; - -/* Custom Checkout session */ -export interface StripeCustomCheckoutSession { - billingAddress: StripeCustomCheckoutContact | null; - businessName: string | null; - canConfirm: boolean; - confirmationRequirements: StripeCustomCheckoutConfirmationRequirement[]; - currency: string; - currencyOptions: Array | null; - discountAmounts: Array | null; - email: string | null; - id: string; - lastPaymentError: StripeCustomCheckoutLastPaymentError | null; - lineItems: Array; - livemode: boolean; - phoneNumber: string | null; - recurring: StripeCustomCheckoutRecurring | null; - savedPaymentMethods: Array | null; - shipping: StripeCustomCheckoutShipping | null; - shippingAddress: StripeCustomCheckoutContact | null; - shippingOptions: Array; - status: StripeCustomCheckoutStatus; - tax: StripeCustomCheckoutTaxStatus; - taxAmounts: Array | null; - total: StripeCustomCheckoutTotalSummary; -} - -export type StripeCustomCheckoutResult = - | {session: StripeCustomCheckoutSession; error?: undefined} - | {session?: undefined; error: StripeError}; - -export type StripeCustomCheckoutPaymentElementOptions = { - layout?: Layout | LayoutObject; - paymentMethodOrder?: Array; - readonly?: boolean; - terms?: TermsOption; - fields?: FieldsOption; -}; - -export type StripeCustomCheckoutAddressElementOptions = { - mode: AddressMode; - contacts?: ContactOption[]; - display?: { - name?: 'full' | 'split' | 'organization'; - }; -}; - -export type StripeCustomCheckoutExpressCheckoutElementOptions = { - buttonHeight: StripeExpressCheckoutElementOptions['buttonHeight']; - buttonTheme: StripeExpressCheckoutElementOptions['buttonTheme']; - buttonType: StripeExpressCheckoutElementOptions['buttonType']; - layout: StripeExpressCheckoutElementOptions['layout']; - paymentMethodOrder: StripeExpressCheckoutElementOptions['paymentMethodOrder']; - paymentMethods: StripeExpressCheckoutElementOptions['paymentMethods']; -}; - -export type StripeCustomCheckoutUpdateHandler = ( - session: StripeCustomCheckoutSession -) => void; - -export type StripeCustomCheckoutExpressCheckoutElementConfirmEvent = StripeExpressCheckoutElementConfirmEvent & { - confirm: () => Promise; -}; - -export type StripeCustomCheckoutExpressCheckoutElement = StripeElementBase & { - /** - * Triggered when the element is fully rendered. - */ - on( - eventType: 'ready', - handler: (event: StripeExpressCheckoutElementReadyEvent) => any - ): StripeCustomCheckoutExpressCheckoutElement; - once( - eventType: 'ready', - handler: (event: StripeExpressCheckoutElementReadyEvent) => any - ): StripeCustomCheckoutExpressCheckoutElement; - off( - eventType: 'ready', - handler?: (event: StripeExpressCheckoutElementReadyEvent) => any - ): StripeCustomCheckoutExpressCheckoutElement; - - /** - * Triggered when the element gains focus. - */ - on( - eventType: 'focus', - handler: (event: {elementType: 'expressCheckout'}) => any - ): StripeCustomCheckoutExpressCheckoutElement; - once( - eventType: 'focus', - handler: (event: {elementType: 'expressCheckout'}) => any - ): StripeCustomCheckoutExpressCheckoutElement; - off( - eventType: 'focus', - handler?: (event: {elementType: 'expressCheckout'}) => any - ): StripeCustomCheckoutExpressCheckoutElement; - - /** - * Triggered when the element loses focus. - */ - on( - eventType: 'blur', - handler: (event: {elementType: 'expressCheckout'}) => any - ): StripeCustomCheckoutExpressCheckoutElement; - once( - eventType: 'blur', - handler: (event: {elementType: 'expressCheckout'}) => any - ): StripeCustomCheckoutExpressCheckoutElement; - off( - eventType: 'blur', - handler?: (event: {elementType: 'expressCheckout'}) => any - ): StripeCustomCheckoutExpressCheckoutElement; - - /** - * Triggered when the escape key is pressed within the element. - */ - on( - eventType: 'escape', - handler: (event: {elementType: 'expressCheckout'}) => any - ): StripeCustomCheckoutExpressCheckoutElement; - once( - eventType: 'escape', - handler: (event: {elementType: 'expressCheckout'}) => any - ): StripeCustomCheckoutExpressCheckoutElement; - off( - eventType: 'escape', - handler?: (event: {elementType: 'expressCheckout'}) => any - ): StripeCustomCheckoutExpressCheckoutElement; - - /** - * Triggered when the element fails to load. - */ - on( - eventType: 'loaderror', - handler: (event: { - elementType: 'expressCheckout'; - error: StripeError; - }) => any - ): StripeCustomCheckoutExpressCheckoutElement; - once( - eventType: 'loaderror', - handler: (event: { - elementType: 'expressCheckout'; - error: StripeError; - }) => any - ): StripeCustomCheckoutExpressCheckoutElement; - off( - eventType: 'loaderror', - handler?: (event: { - elementType: 'expressCheckout'; - error: StripeError; - }) => any - ): StripeCustomCheckoutExpressCheckoutElement; - - /** - * Triggered when a buyer authorizes a payment within a supported payment method. - */ - on( - eventType: 'confirm', - handler: ( - event: StripeCustomCheckoutExpressCheckoutElementConfirmEvent - ) => any - ): StripeCustomCheckoutExpressCheckoutElement; - once( - eventType: 'confirm', - handler: ( - event: StripeCustomCheckoutExpressCheckoutElementConfirmEvent - ) => any - ): StripeCustomCheckoutExpressCheckoutElement; - off( - eventType: 'confirm', - handler?: ( - event: StripeCustomCheckoutExpressCheckoutElementConfirmEvent - ) => any - ): StripeCustomCheckoutExpressCheckoutElement; - - /** - * Updates the options the `ExpressCheckoutElement` was initialized with. - * Updates are merged into the existing configuration. - */ - update: StripeExpressCheckoutElement['update']; -}; - -export interface StripeCustomCheckout { - /* Custom Checkout methods */ - applyPromotionCode: ( - promotionCode: string - ) => Promise; - removePromotionCode: () => Promise; - updateShippingAddress: ( - shippingAddress: StripeCustomCheckoutContact | null - ) => Promise; - updateBillingAddress: ( - billingAddress: StripeCustomCheckoutContact | null - ) => Promise; - updatePhoneNumber: ( - phoneNumber: string - ) => Promise; - updateEmail: (email: string) => Promise; - updateLineItemQuantity: (args: { - lineItem: string; - quantity: number; - }) => Promise; - updateShippingOption: ( - shippingOption: string - ) => Promise; - confirm: (args?: { - returnUrl?: string; - redirect?: StripeCustomCheckoutRedirectBehavior; - paymentMethod?: string; - savePaymentMethod?: boolean; - }) => Promise; - session: () => StripeCustomCheckoutSession; - on: (event: 'change', handler: StripeCustomCheckoutUpdateHandler) => void; - - /* Elements methods */ - changeAppearance: (appearance: Appearance) => void; - getElement(elementType: 'payment'): StripePaymentElement | null; - getElement( - elementType: 'address', - mode: AddressMode - ): StripeAddressElement | null; - getElement( - elementType: 'expressCheckout' - ): StripeCustomCheckoutExpressCheckoutElement | null; - /* Requires beta access: Contact [Stripe support](https://support.stripe.com/) for more information. */ - getElement( - elementType: 'currencySelector' - ): StripeCurrencySelectorElement | null; - createElement( - elementType: 'payment', - options?: StripeCustomCheckoutPaymentElementOptions - ): StripePaymentElement; - createElement( - elementType: 'address', - options: StripeCustomCheckoutAddressElementOptions - ): StripeAddressElement; - createElement( - elementType: 'expressCheckout', - options: StripeCustomCheckoutExpressCheckoutElementOptions - ): StripeCustomCheckoutExpressCheckoutElement; - /* Requires beta access: Contact [Stripe support](https://support.stripe.com/) for more information. */ - createElement(elementType: 'currencySelector'): StripeCurrencySelectorElement; -} diff --git a/types/stripe-js/hosted-checkout.d.ts b/types/stripe-js/hosted-checkout.d.ts new file mode 100644 index 00000000..edbf0a52 --- /dev/null +++ b/types/stripe-js/hosted-checkout.d.ts @@ -0,0 +1,153 @@ +export interface RedirectToCheckoutServerOptions { + /** + * The ID of the [Checkout Session](https://stripe.com/docs/api/checkout/sessions) that is used in [Checkout's server integration](https://stripe.com/docs/payments/checkout/one-time). + */ + sessionId: string; +} + +export interface RedirectToCheckoutClientOptions { + /** + * The URL to which Stripe should send customers when payment is complete. + * If you’d like access to the Checkout Session for the successful payment, read more about it in our guide on [fulfilling your payments with webhooks](https://stripe.com/docs/payments/checkout/fulfillment#webhooks). + */ + successUrl: string; + + /** + * The URL to which Stripe should send customers when payment is canceled. + */ + cancelUrl: string; + + /** + * An array of objects representing the items that your customer would like to purchase. + * These items are shown as line items in the Checkout interface and make up the total amount to be collected by Checkout. + */ + lineItems?: Array<{ + /** + * The ID of the price that the customer would like to purchase. SKU or plan IDs may also be used. + */ + price?: string; + + /** + * The quantity of units for the item. + */ + quantity?: number; + }>; + + /** + * An array of objects representing the items that your customer would like to purchase. + * These items are shown as line items in the Checkout interface and make up the total amount to be collected by Checkout. + * + * @deprecated + */ + items?: Array<{ + /** + * The ID of the SKU that the customer would like to purchase + */ + sku?: string; + + /** + * The ID of the plan that the customer would like to subscribe to. + */ + plan?: string; + + /** + * The quantity of units for the item. + */ + quantity?: number; + }>; + + /** + * The mode of the Checkout Session. Required if using lineItems. + */ + mode?: 'payment' | 'subscription'; + + /** + * A unique string to reference the Checkout session. + * This can be a customer ID, a cart ID, or similar. + * It is included in the `checkout.session.completed` webhook and can be used to fulfill the purchase. + */ + clientReferenceId?: string; + + /** + * The email address used to create the customer object. + * If you already know your customer's email address, use this attribute to prefill it on Checkout. + */ + customerEmail?: string; + + /** + * Specify whether Checkout should collect the customer’s billing address. + * If set to `required`, Checkout will attempt to collect the customer’s billing address. + * If not set or set to `auto` Checkout will only attempt to collect the billing address when necessary. + */ + billingAddressCollection?: 'auto' | 'required'; + + /** + * Provides configuration for Checkout to collect a shipping address from a customer. + */ + shippingAddressCollection?: { + /** + * An array of two-letter ISO country codes representing which countries + * Checkout should provide as options for shipping locations. The codes are + * expected to be uppercase. Unsupported country codes: AS, CX, CC, CU, HM, IR, KP, MH, FM, NF, MP, PW, SD, SY, UM, VI. + */ + allowedCountries: string[]; + }; + + /** + * The [IETF language tag](https://en.wikipedia.org/wiki/IETF_language_tag) of the locale to display Checkout in. + * Default is `auto` (Stripe detects the locale of the browser). + */ + locale?: CheckoutLocale; + + /** + * Describes the type of transaction being performed by Checkout in order to customize relevant text on the page, such as the **Submit** button. + * `submitType` can only be specified when using using line items or SKUs, and not subscriptions. + * The default is `auto`. + */ + submitType?: 'auto' | 'book' | 'donate' | 'pay'; +} + +export type RedirectToCheckoutOptions = + | RedirectToCheckoutServerOptions + | RedirectToCheckoutClientOptions; + +export type CheckoutLocale = + | 'auto' + | 'bg' + | 'cs' + | 'da' + | 'de' + | 'el' + | 'en' + | 'en-GB' + | 'es' + | 'es-419' + | 'et' + | 'fi' + | 'fil' + | 'fr' + | 'fr-CA' + | 'hr' + | 'hu' + | 'id' + | 'it' + | 'ja' + | 'lt' + | 'lv' + | 'ms' + | 'mt' + | 'nb' + | 'nl' + | 'pl' + | 'pt' + | 'pt-BR' + | 'ro' + | 'ru' + | 'sk' + | 'sl' + | 'sv' + | 'th' + | 'tr' + | 'zh' + | 'zh-HK' + | 'zh-TW'; diff --git a/types/stripe-js/index.d.ts b/types/stripe-js/index.d.ts index a331322b..022439d0 100644 --- a/types/stripe-js/index.d.ts +++ b/types/stripe-js/index.d.ts @@ -1,5 +1,5 @@ +export * from './hosted-checkout'; export * from './checkout'; -export * from './custom-checkout'; export * from './embedded-checkout'; export * from './elements'; export * from './elements-group'; diff --git a/types/stripe-js/stripe.d.ts b/types/stripe-js/stripe.d.ts index b3ea005f..59ba6148 100644 --- a/types/stripe-js/stripe.d.ts +++ b/types/stripe-js/stripe.d.ts @@ -13,13 +13,10 @@ import { StripeElementsOptionsClientSecret, StripeElementsOptionsMode, } from './elements-group'; -import {CheckoutLocale, RedirectToCheckoutOptions} from './checkout'; +import {CheckoutLocale, RedirectToCheckoutOptions} from './hosted-checkout'; import {PaymentRequestOptions, PaymentRequest} from './payment-request'; import {StripeElement, StripeElementLocale} from './elements-group'; -import { - StripeCustomCheckoutOptions, - StripeCustomCheckout, -} from './custom-checkout'; +import {StripeCheckoutOptions, StripeCheckout} from './checkout'; import { StripeEmbeddedCheckoutOptions, StripeEmbeddedCheckout, @@ -1268,9 +1265,7 @@ export interface Stripe { * Requires beta access: * Contact [Stripe support](https://support.stripe.com/) for more information. */ - initCustomCheckout( - options: StripeCustomCheckoutOptions - ): Promise; + initCheckout(options: StripeCheckoutOptions): Promise; /** * Use `stripe.initEmbeddedCheckout` to initialize an embedded Checkout instance