Reputation: 11
I am Implementing Stripe Subscription with 3d Authentication but Subscription got incomplete after successfull verification can anybody help me with this ? Technology--- MERN--
Steps
const paymentMethod = await stripe.createPaymentMethod({
type: "card",
card: elements.getElement(CardElement),
billing_details: {
name: name,
email: email,
address: {
line1: address,
city,
state,
postal_code: zipcode,
country,
},
// phone: "+1 555-555-5555",
},
});
let customer = await stripe.customers.list({ email: email, limit: 1 });
if (customer.data.length === 0) {
customer = await stripe.customers.create({
email,
name,
address: {
line1: billing.address,
city: billing.city,
state: billing.state,
postal_code: billing.zipcode,
country: billing.country,
},
});
} else {
customer = customer?.data[0];
}
const setupIntent = await stripe.setupIntents.create({
payment_method: paymentMethod,
customer: customer.id,
confirm: true,
usage: "on_session",
metadata: {
data: JSON.stringify(req.body),
},
});
const confirmedSetupIntent = await stripe.setupIntents.confirm(
setupIntent.id,
{
return_url: `${CLIENT_BASE_URL}/check-payment-status/${pricePlanId}`,
}
);
passing this return url on frontend
event setup_intent.succeeded
const customerList = await stripe.customers.list({
email: email,
limit: 1,
});
const customer = customerList.data.length > 0 ? customerList.data[0] : null;
if (customer) {
await stripe.customers.update(customer.id, {
name,
invoice_settings: {
default_payment_method: paymentMethodId,
},
});
}
// Check if customer has an active subscription
const subscriptionList = await stripe.subscriptions.list({
customer: account.customerId,
status: "active",
limit: 1,
});
let subscription = null;
subscription = await stripe.subscriptions.create({
customer: account.customerId,
items: [{ price: pricePlanId }],
metadata: {
product_name: plan.name,
userId: user._id,
amount: plan.amount,
credits: plan.credits,
planId: pricePlanId,
billingName: billing.name,
billingEmail: billing.email,
},
payment_settings: {
payment_method_types: ["card"],
save_default_payment_method: "on_subscription",
},
expand: ["latest_invoice.payment_intent"],
off_session: true,
});
c
an anybody tell me what is wrong in this code
Upvotes: 0
Views: 627
Reputation: 941
Your flow is not exactly what Stripe recommends in their docs. There shouldn't be any need to directly create a Payment Method or a Setup Intent for a Subscription integration. See these docs for the recommended integration guide.
Upvotes: -1