0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-18 02:21:47 -05:00

Updated handleStripeWebhook middleware

no-issue

This adds the handlers for the new events we want to listen to
This commit is contained in:
Fabien O'Carroll 2019-10-08 14:22:26 +07:00
parent 68d65c905a
commit 8829b545a9

View file

@ -194,20 +194,35 @@ module.exports = function MembersApi({
middleware.handleStripeWebhook.use(ensureStripe, body.raw({type: 'application/json'}), async function (req, res) {
try {
const event = await stripe.parseWebhook(req.body, req.headers['stripe-signature']);
if (event.type !== 'checkout.session.completed') {
res.writeHead(200);
return res.end();
if (event.type === 'customer.subscription.deleted') {
await stripe.handleCustomerSubscriptionDeletedWebhook(event.data.object);
}
const customer = await stripe.getCustomer(event.data.object.customer, {
expand: ['subscriptions.data.default_payment_method']
});
const member = await users.get({email: customer.email}) || await users.create({email: customer.email});
if (event.type === 'customer.subscription.updated') {
await stripe.handleCustomerSubscriptionUpdatedWebhook(event.data.object);
}
await stripe.handleCheckoutSessionCompletedWebhook(member, customer);
if (event.type === 'invoice.payment_succeeded') {
await stripe.handleInvoicePaymentSucceededWebhook(event.data.object);
}
if (event.type === 'invoice.payment_failed') {
await stripe.handleInvoicePaymentFailedWebhook(event.data.object);
}
if (event.type === 'checkout.session.completed') {
const customer = await stripe.getCustomer(event.data.object.customer, {
expand: ['subscriptions.data.default_payment_method']
});
const member = await users.get({email: customer.email}) || await users.create({email: customer.email});
await stripe.handleCheckoutSessionCompletedWebhook(member, customer);
const emailType = 'signup';
await sendEmailWithMagicLink(customer.email, emailType, {forceEmailType: true});
}
const emailType = 'signup';
await sendEmailWithMagicLink(customer.email, emailType, {forceEmailType: true});
res.writeHead(200);
res.end();
} catch (err) {