-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbilling.webhook.controller.js
More file actions
54 lines (49 loc) · 1.67 KB
/
Copy pathbilling.webhook.controller.js
File metadata and controls
54 lines (49 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* Module dependencies
*/
import config from '../../../config/index.js';
import getStripe from '../lib/stripe.js';
import BillingWebhookService from '../services/billing.webhook.service.js';
/**
* @desc Endpoint to handle Stripe webhook events
* @param {Object} req - Express request object
* @param {Object} res - Express response object
* @returns {Promise<void>}
*/
const handleWebhook = async (req, res) => {
const stripe = getStripe();
if (!stripe) return res.status(400).json({ error: 'Stripe is not configured' });
const sig = req.headers['stripe-signature'];
const { webhookSecret } = config.stripe;
let event;
try {
event = stripe.webhooks.constructEvent(req.body, sig, webhookSecret);
} catch (err) {
return res.status(400).json({ error: 'Webhook signature verification failed' });
}
try {
switch (event.type) {
case 'checkout.session.completed':
await BillingWebhookService.handleCheckoutCompleted(event.data.object);
break;
case 'customer.subscription.updated':
await BillingWebhookService.handleSubscriptionUpdated(event.data.object, event);
break;
case 'customer.subscription.deleted':
await BillingWebhookService.handleSubscriptionDeleted(event.data.object);
break;
case 'invoice.payment_failed':
await BillingWebhookService.handleInvoicePaymentFailed(event.data.object);
break;
default:
break;
}
return res.status(200).json({ received: true });
} catch (err) {
console.error('Stripe webhook handler error:', err);
return res.status(500).json({ error: 'Webhook handler failed' });
}
};
export default {
handleWebhook,
};