@forklaunch/implementation-billing-stripe 0.3.5 → 0.4.0
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 -2914
- package/lib/domain/schemas/index.d.ts +323 -2914
- package/lib/domain/schemas/index.js +181 -257
- package/lib/domain/schemas/index.mjs +130 -131
- package/lib/domain/types/index.d.mts +135 -375
- package/lib/domain/types/index.d.ts +135 -375
- package/lib/domain/types/index.js +4 -8
- package/lib/eject/services/webhook.service.ts +21 -0
- package/lib/services/index.d.mts +148 -461
- package/lib/services/index.d.ts +148 -461
- package/lib/services/index.js +147 -229
- package/lib/services/index.mjs +119 -194
- package/package.json +11 -11
package/lib/services/index.mjs
CHANGED
|
@@ -1,15 +1,7 @@
|
|
|
1
1
|
// services/billingPortal.service.ts
|
|
2
|
-
import { BaseBillingPortalService } from
|
|
2
|
+
import { BaseBillingPortalService } from "@forklaunch/implementation-billing-base/services";
|
|
3
3
|
var StripeBillingPortalService = class {
|
|
4
|
-
constructor(
|
|
5
|
-
stripeClient,
|
|
6
|
-
em,
|
|
7
|
-
cache,
|
|
8
|
-
openTelemetryCollector,
|
|
9
|
-
schemaValidator,
|
|
10
|
-
mappers,
|
|
11
|
-
options
|
|
12
|
-
) {
|
|
4
|
+
constructor(stripeClient, em, cache, openTelemetryCollector, schemaValidator, mappers, options) {
|
|
13
5
|
this.options = options;
|
|
14
6
|
this.stripeClient = stripeClient;
|
|
15
7
|
this.em = em;
|
|
@@ -60,10 +52,9 @@ var StripeBillingPortalService = class {
|
|
|
60
52
|
return this.baseBillingPortalService.expireBillingPortalSession(idDto);
|
|
61
53
|
}
|
|
62
54
|
async updateBillingPortalSession(billingPortalDto, ...args) {
|
|
63
|
-
const existingSession =
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
});
|
|
55
|
+
const existingSession = await this.baseBillingPortalService.getBillingPortalSession({
|
|
56
|
+
id: billingPortalDto.id
|
|
57
|
+
});
|
|
67
58
|
const session = await this.stripeClient.billingPortal.sessions.create({
|
|
68
59
|
...billingPortalDto.stripeFields,
|
|
69
60
|
customer: existingSession.customerId
|
|
@@ -85,17 +76,9 @@ var StripeBillingPortalService = class {
|
|
|
85
76
|
};
|
|
86
77
|
|
|
87
78
|
// services/checkoutSession.service.ts
|
|
88
|
-
import { BaseCheckoutSessionService } from
|
|
79
|
+
import { BaseCheckoutSessionService } from "@forklaunch/implementation-billing-base/services";
|
|
89
80
|
var StripeCheckoutSessionService = class {
|
|
90
|
-
constructor(
|
|
91
|
-
stripeClient,
|
|
92
|
-
em,
|
|
93
|
-
cache,
|
|
94
|
-
openTelemetryCollector,
|
|
95
|
-
schemaValidator,
|
|
96
|
-
mappers,
|
|
97
|
-
options
|
|
98
|
-
) {
|
|
81
|
+
constructor(stripeClient, em, cache, openTelemetryCollector, schemaValidator, mappers, options) {
|
|
99
82
|
this.options = options;
|
|
100
83
|
this.stripeClient = stripeClient;
|
|
101
84
|
this.em = em;
|
|
@@ -144,8 +127,7 @@ var StripeCheckoutSessionService = class {
|
|
|
144
127
|
const session = await this.stripeClient.checkout.sessions.retrieve(
|
|
145
128
|
idDto.id
|
|
146
129
|
);
|
|
147
|
-
const databaseCheckoutSession =
|
|
148
|
-
await this.baseCheckoutSessionService.getCheckoutSession(idDto);
|
|
130
|
+
const databaseCheckoutSession = await this.baseCheckoutSessionService.getCheckoutSession(idDto);
|
|
149
131
|
databaseCheckoutSession.stripeFields = session;
|
|
150
132
|
return databaseCheckoutSession;
|
|
151
133
|
}
|
|
@@ -156,7 +138,7 @@ var StripeCheckoutSessionService = class {
|
|
|
156
138
|
async handleCheckoutSuccess({ id }) {
|
|
157
139
|
await this.stripeClient.checkout.sessions.update(id, {
|
|
158
140
|
metadata: {
|
|
159
|
-
status:
|
|
141
|
+
status: "SUCCESS"
|
|
160
142
|
}
|
|
161
143
|
});
|
|
162
144
|
await this.baseCheckoutSessionService.handleCheckoutSuccess({ id });
|
|
@@ -164,7 +146,7 @@ var StripeCheckoutSessionService = class {
|
|
|
164
146
|
async handleCheckoutFailure({ id }) {
|
|
165
147
|
await this.stripeClient.checkout.sessions.update(id, {
|
|
166
148
|
metadata: {
|
|
167
|
-
status:
|
|
149
|
+
status: "FAILED"
|
|
168
150
|
}
|
|
169
151
|
});
|
|
170
152
|
await this.baseCheckoutSessionService.handleCheckoutFailure({ id });
|
|
@@ -172,17 +154,9 @@ var StripeCheckoutSessionService = class {
|
|
|
172
154
|
};
|
|
173
155
|
|
|
174
156
|
// services/paymentLink.service.ts
|
|
175
|
-
import { BasePaymentLinkService } from
|
|
157
|
+
import { BasePaymentLinkService } from "@forklaunch/implementation-billing-base/services";
|
|
176
158
|
var StripePaymentLinkService = class {
|
|
177
|
-
constructor(
|
|
178
|
-
stripeClient,
|
|
179
|
-
em,
|
|
180
|
-
cache,
|
|
181
|
-
openTelemetryCollector,
|
|
182
|
-
schemaValidator,
|
|
183
|
-
mappers,
|
|
184
|
-
options
|
|
185
|
-
) {
|
|
159
|
+
constructor(stripeClient, em, cache, openTelemetryCollector, schemaValidator, mappers, options) {
|
|
186
160
|
this.options = options;
|
|
187
161
|
this.stripeClient = stripeClient;
|
|
188
162
|
this.em = em;
|
|
@@ -216,11 +190,10 @@ var StripePaymentLinkService = class {
|
|
|
216
190
|
{
|
|
217
191
|
...paymentLinkDto,
|
|
218
192
|
id: session.id,
|
|
219
|
-
amount:
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
) ?? 0
|
|
193
|
+
amount: session.line_items?.data.reduce(
|
|
194
|
+
(total, item) => total + item.amount_total,
|
|
195
|
+
0
|
|
196
|
+
) ?? 0
|
|
224
197
|
},
|
|
225
198
|
this.em,
|
|
226
199
|
session,
|
|
@@ -242,11 +215,10 @@ var StripePaymentLinkService = class {
|
|
|
242
215
|
{
|
|
243
216
|
...paymentLinkDto,
|
|
244
217
|
id: session.id,
|
|
245
|
-
amount:
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
) ?? 0
|
|
218
|
+
amount: session.line_items?.data.reduce(
|
|
219
|
+
(total, item) => total + item.amount_total,
|
|
220
|
+
0
|
|
221
|
+
) ?? 0
|
|
250
222
|
},
|
|
251
223
|
this.em,
|
|
252
224
|
session
|
|
@@ -258,15 +230,14 @@ var StripePaymentLinkService = class {
|
|
|
258
230
|
}
|
|
259
231
|
async getPaymentLink({ id }) {
|
|
260
232
|
const stripePaymentLink = await this.stripeClient.paymentLinks.retrieve(id);
|
|
261
|
-
const databasePaymentLink =
|
|
262
|
-
await this.basePaymentLinkService.getPaymentLink({ id });
|
|
233
|
+
const databasePaymentLink = await this.basePaymentLinkService.getPaymentLink({ id });
|
|
263
234
|
databasePaymentLink.stripeFields = stripePaymentLink;
|
|
264
235
|
return databasePaymentLink;
|
|
265
236
|
}
|
|
266
237
|
async expirePaymentLink({ id }) {
|
|
267
238
|
await this.stripeClient.paymentLinks.update(id, {
|
|
268
239
|
metadata: {
|
|
269
|
-
status:
|
|
240
|
+
status: "EXPIRED"
|
|
270
241
|
}
|
|
271
242
|
});
|
|
272
243
|
await this.basePaymentLinkService.expirePaymentLink({ id });
|
|
@@ -274,7 +245,7 @@ var StripePaymentLinkService = class {
|
|
|
274
245
|
async handlePaymentSuccess({ id }) {
|
|
275
246
|
await this.stripeClient.paymentLinks.update(id, {
|
|
276
247
|
metadata: {
|
|
277
|
-
status:
|
|
248
|
+
status: "COMPLETED"
|
|
278
249
|
}
|
|
279
250
|
});
|
|
280
251
|
await this.basePaymentLinkService.handlePaymentSuccess({ id });
|
|
@@ -282,7 +253,7 @@ var StripePaymentLinkService = class {
|
|
|
282
253
|
async handlePaymentFailure({ id }) {
|
|
283
254
|
await this.stripeClient.paymentLinks.update(id, {
|
|
284
255
|
metadata: {
|
|
285
|
-
status:
|
|
256
|
+
status: "FAILED"
|
|
286
257
|
}
|
|
287
258
|
});
|
|
288
259
|
await this.basePaymentLinkService.handlePaymentFailure({ id });
|
|
@@ -291,8 +262,7 @@ var StripePaymentLinkService = class {
|
|
|
291
262
|
const stripePaymentLinks = await this.stripeClient.paymentLinks.list({
|
|
292
263
|
active: true
|
|
293
264
|
});
|
|
294
|
-
const databasePaymentLinks =
|
|
295
|
-
await this.basePaymentLinkService.listPaymentLinks(idsDto);
|
|
265
|
+
const databasePaymentLinks = await this.basePaymentLinkService.listPaymentLinks(idsDto);
|
|
296
266
|
return await Promise.all(
|
|
297
267
|
databasePaymentLinks.map(async (paymentLink) => {
|
|
298
268
|
const stripePaymentLink = stripePaymentLinks.data.find(
|
|
@@ -311,16 +281,9 @@ var StripePaymentLinkService = class {
|
|
|
311
281
|
};
|
|
312
282
|
|
|
313
283
|
// services/plan.service.ts
|
|
314
|
-
import { BasePlanService } from
|
|
284
|
+
import { BasePlanService } from "@forklaunch/implementation-billing-base/services";
|
|
315
285
|
var StripePlanService = class {
|
|
316
|
-
constructor(
|
|
317
|
-
stripeClient,
|
|
318
|
-
em,
|
|
319
|
-
openTelemetryCollector,
|
|
320
|
-
schemaValidator,
|
|
321
|
-
mappers,
|
|
322
|
-
options
|
|
323
|
-
) {
|
|
286
|
+
constructor(stripeClient, em, openTelemetryCollector, schemaValidator, mappers, options) {
|
|
324
287
|
this.options = options;
|
|
325
288
|
this.stripeClient = stripeClient;
|
|
326
289
|
this.em = em;
|
|
@@ -352,7 +315,7 @@ var StripePlanService = class {
|
|
|
352
315
|
{
|
|
353
316
|
...planDto,
|
|
354
317
|
externalId: stripePlan.id,
|
|
355
|
-
billingProvider:
|
|
318
|
+
billingProvider: "stripe"
|
|
356
319
|
},
|
|
357
320
|
em ?? this.em,
|
|
358
321
|
stripePlan
|
|
@@ -361,13 +324,12 @@ var StripePlanService = class {
|
|
|
361
324
|
}
|
|
362
325
|
async getPlan(idDto, em) {
|
|
363
326
|
const plan = await this.stripeClient.plans.retrieve(idDto.id);
|
|
364
|
-
const id = (
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
)?.id;
|
|
327
|
+
const id = (await em?.findOne(
|
|
328
|
+
this.options?.databaseTableName ?? "plan",
|
|
329
|
+
{ externalId: idDto.id }
|
|
330
|
+
))?.id;
|
|
369
331
|
if (!id) {
|
|
370
|
-
throw new Error(
|
|
332
|
+
throw new Error("Plan not found");
|
|
371
333
|
}
|
|
372
334
|
const planEntity = await this.basePlanService.getPlan({ id }, em);
|
|
373
335
|
planEntity.stripeFields = plan;
|
|
@@ -375,8 +337,8 @@ var StripePlanService = class {
|
|
|
375
337
|
}
|
|
376
338
|
async updatePlan(planDto, em) {
|
|
377
339
|
const existingPlan = await this.stripeClient.plans.retrieve(planDto.id);
|
|
378
|
-
const plan = await this.stripeClient.plans.del(planDto.id).then(
|
|
379
|
-
this.stripeClient.plans.create({
|
|
340
|
+
const plan = await this.stripeClient.plans.del(planDto.id).then(
|
|
341
|
+
() => this.stripeClient.plans.create({
|
|
380
342
|
...planDto.stripeFields,
|
|
381
343
|
interval: planDto.cadence ?? existingPlan.interval,
|
|
382
344
|
product: planDto.name,
|
|
@@ -388,7 +350,7 @@ var StripePlanService = class {
|
|
|
388
350
|
{
|
|
389
351
|
...planDto,
|
|
390
352
|
externalId: plan.id,
|
|
391
|
-
billingProvider:
|
|
353
|
+
billingProvider: "stripe"
|
|
392
354
|
},
|
|
393
355
|
em ?? this.em,
|
|
394
356
|
plan
|
|
@@ -406,15 +368,12 @@ var StripePlanService = class {
|
|
|
406
368
|
const plans = await this.stripeClient.plans.list({
|
|
407
369
|
active: true
|
|
408
370
|
});
|
|
409
|
-
const planIds = (
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
)
|
|
414
|
-
?.filter((s) => idsDto?.ids?.includes(s.id))
|
|
415
|
-
?.map((s) => s.id);
|
|
371
|
+
const planIds = (await em?.findAll(
|
|
372
|
+
this.options?.databaseTableName ?? "plan",
|
|
373
|
+
{ where: { externalId: { $in: plans.data.map((plan) => plan.id) } } }
|
|
374
|
+
))?.filter((s) => idsDto?.ids?.includes(s.id))?.map((s) => s.id);
|
|
416
375
|
if (!planIds) {
|
|
417
|
-
throw new Error(
|
|
376
|
+
throw new Error("Plans not found");
|
|
418
377
|
}
|
|
419
378
|
return await Promise.all(
|
|
420
379
|
(await this.basePlanService.listPlans({ ids: planIds }, em)).map(
|
|
@@ -430,16 +389,9 @@ var StripePlanService = class {
|
|
|
430
389
|
};
|
|
431
390
|
|
|
432
391
|
// services/subscription.service.ts
|
|
433
|
-
import { BaseSubscriptionService } from
|
|
392
|
+
import { BaseSubscriptionService } from "@forklaunch/implementation-billing-base/services";
|
|
434
393
|
var StripeSubscriptionService = class {
|
|
435
|
-
constructor(
|
|
436
|
-
stripeClient,
|
|
437
|
-
em,
|
|
438
|
-
openTelemetryCollector,
|
|
439
|
-
schemaValidator,
|
|
440
|
-
mappers,
|
|
441
|
-
options
|
|
442
|
-
) {
|
|
394
|
+
constructor(stripeClient, em, openTelemetryCollector, schemaValidator, mappers, options) {
|
|
443
395
|
this.options = options;
|
|
444
396
|
this.stripeClient = stripeClient;
|
|
445
397
|
this.em = em;
|
|
@@ -474,15 +426,14 @@ var StripeSubscriptionService = class {
|
|
|
474
426
|
{
|
|
475
427
|
...subscriptionDto,
|
|
476
428
|
externalId: subscription.id,
|
|
477
|
-
billingProvider:
|
|
429
|
+
billingProvider: "stripe"
|
|
478
430
|
},
|
|
479
431
|
em ?? this.em,
|
|
480
432
|
subscription
|
|
481
433
|
);
|
|
482
434
|
}
|
|
483
435
|
async getSubscription(idDto, em) {
|
|
484
|
-
const subscriptionEntity =
|
|
485
|
-
await this.baseSubscriptionService.getSubscription(idDto, em);
|
|
436
|
+
const subscriptionEntity = await this.baseSubscriptionService.getSubscription(idDto, em);
|
|
486
437
|
const stripeSubscription = await this.stripeClient.subscriptions.retrieve(
|
|
487
438
|
idDto.id
|
|
488
439
|
);
|
|
@@ -490,8 +441,7 @@ var StripeSubscriptionService = class {
|
|
|
490
441
|
return subscriptionEntity;
|
|
491
442
|
}
|
|
492
443
|
async getUserSubscription(idDto, em) {
|
|
493
|
-
const subscriptionEntity =
|
|
494
|
-
await this.baseSubscriptionService.getUserSubscription(idDto, em);
|
|
444
|
+
const subscriptionEntity = await this.baseSubscriptionService.getUserSubscription(idDto, em);
|
|
495
445
|
const stripeSubscription = await this.stripeClient.subscriptions.retrieve(
|
|
496
446
|
idDto.id
|
|
497
447
|
);
|
|
@@ -499,19 +449,17 @@ var StripeSubscriptionService = class {
|
|
|
499
449
|
return subscriptionEntity;
|
|
500
450
|
}
|
|
501
451
|
async getOrganizationSubscription(idDto, em) {
|
|
502
|
-
const id = (
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
)?.id;
|
|
452
|
+
const id = (await em?.findOne(
|
|
453
|
+
this.options?.databaseTableName ?? "subscription",
|
|
454
|
+
{ externalId: idDto.id }
|
|
455
|
+
))?.id;
|
|
507
456
|
if (!id) {
|
|
508
|
-
throw new Error(
|
|
457
|
+
throw new Error("Subscription not found");
|
|
509
458
|
}
|
|
510
|
-
const subscriptionEntity =
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
);
|
|
459
|
+
const subscriptionEntity = await this.baseSubscriptionService.getOrganizationSubscription(
|
|
460
|
+
{ id },
|
|
461
|
+
em
|
|
462
|
+
);
|
|
515
463
|
const stripeSubscription = await this.stripeClient.subscriptions.retrieve(
|
|
516
464
|
idDto.id
|
|
517
465
|
);
|
|
@@ -534,7 +482,7 @@ var StripeSubscriptionService = class {
|
|
|
534
482
|
{
|
|
535
483
|
...subscriptionDto,
|
|
536
484
|
externalId: subscription.id,
|
|
537
|
-
billingProvider:
|
|
485
|
+
billingProvider: "stripe",
|
|
538
486
|
providerFields: subscription
|
|
539
487
|
},
|
|
540
488
|
em ?? this.em,
|
|
@@ -546,18 +494,15 @@ var StripeSubscriptionService = class {
|
|
|
546
494
|
await this.baseSubscriptionService.deleteSubscription(idDto, em);
|
|
547
495
|
}
|
|
548
496
|
async listSubscriptions(idsDto, em) {
|
|
549
|
-
const subscriptions = (
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
where: { externalId: { $in: subscriptions.map((s) => s.id) } }
|
|
557
|
-
})
|
|
558
|
-
)?.map((s) => s.id);
|
|
497
|
+
const subscriptions = (await this.stripeClient.subscriptions.list({
|
|
498
|
+
status: "active"
|
|
499
|
+
})).data.filter((s) => idsDto.ids?.includes(s.id));
|
|
500
|
+
const ids = (await em?.findAll(
|
|
501
|
+
this.options?.databaseTableName ?? "subscription",
|
|
502
|
+
{ where: { externalId: { $in: subscriptions.map((s) => s.id) } } }
|
|
503
|
+
))?.map((s) => s.id);
|
|
559
504
|
if (!ids) {
|
|
560
|
-
throw new Error(
|
|
505
|
+
throw new Error("Subscriptions not found");
|
|
561
506
|
}
|
|
562
507
|
return await Promise.all(
|
|
563
508
|
(await this.baseSubscriptionService.listSubscriptions({ ids }, em)).map(
|
|
@@ -583,7 +528,7 @@ var StripeSubscriptionService = class {
|
|
|
583
528
|
|
|
584
529
|
// domain/enum/billingProvider.enum.ts
|
|
585
530
|
var BillingProviderEnum = {
|
|
586
|
-
STRIPE:
|
|
531
|
+
STRIPE: "stripe"
|
|
587
532
|
};
|
|
588
533
|
|
|
589
534
|
// services/webhook.service.ts
|
|
@@ -597,17 +542,7 @@ var StripeWebhookService = class {
|
|
|
597
542
|
paymentLinkService;
|
|
598
543
|
planService;
|
|
599
544
|
subscriptionService;
|
|
600
|
-
constructor(
|
|
601
|
-
stripeClient,
|
|
602
|
-
em,
|
|
603
|
-
schemaValidator,
|
|
604
|
-
openTelemetryCollector,
|
|
605
|
-
billingPortalService,
|
|
606
|
-
checkoutSessionService,
|
|
607
|
-
paymentLinkService,
|
|
608
|
-
planService,
|
|
609
|
-
subscriptionService
|
|
610
|
-
) {
|
|
545
|
+
constructor(stripeClient, em, schemaValidator, openTelemetryCollector, billingPortalService, checkoutSessionService, paymentLinkService, planService, subscriptionService) {
|
|
611
546
|
this.stripeClient = stripeClient;
|
|
612
547
|
this.em = em;
|
|
613
548
|
this.schemaValidator = schemaValidator;
|
|
@@ -620,11 +555,20 @@ var StripeWebhookService = class {
|
|
|
620
555
|
}
|
|
621
556
|
async handleWebhookEvent(event) {
|
|
622
557
|
if (this.openTelemetryCollector) {
|
|
623
|
-
this.openTelemetryCollector.info(
|
|
558
|
+
this.openTelemetryCollector.info("Handling webhook event", event);
|
|
559
|
+
}
|
|
560
|
+
if (await this.em.findOne("StripeWebhookEvent", {
|
|
561
|
+
idempotencyKey: event.request?.idempotency_key
|
|
562
|
+
})) {
|
|
563
|
+
this.openTelemetryCollector.info(
|
|
564
|
+
"Webhook event already processed",
|
|
565
|
+
event
|
|
566
|
+
);
|
|
567
|
+
return;
|
|
624
568
|
}
|
|
625
569
|
const eventType = event.type;
|
|
626
570
|
switch (eventType) {
|
|
627
|
-
case
|
|
571
|
+
case "billing_portal.session.created": {
|
|
628
572
|
this.billingPortalService.baseBillingPortalService.createBillingPortalSession(
|
|
629
573
|
{
|
|
630
574
|
id: event.data.object.id,
|
|
@@ -635,156 +579,130 @@ var StripeWebhookService = class {
|
|
|
635
579
|
);
|
|
636
580
|
break;
|
|
637
581
|
}
|
|
638
|
-
case
|
|
582
|
+
case "checkout.session.expired": {
|
|
639
583
|
this.checkoutSessionService.handleCheckoutFailure({
|
|
640
584
|
id: event.data.object.id
|
|
641
585
|
});
|
|
642
586
|
break;
|
|
643
587
|
}
|
|
644
|
-
case
|
|
588
|
+
case "checkout.session.completed": {
|
|
645
589
|
this.checkoutSessionService.handleCheckoutSuccess({
|
|
646
590
|
id: event.data.object.id
|
|
647
591
|
});
|
|
648
592
|
break;
|
|
649
593
|
}
|
|
650
|
-
case
|
|
594
|
+
case "payment_link.created":
|
|
651
595
|
{
|
|
652
596
|
this.paymentLinkService.basePaymentLinkService.createPaymentLink({
|
|
653
597
|
id: event.data.object.id,
|
|
654
|
-
amount:
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
) ?? 0,
|
|
598
|
+
amount: event.data.object.line_items?.data.reduce(
|
|
599
|
+
(total, item) => total + item.amount_total,
|
|
600
|
+
0
|
|
601
|
+
) ?? 0,
|
|
659
602
|
paymentMethods: event.data.object.payment_method_types,
|
|
660
|
-
status:
|
|
603
|
+
status: "CREATED",
|
|
661
604
|
currency: event.data.object.currency
|
|
662
605
|
});
|
|
663
606
|
}
|
|
664
607
|
break;
|
|
665
|
-
case
|
|
608
|
+
case "payment_link.updated": {
|
|
666
609
|
this.paymentLinkService.basePaymentLinkService.updatePaymentLink({
|
|
667
610
|
id: event.data.object.id,
|
|
668
|
-
amount:
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
) ?? 0,
|
|
611
|
+
amount: event.data.object.line_items?.data.reduce(
|
|
612
|
+
(total, item) => total + item.amount_total,
|
|
613
|
+
0
|
|
614
|
+
) ?? 0,
|
|
673
615
|
paymentMethods: event.data.object.payment_method_types,
|
|
674
|
-
status:
|
|
616
|
+
status: "UPDATED",
|
|
675
617
|
currency: event.data.object.currency
|
|
676
618
|
});
|
|
677
619
|
break;
|
|
678
620
|
}
|
|
679
|
-
case
|
|
680
|
-
if (
|
|
681
|
-
typeof event.data.object.product === 'object' &&
|
|
682
|
-
event.data.object.product != null &&
|
|
683
|
-
event.data.object.amount != null
|
|
684
|
-
) {
|
|
621
|
+
case "plan.created": {
|
|
622
|
+
if (typeof event.data.object.product === "object" && event.data.object.product != null && event.data.object.amount != null) {
|
|
685
623
|
this.planService.basePlanService.createPlan({
|
|
686
624
|
id: event.data.object.id,
|
|
687
625
|
billingProvider: BillingProviderEnum.STRIPE,
|
|
688
626
|
cadence: event.data.object.interval,
|
|
689
627
|
currency: event.data.object.currency,
|
|
690
628
|
active: true,
|
|
691
|
-
name:
|
|
692
|
-
typeof event.data.object.product === 'string'
|
|
693
|
-
? event.data.object.product
|
|
694
|
-
: event.data.object.product?.id,
|
|
629
|
+
name: typeof event.data.object.product === "string" ? event.data.object.product : event.data.object.product?.id,
|
|
695
630
|
price: event.data.object.amount,
|
|
696
631
|
externalId: event.data.object.id
|
|
697
632
|
});
|
|
698
633
|
} else {
|
|
699
|
-
throw new Error(
|
|
634
|
+
throw new Error("Invalid plan");
|
|
700
635
|
}
|
|
701
636
|
break;
|
|
702
637
|
}
|
|
703
|
-
case
|
|
704
|
-
if (
|
|
705
|
-
typeof event.data.object.product === 'object' &&
|
|
706
|
-
event.data.object.product != null &&
|
|
707
|
-
event.data.object.amount != null
|
|
708
|
-
) {
|
|
638
|
+
case "plan.updated": {
|
|
639
|
+
if (typeof event.data.object.product === "object" && event.data.object.product != null && event.data.object.amount != null) {
|
|
709
640
|
this.planService.basePlanService.updatePlan({
|
|
710
641
|
id: event.data.object.id,
|
|
711
642
|
billingProvider: BillingProviderEnum.STRIPE,
|
|
712
643
|
cadence: event.data.object.interval,
|
|
713
644
|
currency: event.data.object.currency,
|
|
714
645
|
active: true,
|
|
715
|
-
name:
|
|
716
|
-
typeof event.data.object.product === 'string'
|
|
717
|
-
? event.data.object.product
|
|
718
|
-
: event.data.object.product?.id,
|
|
646
|
+
name: typeof event.data.object.product === "string" ? event.data.object.product : event.data.object.product?.id,
|
|
719
647
|
price: event.data.object.amount,
|
|
720
648
|
externalId: event.data.object.id
|
|
721
649
|
});
|
|
722
650
|
} else {
|
|
723
|
-
throw new Error(
|
|
651
|
+
throw new Error("Invalid plan");
|
|
724
652
|
}
|
|
725
653
|
break;
|
|
726
654
|
}
|
|
727
|
-
case
|
|
655
|
+
case "plan.deleted": {
|
|
728
656
|
this.planService.deletePlan({
|
|
729
657
|
id: event.data.object.id
|
|
730
658
|
});
|
|
731
659
|
break;
|
|
732
660
|
}
|
|
733
|
-
case
|
|
661
|
+
case "customer.subscription.created": {
|
|
734
662
|
this.subscriptionService.baseSubscriptionService.createSubscription({
|
|
735
663
|
id: event.data.object.id,
|
|
736
|
-
partyId:
|
|
737
|
-
|
|
738
|
-
? event.data.object.customer
|
|
739
|
-
: event.data.object.customer.id,
|
|
740
|
-
partyType: 'USER',
|
|
664
|
+
partyId: typeof event.data.object.customer === "string" ? event.data.object.customer : event.data.object.customer.id,
|
|
665
|
+
partyType: "USER",
|
|
741
666
|
description: event.data.object.description ?? void 0,
|
|
742
667
|
active: true,
|
|
743
668
|
productId: event.data.object.items.data[0].plan.id,
|
|
744
669
|
externalId: event.data.object.id,
|
|
745
670
|
billingProvider: BillingProviderEnum.STRIPE,
|
|
746
671
|
startDate: new Date(event.data.object.created),
|
|
747
|
-
endDate: event.data.object.cancel_at
|
|
748
|
-
? new Date(event.data.object.cancel_at)
|
|
749
|
-
: /* @__PURE__ */ new Date(Infinity),
|
|
672
|
+
endDate: event.data.object.cancel_at ? new Date(event.data.object.cancel_at) : /* @__PURE__ */ new Date(Infinity),
|
|
750
673
|
status: event.data.object.status
|
|
751
674
|
});
|
|
752
675
|
break;
|
|
753
676
|
}
|
|
754
|
-
case
|
|
677
|
+
case "customer.subscription.updated": {
|
|
755
678
|
this.subscriptionService.baseSubscriptionService.updateSubscription({
|
|
756
679
|
id: event.data.object.id,
|
|
757
|
-
partyId:
|
|
758
|
-
|
|
759
|
-
? event.data.object.customer
|
|
760
|
-
: event.data.object.customer.id,
|
|
761
|
-
partyType: 'USER',
|
|
680
|
+
partyId: typeof event.data.object.customer === "string" ? event.data.object.customer : event.data.object.customer.id,
|
|
681
|
+
partyType: "USER",
|
|
762
682
|
description: event.data.object.description ?? void 0,
|
|
763
683
|
active: true,
|
|
764
684
|
externalId: event.data.object.id,
|
|
765
685
|
billingProvider: BillingProviderEnum.STRIPE,
|
|
766
686
|
startDate: new Date(event.data.object.created),
|
|
767
|
-
endDate: event.data.object.cancel_at
|
|
768
|
-
? new Date(event.data.object.cancel_at)
|
|
769
|
-
: /* @__PURE__ */ new Date(Infinity),
|
|
687
|
+
endDate: event.data.object.cancel_at ? new Date(event.data.object.cancel_at) : /* @__PURE__ */ new Date(Infinity),
|
|
770
688
|
productId: event.data.object.items.data[0].plan.id,
|
|
771
689
|
status: event.data.object.status
|
|
772
690
|
});
|
|
773
691
|
break;
|
|
774
692
|
}
|
|
775
|
-
case
|
|
693
|
+
case "customer.subscription.deleted": {
|
|
776
694
|
this.subscriptionService.deleteSubscription({
|
|
777
695
|
id: event.data.object.id
|
|
778
696
|
});
|
|
779
697
|
break;
|
|
780
698
|
}
|
|
781
|
-
case
|
|
699
|
+
case "customer.subscription.paused": {
|
|
782
700
|
this.subscriptionService.cancelSubscription({
|
|
783
701
|
id: event.data.object.id
|
|
784
702
|
});
|
|
785
703
|
break;
|
|
786
704
|
}
|
|
787
|
-
case
|
|
705
|
+
case "customer.subscription.resumed": {
|
|
788
706
|
this.subscriptionService.resumeSubscription({
|
|
789
707
|
id: event.data.object.id
|
|
790
708
|
});
|
|
@@ -792,11 +710,18 @@ var StripeWebhookService = class {
|
|
|
792
710
|
}
|
|
793
711
|
default:
|
|
794
712
|
this.openTelemetryCollector.info(
|
|
795
|
-
|
|
713
|
+
"Unprocessed stripe event type",
|
|
796
714
|
eventType
|
|
797
715
|
);
|
|
798
716
|
break;
|
|
799
717
|
}
|
|
718
|
+
await this.em.insert("StripeWebhookEvent", {
|
|
719
|
+
stripeId: event.id,
|
|
720
|
+
idempotencyKey: event.request?.idempotency_key,
|
|
721
|
+
eventType: event.type,
|
|
722
|
+
eventData: event.data
|
|
723
|
+
});
|
|
724
|
+
await this.em.flush();
|
|
800
725
|
}
|
|
801
726
|
};
|
|
802
727
|
export {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@forklaunch/implementation-billing-stripe",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Stripe implementation for forklaunch billing",
|
|
5
5
|
"homepage": "https://github.com/forklaunch/forklaunch-js#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -42,23 +42,23 @@
|
|
|
42
42
|
"lib/**"
|
|
43
43
|
],
|
|
44
44
|
"dependencies": {
|
|
45
|
-
"@forklaunch/common": "^0.6.
|
|
46
|
-
"@forklaunch/core": "^0.
|
|
47
|
-
"@forklaunch/internal": "^0.3.
|
|
48
|
-
"@forklaunch/validator": "^0.10.
|
|
49
|
-
"@mikro-orm/core": "^6.5.
|
|
45
|
+
"@forklaunch/common": "^0.6.12",
|
|
46
|
+
"@forklaunch/core": "^0.15.1",
|
|
47
|
+
"@forklaunch/internal": "^0.3.12",
|
|
48
|
+
"@forklaunch/validator": "^0.10.12",
|
|
49
|
+
"@mikro-orm/core": "^6.5.5",
|
|
50
50
|
"@sinclair/typebox": "^0.34.41",
|
|
51
51
|
"ajv": "^8.17.1",
|
|
52
52
|
"stripe": "^18.5.0",
|
|
53
|
-
"zod": "^4.1.
|
|
54
|
-
"@forklaunch/implementation-billing-base": "0.
|
|
55
|
-
"@forklaunch/interfaces-billing": "0.
|
|
53
|
+
"zod": "^4.1.11",
|
|
54
|
+
"@forklaunch/implementation-billing-base": "0.7.0",
|
|
55
|
+
"@forklaunch/interfaces-billing": "0.7.0"
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
|
-
"@typescript/native-preview": "7.0.0-dev.
|
|
58
|
+
"@typescript/native-preview": "7.0.0-dev.20250922.1",
|
|
59
59
|
"depcheck": "^1.4.7",
|
|
60
60
|
"prettier": "^3.6.2",
|
|
61
|
-
"typedoc": "^0.28.
|
|
61
|
+
"typedoc": "^0.28.13"
|
|
62
62
|
},
|
|
63
63
|
"scripts": {
|
|
64
64
|
"build": "tsc --noEmit && tsup domain/schemas/index.ts services/index.ts domain/enum/index.ts domain/types/index.ts --format cjs,esm --no-splitting --dts --tsconfig tsconfig.json --out-dir lib --clean && if [ -f eject-package.bash ]; then pnpm package:eject; fi",
|