@langwatch/server 3.1.0-beta.3 → 3.1.0-beta.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/langwatch/ee/admin/ImpersonationBanner.tsx +61 -0
- package/langwatch/ee/admin/ImpersonationSwitchBackMenuItem.tsx +30 -0
- package/langwatch/ee/admin/__tests__/ImpersonationBanner.integration.test.tsx +82 -0
- package/langwatch/ee/admin/__tests__/impersonation.service.unit.test.ts +231 -0
- package/langwatch/ee/admin/backoffice/BackofficeTable.tsx +200 -0
- package/langwatch/ee/admin/backoffice/__tests__/adminClient.unit.test.ts +147 -0
- package/langwatch/ee/admin/backoffice/__tests__/dateInputToISO.unit.test.ts +42 -0
- package/langwatch/ee/admin/backoffice/__tests__/userVisibility.unit.test.ts +174 -0
- package/langwatch/ee/admin/backoffice/adminClient.ts +114 -0
- package/langwatch/ee/admin/backoffice/resources/OrganizationFeaturesView.tsx +373 -0
- package/langwatch/ee/admin/backoffice/resources/OrganizationsView.tsx +677 -0
- package/langwatch/ee/admin/backoffice/resources/ProjectsView.tsx +647 -0
- package/langwatch/ee/admin/backoffice/resources/SubscriptionsView.tsx +628 -0
- package/langwatch/ee/admin/backoffice/resources/UsersView.tsx +556 -0
- package/langwatch/ee/admin/backoffice/useAdminResource.ts +104 -0
- package/langwatch/ee/admin/backoffice/userVisibility.ts +105 -0
- package/langwatch/ee/admin/impersonation.service.ts +170 -0
- package/langwatch/ee/admin/isAdmin.ts +9 -0
- package/langwatch/ee/admin/routes/admin.ts +531 -0
- package/langwatch/ee/admin/safeSelects.ts +64 -0
- package/langwatch/ee/billing/__tests__/customerService.unit.test.ts +183 -0
- package/langwatch/ee/billing/__tests__/growthSeatEvent.unit.test.ts +213 -0
- package/langwatch/ee/billing/__tests__/licensePurchaseHandler.unit.test.ts +250 -0
- package/langwatch/ee/billing/__tests__/notification.service.unit.test.ts +768 -0
- package/langwatch/ee/billing/__tests__/planLimits.unit.test.ts +19 -0
- package/langwatch/ee/billing/__tests__/planProvider.unit.test.ts +440 -0
- package/langwatch/ee/billing/__tests__/planTypes.unit.test.ts +32 -0
- package/langwatch/ee/billing/__tests__/pricing.unit.test.ts +54 -0
- package/langwatch/ee/billing/__tests__/seatEventSubscription.unit.test.ts +717 -0
- package/langwatch/ee/billing/__tests__/stripe.integration.test.ts +344 -0
- package/langwatch/ee/billing/__tests__/stripePrices.types.unit.test.ts +27 -0
- package/langwatch/ee/billing/__tests__/stripePricesLoader.unit.test.ts +72 -0
- package/langwatch/ee/billing/__tests__/subscription.service.unit.test.ts +730 -0
- package/langwatch/ee/billing/__tests__/subscriptionItemCalculator.unit.test.ts +176 -0
- package/langwatch/ee/billing/__tests__/usage-limit.service.unit.test.ts +706 -0
- package/langwatch/ee/billing/__tests__/usageReportingService.integration.test.ts +421 -0
- package/langwatch/ee/billing/__tests__/usageReportingService.unit.test.ts +515 -0
- package/langwatch/ee/billing/__tests__/webhookService.unit.test.ts +958 -0
- package/langwatch/ee/billing/currencyRouter.ts +18 -0
- package/langwatch/ee/billing/errors.ts +129 -0
- package/langwatch/ee/billing/index.ts +67 -0
- package/langwatch/ee/billing/middleware.ts +17 -0
- package/langwatch/ee/billing/notifications/notification.service.ts +651 -0
- package/langwatch/ee/billing/notifications/repositories/notification.repository.ts +51 -0
- package/langwatch/ee/billing/notifications/types.ts +9 -0
- package/langwatch/ee/billing/notifications/usage-limit.service.ts +622 -0
- package/langwatch/ee/billing/nurturing/hooks/activityTracking.ts +90 -0
- package/langwatch/ee/billing/nurturing/hooks/activityTracking.unit.test.ts +237 -0
- package/langwatch/ee/billing/nurturing/hooks/featureAdoption.ts +120 -0
- package/langwatch/ee/billing/nurturing/hooks/featureAdoption.unit.test.ts +324 -0
- package/langwatch/ee/billing/nurturing/hooks/index.ts +17 -0
- package/langwatch/ee/billing/nurturing/hooks/productInterest.ts +68 -0
- package/langwatch/ee/billing/nurturing/hooks/productInterest.unit.test.ts +138 -0
- package/langwatch/ee/billing/nurturing/hooks/promptCreation.integration.test.ts +288 -0
- package/langwatch/ee/billing/nurturing/hooks/promptCreation.ts +131 -0
- package/langwatch/ee/billing/nurturing/hooks/promptCreation.unit.test.ts +134 -0
- package/langwatch/ee/billing/nurturing/hooks/signupIdentification.ts +101 -0
- package/langwatch/ee/billing/nurturing/hooks/signupIdentification.unit.test.ts +317 -0
- package/langwatch/ee/billing/nurturing/hooks/subscriptionSync.ts +49 -0
- package/langwatch/ee/billing/nurturing/hooks/subscriptionSync.unit.test.ts +170 -0
- package/langwatch/ee/billing/nurturing/hooks/userSync.ts +143 -0
- package/langwatch/ee/billing/nurturing/hooks/userSync.unit.test.ts +307 -0
- package/langwatch/ee/billing/nurturing/nurturing.service.ts +179 -0
- package/langwatch/ee/billing/nurturing/nurturing.service.unit.test.ts +266 -0
- package/langwatch/ee/billing/nurturing/nurturing.service.wiring.unit.test.ts +109 -0
- package/langwatch/ee/billing/nurturing/types.ts +125 -0
- package/langwatch/ee/billing/planLimits.ts +190 -0
- package/langwatch/ee/billing/planProvider.ts +119 -0
- package/langwatch/ee/billing/planTypes.ts +53 -0
- package/langwatch/ee/billing/pricing.ts +57 -0
- package/langwatch/ee/billing/services/billableEventsQuery.ts +248 -0
- package/langwatch/ee/billing/services/customerService.ts +90 -0
- package/langwatch/ee/billing/services/licensePurchaseHandler.ts +73 -0
- package/langwatch/ee/billing/services/seatEventSubscription.ts +351 -0
- package/langwatch/ee/billing/services/seatSyncService.ts +34 -0
- package/langwatch/ee/billing/services/subscription.repository.ts +203 -0
- package/langwatch/ee/billing/services/subscription.service.ts +549 -0
- package/langwatch/ee/billing/services/subscriptionItemCalculator.ts +249 -0
- package/langwatch/ee/billing/services/usageReportingService.ts +301 -0
- package/langwatch/ee/billing/services/webhookService.ts +858 -0
- package/langwatch/ee/billing/stripe/stripeCatalog.json +1781 -0
- package/langwatch/ee/billing/stripe/stripeClient.ts +13 -0
- package/langwatch/ee/billing/stripe/stripePriceCatalog.ts +70 -0
- package/langwatch/ee/billing/stripe/stripePrices.types.ts +118 -0
- package/langwatch/ee/billing/subscriptionRouter.ts +202 -0
- package/langwatch/ee/billing/types.ts +102 -0
- package/langwatch/ee/billing/utils/currency.ts +99 -0
- package/langwatch/ee/billing/utils/growthSeatEvent.ts +137 -0
- package/langwatch/ee/licensing/__tests__/constants.unit.test.ts +56 -0
- package/langwatch/ee/licensing/__tests__/defaults.unit.test.ts +192 -0
- package/langwatch/ee/licensing/__tests__/fixtures/testKeys.ts +86 -0
- package/langwatch/ee/licensing/__tests__/fixtures/testLicenses.ts +92 -0
- package/langwatch/ee/licensing/__tests__/licenseGenerationService.unit.test.ts +232 -0
- package/langwatch/ee/licensing/__tests__/licenseHandler.integration.test.ts +419 -0
- package/langwatch/ee/licensing/__tests__/planMapping.unit.test.ts +195 -0
- package/langwatch/ee/licensing/__tests__/planTemplates.unit.test.ts +193 -0
- package/langwatch/ee/licensing/__tests__/signing.unit.test.ts +164 -0
- package/langwatch/ee/licensing/__tests__/validation.unit.test.ts +281 -0
- package/langwatch/ee/licensing/client.ts +29 -0
- package/langwatch/ee/licensing/constants.ts +195 -0
- package/langwatch/ee/licensing/defaults.ts +55 -0
- package/langwatch/ee/licensing/errors.ts +11 -0
- package/langwatch/ee/licensing/index.ts +52 -0
- package/langwatch/ee/licensing/licenseGenerationService.ts +87 -0
- package/langwatch/ee/licensing/licenseHandler.ts +308 -0
- package/langwatch/ee/licensing/planInfo.ts +46 -0
- package/langwatch/ee/licensing/planMapping.ts +37 -0
- package/langwatch/ee/licensing/planTemplates.ts +109 -0
- package/langwatch/ee/licensing/server.ts +43 -0
- package/langwatch/ee/licensing/signing.ts +43 -0
- package/langwatch/ee/licensing/types.ts +156 -0
- package/langwatch/ee/licensing/validation.ts +107 -0
- package/langwatch/ee/managed-providers/ManagedModelProviderAlert.tsx +29 -0
- package/langwatch/ee/managed-providers/managedBedrockConfig.ts +226 -0
- package/langwatch/ee/saas/ExtraFooterComponents.tsx +169 -0
- package/langwatch/package.json +1 -1
- package/langwatch_nlp/uv.lock +179 -5
- package/package.json +1 -1
- package/packages/server/dist/cli.cjs +192 -142
|
@@ -0,0 +1,549 @@
|
|
|
1
|
+
import { Currency, type OrganizationUserRole, type PrismaClient } from "@prisma/client";
|
|
2
|
+
import type Stripe from "stripe";
|
|
3
|
+
import type { DisplayInvoice, SubscriptionService } from "../../../src/server/app-layer/subscription/subscription.service";
|
|
4
|
+
import type { SubscriptionRepository } from "../../../src/server/app-layer/subscription/subscription.repository";
|
|
5
|
+
import type { OrganizationRepository } from "../../../src/server/app-layer/organizations/repositories/organization.repository";
|
|
6
|
+
import { PrismaOrganizationRepository } from "../../../src/server/app-layer/organizations/repositories/organization.prisma.repository";
|
|
7
|
+
import { traced } from "../../../src/server/app-layer/tracing";
|
|
8
|
+
import { getApp } from "../../../src/server/app-layer/app";
|
|
9
|
+
import {
|
|
10
|
+
type PlanTypes as PlanType,
|
|
11
|
+
PlanTypes,
|
|
12
|
+
SubscriptionStatus,
|
|
13
|
+
} from "../planTypes";
|
|
14
|
+
import type { StripePriceName } from "../stripe/stripePrices.types";
|
|
15
|
+
import type {
|
|
16
|
+
createItemsToAdd,
|
|
17
|
+
getItemsToUpdate,
|
|
18
|
+
prices,
|
|
19
|
+
} from "./subscriptionItemCalculator";
|
|
20
|
+
import { isStripePriceName, stripePricesFile } from "../stripe/stripePriceCatalog";
|
|
21
|
+
import {
|
|
22
|
+
InvalidPlanError,
|
|
23
|
+
OrganizationNotFoundError,
|
|
24
|
+
SeatBillingUnavailableError,
|
|
25
|
+
SubscriptionCreationFailedError,
|
|
26
|
+
} from "../errors";
|
|
27
|
+
import { isGrowthSeatEventPlan, type BillingInterval } from "../utils/growthSeatEvent";
|
|
28
|
+
import type { SeatEventSubscriptionFns } from "./seatEventSubscription";
|
|
29
|
+
import { PrismaSubscriptionRepository } from "./subscription.repository";
|
|
30
|
+
|
|
31
|
+
export const RECENT_INVOICES_LIMIT = 4;
|
|
32
|
+
|
|
33
|
+
type ItemCalculator = {
|
|
34
|
+
getItemsToUpdate: typeof getItemsToUpdate;
|
|
35
|
+
createItemsToAdd: typeof createItemsToAdd;
|
|
36
|
+
prices: typeof prices;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* EE (SaaS) implementation of {@link SubscriptionService}.
|
|
41
|
+
* Manages Stripe subscription lifecycle: checkouts, upgrades, cancellations, billing portal.
|
|
42
|
+
*
|
|
43
|
+
* Uses {@link SubscriptionRepository} for subscription-table operations and
|
|
44
|
+
* {@link OrganizationRepository} for organization lookups via interface-based DI.
|
|
45
|
+
*/
|
|
46
|
+
export class EESubscriptionService implements SubscriptionService {
|
|
47
|
+
private readonly prisma: PrismaClient;
|
|
48
|
+
private readonly repository: SubscriptionRepository;
|
|
49
|
+
private readonly stripe: Stripe;
|
|
50
|
+
private readonly itemCalculator: ItemCalculator;
|
|
51
|
+
private readonly organizationRepository: OrganizationRepository;
|
|
52
|
+
private readonly seatEventFns?: SeatEventSubscriptionFns;
|
|
53
|
+
|
|
54
|
+
constructor({
|
|
55
|
+
prisma,
|
|
56
|
+
repository,
|
|
57
|
+
stripe,
|
|
58
|
+
itemCalculator,
|
|
59
|
+
organizationRepository,
|
|
60
|
+
seatEventFns,
|
|
61
|
+
}: {
|
|
62
|
+
prisma: PrismaClient;
|
|
63
|
+
repository: SubscriptionRepository;
|
|
64
|
+
stripe: Stripe;
|
|
65
|
+
itemCalculator: ItemCalculator;
|
|
66
|
+
organizationRepository: OrganizationRepository;
|
|
67
|
+
seatEventFns?: SeatEventSubscriptionFns;
|
|
68
|
+
}) {
|
|
69
|
+
this.prisma = prisma;
|
|
70
|
+
this.repository = repository;
|
|
71
|
+
this.stripe = stripe;
|
|
72
|
+
this.itemCalculator = itemCalculator;
|
|
73
|
+
this.organizationRepository = organizationRepository;
|
|
74
|
+
this.seatEventFns = seatEventFns;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Factory method that wires up the Prisma repository automatically.
|
|
79
|
+
*/
|
|
80
|
+
static create({
|
|
81
|
+
stripe,
|
|
82
|
+
db,
|
|
83
|
+
itemCalculator,
|
|
84
|
+
seatEventFns,
|
|
85
|
+
}: {
|
|
86
|
+
stripe: Stripe;
|
|
87
|
+
db: PrismaClient;
|
|
88
|
+
itemCalculator: ItemCalculator;
|
|
89
|
+
seatEventFns?: SeatEventSubscriptionFns;
|
|
90
|
+
}): SubscriptionService {
|
|
91
|
+
const repository = new PrismaSubscriptionRepository(db);
|
|
92
|
+
const organizationRepository = new PrismaOrganizationRepository(db);
|
|
93
|
+
return traced(
|
|
94
|
+
new EESubscriptionService({
|
|
95
|
+
prisma: db,
|
|
96
|
+
repository,
|
|
97
|
+
stripe,
|
|
98
|
+
itemCalculator,
|
|
99
|
+
organizationRepository,
|
|
100
|
+
seatEventFns,
|
|
101
|
+
}),
|
|
102
|
+
"EESubscriptionService",
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async getLastNonCancelledSubscription(organizationId: string) {
|
|
107
|
+
return await this.repository.findLastNonCancelled(organizationId);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async updateSubscriptionItems({
|
|
111
|
+
organizationId,
|
|
112
|
+
plan,
|
|
113
|
+
upgradeMembers,
|
|
114
|
+
upgradeTraces,
|
|
115
|
+
totalMembers,
|
|
116
|
+
totalTraces,
|
|
117
|
+
}: {
|
|
118
|
+
organizationId: string;
|
|
119
|
+
plan: PlanType;
|
|
120
|
+
upgradeMembers: boolean;
|
|
121
|
+
upgradeTraces: boolean;
|
|
122
|
+
totalMembers: number;
|
|
123
|
+
totalTraces: number;
|
|
124
|
+
}): Promise<{ success: boolean }> {
|
|
125
|
+
const effectiveMembers = upgradeMembers ? totalMembers : 0;
|
|
126
|
+
const effectiveTraces = upgradeTraces ? totalTraces : 0;
|
|
127
|
+
|
|
128
|
+
if (this.seatEventFns) {
|
|
129
|
+
const pricingModel =
|
|
130
|
+
await this.organizationRepository.getPricingModel(organizationId);
|
|
131
|
+
if (pricingModel === "SEAT_EVENT") {
|
|
132
|
+
return this.seatEventFns.updateSeatEventItems({
|
|
133
|
+
organizationId,
|
|
134
|
+
totalMembers: effectiveMembers,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const lastSubscription =
|
|
140
|
+
await this.repository.findLastNonCancelled(organizationId);
|
|
141
|
+
|
|
142
|
+
if (
|
|
143
|
+
lastSubscription &&
|
|
144
|
+
lastSubscription.stripeSubscriptionId &&
|
|
145
|
+
lastSubscription.status !== SubscriptionStatus.PENDING
|
|
146
|
+
) {
|
|
147
|
+
const subscription = await this.stripe.subscriptions.retrieve(
|
|
148
|
+
lastSubscription.stripeSubscriptionId,
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
const currentItems = subscription.items.data;
|
|
152
|
+
|
|
153
|
+
const itemsToUpdate = this.itemCalculator.getItemsToUpdate({
|
|
154
|
+
currentItems,
|
|
155
|
+
plan,
|
|
156
|
+
tracesToAdd: effectiveTraces,
|
|
157
|
+
membersToAdd: effectiveMembers,
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
await this.stripe.subscriptions.update(
|
|
161
|
+
lastSubscription.stripeSubscriptionId,
|
|
162
|
+
{ items: itemsToUpdate },
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
return { success: true };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return { success: false };
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
async createOrUpdateSubscription({
|
|
172
|
+
organizationId,
|
|
173
|
+
baseUrl,
|
|
174
|
+
plan,
|
|
175
|
+
membersToAdd = 0,
|
|
176
|
+
tracesToAdd = 0,
|
|
177
|
+
customerId,
|
|
178
|
+
currency,
|
|
179
|
+
billingInterval,
|
|
180
|
+
}: {
|
|
181
|
+
organizationId: string;
|
|
182
|
+
baseUrl: string;
|
|
183
|
+
plan: PlanType;
|
|
184
|
+
membersToAdd?: number;
|
|
185
|
+
tracesToAdd?: number;
|
|
186
|
+
customerId: string;
|
|
187
|
+
currency?: Currency;
|
|
188
|
+
billingInterval?: BillingInterval;
|
|
189
|
+
}): Promise<{ url: string | null }> {
|
|
190
|
+
if (isGrowthSeatEventPlan(plan) && this.seatEventFns) {
|
|
191
|
+
const pricingModel =
|
|
192
|
+
await this.organizationRepository.getPricingModel(organizationId);
|
|
193
|
+
|
|
194
|
+
return this.seatEventFns.createSeatEventCheckout({
|
|
195
|
+
organizationId,
|
|
196
|
+
customerId,
|
|
197
|
+
baseUrl,
|
|
198
|
+
currency: currency ?? Currency.EUR,
|
|
199
|
+
billingInterval: billingInterval ?? "monthly",
|
|
200
|
+
membersToAdd,
|
|
201
|
+
isUpgradeFromTiered: pricingModel === "TIERED",
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const lastSubscription =
|
|
206
|
+
await this.repository.findLastNonCancelled(organizationId);
|
|
207
|
+
|
|
208
|
+
if (
|
|
209
|
+
lastSubscription &&
|
|
210
|
+
lastSubscription.stripeSubscriptionId &&
|
|
211
|
+
lastSubscription.status !== SubscriptionStatus.PENDING
|
|
212
|
+
) {
|
|
213
|
+
if (plan === PlanTypes.FREE) {
|
|
214
|
+
return this.cancelSubscription({
|
|
215
|
+
stripeSubscriptionId: lastSubscription.stripeSubscriptionId,
|
|
216
|
+
subscriptionId: lastSubscription.id,
|
|
217
|
+
baseUrl,
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
return this.upgradeSubscription({
|
|
222
|
+
stripeSubscriptionId: lastSubscription.stripeSubscriptionId,
|
|
223
|
+
subscriptionId: lastSubscription.id,
|
|
224
|
+
plan,
|
|
225
|
+
tracesToAdd,
|
|
226
|
+
membersToAdd,
|
|
227
|
+
baseUrl,
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (plan === PlanTypes.FREE) {
|
|
232
|
+
return { url: `${baseUrl}/settings/subscription` };
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return this.createNewCheckout({
|
|
236
|
+
organizationId,
|
|
237
|
+
plan,
|
|
238
|
+
tracesToAdd,
|
|
239
|
+
membersToAdd,
|
|
240
|
+
customerId,
|
|
241
|
+
baseUrl,
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
async createBillingPortalSession({
|
|
246
|
+
customerId,
|
|
247
|
+
baseUrl,
|
|
248
|
+
organizationId,
|
|
249
|
+
}: {
|
|
250
|
+
customerId: string;
|
|
251
|
+
baseUrl: string;
|
|
252
|
+
organizationId: string;
|
|
253
|
+
}): Promise<{ url: string }> {
|
|
254
|
+
if (this.seatEventFns) {
|
|
255
|
+
const pricingModel =
|
|
256
|
+
await this.organizationRepository.getPricingModel(organizationId);
|
|
257
|
+
if (pricingModel === "SEAT_EVENT") {
|
|
258
|
+
return this.seatEventFns.seatEventBillingPortalUrl({
|
|
259
|
+
customerId,
|
|
260
|
+
baseUrl,
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const session = await this.stripe.billingPortal.sessions.create({
|
|
266
|
+
customer: customerId,
|
|
267
|
+
return_url: `${baseUrl}/settings/subscription`,
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
return { url: session.url };
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
async previewProration({
|
|
274
|
+
organizationId,
|
|
275
|
+
newTotalSeats,
|
|
276
|
+
}: {
|
|
277
|
+
organizationId: string;
|
|
278
|
+
newTotalSeats: number;
|
|
279
|
+
}): Promise<{
|
|
280
|
+
formattedAmountDue: string;
|
|
281
|
+
formattedRecurringTotal: string;
|
|
282
|
+
billingInterval: string;
|
|
283
|
+
}> {
|
|
284
|
+
if (!this.seatEventFns) {
|
|
285
|
+
throw new SeatBillingUnavailableError();
|
|
286
|
+
}
|
|
287
|
+
return this.seatEventFns.previewProration({ organizationId, newTotalSeats });
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
async createSubscriptionWithInvites({
|
|
291
|
+
organizationId,
|
|
292
|
+
baseUrl,
|
|
293
|
+
membersToAdd,
|
|
294
|
+
customerId,
|
|
295
|
+
currency,
|
|
296
|
+
billingInterval,
|
|
297
|
+
invites,
|
|
298
|
+
}: {
|
|
299
|
+
organizationId: string;
|
|
300
|
+
baseUrl: string;
|
|
301
|
+
membersToAdd: number;
|
|
302
|
+
customerId: string;
|
|
303
|
+
currency?: Currency;
|
|
304
|
+
billingInterval?: BillingInterval;
|
|
305
|
+
invites: Array<{ email: string; role: OrganizationUserRole }>;
|
|
306
|
+
}): Promise<{ url: string | null }> {
|
|
307
|
+
if (!this.seatEventFns) {
|
|
308
|
+
throw new SeatBillingUnavailableError();
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// TECH-DEBT: extract to TeamRepository
|
|
312
|
+
const firstTeam = await this.prisma.team.findFirst({
|
|
313
|
+
where: { organizationId },
|
|
314
|
+
orderBy: { createdAt: "asc" },
|
|
315
|
+
select: { id: true },
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
const teamIds = firstTeam?.id ?? "";
|
|
319
|
+
|
|
320
|
+
const pricingModel =
|
|
321
|
+
await this.organizationRepository.getPricingModel(organizationId);
|
|
322
|
+
|
|
323
|
+
return this.seatEventFns.createSeatEventCheckout({
|
|
324
|
+
organizationId,
|
|
325
|
+
customerId,
|
|
326
|
+
baseUrl,
|
|
327
|
+
currency: currency ?? Currency.EUR,
|
|
328
|
+
billingInterval: billingInterval ?? "monthly",
|
|
329
|
+
membersToAdd,
|
|
330
|
+
isUpgradeFromTiered: pricingModel === "TIERED",
|
|
331
|
+
invites: invites.map((inv) => ({
|
|
332
|
+
email: inv.email,
|
|
333
|
+
role: inv.role,
|
|
334
|
+
teamIds,
|
|
335
|
+
})),
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
async notifyProspective({
|
|
340
|
+
organizationId,
|
|
341
|
+
plan,
|
|
342
|
+
customerName,
|
|
343
|
+
customerEmail,
|
|
344
|
+
note,
|
|
345
|
+
actorEmail,
|
|
346
|
+
}: {
|
|
347
|
+
organizationId: string;
|
|
348
|
+
plan: PlanType;
|
|
349
|
+
customerName?: string;
|
|
350
|
+
customerEmail?: string;
|
|
351
|
+
note?: string;
|
|
352
|
+
actorEmail: string;
|
|
353
|
+
}): Promise<{ success: boolean }> {
|
|
354
|
+
const organization =
|
|
355
|
+
await this.organizationRepository.findNameById(organizationId);
|
|
356
|
+
|
|
357
|
+
if (!organization) {
|
|
358
|
+
throw new OrganizationNotFoundError();
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
await getApp().notifications.sendSlackSubscriptionEvent({
|
|
362
|
+
type: "prospective",
|
|
363
|
+
organizationId: organization.id,
|
|
364
|
+
organizationName: organization.name,
|
|
365
|
+
plan,
|
|
366
|
+
customerName,
|
|
367
|
+
customerEmail: customerEmail ?? actorEmail,
|
|
368
|
+
note,
|
|
369
|
+
actorEmail,
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
return { success: true };
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
async listInvoices({
|
|
376
|
+
organizationId,
|
|
377
|
+
}: {
|
|
378
|
+
organizationId: string;
|
|
379
|
+
}): Promise<DisplayInvoice[]> {
|
|
380
|
+
const stripeCustomerId = await this.organizationRepository.getStripeCustomerId(organizationId);
|
|
381
|
+
|
|
382
|
+
if (!stripeCustomerId) {
|
|
383
|
+
return [];
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
const invoices = await this.stripe.invoices.list({
|
|
387
|
+
customer: stripeCustomerId,
|
|
388
|
+
limit: RECENT_INVOICES_LIMIT,
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
return invoices.data
|
|
392
|
+
.filter((inv) => inv.status !== "draft")
|
|
393
|
+
.map((inv) => ({
|
|
394
|
+
id: inv.id,
|
|
395
|
+
number: inv.number ?? null,
|
|
396
|
+
date: inv.created,
|
|
397
|
+
amountDue: inv.amount_due,
|
|
398
|
+
currency: inv.currency,
|
|
399
|
+
status: inv.status ?? "unknown",
|
|
400
|
+
pdfUrl: inv.invoice_pdf ?? null,
|
|
401
|
+
hostedUrl: inv.hosted_invoice_url ?? null,
|
|
402
|
+
}));
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// ---------------------------------------------------------------------------
|
|
406
|
+
// Private helpers extracted from createOrUpdateSubscription
|
|
407
|
+
// ---------------------------------------------------------------------------
|
|
408
|
+
|
|
409
|
+
private async cancelSubscription({
|
|
410
|
+
stripeSubscriptionId,
|
|
411
|
+
subscriptionId,
|
|
412
|
+
baseUrl,
|
|
413
|
+
}: {
|
|
414
|
+
stripeSubscriptionId: string;
|
|
415
|
+
subscriptionId: string;
|
|
416
|
+
baseUrl: string;
|
|
417
|
+
}): Promise<{ url: string | null }> {
|
|
418
|
+
const response = await this.stripe.subscriptions.cancel(
|
|
419
|
+
stripeSubscriptionId,
|
|
420
|
+
);
|
|
421
|
+
|
|
422
|
+
if (response.status === "canceled") {
|
|
423
|
+
await this.repository.updateStatus({
|
|
424
|
+
id: subscriptionId,
|
|
425
|
+
status: SubscriptionStatus.CANCELLED,
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
return { url: `${baseUrl}/settings/subscription` };
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
private async upgradeSubscription({
|
|
433
|
+
stripeSubscriptionId,
|
|
434
|
+
subscriptionId,
|
|
435
|
+
plan,
|
|
436
|
+
tracesToAdd,
|
|
437
|
+
membersToAdd,
|
|
438
|
+
baseUrl,
|
|
439
|
+
}: {
|
|
440
|
+
stripeSubscriptionId: string;
|
|
441
|
+
subscriptionId: string;
|
|
442
|
+
plan: PlanType;
|
|
443
|
+
tracesToAdd: number;
|
|
444
|
+
membersToAdd: number;
|
|
445
|
+
baseUrl: string;
|
|
446
|
+
}): Promise<{ url: string | null }> {
|
|
447
|
+
const currentStripeSubscription = await this.stripe.subscriptions.retrieve(
|
|
448
|
+
stripeSubscriptionId,
|
|
449
|
+
);
|
|
450
|
+
|
|
451
|
+
const itemsToUpdate = this.itemCalculator.getItemsToUpdate({
|
|
452
|
+
currentItems: currentStripeSubscription.items.data,
|
|
453
|
+
plan,
|
|
454
|
+
tracesToAdd,
|
|
455
|
+
membersToAdd,
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
const response = await this.stripe.subscriptions.update(
|
|
459
|
+
stripeSubscriptionId,
|
|
460
|
+
{ items: itemsToUpdate },
|
|
461
|
+
);
|
|
462
|
+
|
|
463
|
+
if (response.status === "active") {
|
|
464
|
+
await this.repository.updatePlan({
|
|
465
|
+
id: subscriptionId,
|
|
466
|
+
plan,
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
return { url: `${baseUrl}/settings/subscription?success` };
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
private async createNewCheckout({
|
|
474
|
+
organizationId,
|
|
475
|
+
plan,
|
|
476
|
+
tracesToAdd,
|
|
477
|
+
membersToAdd,
|
|
478
|
+
customerId,
|
|
479
|
+
baseUrl,
|
|
480
|
+
}: {
|
|
481
|
+
organizationId: string;
|
|
482
|
+
plan: PlanType;
|
|
483
|
+
tracesToAdd: number;
|
|
484
|
+
membersToAdd: number;
|
|
485
|
+
customerId: string;
|
|
486
|
+
baseUrl: string;
|
|
487
|
+
}): Promise<{ url: string | null }> {
|
|
488
|
+
if (!isStripePriceName(plan as StripePriceName)) {
|
|
489
|
+
throw new InvalidPlanError(plan);
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
const itemsToAdd = this.itemCalculator.createItemsToAdd(
|
|
493
|
+
plan,
|
|
494
|
+
{ quantity: tracesToAdd },
|
|
495
|
+
{ quantity: membersToAdd },
|
|
496
|
+
);
|
|
497
|
+
|
|
498
|
+
itemsToAdd.push({
|
|
499
|
+
price: this.itemCalculator.prices[plan as StripePriceName],
|
|
500
|
+
quantity: 1,
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
const subscription = await this.repository.createPending({
|
|
504
|
+
organizationId,
|
|
505
|
+
plan,
|
|
506
|
+
});
|
|
507
|
+
|
|
508
|
+
if (!subscription) {
|
|
509
|
+
throw new SubscriptionCreationFailedError();
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
// Resolve the checkout currency from the base plan price to prevent
|
|
513
|
+
// Stripe Adaptive Pricing from offering unsupported currencies.
|
|
514
|
+
const SUPPORTED_CHECKOUT_CURRENCIES = ["usd", "eur"] as const;
|
|
515
|
+
const basePriceId = this.itemCalculator.prices[plan as StripePriceName];
|
|
516
|
+
const rawCurrency =
|
|
517
|
+
stripePricesFile.prices[basePriceId]?.currency?.toLowerCase();
|
|
518
|
+
const checkoutCurrency =
|
|
519
|
+
rawCurrency &&
|
|
520
|
+
SUPPORTED_CHECKOUT_CURRENCIES.includes(
|
|
521
|
+
rawCurrency as (typeof SUPPORTED_CHECKOUT_CURRENCIES)[number],
|
|
522
|
+
)
|
|
523
|
+
? rawCurrency
|
|
524
|
+
: "usd";
|
|
525
|
+
|
|
526
|
+
const session = await this.stripe.checkout.sessions.create({
|
|
527
|
+
mode: "subscription",
|
|
528
|
+
currency: checkoutCurrency,
|
|
529
|
+
...({ adaptive_pricing: { enabled: false } } as Record<string, unknown>),
|
|
530
|
+
customer: customerId,
|
|
531
|
+
customer_update: {
|
|
532
|
+
address: "auto",
|
|
533
|
+
name: "auto",
|
|
534
|
+
},
|
|
535
|
+
automatic_tax: { enabled: true },
|
|
536
|
+
billing_address_collection: "required",
|
|
537
|
+
tax_id_collection: {
|
|
538
|
+
enabled: true,
|
|
539
|
+
},
|
|
540
|
+
line_items: itemsToAdd,
|
|
541
|
+
success_url: `${baseUrl}/settings/subscription?success`,
|
|
542
|
+
cancel_url: `${baseUrl}/settings/subscription`,
|
|
543
|
+
client_reference_id: `subscription_setup_${subscription.id}`,
|
|
544
|
+
allow_promotion_codes: true,
|
|
545
|
+
});
|
|
546
|
+
|
|
547
|
+
return { url: session.url };
|
|
548
|
+
}
|
|
549
|
+
}
|