Skip to content

Commit 63f58f6

Browse files
Add types for Elements with Checkout Session beta 6 (#738)
* Add types for Elements with Checkout Session beta 6 * rework valid.ts tests * same change for invalid.ts
1 parent 4a7abd1 commit 63f58f6

File tree

3 files changed

+74
-59
lines changed

3 files changed

+74
-59
lines changed

tests/types/src/invalid.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,3 +483,15 @@ elements.create('paymentMethodMessaging', {
483483
currency: 'USD',
484484
paymentMethodOrder: ['invalid_type'],
485485
});
486+
487+
stripe
488+
.initCheckout({
489+
// @ts-expect-error Object literal may only specify known properties, and 'clientSecret' does not exist in type 'StripeCheckoutOptions'.
490+
clientSecret: 'cs_test_foo',
491+
})
492+
.then((checkout) => {
493+
// @ts-expect-error Property 'createElement' does not exist on type 'StripeCheckout'.
494+
checkout.createElement('payment');
495+
// @ts-expect-error Type 'StripeCheckoutAmount' is not assignable to type 'number'.
496+
const subtotal: number = checkout.session().total.subtotal;
497+
});

tests/types/src/valid.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3713,3 +3713,25 @@ issuingCopyButtonElement.update({
37133713
stripe.createEphemeralKeyNonce({
37143714
issuingCard: '',
37153715
});
3716+
3717+
stripe
3718+
.initCheckout({
3719+
fetchClientSecret: async () => 'cs_test_foo',
3720+
})
3721+
.then((checkout) => {
3722+
const checkoutPaymentElement: StripePaymentElement = checkout.createPaymentElement();
3723+
checkout.getPaymentElement();
3724+
const checkoutAddressElement: StripeAddressElement = checkout.createBillingAddressElement();
3725+
checkout.getBillingAddressElement();
3726+
checkout.applyPromotionCode('code');
3727+
const {
3728+
minorUnitsAmountDivisor,
3729+
lineItems,
3730+
total: {
3731+
taxExclusive: {amount, minorUnitsAmount},
3732+
},
3733+
} = checkout.session();
3734+
const {
3735+
subtotal: {amount: _, minorUnitsAmount: __},
3736+
} = lineItems[0];
3737+
});

types/stripe-js/checkout.d.ts

Lines changed: 40 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@ import {
44
TermsOption,
55
StripePaymentElement,
66
} from './elements/payment';
7-
import {
8-
AddressMode,
9-
ContactOption,
10-
StripeAddressElement,
11-
} from './elements/address';
7+
import {ContactOption, StripeAddressElement} from './elements/address';
128
import {Appearance, CssFontSource, CustomFontSource} from './elements-group';
139
import {StripeError} from './stripe';
1410
import {
@@ -33,7 +29,7 @@ export interface StripeCheckoutElementsOptions {
3329
}
3430

3531
export interface StripeCheckoutOptions {
36-
clientSecret: string;
32+
fetchClientSecret: () => Promise<string>;
3733
elementsOptions?: StripeCheckoutElementsOptions;
3834
}
3935

@@ -54,13 +50,6 @@ export type StripeCheckoutAdjustableQuantity = {
5450

5551
export type StripeCheckoutBillingInterval = 'day' | 'month' | 'week' | 'year';
5652

57-
export type StripeCheckoutConfirmationRequirement =
58-
| 'phoneNumber'
59-
| 'shippingAddress'
60-
| 'billingAddress'
61-
| 'paymentDetails'
62-
| 'email';
63-
6453
export type StripeCheckoutContact = {
6554
name?: string | null;
6655
address: StripeCheckoutAddress;
@@ -71,8 +60,9 @@ export type StripeCheckoutDeliveryEstimate = {
7160
minimum: StripeCheckoutEstimate | null;
7261
};
7362

74-
export type StripeCheckoutDiscountAmount = {
75-
amount: number;
63+
export type StripeCheckoutAmount = {minorUnitsAmount: number; amount: string};
64+
65+
export type StripeCheckoutDiscountAmount = StripeCheckoutAmount & {
7666
displayName: string;
7767
promotionCode: string | null;
7868
recurring:
@@ -82,10 +72,10 @@ export type StripeCheckoutDiscountAmount = {
8272
};
8373

8474
export type StripeCheckoutDueNext = {
85-
amountSubtotal: number;
86-
amountDiscount: number;
87-
amountTaxInclusive: number;
88-
amountTaxExclusive: number;
75+
subtotal: StripeCheckoutAmount;
76+
discount: StripeCheckoutAmount;
77+
taxInclusive: StripeCheckoutAmount;
78+
taxExclusive: StripeCheckoutAmount;
8979
billingCycleAnchor: number | null;
9080
};
9181

@@ -124,20 +114,20 @@ export type StripeCheckoutSavedPaymentMethod = {
124114
};
125115
};
126116

127-
export type StripeCheckoutTaxAmount = {
128-
amount: number;
117+
export type StripeCheckoutTaxAmount = StripeCheckoutAmount & {
129118
inclusive: boolean;
130119
displayName: string;
131120
};
132121

133122
export type StripeCheckoutLineItem = {
134123
id: string;
135124
name: string;
136-
amountDiscount: number;
137-
amountSubtotal: number;
138-
amountTaxExclusive: number;
139-
amountTaxInclusive: number;
140-
unitAmount: number;
125+
discount: StripeCheckoutAmount;
126+
subtotal: StripeCheckoutAmount;
127+
total: StripeCheckoutAmount;
128+
taxExclusive: StripeCheckoutAmount;
129+
taxInclusive: StripeCheckoutAmount;
130+
unitAmount: StripeCheckoutAmount;
141131
description: string | null;
142132
quantity: number;
143133
discountAmounts: Array<StripeCheckoutDiscountAmount> | null;
@@ -164,9 +154,8 @@ export type StripeCheckoutShipping = {
164154
taxAmounts: Array<StripeCheckoutTaxAmount> | null;
165155
};
166156

167-
export type StripeCheckoutShippingOption = {
157+
export type StripeCheckoutShippingOption = StripeCheckoutAmount & {
168158
id: string;
169-
amount: number;
170159
currency: string;
171160
displayName: string | null;
172161
deliveryEstimate: StripeCheckoutDeliveryEstimate | null;
@@ -186,23 +175,22 @@ export type StripeCheckoutTaxStatus =
186175
| {status: 'requires_billing_address'};
187176

188177
export type StripeCheckoutTotalSummary = {
189-
appliedBalance: number;
178+
appliedBalance: StripeCheckoutAmount;
190179
balanceAppliedToNextInvoice: boolean;
191-
discount: number;
192-
shippingRate: number;
193-
subtotal: number;
194-
taxExclusive: number;
195-
taxInclusive: number;
196-
total: number;
180+
discount: StripeCheckoutAmount;
181+
shippingRate: StripeCheckoutAmount;
182+
subtotal: StripeCheckoutAmount;
183+
taxExclusive: StripeCheckoutAmount;
184+
taxInclusive: StripeCheckoutAmount;
185+
total: StripeCheckoutAmount;
197186
};
198187

199188
export type StripeCheckoutTrial = {
200189
trialEnd: number;
201190
trialPeriodDays: number;
202191
};
203192

204-
export type StripeCheckoutCurrencyOption = {
205-
amount: number;
193+
export type StripeCheckoutCurrencyOption = StripeCheckoutAmount & {
206194
currency: string;
207195
currencyConversion?: {fxRate: number; sourceCurrency: string};
208196
};
@@ -324,7 +312,6 @@ export interface StripeCheckoutSession {
324312
billingAddress: StripeCheckoutContact | null;
325313
businessName: string | null;
326314
canConfirm: boolean;
327-
confirmationRequirements: StripeCheckoutConfirmationRequirement[];
328315
currency: string;
329316
currencyOptions: Array<StripeCheckoutCurrencyOption> | null;
330317
discountAmounts: Array<StripeCheckoutDiscountAmount> | null;
@@ -333,6 +320,7 @@ export interface StripeCheckoutSession {
333320
lastPaymentError: StripeCheckoutLastPaymentError | null;
334321
lineItems: Array<StripeCheckoutLineItem>;
335322
livemode: boolean;
323+
minorUnitsAmountDivisor: number;
336324
phoneNumber: string | null;
337325
recurring: StripeCheckoutRecurring | null;
338326
savedPaymentMethods: Array<StripeCheckoutSavedPaymentMethod> | null;
@@ -355,7 +343,6 @@ export type StripeCheckoutPaymentElementOptions = {
355343
};
356344

357345
export type StripeCheckoutAddressElementOptions = {
358-
mode: AddressMode;
359346
contacts?: ContactOption[];
360347
display?: {
361348
name?: 'full' | 'split' | 'organization';
@@ -593,30 +580,24 @@ export interface StripeCheckout {
593580

594581
/* Elements methods */
595582
changeAppearance: (appearance: Appearance) => void;
596-
getElement(elementType: 'payment'): StripePaymentElement | null;
597-
getElement(
598-
elementType: 'address',
599-
mode: AddressMode
600-
): StripeAddressElement | null;
601-
getElement(
602-
elementType: 'expressCheckout'
603-
): StripeCheckoutExpressCheckoutElement | null;
583+
getPaymentElement(): StripePaymentElement | null;
584+
getBillingAddressElement(): StripeAddressElement | null;
585+
getShippingAddressElement(): StripeAddressElement | null;
586+
getExpressCheckoutElement(): StripeCheckoutExpressCheckoutElement | null;
604587
/* Requires beta access: Contact [Stripe support](https://support.stripe.com/) for more information. */
605-
getElement(
606-
elementType: 'currencySelector'
607-
): StripeCurrencySelectorElement | null;
608-
createElement(
609-
elementType: 'payment',
588+
getCurrencySelectorElement(): StripeCurrencySelectorElement | null;
589+
createPaymentElement(
610590
options?: StripeCheckoutPaymentElementOptions
611591
): StripePaymentElement;
612-
createElement(
613-
elementType: 'address',
614-
options: StripeCheckoutAddressElementOptions
592+
createBillingAddressElement(
593+
options?: StripeCheckoutAddressElementOptions
594+
): StripeAddressElement;
595+
createShippingAddressElement(
596+
options?: StripeCheckoutAddressElementOptions
615597
): StripeAddressElement;
616-
createElement(
617-
elementType: 'expressCheckout',
618-
options: StripeCheckoutExpressCheckoutElementOptions
598+
createExpressCheckoutElement(
599+
options?: StripeCheckoutExpressCheckoutElementOptions
619600
): StripeCheckoutExpressCheckoutElement;
620601
/* Requires beta access: Contact [Stripe support](https://support.stripe.com/) for more information. */
621-
createElement(elementType: 'currencySelector'): StripeCurrencySelectorElement;
602+
createCurrencySelectorElement(): StripeCurrencySelectorElement;
622603
}

0 commit comments

Comments
 (0)