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