@forklaunch/implementation-billing-stripe 0.0.1 → 0.0.3

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