Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update types for deferred Intents #427

Merged
merged 8 commits into from
Mar 16, 2023
47 changes: 45 additions & 2 deletions tests/types/src/invalid.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
Stripe,
StripeElements,
StripeCardElement,
StripeIbanElement,
StripePaymentElement,
Expand All @@ -9,13 +8,18 @@ import {
} from '../../../types';

declare const stripe: Stripe;
declare const elements: StripeElements;
declare const cardElement: StripeCardElement;
declare const ibanElement: StripeIbanElement;
declare const paymentElement: StripePaymentElement;
declare const cartElement: StripeCartElement;
declare const expressCheckoutElement: StripeExpressCheckoutElement;

// @ts-expect-error: Passing `clientSecret` or `mode` implies different integration paths which cannot be combined
const elements = stripe.elements({clientSecret: '', mode: ''});

// @ts-expect-error mode must be one of payment, setup, or subscription
stripe.elements({mode: 'test'});

elements.update({
// @ts-expect-error: `clientSecret` is not updatable
clientSecret: 'pk_foo_secret_bar',
Expand Down Expand Up @@ -149,6 +153,9 @@ elements.create('expressCheckout', {
},
});

// @ts-expect-error at least one of elements or clientSecret is required
stripe.confirmPayment({confirmParams: {return_url: ''}});

stripe
.confirmPayment({elements, confirmParams: {return_url: ''}})
.then((res) => {
Expand All @@ -175,6 +182,9 @@ stripe
}
});

// @ts-expect-error either elements or clientSecret is required
stripe.confirmSetup({confirmParams: {return_url: ''}});

stripe.confirmSetup({elements, confirmParams: {return_url: ''}}).then((res) => {
if (res.error) {
}
Expand Down Expand Up @@ -258,3 +268,36 @@ stripe.createEphemeralKeyNonce({});

// @ts-expect-error: Expected 1 arguments, but got 0
stripe.createEphemeralKeyNonce();

// @ts-expect-error type and element are incompatible
stripe.createPaymentMethod({
type: 'card',
element: cardElement,
params: {
billing_details: {
address: '',
},
},
});

// @ts-expect-error type and elements are incompatible
stripe.createPaymentMethod({
type: 'card',
elements: elements,
params: {
billing_details: {
address: '',
},
},
});

// @ts-expect-error element and elements are incompatible
stripe.createPaymentMethod({
element: cardElement,
elements: elements,
params: {
billing_details: {
address: '',
},
},
});
207 changes: 204 additions & 3 deletions tests/types/src/valid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,35 @@ const AVENIR: CustomFontSource = {
};

const elements: StripeElements = stripe.elements({
fonts: [OPEN_SANS, AVENIR],
locale: 'auto',
mode: 'payment',
currency: 'usd',
amount: 1099,
setup_future_usage: 'off_session',
capture_method: 'automatic',
payment_method_types: ['card'],
appearance: {
disableAnimations: false,
theme: 'night',
variables: {
colorIcon: 'blue',
},
rules: {
'.Tab--selected': {
backgroundColor: 'blue',
},
},
labels: 'above',
},
loader: 'auto',
customerOptions: {
customer: 'cus_foo',
ephemeralKey: 'ek_test_foo',
},
});

const elementsClientSecret: StripeElements = stripe.elements({
fonts: [OPEN_SANS, AVENIR],
locale: 'auto',
clientSecret: '',
Expand Down Expand Up @@ -142,12 +171,22 @@ elements.update({
},
labels: 'floating',
},
mode: 'payment',
currency: 'usd',
amount: 1099,
setup_future_usage: 'off_session',
capture_method: 'automatic',
payment_method_types: ['card'],
});

const fetchUpdates = async () => {
const {error} = await elements.fetchUpdates();
};

const handleSubmit = async () => {
const {error} = await elements.submit();
};

const auBankAccountElement = elements.create('auBankAccount', {});

const retrievedAuBankAccountElement: StripeAuBankAccountElement | null = elements.getElement(
Expand Down Expand Up @@ -2084,10 +2123,32 @@ stripe
.handleCardAction('')
.then(({paymentIntent}: {paymentIntent?: PaymentIntent}) => {});

stripe
.handleNextAction('')
.then(({paymentIntent}: {paymentIntent?: PaymentIntent}) => {});

stripe
.verifyMicrodepositsForPayment('', {amounts: [32, 45]})
.then((result: {paymentIntent?: PaymentIntent; error?: StripeError}) => null);

stripe.createPaymentMethod({
elements: elements,
params: {
billing_details: {
name: 'Jenny Rosen',
},
},
});

stripe.createPaymentMethod({
element: cardElement,
params: {
billing_details: {
name: 'Jenny Rosen',
},
},
});

stripe.createPaymentMethod({
type: 'acss_debit',
billing_details: {name: '', email: ''},
Expand Down Expand Up @@ -2584,6 +2645,58 @@ stripe
}) => null
);

// confirmPayment: redirect: 'always' without clientSecret
stripe
.confirmPayment({
elements,
confirmParams: {
return_url: '',
},
})
.then((res) => {
if (res.error) {
}
});
stripe
.confirmPayment({
elements,
confirmParams: {
return_url: '',
},
redirect: 'always',
})
.then((res) => {
if (res.error) {
}
});

// confirmPayment: redirect: 'always' with clientSecret
stripe
.confirmPayment({
clientSecret: '',
confirmParams: {
return_url: '',
},
})
.then((res) => {
if (res.error) {
}
});
stripe
.confirmPayment({
clientSecret: '',
confirmParams: {
return_url: '',
},
elements,
redirect: 'always',
})
.then((res) => {
if (res.error) {
}
});

// confirmPayment: redirect: 'if_required' without clientSecret
stripe
.confirmPayment({
elements,
Expand All @@ -2598,8 +2711,34 @@ stripe
stripe
.confirmPayment({
elements,
redirect: 'if_required',
confirmParams: {},
})
.then((res) => {
if (res.error) {
}
if (res.paymentIntent) {
}
});

// confirmPayment redirect: 'if_required' with clientSecret
stripe
.confirmPayment({
clientSecret: '',
redirect: 'if_required',
})
.then((res) => {
if (res.error) {
}
if (res.paymentIntent) {
}
});
stripe
.confirmPayment({
clientSecret: '',
redirect: 'if_required',
confirmParams: {},
elements,
})
.then((res) => {
if (res.error) {
Expand All @@ -2608,21 +2747,58 @@ stripe
}
});

// confirmSetup: redirect: 'always' without clientSecret
stripe
.confirmSetup({
elements,
confirmParams: {
return_url: '',
},
redirect: 'if_required',
})
.then((res) => {
if (res.error) {
}
if (res.setupIntent) {
});
stripe
.confirmSetup({
elements,
confirmParams: {
return_url: '',
},
redirect: 'always',
})
.then((res) => {
if (res.error) {
}
});

// confirmSetup: redirect: 'always' with clientSecret
stripe
.confirmSetup({
clientSecret: '',
confirmParams: {
return_url: '',
},
})
.then((res) => {
if (res.error) {
}
});
stripe
.confirmSetup({
clientSecret: '',
confirmParams: {
return_url: '',
},
elements,
redirect: 'always',
})
.then((res) => {
if (res.error) {
}
});

// confirmSetup: redirect: 'if_required' without clientSecret
stripe
.confirmSetup({
elements,
Expand All @@ -2634,12 +2810,37 @@ stripe
if (res.setupIntent) {
}
});

stripe
.confirmSetup({
elements,
redirect: 'if_required',
confirmParams: {},
})
.then((res) => {
if (res.error) {
}
if (res.setupIntent) {
}
});

// confirmSetup redirect: 'if_required' with clientSecret
stripe
.confirmSetup({
clientSecret: '',
redirect: 'if_required',
})
.then((res) => {
if (res.error) {
}
if (res.setupIntent) {
}
});
stripe
.confirmSetup({
clientSecret: '',
redirect: 'if_required',
confirmParams: {},
elements,
})
.then((res) => {
if (res.error) {
Expand Down
Loading