@forklaunch/implementation-billing-stripe 0.0.3 → 0.0.4
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 +177 -182
- package/lib/domain/enum/index.d.ts +177 -182
- package/lib/domain/enum/index.js +186 -191
- package/lib/domain/enum/index.mjs +176 -176
- package/lib/domain/schemas/index.d.mts +318 -4084
- package/lib/domain/schemas/index.d.ts +318 -4084
- package/lib/domain/schemas/index.js +248 -324
- package/lib/domain/schemas/index.mjs +197 -198
- 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/schemas/billingPortal.schema.ts +2 -2
- package/lib/eject/domain/schemas/checkoutSession.schema.ts +4 -4
- package/lib/eject/domain/schemas/paymentLink.schema.ts +4 -4
- package/lib/eject/domain/schemas/plan.schema.ts +5 -5
- package/lib/eject/domain/schemas/subscription.schema.ts +3 -3
- package/lib/services/index.d.mts +194 -709
- package/lib/services/index.d.ts +194 -709
- package/lib/services/index.js +244 -359
- package/lib/services/index.mjs +205 -298
- package/package.json +2 -2
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
|
)
|
|
@@ -650,17 +593,7 @@ var StripeSubscriptionService = class {
|
|
|
650
593
|
|
|
651
594
|
// services/webhook.service.ts
|
|
652
595
|
var StripeWebhookService = class {
|
|
653
|
-
constructor(
|
|
654
|
-
stripeClient,
|
|
655
|
-
em,
|
|
656
|
-
schemaValidator,
|
|
657
|
-
openTelemetryCollector,
|
|
658
|
-
billingPortalService,
|
|
659
|
-
checkoutSessionService,
|
|
660
|
-
paymentLinkService,
|
|
661
|
-
planService,
|
|
662
|
-
subscriptionService
|
|
663
|
-
) {
|
|
596
|
+
constructor(stripeClient, em, schemaValidator, openTelemetryCollector, billingPortalService, checkoutSessionService, paymentLinkService, planService, subscriptionService) {
|
|
664
597
|
this.stripeClient = stripeClient;
|
|
665
598
|
this.em = em;
|
|
666
599
|
this.schemaValidator = schemaValidator;
|
|
@@ -673,11 +606,11 @@ var StripeWebhookService = class {
|
|
|
673
606
|
}
|
|
674
607
|
async handleWebhookEvent(event) {
|
|
675
608
|
if (this.openTelemetryCollector) {
|
|
676
|
-
this.openTelemetryCollector.info(
|
|
609
|
+
this.openTelemetryCollector.info("Handling webhook event", event);
|
|
677
610
|
}
|
|
678
611
|
const eventType = event.type;
|
|
679
612
|
switch (eventType) {
|
|
680
|
-
case
|
|
613
|
+
case "billing_portal.session.created": {
|
|
681
614
|
this.billingPortalService.baseBillingPortalService.createBillingPortalSession(
|
|
682
615
|
{
|
|
683
616
|
id: event.data.object.id,
|
|
@@ -689,162 +622,136 @@ var StripeWebhookService = class {
|
|
|
689
622
|
);
|
|
690
623
|
break;
|
|
691
624
|
}
|
|
692
|
-
case
|
|
625
|
+
case "checkout.session.expired": {
|
|
693
626
|
this.checkoutSessionService.handleCheckoutFailure({
|
|
694
627
|
id: event.data.object.id
|
|
695
628
|
});
|
|
696
629
|
break;
|
|
697
630
|
}
|
|
698
|
-
case
|
|
631
|
+
case "checkout.session.completed": {
|
|
699
632
|
this.checkoutSessionService.handleCheckoutSuccess({
|
|
700
633
|
id: event.data.object.id
|
|
701
634
|
});
|
|
702
635
|
break;
|
|
703
636
|
}
|
|
704
|
-
case
|
|
637
|
+
case "payment_link.created":
|
|
705
638
|
{
|
|
706
639
|
this.paymentLinkService.basePaymentLinkService.createPaymentLink({
|
|
707
640
|
id: event.data.object.id,
|
|
708
|
-
amount:
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
) ?? 0,
|
|
641
|
+
amount: event.data.object.line_items?.data.reduce(
|
|
642
|
+
(total, item) => total + item.amount_total,
|
|
643
|
+
0
|
|
644
|
+
) ?? 0,
|
|
713
645
|
paymentMethods: event.data.object.payment_method_types,
|
|
714
|
-
status:
|
|
646
|
+
status: "CREATED",
|
|
715
647
|
currency: event.data.object.currency,
|
|
716
648
|
providerFields: event.data.object
|
|
717
649
|
});
|
|
718
650
|
}
|
|
719
651
|
break;
|
|
720
|
-
case
|
|
652
|
+
case "payment_link.updated": {
|
|
721
653
|
this.paymentLinkService.basePaymentLinkService.updatePaymentLink({
|
|
722
654
|
id: event.data.object.id,
|
|
723
|
-
amount:
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
) ?? 0,
|
|
655
|
+
amount: event.data.object.line_items?.data.reduce(
|
|
656
|
+
(total, item) => total + item.amount_total,
|
|
657
|
+
0
|
|
658
|
+
) ?? 0,
|
|
728
659
|
paymentMethods: event.data.object.payment_method_types,
|
|
729
|
-
status:
|
|
660
|
+
status: "UPDATED",
|
|
730
661
|
currency: event.data.object.currency,
|
|
731
662
|
providerFields: event.data.object
|
|
732
663
|
});
|
|
733
664
|
break;
|
|
734
665
|
}
|
|
735
|
-
case
|
|
736
|
-
if (
|
|
737
|
-
typeof event.data.object.product === 'object' &&
|
|
738
|
-
event.data.object.product != null &&
|
|
739
|
-
event.data.object.amount != null
|
|
740
|
-
) {
|
|
666
|
+
case "plan.created": {
|
|
667
|
+
if (typeof event.data.object.product === "object" && event.data.object.product != null && event.data.object.amount != null) {
|
|
741
668
|
this.planService.basePlanService.createPlan({
|
|
742
669
|
id: event.data.object.id,
|
|
743
|
-
billingProvider:
|
|
670
|
+
billingProvider: "stripe" /* STRIPE */,
|
|
744
671
|
cadence: event.data.object.interval,
|
|
745
672
|
currency: event.data.object.currency,
|
|
746
673
|
active: true,
|
|
747
|
-
name:
|
|
748
|
-
typeof event.data.object.product === 'string'
|
|
749
|
-
? event.data.object.product
|
|
750
|
-
: event.data.object.product?.id,
|
|
674
|
+
name: typeof event.data.object.product === "string" ? event.data.object.product : event.data.object.product?.id,
|
|
751
675
|
price: event.data.object.amount,
|
|
752
676
|
externalId: event.data.object.id,
|
|
753
677
|
providerFields: event.data.object
|
|
754
678
|
});
|
|
755
679
|
} else {
|
|
756
|
-
throw new Error(
|
|
680
|
+
throw new Error("Invalid plan");
|
|
757
681
|
}
|
|
758
682
|
break;
|
|
759
683
|
}
|
|
760
|
-
case
|
|
761
|
-
if (
|
|
762
|
-
typeof event.data.object.product === 'object' &&
|
|
763
|
-
event.data.object.product != null &&
|
|
764
|
-
event.data.object.amount != null
|
|
765
|
-
) {
|
|
684
|
+
case "plan.updated": {
|
|
685
|
+
if (typeof event.data.object.product === "object" && event.data.object.product != null && event.data.object.amount != null) {
|
|
766
686
|
this.planService.basePlanService.updatePlan({
|
|
767
687
|
id: event.data.object.id,
|
|
768
|
-
billingProvider:
|
|
688
|
+
billingProvider: "stripe" /* STRIPE */,
|
|
769
689
|
cadence: event.data.object.interval,
|
|
770
690
|
currency: event.data.object.currency,
|
|
771
691
|
active: true,
|
|
772
|
-
name:
|
|
773
|
-
typeof event.data.object.product === 'string'
|
|
774
|
-
? event.data.object.product
|
|
775
|
-
: event.data.object.product?.id,
|
|
692
|
+
name: typeof event.data.object.product === "string" ? event.data.object.product : event.data.object.product?.id,
|
|
776
693
|
price: event.data.object.amount,
|
|
777
694
|
externalId: event.data.object.id,
|
|
778
695
|
providerFields: event.data.object
|
|
779
696
|
});
|
|
780
697
|
} else {
|
|
781
|
-
throw new Error(
|
|
698
|
+
throw new Error("Invalid plan");
|
|
782
699
|
}
|
|
783
700
|
break;
|
|
784
701
|
}
|
|
785
|
-
case
|
|
702
|
+
case "plan.deleted": {
|
|
786
703
|
this.planService.deletePlan({
|
|
787
704
|
id: event.data.object.id
|
|
788
705
|
});
|
|
789
706
|
break;
|
|
790
707
|
}
|
|
791
|
-
case
|
|
708
|
+
case "customer.subscription.created": {
|
|
792
709
|
this.subscriptionService.baseSubscriptionService.createSubscription({
|
|
793
710
|
id: event.data.object.id,
|
|
794
|
-
partyId:
|
|
795
|
-
|
|
796
|
-
? event.data.object.customer
|
|
797
|
-
: event.data.object.customer.id,
|
|
798
|
-
partyType: 'USER',
|
|
711
|
+
partyId: typeof event.data.object.customer === "string" ? event.data.object.customer : event.data.object.customer.id,
|
|
712
|
+
partyType: "USER",
|
|
799
713
|
description: event.data.object.description ?? void 0,
|
|
800
714
|
active: true,
|
|
801
715
|
productId: event.data.object.items.data[0].plan.id,
|
|
802
716
|
providerFields: event.data.object,
|
|
803
717
|
externalId: event.data.object.id,
|
|
804
|
-
billingProvider:
|
|
718
|
+
billingProvider: "stripe" /* STRIPE */,
|
|
805
719
|
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),
|
|
720
|
+
endDate: event.data.object.cancel_at ? new Date(event.data.object.cancel_at) : /* @__PURE__ */ new Date(Infinity),
|
|
809
721
|
status: event.data.object.status
|
|
810
722
|
});
|
|
811
723
|
break;
|
|
812
724
|
}
|
|
813
|
-
case
|
|
725
|
+
case "customer.subscription.updated": {
|
|
814
726
|
this.subscriptionService.baseSubscriptionService.updateSubscription({
|
|
815
727
|
id: event.data.object.id,
|
|
816
|
-
partyId:
|
|
817
|
-
|
|
818
|
-
? event.data.object.customer
|
|
819
|
-
: event.data.object.customer.id,
|
|
820
|
-
partyType: 'USER',
|
|
728
|
+
partyId: typeof event.data.object.customer === "string" ? event.data.object.customer : event.data.object.customer.id,
|
|
729
|
+
partyType: "USER",
|
|
821
730
|
description: event.data.object.description ?? void 0,
|
|
822
731
|
active: true,
|
|
823
732
|
providerFields: event.data.object,
|
|
824
733
|
externalId: event.data.object.id,
|
|
825
|
-
billingProvider:
|
|
734
|
+
billingProvider: "stripe" /* STRIPE */,
|
|
826
735
|
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),
|
|
736
|
+
endDate: event.data.object.cancel_at ? new Date(event.data.object.cancel_at) : /* @__PURE__ */ new Date(Infinity),
|
|
830
737
|
productId: event.data.object.items.data[0].plan.id,
|
|
831
738
|
status: event.data.object.status
|
|
832
739
|
});
|
|
833
740
|
break;
|
|
834
741
|
}
|
|
835
|
-
case
|
|
742
|
+
case "customer.subscription.deleted": {
|
|
836
743
|
this.subscriptionService.deleteSubscription({
|
|
837
744
|
id: event.data.object.id
|
|
838
745
|
});
|
|
839
746
|
break;
|
|
840
747
|
}
|
|
841
|
-
case
|
|
748
|
+
case "customer.subscription.paused": {
|
|
842
749
|
this.subscriptionService.cancelSubscription({
|
|
843
750
|
id: event.data.object.id
|
|
844
751
|
});
|
|
845
752
|
break;
|
|
846
753
|
}
|
|
847
|
-
case
|
|
754
|
+
case "customer.subscription.resumed": {
|
|
848
755
|
this.subscriptionService.resumeSubscription({
|
|
849
756
|
id: event.data.object.id
|
|
850
757
|
});
|
|
@@ -852,7 +759,7 @@ var StripeWebhookService = class {
|
|
|
852
759
|
}
|
|
853
760
|
default:
|
|
854
761
|
this.openTelemetryCollector.info(
|
|
855
|
-
|
|
762
|
+
"Unprocessed stripe event type",
|
|
856
763
|
eventType
|
|
857
764
|
);
|
|
858
765
|
break;
|