-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbilling.webhook.controller.js
More file actions
71 lines (63 loc) · 2.04 KB
/
Copy pathbilling.webhook.controller.js
File metadata and controls
71 lines (63 loc) · 2.04 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* Module dependencies
*/
import Stripe from 'stripe';
import config from '../../../config/index.js';
import BillingWebhookService from '../services/billing.webhook.service.js';
/**
* Lazily instantiated Stripe client
*/
let stripeClient = null;
/**
* @desc Get or create the Stripe client instance
* @returns {Object|null} Stripe client or null if not configured
*/
const getStripe = () => {
if (stripeClient) return stripeClient;
if (!config.stripe?.secretKey) return null;
stripeClient = new Stripe(config.stripe.secretKey);
return stripeClient;
};
/**
* @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: ${err.message}` });
}
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);
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,
};