[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
This commit is contained in:
Harry 2019-07-23 20:40:44 +01:00 committed by Wesley Wigham
parent 449c42470f
commit 47fb6ba276
2 changed files with 20 additions and 4 deletions

View File

@ -28,7 +28,11 @@ declare namespace stripe {
createSource(element: elements.Element, options?: { owner?: OwnerInfo }): Promise<SourceResponse>;
createSource(options: SourceOptions): Promise<SourceResponse>;
retrieveSource(options: RetrieveSourceOptions): Promise<SourceResponse>;
redirectToCheckout(options: StripeCheckoutOptions): Promise<StripeRedirectResponse>;
// 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<StripeRedirectResponse>;
// tslint:disable-next-line unified-signatures
redirectToCheckout(options: StripeServerCheckoutOptions): Promise<StripeRedirectResponse>;
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;

View File

@ -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 => {