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