From 47fb6ba2763e896fc679f5c4fbec6ff8f41b89a0 Mon Sep 17 00:00:00 2001 From: Harry Date: Tue, 23 Jul 2019 20:40:44 +0100 Subject: [PATCH] [stripe-v3] Update Stripe checkout types to include server implementation (#36856) * Update Stripe checkout types to include server implementation There are 2 ways to use Stripe Checkout: the client implementation and the server implementation. The former requires more parameters, such as redirect URLs and the products for the checkout session. In the server implementation the session is created server-side so the only parameter required for `redirectToCheckout` is the session ID. * Move tslint rule disablement to specific line --- types/stripe-v3/index.d.ts | 14 +++++++++++--- types/stripe-v3/stripe-v3-tests.ts | 10 +++++++++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/types/stripe-v3/index.d.ts b/types/stripe-v3/index.d.ts index 1c6775aab7..b703f86784 100644 --- a/types/stripe-v3/index.d.ts +++ b/types/stripe-v3/index.d.ts @@ -28,7 +28,11 @@ declare namespace stripe { createSource(element: elements.Element, options?: { owner?: OwnerInfo }): Promise; createSource(options: SourceOptions): Promise; retrieveSource(options: RetrieveSourceOptions): Promise; - redirectToCheckout(options: StripeCheckoutOptions): Promise; + // We use function overloading instead of a union here to ensure that redirectToCheckout can only be + // called with either the server options or the client options - not a mix of both. + redirectToCheckout(options: StripeClientCheckoutOptions): Promise; + // tslint:disable-next-line unified-signatures + redirectToCheckout(options: StripeServerCheckoutOptions): Promise; paymentRequest(options: paymentRequest.StripePaymentRequestOptions): paymentRequest.StripePaymentRequest; createPaymentMethod( type: paymentMethod.paymentMethodType, @@ -75,17 +79,21 @@ declare namespace stripe { }; type billingAddressCollectionType = 'required' | 'auto' | ''; - interface StripeCheckoutOptions { + + interface StripeClientCheckoutOptions { items: StripeCheckoutItem[]; successUrl: string; cancelUrl: string; clientReferenceId?: string; customerEmail?: string; billingAddressCollection?: billingAddressCollectionType; - sessionId?: string; locale?: string; } + interface StripeServerCheckoutOptions { + sessionId: string; + } + interface StripeCheckoutItem { sku?: string; plan?: string; diff --git a/types/stripe-v3/stripe-v3-tests.ts b/types/stripe-v3/stripe-v3-tests.ts index 9a47515e9c..4244bc7d9d 100644 --- a/types/stripe-v3/stripe-v3-tests.ts +++ b/types/stripe-v3/stripe-v3-tests.ts @@ -196,7 +196,7 @@ describe("Stripe elements", () => { }); }); - it("should use checkout API", () => { + it("should use checkout API for client implementations", () => { stripe.redirectToCheckout({ items: [ { sku: 'sku_123', quantity: 1 } @@ -208,6 +208,14 @@ describe("Stripe elements", () => { }); }); + it("should use the checkout API for server implementations", () => { + stripe.redirectToCheckout({ + sessionId: 'sess_test_123', + }).then(errorResult => { + console.log(errorResult.error.param); + }); + }); + it("should use payment intents", () => { stripe.retrievePaymentIntent('pi_18eYalAHEMiOZZp1l9ZTjSU0_secret_NibvRz4PMmJqjfb0sqmT7aq2') .then(result => {