QUESTION:
I am currently using a webhook: when the checkout.session.completed
event is received, the user is redirected to the success_url by default even if I try to redirect him to another page with res.redirect("/page") (Node.js). Is there truly no way to redirect him to another page in case my fulfilment code fails ? (That is: the payment succeeds, but the fulfilment code fails, so redirecting to success_url is not appropriate)
REFERENCE:
https://stripe.com/docs/payments/checkout/one-time
https://stripe.com/docs/payments/checkout/fulfillment#webhooks
"The checkout.session.completed webhook is sent to your server before your customer is redirected. Your webhook acknowledgement (any 2xx status code) will trigger the customer’s redirect to the success_url"
WHAT I TRIED:
Chaining Express.js 4's res.status(401) to a redirect
So if I were to send a 4xx status code, shouldn't that prevent the success_url redirect ? That is indeed what I observe.
But I can't seem to redirect to another page though. I tried:
res.status(401).location('/submit/wait/').end();
res.redirect(401, '/submit/wait/');
res.set('Content-Type', 'text/html');
res.status(401).send('<!DOCTYPE html><html><head><meta http-equiv="refresh" content="0; url=/submit/wait"></head></html>`);Is there really no way to then just redirect to another page ?
CODE
router.post('/webhook', bodyParser.raw({type: 'application/json'}), (req, res) => {
const sig = req.headers['stripe-signature'];
let event;
try {
event = stripe.webhooks.constructEvent(req.rawBody, sig, endpointSecret);
} catch (err) {
return res.status(400).send(`Webhook Error: ${err.message}`);
}
if (event.type === 'checkout.session.completed') {
const session = event.data.object;
(async () => {
try {
//FULFIL ORDER CODE
} catch(e) {
console.log("ERROR FULFILLING ORDER: "+e);
res.status(401).location('/submit/wait/').end();
}
})();
}
else {
console.log("DEFAULT RESPONSE");
// Return a response to acknowledge receipt of the event
res.json({received: true});
}
});