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