@forklaunch/implementation-billing-stripe 0.2.8 → 0.3.1

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