I've been at this for weeks and have reviewed several different (1) questions (2) on stack around retrieving the customer ID during the stripe checkout process (node / mongoose). Neither question--despite having answers--seem to solve the issue for either the original askers or myself, so giving it another try...
During the signup flow, I'm pushing the user to a stripe portal using stripe's create-checkout-session after the user has created a profile in my database.
I'd then like to take the stripe customer_id and save it to the database so I can monitor the customer's subscription status since I cannot figure out webhooks (I'm a new-ish/intermediate programmer and am conceptually struggling with grasping webhooks).
Create checkout controller function (pushes users to the stripe portal for checkout)
module.exports.createCheckoutSession = async (req, res) => {
if (req.isAuthenticated()) {
try {
const session = await stripe.checkout.sessions.create({
mode: "subscription",
line_items: [
{
price: priceId,
quantity: 1,
},
],
// ?session_id={CHECKOUT_SESSION_ID} means the redirect will have the session ID set as a query param
success_url: `${webUrl}/users/${req.user._id}/activations/?session_id={CHECKOUT_SESSION_ID}`,
// success_url: `/users/${req.body._id}`,
cancel_url: `${webUrl}/canceled.html`,
// automatic_tax: { enabled: true }
});
console.log("customer ID ", session.customer_id);
console.log("query params ", req.query);
console.log("req params ", req.params);
console.log("session", session);
return res.redirect(303, session.url);
} catch (e) {
res.status(400);
return res.send({
error: {
message: e.message,
}
});
}
}
req.flash('error', "You must login before changing billing");
res.render('/login');
}
My function redirects the user to my 'activations' page as the success url which is where I'm trying to capture the customer_id after it's been created in stripe's checkout but I can't find it. Keeping a lot of the other code out of my activations function, below is how I'm looking for the customer_id:
Activations
module.exports.showActivations = async (req, res) => {
...
console.log("req params ", req.params);
console.log("req headers ", req.headers)
console.log("session ", session);
res.render('users/activations', { activeEvents, qr, qrAgg });
...
}
};
Output of my console.logs
Req.params only has the user_id from my app when they hit my database to signup (prior to going to the stripe portal)
Req.headers has some cookies but nothing from stripe
Session returns 'ReferenceError: session is not defined'
How do I get the customer_id from stripe during the checkout so I can monitor the subscription status?