@forklaunch/implementation-billing-stripe 0.3.0 → 0.3.2
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/lib/domain/enum/index.d.mts +112 -119
- package/lib/domain/enum/index.d.ts +112 -119
- package/lib/domain/enum/index.js +119 -124
- package/lib/domain/enum/index.mjs +109 -109
- package/lib/domain/schemas/index.d.mts +323 -2908
- package/lib/domain/schemas/index.d.ts +323 -2908
- package/lib/domain/schemas/index.js +187 -257
- package/lib/domain/schemas/index.mjs +136 -131
- package/lib/domain/types/index.d.mts +135 -373
- package/lib/domain/types/index.d.ts +135 -373
- package/lib/domain/types/index.js +4 -8
- package/lib/eject/domain/schemas/checkoutSession.schema.ts +3 -0
- package/lib/eject/domain/types/stripe.dto.types.ts +4 -2
- package/lib/services/index.d.mts +148 -461
- package/lib/services/index.d.ts +148 -461
- package/lib/services/index.js +131 -229
- package/lib/services/index.mjs +103 -194
- package/package.json +9 -9
package/lib/services/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
var __defProp = Object.defineProperty;
|
|
3
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
@@ -8,18 +8,14 @@ var __export = (target, all) => {
|
|
|
8
8
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
9
|
};
|
|
10
10
|
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
12
|
for (let key of __getOwnPropNames(from))
|
|
13
13
|
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, {
|
|
15
|
-
get: () => from[key],
|
|
16
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
17
|
-
});
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
18
15
|
}
|
|
19
16
|
return to;
|
|
20
17
|
};
|
|
21
|
-
var __toCommonJS = (mod) =>
|
|
22
|
-
__copyProps(__defProp({}, '__esModule', { value: true }), mod);
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
23
19
|
|
|
24
20
|
// services/index.ts
|
|
25
21
|
var services_exports = {};
|
|
@@ -34,17 +30,9 @@ __export(services_exports, {
|
|
|
34
30
|
module.exports = __toCommonJS(services_exports);
|
|
35
31
|
|
|
36
32
|
// services/billingPortal.service.ts
|
|
37
|
-
var import_services = require(
|
|
33
|
+
var import_services = require("@forklaunch/implementation-billing-base/services");
|
|
38
34
|
var StripeBillingPortalService = class {
|
|
39
|
-
constructor(
|
|
40
|
-
stripeClient,
|
|
41
|
-
em,
|
|
42
|
-
cache,
|
|
43
|
-
openTelemetryCollector,
|
|
44
|
-
schemaValidator,
|
|
45
|
-
mappers,
|
|
46
|
-
options
|
|
47
|
-
) {
|
|
35
|
+
constructor(stripeClient, em, cache, openTelemetryCollector, schemaValidator, mappers, options) {
|
|
48
36
|
this.options = options;
|
|
49
37
|
this.stripeClient = stripeClient;
|
|
50
38
|
this.em = em;
|
|
@@ -52,15 +40,14 @@ var StripeBillingPortalService = class {
|
|
|
52
40
|
this.openTelemetryCollector = openTelemetryCollector;
|
|
53
41
|
this.schemaValidator = schemaValidator;
|
|
54
42
|
this.mappers = mappers;
|
|
55
|
-
this.baseBillingPortalService =
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
);
|
|
43
|
+
this.baseBillingPortalService = new import_services.BaseBillingPortalService(
|
|
44
|
+
em,
|
|
45
|
+
cache,
|
|
46
|
+
openTelemetryCollector,
|
|
47
|
+
schemaValidator,
|
|
48
|
+
mappers,
|
|
49
|
+
options
|
|
50
|
+
);
|
|
64
51
|
}
|
|
65
52
|
baseBillingPortalService;
|
|
66
53
|
stripeClient;
|
|
@@ -96,10 +83,9 @@ var StripeBillingPortalService = class {
|
|
|
96
83
|
return this.baseBillingPortalService.expireBillingPortalSession(idDto);
|
|
97
84
|
}
|
|
98
85
|
async updateBillingPortalSession(billingPortalDto, ...args) {
|
|
99
|
-
const existingSession =
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
});
|
|
86
|
+
const existingSession = await this.baseBillingPortalService.getBillingPortalSession({
|
|
87
|
+
id: billingPortalDto.id
|
|
88
|
+
});
|
|
103
89
|
const session = await this.stripeClient.billingPortal.sessions.create({
|
|
104
90
|
...billingPortalDto.stripeFields,
|
|
105
91
|
customer: existingSession.customerId
|
|
@@ -121,17 +107,9 @@ var StripeBillingPortalService = class {
|
|
|
121
107
|
};
|
|
122
108
|
|
|
123
109
|
// services/checkoutSession.service.ts
|
|
124
|
-
var import_services2 = require(
|
|
110
|
+
var import_services2 = require("@forklaunch/implementation-billing-base/services");
|
|
125
111
|
var StripeCheckoutSessionService = class {
|
|
126
|
-
constructor(
|
|
127
|
-
stripeClient,
|
|
128
|
-
em,
|
|
129
|
-
cache,
|
|
130
|
-
openTelemetryCollector,
|
|
131
|
-
schemaValidator,
|
|
132
|
-
mappers,
|
|
133
|
-
options
|
|
134
|
-
) {
|
|
112
|
+
constructor(stripeClient, em, cache, openTelemetryCollector, schemaValidator, mappers, options) {
|
|
135
113
|
this.options = options;
|
|
136
114
|
this.stripeClient = stripeClient;
|
|
137
115
|
this.em = em;
|
|
@@ -139,15 +117,14 @@ var StripeCheckoutSessionService = class {
|
|
|
139
117
|
this.openTelemetryCollector = openTelemetryCollector;
|
|
140
118
|
this.schemaValidator = schemaValidator;
|
|
141
119
|
this.mappers = mappers;
|
|
142
|
-
this.baseCheckoutSessionService =
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
);
|
|
120
|
+
this.baseCheckoutSessionService = new import_services2.BaseCheckoutSessionService(
|
|
121
|
+
em,
|
|
122
|
+
cache,
|
|
123
|
+
openTelemetryCollector,
|
|
124
|
+
schemaValidator,
|
|
125
|
+
mappers,
|
|
126
|
+
options
|
|
127
|
+
);
|
|
151
128
|
}
|
|
152
129
|
baseCheckoutSessionService;
|
|
153
130
|
stripeClient;
|
|
@@ -181,8 +158,7 @@ var StripeCheckoutSessionService = class {
|
|
|
181
158
|
const session = await this.stripeClient.checkout.sessions.retrieve(
|
|
182
159
|
idDto.id
|
|
183
160
|
);
|
|
184
|
-
const databaseCheckoutSession =
|
|
185
|
-
await this.baseCheckoutSessionService.getCheckoutSession(idDto);
|
|
161
|
+
const databaseCheckoutSession = await this.baseCheckoutSessionService.getCheckoutSession(idDto);
|
|
186
162
|
databaseCheckoutSession.stripeFields = session;
|
|
187
163
|
return databaseCheckoutSession;
|
|
188
164
|
}
|
|
@@ -193,7 +169,7 @@ var StripeCheckoutSessionService = class {
|
|
|
193
169
|
async handleCheckoutSuccess({ id }) {
|
|
194
170
|
await this.stripeClient.checkout.sessions.update(id, {
|
|
195
171
|
metadata: {
|
|
196
|
-
status:
|
|
172
|
+
status: "SUCCESS"
|
|
197
173
|
}
|
|
198
174
|
});
|
|
199
175
|
await this.baseCheckoutSessionService.handleCheckoutSuccess({ id });
|
|
@@ -201,7 +177,7 @@ var StripeCheckoutSessionService = class {
|
|
|
201
177
|
async handleCheckoutFailure({ id }) {
|
|
202
178
|
await this.stripeClient.checkout.sessions.update(id, {
|
|
203
179
|
metadata: {
|
|
204
|
-
status:
|
|
180
|
+
status: "FAILED"
|
|
205
181
|
}
|
|
206
182
|
});
|
|
207
183
|
await this.baseCheckoutSessionService.handleCheckoutFailure({ id });
|
|
@@ -209,17 +185,9 @@ var StripeCheckoutSessionService = class {
|
|
|
209
185
|
};
|
|
210
186
|
|
|
211
187
|
// services/paymentLink.service.ts
|
|
212
|
-
var import_services3 = require(
|
|
188
|
+
var import_services3 = require("@forklaunch/implementation-billing-base/services");
|
|
213
189
|
var StripePaymentLinkService = class {
|
|
214
|
-
constructor(
|
|
215
|
-
stripeClient,
|
|
216
|
-
em,
|
|
217
|
-
cache,
|
|
218
|
-
openTelemetryCollector,
|
|
219
|
-
schemaValidator,
|
|
220
|
-
mappers,
|
|
221
|
-
options
|
|
222
|
-
) {
|
|
190
|
+
constructor(stripeClient, em, cache, openTelemetryCollector, schemaValidator, mappers, options) {
|
|
223
191
|
this.options = options;
|
|
224
192
|
this.stripeClient = stripeClient;
|
|
225
193
|
this.em = em;
|
|
@@ -253,11 +221,10 @@ var StripePaymentLinkService = class {
|
|
|
253
221
|
{
|
|
254
222
|
...paymentLinkDto,
|
|
255
223
|
id: session.id,
|
|
256
|
-
amount:
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
) ?? 0
|
|
224
|
+
amount: session.line_items?.data.reduce(
|
|
225
|
+
(total, item) => total + item.amount_total,
|
|
226
|
+
0
|
|
227
|
+
) ?? 0
|
|
261
228
|
},
|
|
262
229
|
this.em,
|
|
263
230
|
session,
|
|
@@ -279,11 +246,10 @@ var StripePaymentLinkService = class {
|
|
|
279
246
|
{
|
|
280
247
|
...paymentLinkDto,
|
|
281
248
|
id: session.id,
|
|
282
|
-
amount:
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
) ?? 0
|
|
249
|
+
amount: session.line_items?.data.reduce(
|
|
250
|
+
(total, item) => total + item.amount_total,
|
|
251
|
+
0
|
|
252
|
+
) ?? 0
|
|
287
253
|
},
|
|
288
254
|
this.em,
|
|
289
255
|
session
|
|
@@ -295,15 +261,14 @@ var StripePaymentLinkService = class {
|
|
|
295
261
|
}
|
|
296
262
|
async getPaymentLink({ id }) {
|
|
297
263
|
const stripePaymentLink = await this.stripeClient.paymentLinks.retrieve(id);
|
|
298
|
-
const databasePaymentLink =
|
|
299
|
-
await this.basePaymentLinkService.getPaymentLink({ id });
|
|
264
|
+
const databasePaymentLink = await this.basePaymentLinkService.getPaymentLink({ id });
|
|
300
265
|
databasePaymentLink.stripeFields = stripePaymentLink;
|
|
301
266
|
return databasePaymentLink;
|
|
302
267
|
}
|
|
303
268
|
async expirePaymentLink({ id }) {
|
|
304
269
|
await this.stripeClient.paymentLinks.update(id, {
|
|
305
270
|
metadata: {
|
|
306
|
-
status:
|
|
271
|
+
status: "EXPIRED"
|
|
307
272
|
}
|
|
308
273
|
});
|
|
309
274
|
await this.basePaymentLinkService.expirePaymentLink({ id });
|
|
@@ -311,7 +276,7 @@ var StripePaymentLinkService = class {
|
|
|
311
276
|
async handlePaymentSuccess({ id }) {
|
|
312
277
|
await this.stripeClient.paymentLinks.update(id, {
|
|
313
278
|
metadata: {
|
|
314
|
-
status:
|
|
279
|
+
status: "COMPLETED"
|
|
315
280
|
}
|
|
316
281
|
});
|
|
317
282
|
await this.basePaymentLinkService.handlePaymentSuccess({ id });
|
|
@@ -319,7 +284,7 @@ var StripePaymentLinkService = class {
|
|
|
319
284
|
async handlePaymentFailure({ id }) {
|
|
320
285
|
await this.stripeClient.paymentLinks.update(id, {
|
|
321
286
|
metadata: {
|
|
322
|
-
status:
|
|
287
|
+
status: "FAILED"
|
|
323
288
|
}
|
|
324
289
|
});
|
|
325
290
|
await this.basePaymentLinkService.handlePaymentFailure({ id });
|
|
@@ -328,8 +293,7 @@ var StripePaymentLinkService = class {
|
|
|
328
293
|
const stripePaymentLinks = await this.stripeClient.paymentLinks.list({
|
|
329
294
|
active: true
|
|
330
295
|
});
|
|
331
|
-
const databasePaymentLinks =
|
|
332
|
-
await this.basePaymentLinkService.listPaymentLinks(idsDto);
|
|
296
|
+
const databasePaymentLinks = await this.basePaymentLinkService.listPaymentLinks(idsDto);
|
|
333
297
|
return await Promise.all(
|
|
334
298
|
databasePaymentLinks.map(async (paymentLink) => {
|
|
335
299
|
const stripePaymentLink = stripePaymentLinks.data.find(
|
|
@@ -348,16 +312,9 @@ var StripePaymentLinkService = class {
|
|
|
348
312
|
};
|
|
349
313
|
|
|
350
314
|
// services/plan.service.ts
|
|
351
|
-
var import_services4 = require(
|
|
315
|
+
var import_services4 = require("@forklaunch/implementation-billing-base/services");
|
|
352
316
|
var StripePlanService = class {
|
|
353
|
-
constructor(
|
|
354
|
-
stripeClient,
|
|
355
|
-
em,
|
|
356
|
-
openTelemetryCollector,
|
|
357
|
-
schemaValidator,
|
|
358
|
-
mappers,
|
|
359
|
-
options
|
|
360
|
-
) {
|
|
317
|
+
constructor(stripeClient, em, openTelemetryCollector, schemaValidator, mappers, options) {
|
|
361
318
|
this.options = options;
|
|
362
319
|
this.stripeClient = stripeClient;
|
|
363
320
|
this.em = em;
|
|
@@ -389,7 +346,7 @@ var StripePlanService = class {
|
|
|
389
346
|
{
|
|
390
347
|
...planDto,
|
|
391
348
|
externalId: stripePlan.id,
|
|
392
|
-
billingProvider:
|
|
349
|
+
billingProvider: "stripe"
|
|
393
350
|
},
|
|
394
351
|
em ?? this.em,
|
|
395
352
|
stripePlan
|
|
@@ -398,13 +355,12 @@ var StripePlanService = class {
|
|
|
398
355
|
}
|
|
399
356
|
async getPlan(idDto, em) {
|
|
400
357
|
const plan = await this.stripeClient.plans.retrieve(idDto.id);
|
|
401
|
-
const id = (
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
)?.id;
|
|
358
|
+
const id = (await em?.findOne(
|
|
359
|
+
this.options?.databaseTableName ?? "plan",
|
|
360
|
+
{ externalId: idDto.id }
|
|
361
|
+
))?.id;
|
|
406
362
|
if (!id) {
|
|
407
|
-
throw new Error(
|
|
363
|
+
throw new Error("Plan not found");
|
|
408
364
|
}
|
|
409
365
|
const planEntity = await this.basePlanService.getPlan({ id }, em);
|
|
410
366
|
planEntity.stripeFields = plan;
|
|
@@ -412,8 +368,8 @@ var StripePlanService = class {
|
|
|
412
368
|
}
|
|
413
369
|
async updatePlan(planDto, em) {
|
|
414
370
|
const existingPlan = await this.stripeClient.plans.retrieve(planDto.id);
|
|
415
|
-
const plan = await this.stripeClient.plans.del(planDto.id).then(
|
|
416
|
-
this.stripeClient.plans.create({
|
|
371
|
+
const plan = await this.stripeClient.plans.del(planDto.id).then(
|
|
372
|
+
() => this.stripeClient.plans.create({
|
|
417
373
|
...planDto.stripeFields,
|
|
418
374
|
interval: planDto.cadence ?? existingPlan.interval,
|
|
419
375
|
product: planDto.name,
|
|
@@ -425,7 +381,7 @@ var StripePlanService = class {
|
|
|
425
381
|
{
|
|
426
382
|
...planDto,
|
|
427
383
|
externalId: plan.id,
|
|
428
|
-
billingProvider:
|
|
384
|
+
billingProvider: "stripe"
|
|
429
385
|
},
|
|
430
386
|
em ?? this.em,
|
|
431
387
|
plan
|
|
@@ -443,15 +399,12 @@ var StripePlanService = class {
|
|
|
443
399
|
const plans = await this.stripeClient.plans.list({
|
|
444
400
|
active: true
|
|
445
401
|
});
|
|
446
|
-
const planIds = (
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
)
|
|
451
|
-
?.filter((s) => idsDto?.ids?.includes(s.id))
|
|
452
|
-
?.map((s) => s.id);
|
|
402
|
+
const planIds = (await em?.findAll(
|
|
403
|
+
this.options?.databaseTableName ?? "plan",
|
|
404
|
+
{ where: { externalId: { $in: plans.data.map((plan) => plan.id) } } }
|
|
405
|
+
))?.filter((s) => idsDto?.ids?.includes(s.id))?.map((s) => s.id);
|
|
453
406
|
if (!planIds) {
|
|
454
|
-
throw new Error(
|
|
407
|
+
throw new Error("Plans not found");
|
|
455
408
|
}
|
|
456
409
|
return await Promise.all(
|
|
457
410
|
(await this.basePlanService.listPlans({ ids: planIds }, em)).map(
|
|
@@ -467,16 +420,9 @@ var StripePlanService = class {
|
|
|
467
420
|
};
|
|
468
421
|
|
|
469
422
|
// services/subscription.service.ts
|
|
470
|
-
var import_services5 = require(
|
|
423
|
+
var import_services5 = require("@forklaunch/implementation-billing-base/services");
|
|
471
424
|
var StripeSubscriptionService = class {
|
|
472
|
-
constructor(
|
|
473
|
-
stripeClient,
|
|
474
|
-
em,
|
|
475
|
-
openTelemetryCollector,
|
|
476
|
-
schemaValidator,
|
|
477
|
-
mappers,
|
|
478
|
-
options
|
|
479
|
-
) {
|
|
425
|
+
constructor(stripeClient, em, openTelemetryCollector, schemaValidator, mappers, options) {
|
|
480
426
|
this.options = options;
|
|
481
427
|
this.stripeClient = stripeClient;
|
|
482
428
|
this.em = em;
|
|
@@ -511,15 +457,14 @@ var StripeSubscriptionService = class {
|
|
|
511
457
|
{
|
|
512
458
|
...subscriptionDto,
|
|
513
459
|
externalId: subscription.id,
|
|
514
|
-
billingProvider:
|
|
460
|
+
billingProvider: "stripe"
|
|
515
461
|
},
|
|
516
462
|
em ?? this.em,
|
|
517
463
|
subscription
|
|
518
464
|
);
|
|
519
465
|
}
|
|
520
466
|
async getSubscription(idDto, em) {
|
|
521
|
-
const subscriptionEntity =
|
|
522
|
-
await this.baseSubscriptionService.getSubscription(idDto, em);
|
|
467
|
+
const subscriptionEntity = await this.baseSubscriptionService.getSubscription(idDto, em);
|
|
523
468
|
const stripeSubscription = await this.stripeClient.subscriptions.retrieve(
|
|
524
469
|
idDto.id
|
|
525
470
|
);
|
|
@@ -527,8 +472,7 @@ var StripeSubscriptionService = class {
|
|
|
527
472
|
return subscriptionEntity;
|
|
528
473
|
}
|
|
529
474
|
async getUserSubscription(idDto, em) {
|
|
530
|
-
const subscriptionEntity =
|
|
531
|
-
await this.baseSubscriptionService.getUserSubscription(idDto, em);
|
|
475
|
+
const subscriptionEntity = await this.baseSubscriptionService.getUserSubscription(idDto, em);
|
|
532
476
|
const stripeSubscription = await this.stripeClient.subscriptions.retrieve(
|
|
533
477
|
idDto.id
|
|
534
478
|
);
|
|
@@ -536,19 +480,17 @@ var StripeSubscriptionService = class {
|
|
|
536
480
|
return subscriptionEntity;
|
|
537
481
|
}
|
|
538
482
|
async getOrganizationSubscription(idDto, em) {
|
|
539
|
-
const id = (
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
)?.id;
|
|
483
|
+
const id = (await em?.findOne(
|
|
484
|
+
this.options?.databaseTableName ?? "subscription",
|
|
485
|
+
{ externalId: idDto.id }
|
|
486
|
+
))?.id;
|
|
544
487
|
if (!id) {
|
|
545
|
-
throw new Error(
|
|
488
|
+
throw new Error("Subscription not found");
|
|
546
489
|
}
|
|
547
|
-
const subscriptionEntity =
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
);
|
|
490
|
+
const subscriptionEntity = await this.baseSubscriptionService.getOrganizationSubscription(
|
|
491
|
+
{ id },
|
|
492
|
+
em
|
|
493
|
+
);
|
|
552
494
|
const stripeSubscription = await this.stripeClient.subscriptions.retrieve(
|
|
553
495
|
idDto.id
|
|
554
496
|
);
|
|
@@ -571,7 +513,7 @@ var StripeSubscriptionService = class {
|
|
|
571
513
|
{
|
|
572
514
|
...subscriptionDto,
|
|
573
515
|
externalId: subscription.id,
|
|
574
|
-
billingProvider:
|
|
516
|
+
billingProvider: "stripe",
|
|
575
517
|
providerFields: subscription
|
|
576
518
|
},
|
|
577
519
|
em ?? this.em,
|
|
@@ -583,18 +525,15 @@ var StripeSubscriptionService = class {
|
|
|
583
525
|
await this.baseSubscriptionService.deleteSubscription(idDto, em);
|
|
584
526
|
}
|
|
585
527
|
async listSubscriptions(idsDto, em) {
|
|
586
|
-
const subscriptions = (
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
where: { externalId: { $in: subscriptions.map((s) => s.id) } }
|
|
594
|
-
})
|
|
595
|
-
)?.map((s) => s.id);
|
|
528
|
+
const subscriptions = (await this.stripeClient.subscriptions.list({
|
|
529
|
+
status: "active"
|
|
530
|
+
})).data.filter((s) => idsDto.ids?.includes(s.id));
|
|
531
|
+
const ids = (await em?.findAll(
|
|
532
|
+
this.options?.databaseTableName ?? "subscription",
|
|
533
|
+
{ where: { externalId: { $in: subscriptions.map((s) => s.id) } } }
|
|
534
|
+
))?.map((s) => s.id);
|
|
596
535
|
if (!ids) {
|
|
597
|
-
throw new Error(
|
|
536
|
+
throw new Error("Subscriptions not found");
|
|
598
537
|
}
|
|
599
538
|
return await Promise.all(
|
|
600
539
|
(await this.baseSubscriptionService.listSubscriptions({ ids }, em)).map(
|
|
@@ -620,7 +559,7 @@ var StripeSubscriptionService = class {
|
|
|
620
559
|
|
|
621
560
|
// domain/enum/billingProvider.enum.ts
|
|
622
561
|
var BillingProviderEnum = {
|
|
623
|
-
STRIPE:
|
|
562
|
+
STRIPE: "stripe"
|
|
624
563
|
};
|
|
625
564
|
|
|
626
565
|
// services/webhook.service.ts
|
|
@@ -634,17 +573,7 @@ var StripeWebhookService = class {
|
|
|
634
573
|
paymentLinkService;
|
|
635
574
|
planService;
|
|
636
575
|
subscriptionService;
|
|
637
|
-
constructor(
|
|
638
|
-
stripeClient,
|
|
639
|
-
em,
|
|
640
|
-
schemaValidator,
|
|
641
|
-
openTelemetryCollector,
|
|
642
|
-
billingPortalService,
|
|
643
|
-
checkoutSessionService,
|
|
644
|
-
paymentLinkService,
|
|
645
|
-
planService,
|
|
646
|
-
subscriptionService
|
|
647
|
-
) {
|
|
576
|
+
constructor(stripeClient, em, schemaValidator, openTelemetryCollector, billingPortalService, checkoutSessionService, paymentLinkService, planService, subscriptionService) {
|
|
648
577
|
this.stripeClient = stripeClient;
|
|
649
578
|
this.em = em;
|
|
650
579
|
this.schemaValidator = schemaValidator;
|
|
@@ -657,11 +586,11 @@ var StripeWebhookService = class {
|
|
|
657
586
|
}
|
|
658
587
|
async handleWebhookEvent(event) {
|
|
659
588
|
if (this.openTelemetryCollector) {
|
|
660
|
-
this.openTelemetryCollector.info(
|
|
589
|
+
this.openTelemetryCollector.info("Handling webhook event", event);
|
|
661
590
|
}
|
|
662
591
|
const eventType = event.type;
|
|
663
592
|
switch (eventType) {
|
|
664
|
-
case
|
|
593
|
+
case "billing_portal.session.created": {
|
|
665
594
|
this.billingPortalService.baseBillingPortalService.createBillingPortalSession(
|
|
666
595
|
{
|
|
667
596
|
id: event.data.object.id,
|
|
@@ -672,156 +601,130 @@ var StripeWebhookService = class {
|
|
|
672
601
|
);
|
|
673
602
|
break;
|
|
674
603
|
}
|
|
675
|
-
case
|
|
604
|
+
case "checkout.session.expired": {
|
|
676
605
|
this.checkoutSessionService.handleCheckoutFailure({
|
|
677
606
|
id: event.data.object.id
|
|
678
607
|
});
|
|
679
608
|
break;
|
|
680
609
|
}
|
|
681
|
-
case
|
|
610
|
+
case "checkout.session.completed": {
|
|
682
611
|
this.checkoutSessionService.handleCheckoutSuccess({
|
|
683
612
|
id: event.data.object.id
|
|
684
613
|
});
|
|
685
614
|
break;
|
|
686
615
|
}
|
|
687
|
-
case
|
|
616
|
+
case "payment_link.created":
|
|
688
617
|
{
|
|
689
618
|
this.paymentLinkService.basePaymentLinkService.createPaymentLink({
|
|
690
619
|
id: event.data.object.id,
|
|
691
|
-
amount:
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
) ?? 0,
|
|
620
|
+
amount: event.data.object.line_items?.data.reduce(
|
|
621
|
+
(total, item) => total + item.amount_total,
|
|
622
|
+
0
|
|
623
|
+
) ?? 0,
|
|
696
624
|
paymentMethods: event.data.object.payment_method_types,
|
|
697
|
-
status:
|
|
625
|
+
status: "CREATED",
|
|
698
626
|
currency: event.data.object.currency
|
|
699
627
|
});
|
|
700
628
|
}
|
|
701
629
|
break;
|
|
702
|
-
case
|
|
630
|
+
case "payment_link.updated": {
|
|
703
631
|
this.paymentLinkService.basePaymentLinkService.updatePaymentLink({
|
|
704
632
|
id: event.data.object.id,
|
|
705
|
-
amount:
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
) ?? 0,
|
|
633
|
+
amount: event.data.object.line_items?.data.reduce(
|
|
634
|
+
(total, item) => total + item.amount_total,
|
|
635
|
+
0
|
|
636
|
+
) ?? 0,
|
|
710
637
|
paymentMethods: event.data.object.payment_method_types,
|
|
711
|
-
status:
|
|
638
|
+
status: "UPDATED",
|
|
712
639
|
currency: event.data.object.currency
|
|
713
640
|
});
|
|
714
641
|
break;
|
|
715
642
|
}
|
|
716
|
-
case
|
|
717
|
-
if (
|
|
718
|
-
typeof event.data.object.product === 'object' &&
|
|
719
|
-
event.data.object.product != null &&
|
|
720
|
-
event.data.object.amount != null
|
|
721
|
-
) {
|
|
643
|
+
case "plan.created": {
|
|
644
|
+
if (typeof event.data.object.product === "object" && event.data.object.product != null && event.data.object.amount != null) {
|
|
722
645
|
this.planService.basePlanService.createPlan({
|
|
723
646
|
id: event.data.object.id,
|
|
724
647
|
billingProvider: BillingProviderEnum.STRIPE,
|
|
725
648
|
cadence: event.data.object.interval,
|
|
726
649
|
currency: event.data.object.currency,
|
|
727
650
|
active: true,
|
|
728
|
-
name:
|
|
729
|
-
typeof event.data.object.product === 'string'
|
|
730
|
-
? event.data.object.product
|
|
731
|
-
: event.data.object.product?.id,
|
|
651
|
+
name: typeof event.data.object.product === "string" ? event.data.object.product : event.data.object.product?.id,
|
|
732
652
|
price: event.data.object.amount,
|
|
733
653
|
externalId: event.data.object.id
|
|
734
654
|
});
|
|
735
655
|
} else {
|
|
736
|
-
throw new Error(
|
|
656
|
+
throw new Error("Invalid plan");
|
|
737
657
|
}
|
|
738
658
|
break;
|
|
739
659
|
}
|
|
740
|
-
case
|
|
741
|
-
if (
|
|
742
|
-
typeof event.data.object.product === 'object' &&
|
|
743
|
-
event.data.object.product != null &&
|
|
744
|
-
event.data.object.amount != null
|
|
745
|
-
) {
|
|
660
|
+
case "plan.updated": {
|
|
661
|
+
if (typeof event.data.object.product === "object" && event.data.object.product != null && event.data.object.amount != null) {
|
|
746
662
|
this.planService.basePlanService.updatePlan({
|
|
747
663
|
id: event.data.object.id,
|
|
748
664
|
billingProvider: BillingProviderEnum.STRIPE,
|
|
749
665
|
cadence: event.data.object.interval,
|
|
750
666
|
currency: event.data.object.currency,
|
|
751
667
|
active: true,
|
|
752
|
-
name:
|
|
753
|
-
typeof event.data.object.product === 'string'
|
|
754
|
-
? event.data.object.product
|
|
755
|
-
: event.data.object.product?.id,
|
|
668
|
+
name: typeof event.data.object.product === "string" ? event.data.object.product : event.data.object.product?.id,
|
|
756
669
|
price: event.data.object.amount,
|
|
757
670
|
externalId: event.data.object.id
|
|
758
671
|
});
|
|
759
672
|
} else {
|
|
760
|
-
throw new Error(
|
|
673
|
+
throw new Error("Invalid plan");
|
|
761
674
|
}
|
|
762
675
|
break;
|
|
763
676
|
}
|
|
764
|
-
case
|
|
677
|
+
case "plan.deleted": {
|
|
765
678
|
this.planService.deletePlan({
|
|
766
679
|
id: event.data.object.id
|
|
767
680
|
});
|
|
768
681
|
break;
|
|
769
682
|
}
|
|
770
|
-
case
|
|
683
|
+
case "customer.subscription.created": {
|
|
771
684
|
this.subscriptionService.baseSubscriptionService.createSubscription({
|
|
772
685
|
id: event.data.object.id,
|
|
773
|
-
partyId:
|
|
774
|
-
|
|
775
|
-
? event.data.object.customer
|
|
776
|
-
: event.data.object.customer.id,
|
|
777
|
-
partyType: 'USER',
|
|
686
|
+
partyId: typeof event.data.object.customer === "string" ? event.data.object.customer : event.data.object.customer.id,
|
|
687
|
+
partyType: "USER",
|
|
778
688
|
description: event.data.object.description ?? void 0,
|
|
779
689
|
active: true,
|
|
780
690
|
productId: event.data.object.items.data[0].plan.id,
|
|
781
691
|
externalId: event.data.object.id,
|
|
782
692
|
billingProvider: BillingProviderEnum.STRIPE,
|
|
783
693
|
startDate: new Date(event.data.object.created),
|
|
784
|
-
endDate: event.data.object.cancel_at
|
|
785
|
-
? new Date(event.data.object.cancel_at)
|
|
786
|
-
: /* @__PURE__ */ new Date(Infinity),
|
|
694
|
+
endDate: event.data.object.cancel_at ? new Date(event.data.object.cancel_at) : /* @__PURE__ */ new Date(Infinity),
|
|
787
695
|
status: event.data.object.status
|
|
788
696
|
});
|
|
789
697
|
break;
|
|
790
698
|
}
|
|
791
|
-
case
|
|
699
|
+
case "customer.subscription.updated": {
|
|
792
700
|
this.subscriptionService.baseSubscriptionService.updateSubscription({
|
|
793
701
|
id: event.data.object.id,
|
|
794
|
-
partyId:
|
|
795
|
-
|
|
796
|
-
? event.data.object.customer
|
|
797
|
-
: event.data.object.customer.id,
|
|
798
|
-
partyType: 'USER',
|
|
702
|
+
partyId: typeof event.data.object.customer === "string" ? event.data.object.customer : event.data.object.customer.id,
|
|
703
|
+
partyType: "USER",
|
|
799
704
|
description: event.data.object.description ?? void 0,
|
|
800
705
|
active: true,
|
|
801
706
|
externalId: event.data.object.id,
|
|
802
707
|
billingProvider: BillingProviderEnum.STRIPE,
|
|
803
708
|
startDate: new Date(event.data.object.created),
|
|
804
|
-
endDate: event.data.object.cancel_at
|
|
805
|
-
? new Date(event.data.object.cancel_at)
|
|
806
|
-
: /* @__PURE__ */ new Date(Infinity),
|
|
709
|
+
endDate: event.data.object.cancel_at ? new Date(event.data.object.cancel_at) : /* @__PURE__ */ new Date(Infinity),
|
|
807
710
|
productId: event.data.object.items.data[0].plan.id,
|
|
808
711
|
status: event.data.object.status
|
|
809
712
|
});
|
|
810
713
|
break;
|
|
811
714
|
}
|
|
812
|
-
case
|
|
715
|
+
case "customer.subscription.deleted": {
|
|
813
716
|
this.subscriptionService.deleteSubscription({
|
|
814
717
|
id: event.data.object.id
|
|
815
718
|
});
|
|
816
719
|
break;
|
|
817
720
|
}
|
|
818
|
-
case
|
|
721
|
+
case "customer.subscription.paused": {
|
|
819
722
|
this.subscriptionService.cancelSubscription({
|
|
820
723
|
id: event.data.object.id
|
|
821
724
|
});
|
|
822
725
|
break;
|
|
823
726
|
}
|
|
824
|
-
case
|
|
727
|
+
case "customer.subscription.resumed": {
|
|
825
728
|
this.subscriptionService.resumeSubscription({
|
|
826
729
|
id: event.data.object.id
|
|
827
730
|
});
|
|
@@ -829,7 +732,7 @@ var StripeWebhookService = class {
|
|
|
829
732
|
}
|
|
830
733
|
default:
|
|
831
734
|
this.openTelemetryCollector.info(
|
|
832
|
-
|
|
735
|
+
"Unprocessed stripe event type",
|
|
833
736
|
eventType
|
|
834
737
|
);
|
|
835
738
|
break;
|
|
@@ -837,12 +740,11 @@ var StripeWebhookService = class {
|
|
|
837
740
|
}
|
|
838
741
|
};
|
|
839
742
|
// Annotate the CommonJS export names for ESM import in node:
|
|
840
|
-
0 &&
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
});
|
|
743
|
+
0 && (module.exports = {
|
|
744
|
+
StripeBillingPortalService,
|
|
745
|
+
StripeCheckoutSessionService,
|
|
746
|
+
StripePaymentLinkService,
|
|
747
|
+
StripePlanService,
|
|
748
|
+
StripeSubscriptionService,
|
|
749
|
+
StripeWebhookService
|
|
750
|
+
});
|