@forklaunch/implementation-billing-stripe 0.2.4 → 0.2.6

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,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 === "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, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
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) => __copyProps(__defProp({}, "__esModule", { value: true }), 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,10 +34,18 @@ __export(services_exports, {
30
34
  module.exports = __toCommonJS(services_exports);
31
35
 
32
36
  // services/billingPortal.service.ts
33
- var import_services = require("@forklaunch/implementation-billing-base/services");
34
- var import_internal = require("@forklaunch/internal");
37
+ var import_services = require('@forklaunch/implementation-billing-base/services');
38
+ var import_internal = require('@forklaunch/internal');
35
39
  var StripeBillingPortalService = class {
36
- constructor(stripeClient, em, cache, openTelemetryCollector, schemaValidator, mappers, options) {
40
+ constructor(
41
+ stripeClient,
42
+ em,
43
+ cache,
44
+ openTelemetryCollector,
45
+ schemaValidator,
46
+ mappers,
47
+ options
48
+ ) {
37
49
  this.options = options;
38
50
  this.stripeClient = stripeClient;
39
51
  this.em = em;
@@ -41,19 +53,23 @@ var StripeBillingPortalService = class {
41
53
  this.openTelemetryCollector = openTelemetryCollector;
42
54
  this.schemaValidator = schemaValidator;
43
55
  this.mappers = mappers;
44
- this._mappers = (0, import_internal.transformIntoInternalMapper)(mappers, schemaValidator);
45
- this.baseBillingPortalService = new import_services.BaseBillingPortalService(
46
- em,
47
- cache,
48
- openTelemetryCollector,
49
- schemaValidator,
50
- {
51
- BillingPortalMapper: import_internal.IdentityResponseMapper,
52
- CreateBillingPortalMapper: import_internal.IdentityRequestMapper,
53
- UpdateBillingPortalMapper: import_internal.IdentityRequestMapper
54
- },
55
- options
56
+ this._mappers = (0, import_internal.transformIntoInternalMapper)(
57
+ mappers,
58
+ schemaValidator
56
59
  );
60
+ this.baseBillingPortalService =
61
+ new import_services.BaseBillingPortalService(
62
+ em,
63
+ cache,
64
+ openTelemetryCollector,
65
+ schemaValidator,
66
+ {
67
+ BillingPortalMapper: import_internal.IdentityResponseMapper,
68
+ CreateBillingPortalMapper: import_internal.IdentityRequestMapper,
69
+ UpdateBillingPortalMapper: import_internal.IdentityRequestMapper
70
+ },
71
+ options
72
+ );
57
73
  }
58
74
  billingPortalSessionExpiryDurationMs = 5 * 60 * 1e3;
59
75
  baseBillingPortalService;
@@ -69,26 +85,28 @@ var StripeBillingPortalService = class {
69
85
  ...billingPortalDto.stripeFields,
70
86
  customer: billingPortalDto.customerId
71
87
  });
72
- const billingPortalEntity = await this.baseBillingPortalService.createBillingPortalSession(
73
- await this._mappers.CreateBillingPortalMapper.deserializeDtoToEntity(
74
- {
75
- ...billingPortalDto,
76
- id: session.id,
77
- uri: session.url,
78
- expiresAt: new Date(
79
- Date.now() + this.billingPortalSessionExpiryDurationMs
80
- )
81
- },
82
- this.em,
83
- session
84
- )
85
- );
88
+ const billingPortalEntity =
89
+ await this.baseBillingPortalService.createBillingPortalSession(
90
+ await this._mappers.CreateBillingPortalMapper.deserializeDtoToEntity(
91
+ {
92
+ ...billingPortalDto,
93
+ id: session.id,
94
+ uri: session.url,
95
+ expiresAt: new Date(
96
+ Date.now() + this.billingPortalSessionExpiryDurationMs
97
+ )
98
+ },
99
+ this.em,
100
+ session
101
+ )
102
+ );
86
103
  return this._mappers.BillingPortalMapper.serializeEntityToDto(
87
104
  billingPortalEntity
88
105
  );
89
106
  }
90
107
  async getBillingPortalSession(idDto) {
91
- const billingPortalEntity = await this.baseBillingPortalService.getBillingPortalSession(idDto);
108
+ const billingPortalEntity =
109
+ await this.baseBillingPortalService.getBillingPortalSession(idDto);
92
110
  return this._mappers.BillingPortalMapper.serializeEntityToDto(
93
111
  billingPortalEntity
94
112
  );
@@ -97,27 +115,29 @@ var StripeBillingPortalService = class {
97
115
  return this.baseBillingPortalService.expireBillingPortalSession(idDto);
98
116
  }
99
117
  async updateBillingPortalSession(billingPortalDto) {
100
- const existingSession = await this.baseBillingPortalService.getBillingPortalSession({
101
- id: billingPortalDto.id
102
- });
118
+ const existingSession =
119
+ await this.baseBillingPortalService.getBillingPortalSession({
120
+ id: billingPortalDto.id
121
+ });
103
122
  const session = await this.stripeClient.billingPortal.sessions.create({
104
123
  ...billingPortalDto.stripeFields,
105
124
  customer: existingSession.customerId
106
125
  });
107
- const baseBillingPortalDto = await this.baseBillingPortalService.updateBillingPortalSession(
108
- await this._mappers.UpdateBillingPortalMapper.deserializeDtoToEntity(
109
- {
110
- ...billingPortalDto,
111
- id: session.id,
112
- uri: session.url,
113
- expiresAt: new Date(
114
- Date.now() + this.billingPortalSessionExpiryDurationMs
115
- )
116
- },
117
- this.em,
118
- session
119
- )
120
- );
126
+ const baseBillingPortalDto =
127
+ await this.baseBillingPortalService.updateBillingPortalSession(
128
+ await this._mappers.UpdateBillingPortalMapper.deserializeDtoToEntity(
129
+ {
130
+ ...billingPortalDto,
131
+ id: session.id,
132
+ uri: session.url,
133
+ expiresAt: new Date(
134
+ Date.now() + this.billingPortalSessionExpiryDurationMs
135
+ )
136
+ },
137
+ this.em,
138
+ session
139
+ )
140
+ );
121
141
  return this._mappers.BillingPortalMapper.serializeEntityToDto(
122
142
  baseBillingPortalDto
123
143
  );
@@ -125,10 +145,18 @@ var StripeBillingPortalService = class {
125
145
  };
126
146
 
127
147
  // services/checkoutSession.service.ts
128
- var import_services2 = require("@forklaunch/implementation-billing-base/services");
129
- var import_internal2 = require("@forklaunch/internal");
148
+ var import_services2 = require('@forklaunch/implementation-billing-base/services');
149
+ var import_internal2 = require('@forklaunch/internal');
130
150
  var StripeCheckoutSessionService = class {
131
- constructor(stripeClient, em, cache, openTelemetryCollector, schemaValidator, mappers, options) {
151
+ constructor(
152
+ stripeClient,
153
+ em,
154
+ cache,
155
+ openTelemetryCollector,
156
+ schemaValidator,
157
+ mappers,
158
+ options
159
+ ) {
132
160
  this.options = options;
133
161
  this.stripeClient = stripeClient;
134
162
  this.em = em;
@@ -136,19 +164,23 @@ var StripeCheckoutSessionService = class {
136
164
  this.openTelemetryCollector = openTelemetryCollector;
137
165
  this.schemaValidator = schemaValidator;
138
166
  this.mappers = mappers;
139
- this._mappers = (0, import_internal2.transformIntoInternalMapper)(mappers, schemaValidator);
140
- this.baseCheckoutSessionService = new import_services2.BaseCheckoutSessionService(
141
- em,
142
- cache,
143
- openTelemetryCollector,
144
- schemaValidator,
145
- {
146
- CheckoutSessionMapper: import_internal2.IdentityResponseMapper,
147
- CreateCheckoutSessionMapper: import_internal2.IdentityRequestMapper,
148
- UpdateCheckoutSessionMapper: import_internal2.IdentityRequestMapper
149
- },
150
- options
167
+ this._mappers = (0, import_internal2.transformIntoInternalMapper)(
168
+ mappers,
169
+ schemaValidator
151
170
  );
171
+ this.baseCheckoutSessionService =
172
+ new import_services2.BaseCheckoutSessionService(
173
+ em,
174
+ cache,
175
+ openTelemetryCollector,
176
+ schemaValidator,
177
+ {
178
+ CheckoutSessionMapper: import_internal2.IdentityResponseMapper,
179
+ CreateCheckoutSessionMapper: import_internal2.IdentityRequestMapper,
180
+ UpdateCheckoutSessionMapper: import_internal2.IdentityRequestMapper
181
+ },
182
+ options
183
+ );
152
184
  }
153
185
  baseCheckoutSessionService;
154
186
  _mappers;
@@ -166,27 +198,27 @@ var StripeCheckoutSessionService = class {
166
198
  success_url: checkoutSessionDto.successRedirectUri,
167
199
  cancel_url: checkoutSessionDto.cancelRedirectUri
168
200
  });
169
- const checkoutSessionEntity = await this.baseCheckoutSessionService.createCheckoutSession(
170
- await this._mappers.CreateCheckoutSessionMapper.deserializeDtoToEntity(
171
- {
172
- ...checkoutSessionDto,
173
- id: session.id,
174
- uri: session.url,
175
- expiresAt: new Date(Date.now() + 5 * 60 * 1e3),
176
- providerFields: session
177
- },
178
- this.em,
179
- session
180
- )
181
- );
201
+ const checkoutSessionEntity =
202
+ await this.baseCheckoutSessionService.createCheckoutSession(
203
+ await this._mappers.CreateCheckoutSessionMapper.deserializeDtoToEntity(
204
+ {
205
+ ...checkoutSessionDto,
206
+ id: session.id,
207
+ uri: session.url,
208
+ expiresAt: new Date(Date.now() + 5 * 60 * 1e3),
209
+ providerFields: session
210
+ },
211
+ this.em,
212
+ session
213
+ )
214
+ );
182
215
  return this._mappers.CheckoutSessionMapper.serializeEntityToDto(
183
216
  checkoutSessionEntity
184
217
  );
185
218
  }
186
- async getCheckoutSession({
187
- id
188
- }) {
189
- const databaseCheckoutSession = await this.baseCheckoutSessionService.getCheckoutSession({ id });
219
+ async getCheckoutSession({ id }) {
220
+ const databaseCheckoutSession =
221
+ await this.baseCheckoutSessionService.getCheckoutSession({ id });
190
222
  return {
191
223
  ...this._mappers.CheckoutSessionMapper.serializeEntityToDto(
192
224
  databaseCheckoutSession
@@ -201,7 +233,7 @@ var StripeCheckoutSessionService = class {
201
233
  async handleCheckoutSuccess({ id }) {
202
234
  await this.stripeClient.checkout.sessions.update(id, {
203
235
  metadata: {
204
- status: "SUCCESS"
236
+ status: 'SUCCESS'
205
237
  }
206
238
  });
207
239
  await this.baseCheckoutSessionService.handleCheckoutSuccess({ id });
@@ -209,7 +241,7 @@ var StripeCheckoutSessionService = class {
209
241
  async handleCheckoutFailure({ id }) {
210
242
  await this.stripeClient.checkout.sessions.update(id, {
211
243
  metadata: {
212
- status: "FAILED"
244
+ status: 'FAILED'
213
245
  }
214
246
  });
215
247
  await this.baseCheckoutSessionService.handleCheckoutFailure({ id });
@@ -217,10 +249,18 @@ var StripeCheckoutSessionService = class {
217
249
  };
218
250
 
219
251
  // services/paymentLink.service.ts
220
- var import_services3 = require("@forklaunch/implementation-billing-base/services");
221
- var import_internal3 = require("@forklaunch/internal");
252
+ var import_services3 = require('@forklaunch/implementation-billing-base/services');
253
+ var import_internal3 = require('@forklaunch/internal');
222
254
  var StripePaymentLinkService = class {
223
- constructor(stripeClient, em, cache, openTelemetryCollector, schemaValidator, mappers, options) {
255
+ constructor(
256
+ stripeClient,
257
+ em,
258
+ cache,
259
+ openTelemetryCollector,
260
+ schemaValidator,
261
+ mappers,
262
+ options
263
+ ) {
224
264
  this.options = options;
225
265
  this.stripeClient = stripeClient;
226
266
  this.em = em;
@@ -228,7 +268,10 @@ var StripePaymentLinkService = class {
228
268
  this.openTelemetryCollector = openTelemetryCollector;
229
269
  this.schemaValidator = schemaValidator;
230
270
  this.mappers = mappers;
231
- this._mappers = (0, import_internal3.transformIntoInternalMapper)(mappers, schemaValidator);
271
+ this._mappers = (0, import_internal3.transformIntoInternalMapper)(
272
+ mappers,
273
+ schemaValidator
274
+ );
232
275
  this.basePaymentLinkService = new import_services3.BasePaymentLinkService(
233
276
  em,
234
277
  cache,
@@ -256,20 +299,22 @@ var StripePaymentLinkService = class {
256
299
  payment_method_types: paymentLinkDto.paymentMethods,
257
300
  currency: paymentLinkDto.currency
258
301
  });
259
- const paymentLinkEntity = await this.basePaymentLinkService.createPaymentLink(
260
- await this._mappers.CreatePaymentLinkMapper.deserializeDtoToEntity(
261
- {
262
- ...paymentLinkDto,
263
- id: session.id,
264
- amount: session.line_items?.data.reduce(
265
- (total, item) => total + item.amount_total,
266
- 0
267
- ) ?? 0
268
- },
269
- this.em,
270
- session
271
- )
272
- );
302
+ const paymentLinkEntity =
303
+ await this.basePaymentLinkService.createPaymentLink(
304
+ await this._mappers.CreatePaymentLinkMapper.deserializeDtoToEntity(
305
+ {
306
+ ...paymentLinkDto,
307
+ id: session.id,
308
+ amount:
309
+ session.line_items?.data.reduce(
310
+ (total, item) => total + item.amount_total,
311
+ 0
312
+ ) ?? 0
313
+ },
314
+ this.em,
315
+ session
316
+ )
317
+ );
273
318
  return this._mappers.PaymentLinkMapper.serializeEntityToDto(
274
319
  paymentLinkEntity
275
320
  );
@@ -282,26 +327,29 @@ var StripePaymentLinkService = class {
282
327
  payment_method_types: paymentLinkDto.paymentMethods
283
328
  }
284
329
  );
285
- const paymentLinkEntity = await this.basePaymentLinkService.updatePaymentLink(
286
- await this._mappers.UpdatePaymentLinkMapper.deserializeDtoToEntity(
287
- {
288
- ...paymentLinkDto,
289
- id: session.id,
290
- amount: session.line_items?.data.reduce(
291
- (total, item) => total + item.amount_total,
292
- 0
293
- ) ?? 0
294
- },
295
- this.em,
296
- session
297
- )
298
- );
330
+ const paymentLinkEntity =
331
+ await this.basePaymentLinkService.updatePaymentLink(
332
+ await this._mappers.UpdatePaymentLinkMapper.deserializeDtoToEntity(
333
+ {
334
+ ...paymentLinkDto,
335
+ id: session.id,
336
+ amount:
337
+ session.line_items?.data.reduce(
338
+ (total, item) => total + item.amount_total,
339
+ 0
340
+ ) ?? 0
341
+ },
342
+ this.em,
343
+ session
344
+ )
345
+ );
299
346
  return this._mappers.PaymentLinkMapper.serializeEntityToDto(
300
347
  paymentLinkEntity
301
348
  );
302
349
  }
303
350
  async getPaymentLink({ id }) {
304
- const databasePaymentLink = await this.basePaymentLinkService.getPaymentLink({ id });
351
+ const databasePaymentLink =
352
+ await this.basePaymentLinkService.getPaymentLink({ id });
305
353
  return {
306
354
  ...this._mappers.PaymentLinkMapper.serializeEntityToDto(
307
355
  databasePaymentLink
@@ -312,7 +360,7 @@ var StripePaymentLinkService = class {
312
360
  async expirePaymentLink({ id }) {
313
361
  await this.stripeClient.paymentLinks.update(id, {
314
362
  metadata: {
315
- status: "EXPIRED"
363
+ status: 'EXPIRED'
316
364
  }
317
365
  });
318
366
  await this.basePaymentLinkService.expirePaymentLink({ id });
@@ -320,7 +368,7 @@ var StripePaymentLinkService = class {
320
368
  async handlePaymentSuccess({ id }) {
321
369
  await this.stripeClient.paymentLinks.update(id, {
322
370
  metadata: {
323
- status: "COMPLETED"
371
+ status: 'COMPLETED'
324
372
  }
325
373
  });
326
374
  await this.basePaymentLinkService.handlePaymentSuccess({ id });
@@ -328,7 +376,7 @@ var StripePaymentLinkService = class {
328
376
  async handlePaymentFailure({ id }) {
329
377
  await this.stripeClient.paymentLinks.update(id, {
330
378
  metadata: {
331
- status: "FAILED"
379
+ status: 'FAILED'
332
380
  }
333
381
  });
334
382
  await this.basePaymentLinkService.handlePaymentFailure({ id });
@@ -340,9 +388,9 @@ var StripePaymentLinkService = class {
340
388
  return await Promise.all(
341
389
  (await this.basePaymentLinkService.listPaymentLinks(idsDto)).map(
342
390
  async (paymentLink) => ({
343
- ...await this._mappers.PaymentLinkMapper.serializeEntityToDto(
391
+ ...(await this._mappers.PaymentLinkMapper.serializeEntityToDto(
344
392
  paymentLink
345
- ),
393
+ )),
346
394
  stripeFields: paymentLinks.data.find(
347
395
  (paymentLink2) => paymentLink2.id === paymentLink2.id
348
396
  )
@@ -353,17 +401,27 @@ var StripePaymentLinkService = class {
353
401
  };
354
402
 
355
403
  // services/plan.service.ts
356
- var import_services4 = require("@forklaunch/implementation-billing-base/services");
357
- var import_internal4 = require("@forklaunch/internal");
404
+ var import_services4 = require('@forklaunch/implementation-billing-base/services');
405
+ var import_internal4 = require('@forklaunch/internal');
358
406
  var StripePlanService = class {
359
- constructor(stripeClient, em, openTelemetryCollector, schemaValidator, mappers, options) {
407
+ constructor(
408
+ stripeClient,
409
+ em,
410
+ openTelemetryCollector,
411
+ schemaValidator,
412
+ mappers,
413
+ options
414
+ ) {
360
415
  this.options = options;
361
416
  this.stripeClient = stripeClient;
362
417
  this.em = em;
363
418
  this.openTelemetryCollector = openTelemetryCollector;
364
419
  this.schemaValidator = schemaValidator;
365
420
  this.mappers = mappers;
366
- this._mappers = (0, import_internal4.transformIntoInternalMapper)(mappers, schemaValidator);
421
+ this._mappers = (0, import_internal4.transformIntoInternalMapper)(
422
+ mappers,
423
+ schemaValidator
424
+ );
367
425
  this.basePlanService = new import_services4.BasePlanService(
368
426
  em,
369
427
  openTelemetryCollector,
@@ -395,7 +453,7 @@ var StripePlanService = class {
395
453
  {
396
454
  ...planDto,
397
455
  externalId: plan.id,
398
- billingProvider: "stripe"
456
+ billingProvider: 'stripe'
399
457
  },
400
458
  em ?? this.em,
401
459
  plan
@@ -406,24 +464,25 @@ var StripePlanService = class {
406
464
  }
407
465
  async getPlan(idDto, em) {
408
466
  const plan = await this.stripeClient.plans.retrieve(idDto.id);
409
- const id = (await em?.findOne(
410
- this.options?.databaseTableName ?? "plan",
411
- { externalId: idDto.id }
412
- ))?.id;
467
+ const id = (
468
+ await em?.findOne(this.options?.databaseTableName ?? 'plan', {
469
+ externalId: idDto.id
470
+ })
471
+ )?.id;
413
472
  if (!id) {
414
- throw new Error("Plan not found");
473
+ throw new Error('Plan not found');
415
474
  }
416
475
  return {
417
- ...await this._mappers.PlanMapper.serializeEntityToDto(
476
+ ...(await this._mappers.PlanMapper.serializeEntityToDto(
418
477
  await this.basePlanService.getPlan({ id }, em)
419
- ),
478
+ )),
420
479
  stripeFields: plan
421
480
  };
422
481
  }
423
482
  async updatePlan(planDto, em) {
424
483
  const existingPlan = await this.stripeClient.plans.retrieve(planDto.id);
425
- const plan = await this.stripeClient.plans.del(planDto.id).then(
426
- () => this.stripeClient.plans.create({
484
+ const plan = await this.stripeClient.plans.del(planDto.id).then(() =>
485
+ this.stripeClient.plans.create({
427
486
  ...planDto.stripeFields,
428
487
  interval: planDto.cadence ?? existingPlan.interval,
429
488
  product: planDto.name,
@@ -435,7 +494,7 @@ var StripePlanService = class {
435
494
  {
436
495
  ...planDto,
437
496
  externalId: plan.id,
438
- billingProvider: "stripe"
497
+ billingProvider: 'stripe'
439
498
  },
440
499
  em ?? this.em,
441
500
  plan
@@ -452,16 +511,19 @@ var StripePlanService = class {
452
511
  const plans = await this.stripeClient.plans.list({
453
512
  active: true
454
513
  });
455
- const ids = (await em?.findAll(
456
- this.options?.databaseTableName ?? "plan",
457
- { where: { externalId: { $in: plans.data.map((plan) => plan.id) } } }
458
- ))?.filter((s) => idsDto?.ids?.includes(s.id))?.map((s) => s.id);
514
+ const ids = (
515
+ await em?.findAll(this.options?.databaseTableName ?? 'plan', {
516
+ where: { externalId: { $in: plans.data.map((plan) => plan.id) } }
517
+ })
518
+ )
519
+ ?.filter((s) => idsDto?.ids?.includes(s.id))
520
+ ?.map((s) => s.id);
459
521
  if (!ids) {
460
- throw new Error("Plans not found");
522
+ throw new Error('Plans not found');
461
523
  }
462
524
  return await Promise.all(
463
525
  (await this.basePlanService.listPlans({ ids }, em)).map(async (plan) => ({
464
- ...await this._mappers.PlanMapper.serializeEntityToDto(plan),
526
+ ...(await this._mappers.PlanMapper.serializeEntityToDto(plan)),
465
527
  stripeFields: plans.data.find(
466
528
  (stripePlan) => stripePlan.id === plan.externalId
467
529
  )
@@ -471,17 +533,27 @@ var StripePlanService = class {
471
533
  };
472
534
 
473
535
  // services/subscription.service.ts
474
- var import_services5 = require("@forklaunch/implementation-billing-base/services");
475
- var import_internal5 = require("@forklaunch/internal");
536
+ var import_services5 = require('@forklaunch/implementation-billing-base/services');
537
+ var import_internal5 = require('@forklaunch/internal');
476
538
  var StripeSubscriptionService = class {
477
- constructor(stripe, em, openTelemetryCollector, schemaValidator, mappers, options) {
539
+ constructor(
540
+ stripe,
541
+ em,
542
+ openTelemetryCollector,
543
+ schemaValidator,
544
+ mappers,
545
+ options
546
+ ) {
478
547
  this.options = options;
479
548
  this.stripe = stripe;
480
549
  this.em = em;
481
550
  this.openTelemetryCollector = openTelemetryCollector;
482
551
  this.schemaValidator = schemaValidator;
483
552
  this.mappers = mappers;
484
- this._mappers = (0, import_internal5.transformIntoInternalMapper)(mappers, schemaValidator);
553
+ this._mappers = (0, import_internal5.transformIntoInternalMapper)(
554
+ mappers,
555
+ schemaValidator
556
+ );
485
557
  this.baseSubscriptionService = new import_services5.BaseSubscriptionService(
486
558
  em,
487
559
  openTelemetryCollector,
@@ -511,53 +583,55 @@ var StripeSubscriptionService = class {
511
583
  }
512
584
  ]
513
585
  });
514
- const subscriptionEntity = await this.baseSubscriptionService.createSubscription(
515
- await this._mappers.CreateSubscriptionMapper.deserializeDtoToEntity(
516
- {
517
- ...subscriptionDto,
518
- externalId: subscription.id,
519
- billingProvider: "stripe"
520
- },
521
- em ?? this.em,
522
- subscription
523
- ),
524
- em
525
- );
586
+ const subscriptionEntity =
587
+ await this.baseSubscriptionService.createSubscription(
588
+ await this._mappers.CreateSubscriptionMapper.deserializeDtoToEntity(
589
+ {
590
+ ...subscriptionDto,
591
+ externalId: subscription.id,
592
+ billingProvider: 'stripe'
593
+ },
594
+ em ?? this.em,
595
+ subscription
596
+ ),
597
+ em
598
+ );
526
599
  return this._mappers.SubscriptionMapper.serializeEntityToDto(
527
600
  subscriptionEntity
528
601
  );
529
602
  }
530
603
  async getSubscription(idDto, em) {
531
604
  return {
532
- ...await this._mappers.SubscriptionMapper.serializeEntityToDto(
605
+ ...(await this._mappers.SubscriptionMapper.serializeEntityToDto(
533
606
  await this.baseSubscriptionService.getSubscription(idDto, em)
534
- ),
607
+ )),
535
608
  stripeFields: await this.stripe.subscriptions.retrieve(idDto.id)
536
609
  };
537
610
  }
538
611
  async getUserSubscription(idDto, em) {
539
612
  return {
540
- ...await this._mappers.SubscriptionMapper.serializeEntityToDto(
613
+ ...(await this._mappers.SubscriptionMapper.serializeEntityToDto(
541
614
  await this.baseSubscriptionService.getUserSubscription(idDto, em)
542
- ),
615
+ )),
543
616
  stripeFields: await this.stripe.subscriptions.retrieve(idDto.id)
544
617
  };
545
618
  }
546
619
  async getOrganizationSubscription(idDto, em) {
547
- const id = (await em?.findOne(
548
- this.options?.databaseTableName ?? "subscription",
549
- { externalId: idDto.id }
550
- ))?.id;
620
+ const id = (
621
+ await em?.findOne(this.options?.databaseTableName ?? 'subscription', {
622
+ externalId: idDto.id
623
+ })
624
+ )?.id;
551
625
  if (!id) {
552
- throw new Error("Subscription not found");
626
+ throw new Error('Subscription not found');
553
627
  }
554
628
  return {
555
- ...await this._mappers.SubscriptionMapper.serializeEntityToDto(
629
+ ...(await this._mappers.SubscriptionMapper.serializeEntityToDto(
556
630
  await this.baseSubscriptionService.getOrganizationSubscription(
557
631
  { id },
558
632
  em
559
633
  )
560
- ),
634
+ )),
561
635
  stripeFields: await this.stripe.subscriptions.retrieve(idDto.id)
562
636
  };
563
637
  }
@@ -573,19 +647,20 @@ var StripeSubscriptionService = class {
573
647
  ]
574
648
  }
575
649
  );
576
- const subscriptionEntity = await this.baseSubscriptionService.updateSubscription(
577
- await this._mappers.UpdateSubscriptionMapper.deserializeDtoToEntity(
578
- {
579
- ...subscriptionDto,
580
- externalId: subscription.id,
581
- billingProvider: "stripe",
582
- providerFields: subscription
583
- },
584
- em ?? this.em,
585
- subscription
586
- ),
587
- em
588
- );
650
+ const subscriptionEntity =
651
+ await this.baseSubscriptionService.updateSubscription(
652
+ await this._mappers.UpdateSubscriptionMapper.deserializeDtoToEntity(
653
+ {
654
+ ...subscriptionDto,
655
+ externalId: subscription.id,
656
+ billingProvider: 'stripe',
657
+ providerFields: subscription
658
+ },
659
+ em ?? this.em,
660
+ subscription
661
+ ),
662
+ em
663
+ );
589
664
  return this._mappers.SubscriptionMapper.serializeEntityToDto(
590
665
  subscriptionEntity
591
666
  );
@@ -595,23 +670,26 @@ var StripeSubscriptionService = class {
595
670
  await this.baseSubscriptionService.deleteSubscription(idDto, em);
596
671
  }
597
672
  async listSubscriptions(idsDto, em) {
598
- const subscriptions = (await this.stripe.subscriptions.list({
599
- status: "active"
600
- })).data.filter((s) => idsDto.ids?.includes(s.id));
601
- const ids = (await em?.findAll(
602
- this.options?.databaseTableName ?? "subscription",
603
- { where: { externalId: { $in: subscriptions.map((s) => s.id) } } }
604
- ))?.map((s) => s.id);
673
+ const subscriptions = (
674
+ await this.stripe.subscriptions.list({
675
+ status: 'active'
676
+ })
677
+ ).data.filter((s) => idsDto.ids?.includes(s.id));
678
+ const ids = (
679
+ await em?.findAll(this.options?.databaseTableName ?? 'subscription', {
680
+ where: { externalId: { $in: subscriptions.map((s) => s.id) } }
681
+ })
682
+ )?.map((s) => s.id);
605
683
  if (!ids) {
606
- throw new Error("Subscriptions not found");
684
+ throw new Error('Subscriptions not found');
607
685
  }
608
686
  return await Promise.all(
609
687
  (await this.baseSubscriptionService.listSubscriptions({ ids }, em)).map(
610
688
  async (subscription) => {
611
689
  return {
612
- ...await this._mappers.SubscriptionMapper.serializeEntityToDto(
690
+ ...(await this._mappers.SubscriptionMapper.serializeEntityToDto(
613
691
  subscription
614
- ),
692
+ )),
615
693
  stripeFields: subscriptions.find(
616
694
  (s) => s.id === subscription.externalId
617
695
  )
@@ -632,7 +710,7 @@ var StripeSubscriptionService = class {
632
710
 
633
711
  // domain/enum/billingProvider.enum.ts
634
712
  var BillingProviderEnum = {
635
- STRIPE: "stripe"
713
+ STRIPE: 'stripe'
636
714
  };
637
715
 
638
716
  // services/webhook.service.ts
@@ -646,7 +724,17 @@ var StripeWebhookService = class {
646
724
  paymentLinkService;
647
725
  planService;
648
726
  subscriptionService;
649
- constructor(stripeClient, em, schemaValidator, openTelemetryCollector, billingPortalService, checkoutSessionService, paymentLinkService, planService, subscriptionService) {
727
+ constructor(
728
+ stripeClient,
729
+ em,
730
+ schemaValidator,
731
+ openTelemetryCollector,
732
+ billingPortalService,
733
+ checkoutSessionService,
734
+ paymentLinkService,
735
+ planService,
736
+ subscriptionService
737
+ ) {
650
738
  this.stripeClient = stripeClient;
651
739
  this.em = em;
652
740
  this.schemaValidator = schemaValidator;
@@ -659,11 +747,11 @@ var StripeWebhookService = class {
659
747
  }
660
748
  async handleWebhookEvent(event) {
661
749
  if (this.openTelemetryCollector) {
662
- this.openTelemetryCollector.info("Handling webhook event", event);
750
+ this.openTelemetryCollector.info('Handling webhook event', event);
663
751
  }
664
752
  const eventType = event.type;
665
753
  switch (eventType) {
666
- case "billing_portal.session.created": {
754
+ case 'billing_portal.session.created': {
667
755
  this.billingPortalService.baseBillingPortalService.createBillingPortalSession(
668
756
  {
669
757
  id: event.data.object.id,
@@ -675,94 +763,113 @@ var StripeWebhookService = class {
675
763
  );
676
764
  break;
677
765
  }
678
- case "checkout.session.expired": {
766
+ case 'checkout.session.expired': {
679
767
  this.checkoutSessionService.handleCheckoutFailure({
680
768
  id: event.data.object.id
681
769
  });
682
770
  break;
683
771
  }
684
- case "checkout.session.completed": {
772
+ case 'checkout.session.completed': {
685
773
  this.checkoutSessionService.handleCheckoutSuccess({
686
774
  id: event.data.object.id
687
775
  });
688
776
  break;
689
777
  }
690
- case "payment_link.created":
778
+ case 'payment_link.created':
691
779
  {
692
780
  this.paymentLinkService.basePaymentLinkService.createPaymentLink({
693
781
  id: event.data.object.id,
694
- amount: event.data.object.line_items?.data.reduce(
695
- (total, item) => total + item.amount_total,
696
- 0
697
- ) ?? 0,
782
+ amount:
783
+ event.data.object.line_items?.data.reduce(
784
+ (total, item) => total + item.amount_total,
785
+ 0
786
+ ) ?? 0,
698
787
  paymentMethods: event.data.object.payment_method_types,
699
- status: "CREATED",
788
+ status: 'CREATED',
700
789
  currency: event.data.object.currency,
701
790
  providerFields: event.data.object
702
791
  });
703
792
  }
704
793
  break;
705
- case "payment_link.updated": {
794
+ case 'payment_link.updated': {
706
795
  this.paymentLinkService.basePaymentLinkService.updatePaymentLink({
707
796
  id: event.data.object.id,
708
- amount: event.data.object.line_items?.data.reduce(
709
- (total, item) => total + item.amount_total,
710
- 0
711
- ) ?? 0,
797
+ amount:
798
+ event.data.object.line_items?.data.reduce(
799
+ (total, item) => total + item.amount_total,
800
+ 0
801
+ ) ?? 0,
712
802
  paymentMethods: event.data.object.payment_method_types,
713
- status: "UPDATED",
803
+ status: 'UPDATED',
714
804
  currency: event.data.object.currency,
715
805
  providerFields: event.data.object
716
806
  });
717
807
  break;
718
808
  }
719
- case "plan.created": {
720
- if (typeof event.data.object.product === "object" && event.data.object.product != null && event.data.object.amount != null) {
809
+ case 'plan.created': {
810
+ if (
811
+ typeof event.data.object.product === 'object' &&
812
+ event.data.object.product != null &&
813
+ event.data.object.amount != null
814
+ ) {
721
815
  this.planService.basePlanService.createPlan({
722
816
  id: event.data.object.id,
723
817
  billingProvider: BillingProviderEnum.STRIPE,
724
818
  cadence: event.data.object.interval,
725
819
  currency: event.data.object.currency,
726
820
  active: true,
727
- name: typeof event.data.object.product === "string" ? event.data.object.product : event.data.object.product?.id,
821
+ name:
822
+ typeof event.data.object.product === 'string'
823
+ ? event.data.object.product
824
+ : event.data.object.product?.id,
728
825
  price: event.data.object.amount,
729
826
  externalId: event.data.object.id,
730
827
  providerFields: event.data.object
731
828
  });
732
829
  } else {
733
- throw new Error("Invalid plan");
830
+ throw new Error('Invalid plan');
734
831
  }
735
832
  break;
736
833
  }
737
- case "plan.updated": {
738
- if (typeof event.data.object.product === "object" && event.data.object.product != null && event.data.object.amount != null) {
834
+ case 'plan.updated': {
835
+ if (
836
+ typeof event.data.object.product === 'object' &&
837
+ event.data.object.product != null &&
838
+ event.data.object.amount != null
839
+ ) {
739
840
  this.planService.basePlanService.updatePlan({
740
841
  id: event.data.object.id,
741
842
  billingProvider: BillingProviderEnum.STRIPE,
742
843
  cadence: event.data.object.interval,
743
844
  currency: event.data.object.currency,
744
845
  active: true,
745
- name: typeof event.data.object.product === "string" ? event.data.object.product : event.data.object.product?.id,
846
+ name:
847
+ typeof event.data.object.product === 'string'
848
+ ? event.data.object.product
849
+ : event.data.object.product?.id,
746
850
  price: event.data.object.amount,
747
851
  externalId: event.data.object.id,
748
852
  providerFields: event.data.object
749
853
  });
750
854
  } else {
751
- throw new Error("Invalid plan");
855
+ throw new Error('Invalid plan');
752
856
  }
753
857
  break;
754
858
  }
755
- case "plan.deleted": {
859
+ case 'plan.deleted': {
756
860
  this.planService.deletePlan({
757
861
  id: event.data.object.id
758
862
  });
759
863
  break;
760
864
  }
761
- case "customer.subscription.created": {
865
+ case 'customer.subscription.created': {
762
866
  this.subscriptionService.baseSubscriptionService.createSubscription({
763
867
  id: event.data.object.id,
764
- partyId: typeof event.data.object.customer === "string" ? event.data.object.customer : event.data.object.customer.id,
765
- partyType: "USER",
868
+ partyId:
869
+ typeof event.data.object.customer === 'string'
870
+ ? event.data.object.customer
871
+ : event.data.object.customer.id,
872
+ partyType: 'USER',
766
873
  description: event.data.object.description ?? void 0,
767
874
  active: true,
768
875
  productId: event.data.object.items.data[0].plan.id,
@@ -770,41 +877,48 @@ var StripeWebhookService = class {
770
877
  externalId: event.data.object.id,
771
878
  billingProvider: BillingProviderEnum.STRIPE,
772
879
  startDate: new Date(event.data.object.created),
773
- endDate: event.data.object.cancel_at ? new Date(event.data.object.cancel_at) : /* @__PURE__ */ new Date(Infinity),
880
+ endDate: event.data.object.cancel_at
881
+ ? new Date(event.data.object.cancel_at)
882
+ : /* @__PURE__ */ new Date(Infinity),
774
883
  status: event.data.object.status
775
884
  });
776
885
  break;
777
886
  }
778
- case "customer.subscription.updated": {
887
+ case 'customer.subscription.updated': {
779
888
  this.subscriptionService.baseSubscriptionService.updateSubscription({
780
889
  id: event.data.object.id,
781
- partyId: typeof event.data.object.customer === "string" ? event.data.object.customer : event.data.object.customer.id,
782
- partyType: "USER",
890
+ partyId:
891
+ typeof event.data.object.customer === 'string'
892
+ ? event.data.object.customer
893
+ : event.data.object.customer.id,
894
+ partyType: 'USER',
783
895
  description: event.data.object.description ?? void 0,
784
896
  active: true,
785
897
  providerFields: event.data.object,
786
898
  externalId: event.data.object.id,
787
899
  billingProvider: BillingProviderEnum.STRIPE,
788
900
  startDate: new Date(event.data.object.created),
789
- endDate: event.data.object.cancel_at ? new Date(event.data.object.cancel_at) : /* @__PURE__ */ new Date(Infinity),
901
+ endDate: event.data.object.cancel_at
902
+ ? new Date(event.data.object.cancel_at)
903
+ : /* @__PURE__ */ new Date(Infinity),
790
904
  productId: event.data.object.items.data[0].plan.id,
791
905
  status: event.data.object.status
792
906
  });
793
907
  break;
794
908
  }
795
- case "customer.subscription.deleted": {
909
+ case 'customer.subscription.deleted': {
796
910
  this.subscriptionService.deleteSubscription({
797
911
  id: event.data.object.id
798
912
  });
799
913
  break;
800
914
  }
801
- case "customer.subscription.paused": {
915
+ case 'customer.subscription.paused': {
802
916
  this.subscriptionService.cancelSubscription({
803
917
  id: event.data.object.id
804
918
  });
805
919
  break;
806
920
  }
807
- case "customer.subscription.resumed": {
921
+ case 'customer.subscription.resumed': {
808
922
  this.subscriptionService.resumeSubscription({
809
923
  id: event.data.object.id
810
924
  });
@@ -812,7 +926,7 @@ var StripeWebhookService = class {
812
926
  }
813
927
  default:
814
928
  this.openTelemetryCollector.info(
815
- "Unprocessed stripe event type",
929
+ 'Unprocessed stripe event type',
816
930
  eventType
817
931
  );
818
932
  break;
@@ -820,11 +934,12 @@ var StripeWebhookService = class {
820
934
  }
821
935
  };
822
936
  // Annotate the CommonJS export names for ESM import in node:
823
- 0 && (module.exports = {
824
- StripeBillingPortalService,
825
- StripeCheckoutSessionService,
826
- StripePaymentLinkService,
827
- StripePlanService,
828
- StripeSubscriptionService,
829
- StripeWebhookService
830
- });
937
+ 0 &&
938
+ (module.exports = {
939
+ StripeBillingPortalService,
940
+ StripeCheckoutSessionService,
941
+ StripePaymentLinkService,
942
+ StripePlanService,
943
+ StripeSubscriptionService,
944
+ StripeWebhookService
945
+ });