pe: 'StripeInvalidRequestError', 生的: { 代码:' payment_intent_unexpected_state', doc_url: 'https://stripe.com/docs/error-codes/ payment-intent-unexpected-state', message: '无法捕获此 PaymentIntent,因为它的状态为 require_action。只能捕获具有以下状态之一的 PaymentIntent:requires_capture。',
// import stripe from 'stripe';
import axios from 'axios';
import { Console } from 'node:console';
import test from 'node:test';
import stripePackage, { Stripe } from 'stripe';
import { never } from 'zod';
const stripe = new stripePackage(process.env.STRIPE_KEY as string);
console.log('process.env.STRIPE_KEY', process.env.STRIPE_KEY);
// const stripeClient = new stripe(process.env.STRIPE_KEY as string, {
// apiVersion: '2023-10-16',
// });
export const createIntentServices = async (data: any) => {
try {
// console.log(process.env.STRIPE_KEY ," ---",stripeClient)
if (!data.amount) {
throw new Error('Amount is missing');
}
// const token = await stripe.tokens.create({
// card: {
// number: '4242424242424242',
// exp_month: "12",
// exp_year: "2023",
// cvc: '123',
// },
// });
// console.log(token);
const customer = await stripe.customers.create();
const ephemeralKey = await stripe.ephemeralKeys.create(
{ customer: customer.id },
{ apiVersion: '2023-10-16' }
);
const token = await stripe.tokens.create({
account: {
individual: {
first_name: 'Jane',
last_name: 'Doe',
},
tos_shown_and_accepted: true,
},
});
console.log("token",token)
// if (!token.id) {
// throw new Error('Token not generated');
// }
// const confirmationToken = await stripe.testHelpers.confirmationTokens.create({
// payment_method: 'pm_card_visa',
// });
// console.log(confirmationToken);
const paymentIntent = await stripe.paymentIntents.create({
amount: data.amount,
currency: process.env.CURRENCY || 'usd',
payment_method_types: ['card'],
payment_method_options: {
card: {
capture_method: 'manual',
},
},
capture_method:"manual",
// use_stripe_sdk:true,
customer: customer.id,
payment_method:"pm_card_visa",
confirm:true,
description:"transaction for parking",
});
// const paymentIntent = await stripe.paymentIntents.create({
// amount: 2000,
// currency: 'usd',
// automatic_payment_methods: {
// allow_redirects:"never",
// enabled: true,
// },
// confirm:true,
// // return_url:"https://docs.stripe.com/payments/payment-methods/overview",
// payment_method:"pm_card_visa"
// });
// console.log("------",paymentIntent,"--------");
if (!paymentIntent) {
throw new Error('Something went wrong, Try again later');
}
const paymentConfirm = await stripe.paymentIntents.confirm(
paymentIntent.id,
{ payment_method: 'pm_card_visa' }
);
// const paymentConfirm = await stripe.paymentIntents.confirm(paymentIntent.id, confirm);
// console.log(paymentConfirm);
if (paymentIntent.status === 'requires_action') {
console.log("url------",paymentIntent.next_action?.redirect_to_url?.url);
console.log('3DS authentication required.');
}
console.log(paymentIntent);
if (!paymentConfirm) {
throw new Error('Something went wrong, Try again later');
}
return {
message: 'Payment Intent created successfully',
status: 200,
data: {
paymentIntent,
ephemeralKey,
customer,
paymentConfirm,
},
};
} catch (error) {
throw error;
}
};
export const capturePaymentServices = async (data: any) => {
try {
console.log(data);
// const res=await stripe.paymentIntents.retrieve(data.paymentId);
// console.log("-----",res);
if (!data.paymentId) {
throw new Error('PaymentId is missing');
}
const intent = await stripe.paymentIntents.capture(data.paymentId, {
amount_to_capture: data.amount,
});
return {
message: 'Payment captured successfully',
status: 200,
data: intent,
};
} catch (error) {
throw error;
}
};
export const cancelPaymentServices = async (data: any) => {
try {
if (!data.paymentId) {
throw new Error('PaymentId is missing');
}
const paymentIntent = await stripe.paymentIntents.cancel(data.paymentId);
return {
data: paymentIntent,
};
} catch (error) {
throw error;
}
};
我该如何解决这个错误
现在您正在服务器端确认,但您的付款需要3DS才能完成。建议的路线是您确认客户端,如规范流程 (docs) 中所示,如果需要,这将自动处理 3DS。如果您确实想在服务器端进行确认,那么您需要按照https://docs.stripe.com/ payments/3d-secure/authentication-flow所示实施 3DS 处理来完成付款。