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 handleNextAction for PaymentIntent or SetupIntent return results #439

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions tests/types/src/invalid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,23 @@ stripe
}
});

stripe.handleNextAction({clientSecret: ''}).then((res) => {
if (res.paymentIntent) {
// @ts-expect-error If result has a paymentIntent, setupIntent will be undefined
const setupIntentId = res.setupIntent.id;
}
if (res.setupIntent) {
// @ts-expect-error If result has a setupIntent, paymentIntent will be undefined
const paymentIntentId = res.paymentIntent.id;
}
if (res.error) {
// @ts-expect-error If result has an error, paymentIntent will be undefined
const paymentIntentId = res.paymentIntent.id;
// @ts-expect-error If result has an error, setupIntent will be undefined
const setupIntentId = res.setupIntent.id;
}
});

stripe.processOrder({elements, confirmParams: {return_url: ''}}).then((res) => {
if (res.error) {
}
Expand Down
14 changes: 11 additions & 3 deletions tests/types/src/valid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2170,9 +2170,17 @@ stripe
.handleCardAction('')
.then(({paymentIntent}: {paymentIntent?: PaymentIntent}) => {});

stripe
.handleNextAction({clientSecret: ''})
.then(({paymentIntent}: {paymentIntent?: PaymentIntent}) => {});
stripe.handleNextAction({clientSecret: ''}).then((res) => {
if (res.paymentIntent) {
const paymentIntentId = res.paymentIntent.id;
}
if (res.setupIntent) {
const setupIntentId = res.setupIntent.id;
}
if (res.error) {
const errorType = res.error.type;
}
});

stripe
.verifyMicrodepositsForPayment('', {amounts: [32, 45]})
Expand Down
15 changes: 12 additions & 3 deletions types/stripe-js/stripe.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -590,8 +590,8 @@ export interface Stripe {
handleCardAction(clientSecret: string): Promise<PaymentIntentResult>;

/**
* Use `stripe.handleNextAction` in the Payment Intents API finalizing payments on the server flow to finish confirmation of a [PaymentIntent](https://stripe.com/docs/api/payment_intents) or [SetupIntent](https://stripe.com/docs/api/setup_intents) with the `requires_action` status.
* It will throw an error if the `PaymentIntent` has a different status.
* Use `stripe.handleNextAction` in the [finalizing payments on the server](https://stripe.com/docs/payments/finalize-payments-on-the-server) flow to finish confirmation of a [PaymentIntent](https://stripe.com/docs/api/payment_intents) or [SetupIntent](https://stripe.com/docs/api/setup_intents) with the `requires_action` status.
* It will throw an error if the `PaymentIntent` or `SetupIntent` has a different status.
*
* Note that `stripe.handleNextAction` may take several seconds to complete.
* During that time, you should disable your form from being resubmitted and show a waiting indicator like a spinner.
Expand All @@ -605,7 +605,7 @@ export interface Stripe {
*/
handleNextAction(options: {
clientSecret: string;
}): Promise<PaymentIntentResult>;
}): Promise<PaymentIntentOrSetupIntentResult>;

/**
* Use `stripe.verifyMicrodepositsForPayment` in the [Accept a Canadian pre-authorized debit payment](https://stripe.com/docs/payments/acss-debit/accept-a-payment) flow
Expand Down Expand Up @@ -1203,6 +1203,15 @@ export type SetupIntentResult =
| {setupIntent: api.SetupIntent; error?: undefined}
| {setupIntent?: undefined; error: StripeError};

export type PaymentIntentOrSetupIntentResult =
| {
paymentIntent: api.PaymentIntent;
setupIntent?: undefined;
error?: undefined;
}
| {paymentIntent?: undefined; setupIntent: api.SetupIntent; error?: undefined}
| {paymentIntent?: undefined; setupIntent?: undefined; error: StripeError};

export type ProcessOrderResult =
| {paymentIntent: api.PaymentIntent; order: api.Order; error?: undefined}
| {paymentIntent?: undefined; order: api.Order; error?: undefined}
Expand Down