From 4551fe3fc84f4f16aab99107bc07e0f114f2783f Mon Sep 17 00:00:00 2001 From: Polo Li Date: Tue, 29 Aug 2023 14:17:51 -0700 Subject: [PATCH] pl - add Custom Checkout types --- types/stripe-js/custom-checkout.d.ts | 217 +++++++++++++++++++++++++++ types/stripe-js/stripe.d.ts | 12 ++ 2 files changed, 229 insertions(+) create mode 100644 types/stripe-js/custom-checkout.d.ts diff --git a/types/stripe-js/custom-checkout.d.ts b/types/stripe-js/custom-checkout.d.ts new file mode 100644 index 00000000..144c8fbc --- /dev/null +++ b/types/stripe-js/custom-checkout.d.ts @@ -0,0 +1,217 @@ +import { + LayoutObject, + Layout, + TermsOption, + StripePaymentElement, +} from './elements/payment'; +import { + AddressMode, + ContactOption, + StripeAddressElement, +} from './elements/address'; +import {Appearance, CssFontSource, CustomFontSource} from './elements-group'; + +/** + * Requires beta access: + * Contact [Stripe support](https://support.stripe.com/) for more information. + */ + +/** + * StripeCustomCheckoutInitOptions + */ +export interface StripeCustomCheckoutElementsOptions { + appearance?: Appearance; + loader?: 'auto' | 'always' | 'never'; + fonts?: Array; +} + +export interface StripeCustomCheckoutOptions { + clientSecret: string; + elementsOptions?: StripeCustomCheckoutElementsOptions; +} + +/** + * StripeCustomCheckoutActions + */ + +export type StripeCustomCheckoutAddress = { + country: string | null; + line1: string | null; + line2?: string | null; + city: string | null; + postal_code: string | null; + state: string | null; +}; + +export type StripeCustomCheckoutShippingAddress = { + name?: string | null; + address: StripeCustomCheckoutAddress; +}; + +export type StripeCustomCheckoutBillingAddress = StripeCustomCheckoutShippingAddress; + +export interface StripeCustomCheckoutActions { + applyPromotionCode: (promotionCode: string) => Promise; + removePromotionCode: () => Promise; + updateShippingAddress: ( + shippingAddress: StripeCustomCheckoutShippingAddress + ) => Promise; + updateBillingAddress: ( + billingAddress: StripeCustomCheckoutBillingAddress + ) => Promise; + updatePhoneNumber: (phoneNumber: string) => void; + updateEmail: (email: string) => void; + updateCurrency: (currency: string) => void; + updateLineItemQuantity: (args: { + lineItem: string; + quantity: number; + }) => Promise; + updateShippingOption: (shippingOption: string) => Promise; + confirm: (args?: {return_url?: string}) => Promise; +} + +/** + * StripeCustomCheckoutSession + */ + +export type StripeCustomCheckoutTaxAmount = { + amount: number; + inclusive: boolean; + displayName: string; +}; + +export type StripeCustomCheckoutDiscountAmount = { + amount: number; + displayName: string; + promotionCode?: string | 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 StripeCustomCheckoutDeliveryEstimate = { + maximum?: StripeCustomCheckoutEstimate | null; + minimum?: StripeCustomCheckoutEstimate | null; +}; + +export type StripeCustomCheckoutEstimate = { + unit: 'business_day' | 'day' | 'hour' | 'week' | 'month'; + value: number; +}; + +export type StripeCustomCheckoutLineItemDiscountAmount = { + amount: number; + promotionCode?: string | null; +}; + +export type StripeCustomCheckoutBillingInterval = + | 'day' + | 'month' + | 'week' + | 'year'; + +export type StripeCustomCheckoutLineItem = { + id: string; + name: string; + amount: number; + unitAmount: number; + description?: string | null; + quantity: number; + discountAmounts?: Array | null; + recurring?: { + interval: StripeCustomCheckoutBillingInterval; + interval_count: number; + }; +}; + +export type StripeCustomCheckoutTotalSummary = { + subtotal: number; + taxExclusive: number; + taxInclusive: number; + shippingRate: number; + discount: number; + total: number; +}; + +export type StripeCustomCheckoutConfirmationRequirement = + | 'phoneNumber' + | 'shippingAddress' + | 'billingAddress' + | 'paymentDetails' + | 'email'; + +export interface StripeCustomCheckoutSession { + lineItems: Array; + taxAmounts?: Array | null; + discountAmounts?: Array | null; + currency: string; + shipping?: StripeCustomCheckoutShipping | null; + shippingOptions: Array; + shippingAddress?: StripeCustomCheckoutShippingAddress | null; + billingAddress?: StripeCustomCheckoutBillingAddress | null; + phoneNumber?: string | null; + email?: string | null; + total: StripeCustomCheckoutTotalSummary; + confirmationRequirements: StripeCustomCheckoutConfirmationRequirement[]; + canConfirm: boolean; +} + +/** + * StripeCustomCheckoutElements + */ +export type StripeCustomCheckoutPaymentElementOptions = { + layout?: Layout | LayoutObject; + paymentMethodOrder?: Array; + readonly?: boolean; + terms?: TermsOption; +}; + +export type StripeCustomCheckoutAddressElementOptions = { + mode: AddressMode; + contacts?: ContactOption[]; + display?: { + name?: 'full' | 'split' | 'organization'; + }; +}; + +export interface StripeCustomCheckoutElementsActions { + changeAppearance: (appearance: Appearance) => void; + getElement(elementType: 'payment'): StripePaymentElement | null; + getElement( + elementType: 'address', + mode: AddressMode + ): StripeAddressElement | null; + createElement( + elementType: 'payment', + options?: StripeCustomCheckoutPaymentElementOptions + ): StripePaymentElement; + createElement( + elementType: 'address', + options: StripeCustomCheckoutAddressElementOptions + ): StripeAddressElement; +} + +/** + * StripeCustomCheckout + */ +export type StripeCustomCheckoutUpdateHandler = ( + session: StripeCustomCheckoutSession +) => void; + +export interface StripeCustomCheckout + extends StripeCustomCheckoutActions, + StripeCustomCheckoutSession, + StripeCustomCheckoutElementsActions { + session: () => StripeCustomCheckoutSession; + on: (event: 'change', handler: StripeCustomCheckoutUpdateHandler) => void; +} diff --git a/types/stripe-js/stripe.d.ts b/types/stripe-js/stripe.d.ts index 44f12f3f..bdf5dcfe 100644 --- a/types/stripe-js/stripe.d.ts +++ b/types/stripe-js/stripe.d.ts @@ -15,6 +15,10 @@ import { import {CheckoutLocale, RedirectToCheckoutOptions} from './checkout'; import {PaymentRequestOptions, PaymentRequest} from './payment-request'; import {StripeElement, StripeElementLocale} from './elements-group'; +import { + StripeCustomCheckoutOptions, + StripeCustomCheckout, +} from './custom-checkout'; export interface Stripe { ///////////////////////////// @@ -1193,6 +1197,14 @@ export interface Stripe { createEphemeralKeyNonce( options: ephemeralKeys.EphemeralKeyNonceOptions ): Promise; + + /** + * Requires beta access: + * Contact [Stripe support](https://support.stripe.com/) for more information. + */ + initCustomCheckout( + options: StripeCustomCheckoutOptions + ): Promise; } export type PaymentIntentResult =