Skip to content

Commit 1a679ae

Browse files
[breaking] Update types for Checkout SDK (#803)
* Update types for Checkout SDK * pl - fix comment
1 parent 458ea74 commit 1a679ae

File tree

4 files changed

+100
-64
lines changed

4 files changed

+100
-64
lines changed

tests/types/src/invalid.ts

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -498,23 +498,30 @@ elements.create('paymentMethodMessaging', {
498498

499499
stripe
500500
.initCheckout({
501-
// @ts-expect-error Object literal may only specify known properties, and 'clientSecret' does not exist in type 'StripeCheckoutOptions'.
502-
clientSecret: 'cs_test_foo',
501+
// @ts-expect-error Object literal may only specify known properties, and 'fetchClientSecret' does not exist in type 'StripeCheckoutOptions'.
502+
fetchClientSecret: () => Promise.resolve('cs_test_foo'),
503503
})
504-
.then((checkout) => {
505-
// @ts-expect-error Property 'createElement' does not exist on type 'StripeCheckout'.
506-
checkout.createElement('payment');
504+
// @ts-expect-error Property 'then' does not exist on type 'StripeCheckout'.
505+
.then((checkout) => {});
506+
507+
const checkout = stripe.initCheckout({clientSecret: 'cs_test_foo'});
508+
// @ts-expect-error Property 'createElement' does not exist on type 'StripeCheckout'.
509+
checkout.createElement('payment');
510+
// @ts-expect-error - doesn't take a number
511+
checkout.loadFonts(42);
512+
checkout.loadFonts([
513+
{
514+
cssSrc: 'https://example.com/font.css',
515+
// @ts-expect-error - this is not a valid field
516+
extraWrongField: false,
517+
},
518+
]);
519+
checkout.loadActions().then((loadActionsResult) => {
520+
if (loadActionsResult.type === 'success') {
521+
const {actions} = loadActionsResult;
507522
// @ts-expect-error Type 'StripeCheckoutAmount' is not assignable to type 'number'.
508-
const subtotal: number = checkout.session().total.subtotal;
509-
510-
// @ts-expect-error - doesn't take a number
511-
checkout.loadFonts(42);
512-
513-
checkout.loadFonts([
514-
{
515-
cssSrc: 'https://example.com/font.css',
516-
// @ts-expect-error - this is not a valid field
517-
extraWrongField: false,
518-
},
519-
]);
520-
});
523+
const subtotal: number = actions.getSession().total.subtotal;
524+
} else {
525+
const {error} = loadActionsResult;
526+
}
527+
});

tests/types/src/valid.ts

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3829,52 +3829,71 @@ stripe.createEphemeralKeyNonce({
38293829
issuingCard: '',
38303830
});
38313831

3832-
stripe
3833-
.initCheckout({
3834-
fetchClientSecret: async () => 'cs_test_foo',
3835-
})
3836-
.then(async (checkout) => {
3837-
const checkoutPaymentElement: StripePaymentElement = checkout.createPaymentElement();
3838-
checkout.getPaymentElement();
3839-
const checkoutAddressElement: StripeAddressElement = checkout.createBillingAddressElement();
3840-
checkout.getBillingAddressElement();
3841-
checkout.applyPromotionCode('code');
3842-
3843-
checkout.loadFonts([
3844-
{
3845-
cssSrc: 'https://example.com/font.css',
3846-
},
3847-
{
3848-
src: 'url(https://example.com/Blah.woff)',
3849-
family: 'Blah',
3850-
weight: '500',
3851-
style: 'italic',
3852-
unicodeRange: 'some range',
3853-
display: 'display value',
3854-
},
3855-
]);
3832+
stripe.initCheckout({clientSecret: Promise.resolve('cs_test_foo')});
3833+
const checkout = stripe.initCheckout({clientSecret: 'cs_test_foo'});
3834+
const checkoutPaymentElement: StripePaymentElement = checkout.createPaymentElement();
3835+
checkout.getPaymentElement();
3836+
const checkoutAddressElement: StripeAddressElement = checkout.createBillingAddressElement();
3837+
checkout.getBillingAddressElement();
3838+
checkout.loadFonts([
3839+
{
3840+
cssSrc: 'https://example.com/font.css',
3841+
},
3842+
{
3843+
src: 'url(https://example.com/Blah.woff)',
3844+
family: 'Blah',
3845+
weight: '500',
3846+
style: 'italic',
3847+
unicodeRange: 'some range',
3848+
display: 'display value',
3849+
},
3850+
]);
3851+
3852+
checkout.loadActions().then((loadActionsResult) => {
3853+
if (loadActionsResult.type === 'success') {
3854+
const {actions} = loadActionsResult;
3855+
actions.applyPromotionCode('code').then((result) => {
3856+
if (result.type === 'success') {
3857+
const {session} = result;
3858+
} else {
3859+
const {error} = result;
3860+
}
3861+
});
3862+
actions.removePromotionCode().then((result) => {
3863+
if (result.type === 'success') {
3864+
const {session} = result;
3865+
} else {
3866+
const {error} = result;
3867+
}
3868+
});
38563869

3870+
const session = actions.getSession();
38573871
const {
38583872
minorUnitsAmountDivisor,
38593873
lineItems,
38603874
total: {
38613875
taxExclusive: {amount, minorUnitsAmount},
38623876
},
3863-
} = checkout.session();
3877+
} = session;
38643878
const {
38653879
subtotal: {amount: _, minorUnitsAmount: __},
38663880
} = lineItems[0];
3867-
const result = await checkout.confirm();
3868-
if (result.type === 'success') {
3869-
const {session} = result;
3870-
} else {
3871-
const {error} = result;
3872-
}
3873-
});
3881+
3882+
actions.confirm().then((result) => {
3883+
if (result.type === 'success') {
3884+
const {session} = result;
3885+
} else {
3886+
const {error} = result;
3887+
}
3888+
});
3889+
} else {
3890+
const {error} = loadActionsResult;
3891+
}
3892+
});
38743893

38753894
// savedPaymentMethod variations for initCheckout:
38763895
stripe.initCheckout({
3877-
fetchClientSecret: async () => 'cs_test_foo',
3896+
clientSecret: 'cs_test_foo',
38783897
elementsOptions: {
38793898
savedPaymentMethod: {
38803899
enableSave: 'never',
@@ -3885,7 +3904,7 @@ stripe.initCheckout({
38853904

38863905
// only enableSave
38873906
stripe.initCheckout({
3888-
fetchClientSecret: async () => 'cs_test_foo',
3907+
clientSecret: 'cs_test_foo',
38893908
elementsOptions: {
38903909
savedPaymentMethod: {
38913910
enableSave: 'auto',
@@ -3895,7 +3914,7 @@ stripe.initCheckout({
38953914

38963915
// only enableRedisplay
38973916
stripe.initCheckout({
3898-
fetchClientSecret: async () => 'cs_test_foo',
3917+
clientSecret: 'cs_test_foo',
38993918
elementsOptions: {
39003919
savedPaymentMethod: {
39013920
enableRedisplay: 'never',
@@ -3905,7 +3924,7 @@ stripe.initCheckout({
39053924

39063925
// empty savedPaymentMethod object
39073926
stripe.initCheckout({
3908-
fetchClientSecret: async () => 'cs_test_foo',
3927+
clientSecret: 'cs_test_foo',
39093928
elementsOptions: {
39103929
savedPaymentMethod: {},
39113930
},

types/stripe-js/checkout.d.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export interface StripeCheckoutElementsOptions {
3232
}
3333

3434
export interface StripeCheckoutOptions {
35-
fetchClientSecret: () => Promise<string>;
35+
clientSecret: Promise<string> | string;
3636
elementsOptions?: StripeCheckoutElementsOptions;
3737
}
3838

@@ -556,8 +556,8 @@ export type StripeCheckoutRunServerUpdateResult =
556556
| {type: 'success'; session: StripeCheckoutSession}
557557
| {type: 'error'; error: AnyBuyerError};
558558

559-
export interface StripeCheckout {
560-
/* Custom Checkout methods */
559+
type LoadActionsError = {message: string; code: null};
560+
type LoadActionsSuccess = {
561561
applyPromotionCode: (
562562
promotionCode: string
563563
) => Promise<StripeCheckoutApplyPromotionCodeResult>;
@@ -569,9 +569,11 @@ export interface StripeCheckout {
569569
billingAddress: StripeCheckoutContact | null
570570
) => Promise<StripeCheckoutUpdateAddressResult>;
571571
updatePhoneNumber: (
572-
phoneNumber: string
572+
phoneNumber: string | null
573573
) => Promise<StripeCheckoutUpdatePhoneNumberResult>;
574-
updateEmail: (email: string) => Promise<StripeCheckoutUpdateEmailResult>;
574+
updateEmail: (
575+
email: string | null
576+
) => Promise<StripeCheckoutUpdateEmailResult>;
575577
updateLineItemQuantity: (args: {
576578
lineItem: string;
577579
quantity: number;
@@ -593,11 +595,18 @@ export interface StripeCheckout {
593595
shippingAddress?: StripeCheckoutContact;
594596
expressCheckoutConfirmEvent?: StripeExpressCheckoutElementConfirmEvent;
595597
}) => Promise<StripeCheckoutConfirmResult>;
596-
session: () => StripeCheckoutSession;
597-
on: (event: 'change', handler: StripeCheckoutUpdateHandler) => void;
598+
getSession: () => StripeCheckoutSession;
598599
runServerUpdate: (
599600
userFunction: RunServerUpdateFunction
600601
) => Promise<StripeCheckoutRunServerUpdateResult>;
602+
};
603+
export type StripeCheckoutLoadActionsResult =
604+
| {type: 'success'; actions: LoadActionsSuccess}
605+
| {type: 'error'; error: LoadActionsError};
606+
607+
export interface StripeCheckout {
608+
on: (event: 'change', handler: StripeCheckoutUpdateHandler) => void;
609+
loadActions: () => Promise<StripeCheckoutLoadActionsResult>;
601610

602611
/* Elements methods */
603612
changeAppearance: (appearance: Appearance) => void;

types/stripe-js/stripe.d.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,10 +1280,11 @@ export interface Stripe {
12801280
): Promise<EphemeralKeyNonceResult>;
12811281

12821282
/**
1283-
* Requires beta access:
1284-
* Contact [Stripe support](https://support.stripe.com/) for more information.
1283+
* Use `stripe.initCheckout` to initialize a Checkout instance
1284+
*
1285+
* * @docs https://docs.stripe.com/payments/accept-a-payment?platform=web&ui=embedded-components
12851286
*/
1286-
initCheckout(options: StripeCheckoutOptions): Promise<StripeCheckout>;
1287+
initCheckout(options: StripeCheckoutOptions): StripeCheckout;
12871288

12881289
/**
12891290
* Use `stripe.initEmbeddedCheckout` to initialize an embedded Checkout instance

0 commit comments

Comments
 (0)