So I need to add a stripe unsubscribe button. For that I need the function that contacts stripe API to be hidden as it uses a secret key. So I would like that function to be in the app.js file.
Basically in billing.ejs I have:
<button onclick="<% unSubscribe() %>" class="btn btn-lg btn-success">Unsubscribe by clicking here</button>
and in app.js I have:
app.get('/billing', function(req, res, next) {
function unSubscribe() {
stripe.subscriptions.del(
req.user.subscriptionId,
function(err, confirmation) {
// asynchronously called
}
);
}
stripe.checkout.sessions.create({
customer_email: req.user.email,
payment_method_types: ['card'],
subscription_data: {
items: [{
plan: process.env.STRIPE_PLAN,
}],
},
success_url: process.env.BASE_URL + ':3000/billing?session_id={CHECKOUT_SESSION_ID}',
cancel_url: process.env.BASE_URL + ':3000/billing',
}, function(err, session) {
if (err) return next(err);
res.render('billing', { STRIPE_PUBLIC_KEY: process.env.STRIPE_PUBLIC_KEY, sessionId: session.id, subscriptionActive: req.user.subscriptionActive })
});
})
So the unSubcribe function is not working. If I pass it to res.render() then it works BUT also activates the function whenever the page loads. I am pretty lost here what to try next. Any help is appreciated.