-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbilling.webhook.service.js
More file actions
167 lines (146 loc) · 5.55 KB
/
Copy pathbilling.webhook.service.js
File metadata and controls
167 lines (146 loc) · 5.55 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**
* Module dependencies
*/
import mongoose from 'mongoose';
import config from '../../../config/index.js';
import SubscriptionRepository from '../repositories/billing.subscription.repository.js';
import billingEvents from '../lib/events.js';
const Organization = mongoose.model('Organization');
/**
* Plan rank lookup — higher index means higher-tier plan.
* Used to determine upgrade vs downgrade.
*/
const planRanks = Object.fromEntries((config.billing?.plans || []).map((p, i) => [p, i]));
/**
* @desc Resolve the plan name from a Stripe subscription object.
* In webhook payloads, price.product is typically a string ID (not expanded),
* so we check price.metadata first, then fall back to plan.metadata.
* @param {Object} subscription - Stripe subscription object
* @returns {string} plan name
*/
const resolvePlan = (subscription) => {
const item = subscription.items?.data?.[0];
return item?.price?.metadata?.planId || item?.plan?.metadata?.planId || 'free';
};
/**
* @desc Sync the organization plan field to match the subscription plan
* @param {String} organizationId - Organization document ID
* @param {String} plan - Plan name to set
* @returns {Promise<void>}
*/
const syncOrganizationPlan = async (organizationId, plan) => {
if (!organizationId || !mongoose.Types.ObjectId.isValid(organizationId)) return;
await Organization.findByIdAndUpdate(organizationId, { plan }, { runValidators: true }).exec();
};
/**
* @desc Handle checkout.session.completed event — create or update subscription
* @param {Object} session - Stripe checkout session object
* @returns {Promise<void>}
*/
const handleCheckoutCompleted = async (session) => {
const { customer: stripeCustomerId, subscription: stripeSubscriptionId, metadata } = session;
let organizationId = metadata?.organizationId;
const plan = metadata?.plan || 'free';
// Fallback: resolve organizationId from stripeCustomerId if metadata is missing
if (!organizationId) {
const sub = await SubscriptionRepository.findByStripeCustomerId(stripeCustomerId);
if (sub) organizationId = String(sub.organization?._id || sub.organization);
}
if (!organizationId || !mongoose.Types.ObjectId.isValid(organizationId)) return;
const existing = await SubscriptionRepository.findByOrganization(organizationId);
if (existing) {
await SubscriptionRepository.update({
_id: existing._id,
stripeCustomerId,
stripeSubscriptionId,
plan,
status: 'active',
});
} else {
await SubscriptionRepository.create({
organization: organizationId,
stripeCustomerId,
stripeSubscriptionId,
plan,
status: 'active',
});
}
await syncOrganizationPlan(organizationId, plan);
};
/**
* @desc Handle customer.subscription.updated event — sync subscription state
* @param {Object} subscription - Stripe subscription object
* @param {Object} event - Full Stripe event (with data.previous_attributes for plan change detection)
* @returns {Promise<void>}
*/
const handleSubscriptionUpdated = async (subscription, event) => {
const existing = await SubscriptionRepository.findByStripeSubscriptionId(subscription.id);
if (!existing) return;
const newPlan = resolvePlan(subscription);
await SubscriptionRepository.update({
_id: existing._id,
plan: newPlan,
status: subscription.status,
currentPeriodEnd: new Date(subscription.current_period_end * 1000),
cancelAtPeriodEnd: subscription.cancel_at_period_end,
});
const organizationId = String(existing.organization?._id || existing.organization);
await syncOrganizationPlan(organizationId, newPlan);
// Detect plan change from previous_attributes and emit event
const previousItems = event?.data?.previous_attributes?.items?.data;
if (previousItems) {
const previousPlan = previousItems[0]?.price?.metadata?.planId
|| previousItems[0]?.plan?.metadata?.planId
|| null;
if (previousPlan && previousPlan !== newPlan) {
const prevRank = planRanks[previousPlan];
const newRank = planRanks[newPlan];
const isDowngrade = prevRank != null && newRank != null ? prevRank > newRank : null;
try {
billingEvents.emit('plan.changed', {
organizationId,
previousPlan,
newPlan,
subscription,
isDowngrade,
});
} catch { /* listener errors must not disrupt webhook processing */ }
}
}
};
/**
* @desc Handle customer.subscription.deleted event — cancel subscription
* @param {Object} subscription - Stripe subscription object
* @returns {Promise<void>}
*/
const handleSubscriptionDeleted = async (subscription) => {
const existing = await SubscriptionRepository.findByStripeSubscriptionId(subscription.id);
if (!existing) return;
await SubscriptionRepository.update({
_id: existing._id,
plan: 'free',
status: 'canceled',
});
await syncOrganizationPlan(existing.organization?._id || existing.organization, 'free');
};
/**
* @desc Handle invoice.payment_failed event — mark subscription as past_due
* @param {Object} invoice - Stripe invoice object
* @returns {Promise<void>}
*/
const handleInvoicePaymentFailed = async (invoice) => {
const { subscription: stripeSubscriptionId } = invoice;
if (!stripeSubscriptionId) return;
const existing = await SubscriptionRepository.findByStripeSubscriptionId(stripeSubscriptionId);
if (!existing) return;
await SubscriptionRepository.update({
_id: existing._id,
status: 'past_due',
});
};
export default {
handleCheckoutCompleted,
handleSubscriptionUpdated,
handleSubscriptionDeleted,
handleInvoicePaymentFailed,
};