@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.
@@ -1,4 +1,4 @@
1
- 'use strict';
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 ((from && typeof from === 'object') || typeof from === 'function') {
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('@forklaunch/implementation-billing-base/services');
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
- new import_services.BaseBillingPortalService(
57
- em,
58
- cache,
59
- openTelemetryCollector,
60
- schemaValidator,
61
- mappers,
62
- options
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
- await this.baseBillingPortalService.getBillingPortalSession({
101
- id: billingPortalDto.id
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('@forklaunch/implementation-billing-base/services');
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
- new import_services2.BaseCheckoutSessionService(
144
- em,
145
- cache,
146
- openTelemetryCollector,
147
- schemaValidator,
148
- mappers,
149
- options
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: 'SUCCESS'
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: 'FAILED'
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('@forklaunch/implementation-billing-base/services');
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
- session.line_items?.data.reduce(
258
- (total, item) => total + item.amount_total,
259
- 0
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
- session.line_items?.data.reduce(
284
- (total, item) => total + item.amount_total,
285
- 0
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: 'EXPIRED'
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: 'COMPLETED'
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: 'FAILED'
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('@forklaunch/implementation-billing-base/services');
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: 'stripe'
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
- await em?.findOne(this.options?.databaseTableName ?? 'plan', {
403
- externalId: idDto.id
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('Plan not found');
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: 'stripe'
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
- 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);
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('Plans not found');
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('@forklaunch/implementation-billing-base/services');
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: 'stripe'
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
- await em?.findOne(this.options?.databaseTableName ?? 'subscription', {
541
- externalId: idDto.id
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('Subscription not found');
488
+ throw new Error("Subscription not found");
546
489
  }
547
- const subscriptionEntity =
548
- await this.baseSubscriptionService.getOrganizationSubscription(
549
- { id },
550
- em
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: 'stripe',
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
- 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);
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('Subscriptions not found');
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: '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,20 @@ var StripeWebhookService = class {
657
586
  }
658
587
  async handleWebhookEvent(event) {
659
588
  if (this.openTelemetryCollector) {
660
- this.openTelemetryCollector.info('Handling webhook event', event);
589
+ this.openTelemetryCollector.info("Handling webhook event", event);
590
+ }
591
+ if (await this.em.findOne("StripeWebhookEvent", {
592
+ idempotencyKey: event.request?.idempotency_key
593
+ })) {
594
+ this.openTelemetryCollector.info(
595
+ "Webhook event already processed",
596
+ event
597
+ );
598
+ return;
661
599
  }
662
600
  const eventType = event.type;
663
601
  switch (eventType) {
664
- case 'billing_portal.session.created': {
602
+ case "billing_portal.session.created": {
665
603
  this.billingPortalService.baseBillingPortalService.createBillingPortalSession(
666
604
  {
667
605
  id: event.data.object.id,
@@ -672,156 +610,130 @@ var StripeWebhookService = class {
672
610
  );
673
611
  break;
674
612
  }
675
- case 'checkout.session.expired': {
613
+ case "checkout.session.expired": {
676
614
  this.checkoutSessionService.handleCheckoutFailure({
677
615
  id: event.data.object.id
678
616
  });
679
617
  break;
680
618
  }
681
- case 'checkout.session.completed': {
619
+ case "checkout.session.completed": {
682
620
  this.checkoutSessionService.handleCheckoutSuccess({
683
621
  id: event.data.object.id
684
622
  });
685
623
  break;
686
624
  }
687
- case 'payment_link.created':
625
+ case "payment_link.created":
688
626
  {
689
627
  this.paymentLinkService.basePaymentLinkService.createPaymentLink({
690
628
  id: event.data.object.id,
691
- amount:
692
- event.data.object.line_items?.data.reduce(
693
- (total, item) => total + item.amount_total,
694
- 0
695
- ) ?? 0,
629
+ amount: event.data.object.line_items?.data.reduce(
630
+ (total, item) => total + item.amount_total,
631
+ 0
632
+ ) ?? 0,
696
633
  paymentMethods: event.data.object.payment_method_types,
697
- status: 'CREATED',
634
+ status: "CREATED",
698
635
  currency: event.data.object.currency
699
636
  });
700
637
  }
701
638
  break;
702
- case 'payment_link.updated': {
639
+ case "payment_link.updated": {
703
640
  this.paymentLinkService.basePaymentLinkService.updatePaymentLink({
704
641
  id: event.data.object.id,
705
- amount:
706
- event.data.object.line_items?.data.reduce(
707
- (total, item) => total + item.amount_total,
708
- 0
709
- ) ?? 0,
642
+ amount: event.data.object.line_items?.data.reduce(
643
+ (total, item) => total + item.amount_total,
644
+ 0
645
+ ) ?? 0,
710
646
  paymentMethods: event.data.object.payment_method_types,
711
- status: 'UPDATED',
647
+ status: "UPDATED",
712
648
  currency: event.data.object.currency
713
649
  });
714
650
  break;
715
651
  }
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
- ) {
652
+ case "plan.created": {
653
+ if (typeof event.data.object.product === "object" && event.data.object.product != null && event.data.object.amount != null) {
722
654
  this.planService.basePlanService.createPlan({
723
655
  id: event.data.object.id,
724
656
  billingProvider: BillingProviderEnum.STRIPE,
725
657
  cadence: event.data.object.interval,
726
658
  currency: event.data.object.currency,
727
659
  active: true,
728
- name:
729
- typeof event.data.object.product === 'string'
730
- ? event.data.object.product
731
- : event.data.object.product?.id,
660
+ name: typeof event.data.object.product === "string" ? event.data.object.product : event.data.object.product?.id,
732
661
  price: event.data.object.amount,
733
662
  externalId: event.data.object.id
734
663
  });
735
664
  } else {
736
- throw new Error('Invalid plan');
665
+ throw new Error("Invalid plan");
737
666
  }
738
667
  break;
739
668
  }
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
- ) {
669
+ case "plan.updated": {
670
+ if (typeof event.data.object.product === "object" && event.data.object.product != null && event.data.object.amount != null) {
746
671
  this.planService.basePlanService.updatePlan({
747
672
  id: event.data.object.id,
748
673
  billingProvider: BillingProviderEnum.STRIPE,
749
674
  cadence: event.data.object.interval,
750
675
  currency: event.data.object.currency,
751
676
  active: true,
752
- name:
753
- typeof event.data.object.product === 'string'
754
- ? event.data.object.product
755
- : event.data.object.product?.id,
677
+ name: typeof event.data.object.product === "string" ? event.data.object.product : event.data.object.product?.id,
756
678
  price: event.data.object.amount,
757
679
  externalId: event.data.object.id
758
680
  });
759
681
  } else {
760
- throw new Error('Invalid plan');
682
+ throw new Error("Invalid plan");
761
683
  }
762
684
  break;
763
685
  }
764
- case 'plan.deleted': {
686
+ case "plan.deleted": {
765
687
  this.planService.deletePlan({
766
688
  id: event.data.object.id
767
689
  });
768
690
  break;
769
691
  }
770
- case 'customer.subscription.created': {
692
+ case "customer.subscription.created": {
771
693
  this.subscriptionService.baseSubscriptionService.createSubscription({
772
694
  id: event.data.object.id,
773
- partyId:
774
- typeof event.data.object.customer === 'string'
775
- ? event.data.object.customer
776
- : event.data.object.customer.id,
777
- partyType: 'USER',
695
+ partyId: typeof event.data.object.customer === "string" ? event.data.object.customer : event.data.object.customer.id,
696
+ partyType: "USER",
778
697
  description: event.data.object.description ?? void 0,
779
698
  active: true,
780
699
  productId: event.data.object.items.data[0].plan.id,
781
700
  externalId: event.data.object.id,
782
701
  billingProvider: BillingProviderEnum.STRIPE,
783
702
  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),
703
+ endDate: event.data.object.cancel_at ? new Date(event.data.object.cancel_at) : /* @__PURE__ */ new Date(Infinity),
787
704
  status: event.data.object.status
788
705
  });
789
706
  break;
790
707
  }
791
- case 'customer.subscription.updated': {
708
+ case "customer.subscription.updated": {
792
709
  this.subscriptionService.baseSubscriptionService.updateSubscription({
793
710
  id: event.data.object.id,
794
- partyId:
795
- typeof event.data.object.customer === 'string'
796
- ? event.data.object.customer
797
- : event.data.object.customer.id,
798
- partyType: 'USER',
711
+ partyId: typeof event.data.object.customer === "string" ? event.data.object.customer : event.data.object.customer.id,
712
+ partyType: "USER",
799
713
  description: event.data.object.description ?? void 0,
800
714
  active: true,
801
715
  externalId: event.data.object.id,
802
716
  billingProvider: BillingProviderEnum.STRIPE,
803
717
  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),
718
+ endDate: event.data.object.cancel_at ? new Date(event.data.object.cancel_at) : /* @__PURE__ */ new Date(Infinity),
807
719
  productId: event.data.object.items.data[0].plan.id,
808
720
  status: event.data.object.status
809
721
  });
810
722
  break;
811
723
  }
812
- case 'customer.subscription.deleted': {
724
+ case "customer.subscription.deleted": {
813
725
  this.subscriptionService.deleteSubscription({
814
726
  id: event.data.object.id
815
727
  });
816
728
  break;
817
729
  }
818
- case 'customer.subscription.paused': {
730
+ case "customer.subscription.paused": {
819
731
  this.subscriptionService.cancelSubscription({
820
732
  id: event.data.object.id
821
733
  });
822
734
  break;
823
735
  }
824
- case 'customer.subscription.resumed': {
736
+ case "customer.subscription.resumed": {
825
737
  this.subscriptionService.resumeSubscription({
826
738
  id: event.data.object.id
827
739
  });
@@ -829,20 +741,26 @@ var StripeWebhookService = class {
829
741
  }
830
742
  default:
831
743
  this.openTelemetryCollector.info(
832
- 'Unprocessed stripe event type',
744
+ "Unprocessed stripe event type",
833
745
  eventType
834
746
  );
835
747
  break;
836
748
  }
749
+ await this.em.insert("StripeWebhookEvent", {
750
+ stripeId: event.id,
751
+ idempotencyKey: event.request?.idempotency_key,
752
+ eventType: event.type,
753
+ eventData: event.data
754
+ });
755
+ await this.em.flush();
837
756
  }
838
757
  };
839
758
  // Annotate the CommonJS export names for ESM import in node:
840
- 0 &&
841
- (module.exports = {
842
- StripeBillingPortalService,
843
- StripeCheckoutSessionService,
844
- StripePaymentLinkService,
845
- StripePlanService,
846
- StripeSubscriptionService,
847
- StripeWebhookService
848
- });
759
+ 0 && (module.exports = {
760
+ StripeBillingPortalService,
761
+ StripeCheckoutSessionService,
762
+ StripePaymentLinkService,
763
+ StripePlanService,
764
+ StripeSubscriptionService,
765
+ StripeWebhookService
766
+ });