所以我让我的用户创建一个免费试用的订阅。
节点
const subscriptionWithTrial = async (req, res) => {
const {email, payment_method, user_id} = req.body;
const customer = await stripe.customers.create({
payment_method: payment_method,
email: email,
invoice_settings: {
default_payment_method: payment_method,
},
});
const subscription = await stripe.subscriptions.create({
customer: customer.id,
items: [{ plan: process.env.STRIPE_PRODUCT }],
trial_period_days: 14,
expand: ['latest_invoice.payment_intent', 'pending_setup_intent'],
});
res.json({subscription});
}
app.post('/subWithTrial', payment.subscriptionWithTrial);
反应
const handleSubmitSubTrial = async (event) => {
if (!stripe || !elements) {
console.log("stripe.js hasn't loaded")
// Stripe.js has not yet loaded.
// Make sure to disable form submission until Stripe.js has loaded.
return;
}
const result = await stripe.createPaymentMethod({
type: 'card',
card: elements.getElement(CardElement),
billing_details: {
email: email,
},
});
stripe.createToken(elements.getElement(CardElement)).then(function(result) {
});
if (result.error) {
console.log(result.error.message);
} else {
try {
const res = await axios.post('/subWithTrial', {'payment_method': result.paymentMethod.id, 'email': email, 'user_id': auth0Context.user.sub});
const {pending_setup_intent, latest_invoice} = res.data.subscription;
if (pending_setup_intent) {
const {client_secret, status} = res.data.subscription.pending_setup_intent;
if (status === "requires_action") {
stripe.confirmCardSetup(client_secret).then(function(result) {
if (result.error) {
console.log('There was an issue!');
console.log(result.error);
} else {
// The setup has succeeded. Display a success message.
console.log('Success');
}
我使用的是卡号4000008260003178
,所以我非常确定它应该需要身份验证(这是有效的),然后由于在线stripe.confirmCardSetup(client_secret).then(function(result) { if (result.error) {
上资金不足而导致result.error
。但这并不会发生。相反,它给我的是console.log('Success');
发布于 2021-05-27 09:54:04
这是意料之中的。stripe.confirmCardSetup
实际上并没有试图转移资金,它只是将卡设置为在不需要3DS的情况下进行未来的休息日支付。
由于您创建的订阅有14天的试用期,在此之前,实际的第一次发票付款不会发生。这意味着,在这种情况下,测试卡能够正确设置,直到14天后试用期结束时才会看到支付失败。
https://stackoverflow.com/questions/67714326
复制相似问题