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