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