@forklaunch/implementation-billing-base 0.1.2 → 0.1.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/__test__/schemaEquality.test.d.ts +2 -0
- package/lib/__test__/schemaEquality.test.d.ts.map +1 -0
- package/lib/__test__/schemaEquality.test.js +226 -0
- package/{schemas/typebox → lib/eject/schemas/zod}/billingPortal.schema.ts +1 -1
- package/{schemas/typebox → lib/eject/schemas/zod}/checkoutSession.schema.ts +1 -2
- package/{schemas/typebox → lib/eject/schemas/zod}/paymentLink.schema.ts +1 -1
- package/{schemas/typebox → lib/eject/schemas/zod}/plan.schema.ts +1 -1
- package/{schemas/typebox → lib/eject/schemas/zod}/subscription.schema.ts +1 -1
- package/lib/eject/services/services/billingPortal.service.ts +148 -0
- package/lib/eject/services/services/checkoutSession.service.ts +135 -0
- package/lib/eject/services/services/paymentLink.service.ts +173 -0
- package/lib/eject/services/services/plan.service.ts +135 -0
- package/lib/eject/services/subscription.service.ts +239 -0
- package/lib/index.d.ts +11 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/jest.config.d.ts +4 -0
- package/lib/jest.config.d.ts.map +1 -0
- package/lib/jest.config.js +19 -0
- package/lib/schemas/billingPortal.schema.d.ts +42 -0
- package/lib/schemas/billingPortal.schema.d.ts.map +1 -0
- package/lib/schemas/billingPortal.schema.js +14 -0
- package/lib/schemas/checkoutSession.schema.d.ts +55 -0
- package/lib/schemas/checkoutSession.schema.d.ts.map +1 -0
- package/lib/schemas/checkoutSession.schema.js +14 -0
- package/lib/schemas/paymentLink.schema.d.ts +66 -0
- package/lib/schemas/paymentLink.schema.d.ts.map +1 -0
- package/lib/schemas/paymentLink.schema.js +14 -0
- package/lib/schemas/plan.schema.d.ts +78 -0
- package/lib/schemas/plan.schema.d.ts.map +1 -0
- package/lib/schemas/plan.schema.js +14 -0
- package/lib/schemas/subscription.schema.d.ts +90 -0
- package/lib/schemas/subscription.schema.d.ts.map +1 -0
- package/lib/schemas/subscription.schema.js +14 -0
- package/lib/schemas/typebox/billingPortal.schema.d.ts +20 -0
- package/lib/schemas/typebox/billingPortal.schema.d.ts.map +1 -0
- package/lib/schemas/typebox/billingPortal.schema.js +20 -0
- package/lib/schemas/typebox/checkoutSession.schema.d.ts +28 -0
- package/lib/schemas/typebox/checkoutSession.schema.d.ts.map +1 -0
- package/lib/schemas/typebox/checkoutSession.schema.js +27 -0
- package/lib/schemas/typebox/paymentLink.schema.d.ts +33 -0
- package/lib/schemas/typebox/paymentLink.schema.d.ts.map +1 -0
- package/lib/schemas/typebox/paymentLink.schema.js +32 -0
- package/lib/schemas/typebox/plan.schema.d.ts +39 -0
- package/lib/schemas/typebox/plan.schema.d.ts.map +1 -0
- package/lib/schemas/typebox/plan.schema.js +38 -0
- package/lib/schemas/typebox/subscription.schema.d.ts +45 -0
- package/lib/schemas/typebox/subscription.schema.d.ts.map +1 -0
- package/lib/schemas/typebox/subscription.schema.js +44 -0
- package/lib/schemas/zod/billingPortal.schema.d.ts +20 -0
- package/lib/schemas/zod/billingPortal.schema.d.ts.map +1 -0
- package/lib/schemas/zod/billingPortal.schema.js +20 -0
- package/lib/schemas/zod/checkoutSession.schema.d.ts +27 -0
- package/lib/schemas/zod/checkoutSession.schema.d.ts.map +1 -0
- package/lib/schemas/zod/checkoutSession.schema.js +26 -0
- package/lib/schemas/zod/paymentLink.schema.d.ts +33 -0
- package/lib/schemas/zod/paymentLink.schema.d.ts.map +1 -0
- package/lib/schemas/zod/paymentLink.schema.js +32 -0
- package/lib/schemas/zod/plan.schema.d.ts +39 -0
- package/lib/schemas/zod/plan.schema.d.ts.map +1 -0
- package/lib/schemas/zod/plan.schema.js +38 -0
- package/lib/schemas/zod/subscription.schema.d.ts +45 -0
- package/lib/schemas/zod/subscription.schema.d.ts.map +1 -0
- package/lib/schemas/zod/subscription.schema.js +44 -0
- package/lib/services/billingPortal.service.d.ts +44 -0
- package/lib/services/billingPortal.service.d.ts.map +1 -0
- package/lib/services/billingPortal.service.js +51 -0
- package/lib/services/checkoutSession.service.d.ts +45 -0
- package/lib/services/checkoutSession.service.d.ts.map +1 -0
- package/lib/services/checkoutSession.service.js +49 -0
- package/lib/services/paymentLink.service.d.ts +48 -0
- package/lib/services/paymentLink.service.d.ts.map +1 -0
- package/lib/services/paymentLink.service.js +72 -0
- package/lib/services/plan.service.d.ts +42 -0
- package/lib/services/plan.service.d.ts.map +1 -0
- package/lib/services/plan.service.js +43 -0
- package/lib/services/subscription.service.d.ts +52 -0
- package/lib/services/subscription.service.d.ts.map +1 -0
- package/lib/services/subscription.service.js +94 -0
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/lib/vitest.config.d.ts +3 -0
- package/lib/vitest.config.d.ts.map +1 -0
- package/lib/vitest.config.js +7 -0
- package/package.json +6 -4
- package/__test__/schemaEquality.test.ts +0 -411
- package/schemas/billingPortal.schema.ts +0 -28
- package/schemas/checkoutSession.schema.ts +0 -28
- package/schemas/paymentLink.schema.ts +0 -28
- package/schemas/plan.schema.ts +0 -28
- package/schemas/subscription.schema.ts +0 -28
- package/tsconfig.json +0 -7
- /package/{schemas/zod → lib/eject/schemas}/billingPortal.schema.ts +0 -0
- /package/{schemas/zod → lib/eject/schemas}/checkoutSession.schema.ts +0 -0
- /package/{schemas/zod → lib/eject/schemas}/paymentLink.schema.ts +0 -0
- /package/{schemas/zod → lib/eject/schemas}/plan.schema.ts +0 -0
- /package/{schemas/zod → lib/eject/schemas}/subscription.schema.ts +0 -0
- /package/{services → lib/eject/services}/billingPortal.service.ts +0 -0
- /package/{services → lib/eject/services}/checkoutSession.service.ts +0 -0
- /package/{services → lib/eject/services}/paymentLink.service.ts +0 -0
- /package/{services → lib/eject/services}/plan.service.ts +0 -0
- /package/{services → lib/eject/services/services}/subscription.service.ts +0 -0
- /package/{index.ts → lib/index.js} +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schemaEquality.test.d.ts","sourceRoot":"","sources":["../../__test__/schemaEquality.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { isTrue } from '@forklaunch/common';
|
|
2
|
+
import { DummyEnum, testSchemaEquality } from '@forklaunch/core/test';
|
|
3
|
+
import { BillingPortalSchema as TypeboxBillingPortalSchema, CreateBillingPortalSchema as TypeboxCreateBillingPortalSchema, UpdateBillingPortalSchema as TypeboxUpdateBillingPortalSchema } from '../schemas/typebox/billingPortal.schema';
|
|
4
|
+
import { CheckoutSessionSchema as TypeboxCheckoutSessionSchema, CreateCheckoutSessionSchema as TypeboxCreateCheckoutSessionSchema, UpdateCheckoutSessionSchema as TypeboxUpdateCheckoutSessionSchema } from '../schemas/typebox/checkoutSession.schema';
|
|
5
|
+
import { CreatePaymentLinkSchema as TypeboxCreatePaymentLinkSchema, PaymentLinkSchema as TypeboxPaymentLinkSchema, UpdatePaymentLinkSchema as TypeboxUpdatePaymentLinkSchema } from '../schemas/typebox/paymentLink.schema';
|
|
6
|
+
import { CreatePlanSchema as TypeboxCreatePlanSchema, PlanSchema as TypeboxPlanSchema, UpdatePlanSchema as TypeboxUpdatePlanSchema } from '../schemas/typebox/plan.schema';
|
|
7
|
+
import { CreateSubscriptionSchema as TypeboxCreateSubscriptionSchema, SubscriptionSchema as TypeboxSubscriptionSchema, UpdateSubscriptionSchema as TypeboxUpdateSubscriptionSchema } from '../schemas/typebox/subscription.schema';
|
|
8
|
+
import { BillingPortalSchema as ZodBillingPortalSchema, CreateBillingPortalSchema as ZodCreateBillingPortalSchema, UpdateBillingPortalSchema as ZodUpdateBillingPortalSchema } from '../schemas/zod/billingPortal.schema';
|
|
9
|
+
import { CheckoutSessionSchema as ZodCheckoutSessionSchema, CreateCheckoutSessionSchema as ZodCreateCheckoutSessionSchema, UpdateCheckoutSessionSchema as ZodUpdateCheckoutSessionSchema } from '../schemas/zod/checkoutSession.schema';
|
|
10
|
+
import { CreatePaymentLinkSchema as ZodCreatePaymentLinkSchema, PaymentLinkSchema as ZodPaymentLinkSchema, UpdatePaymentLinkSchema as ZodUpdatePaymentLinkSchema } from '../schemas/zod/paymentLink.schema';
|
|
11
|
+
import { CreatePlanSchema as ZodCreatePlanSchema, PlanSchema as ZodPlanSchema, UpdatePlanSchema as ZodUpdatePlanSchema } from '../schemas/zod/plan.schema';
|
|
12
|
+
import { CreateSubscriptionSchema as ZodCreateSubscriptionSchema, SubscriptionSchema as ZodSubscriptionSchema, UpdateSubscriptionSchema as ZodUpdateSubscriptionSchema } from '../schemas/zod/subscription.schema';
|
|
13
|
+
const zodUpdateBillingPortalSchema = ZodUpdateBillingPortalSchema(false);
|
|
14
|
+
const typeboxUpdateBillingPortalSchema = TypeboxUpdateBillingPortalSchema(false);
|
|
15
|
+
const zodBillingPortalSchema = ZodBillingPortalSchema(false);
|
|
16
|
+
const typeboxBillingPortalSchema = TypeboxBillingPortalSchema(false);
|
|
17
|
+
const zodCreateCheckoutSessionSchema = ZodCreateCheckoutSessionSchema(DummyEnum);
|
|
18
|
+
const typeboxCreateCheckoutSessionSchema = TypeboxCreateCheckoutSessionSchema(DummyEnum);
|
|
19
|
+
const zodUpdateCheckoutSessionSchema = ZodUpdateCheckoutSessionSchema(false)(DummyEnum);
|
|
20
|
+
const typeboxUpdateCheckoutSessionSchema = TypeboxUpdateCheckoutSessionSchema(false)(DummyEnum);
|
|
21
|
+
const zodCheckoutSessionSchema = ZodCheckoutSessionSchema(false)(DummyEnum);
|
|
22
|
+
const typeboxCheckoutSessionSchema = TypeboxCheckoutSessionSchema(false)(DummyEnum);
|
|
23
|
+
const zodCreatePaymentLinkSchema = ZodCreatePaymentLinkSchema(DummyEnum);
|
|
24
|
+
const typeboxCreatePaymentLinkSchema = TypeboxCreatePaymentLinkSchema(DummyEnum);
|
|
25
|
+
const zodUpdatePaymentLinkSchema = ZodUpdatePaymentLinkSchema(false)(DummyEnum);
|
|
26
|
+
const typeboxUpdatePaymentLinkSchema = TypeboxUpdatePaymentLinkSchema(false)(DummyEnum);
|
|
27
|
+
const zodPaymentLinkSchema = ZodPaymentLinkSchema(false)(DummyEnum);
|
|
28
|
+
const typeboxPaymentLinkSchema = TypeboxPaymentLinkSchema(false)(DummyEnum);
|
|
29
|
+
const zodCreatePlanSchema = ZodCreatePlanSchema(DummyEnum, DummyEnum);
|
|
30
|
+
const typeboxCreatePlanSchema = TypeboxCreatePlanSchema(DummyEnum, DummyEnum);
|
|
31
|
+
const zodUpdatePlanSchema = ZodUpdatePlanSchema(false)(DummyEnum, DummyEnum);
|
|
32
|
+
const typeboxUpdatePlanSchema = TypeboxUpdatePlanSchema(false)(DummyEnum, DummyEnum);
|
|
33
|
+
const zodPlanSchema = ZodPlanSchema(false)(DummyEnum, DummyEnum);
|
|
34
|
+
const typeboxPlanSchema = TypeboxPlanSchema(false)(DummyEnum, DummyEnum);
|
|
35
|
+
const zodCreateSubscriptionSchema = ZodCreateSubscriptionSchema(DummyEnum, DummyEnum);
|
|
36
|
+
const typeboxCreateSubscriptionSchema = TypeboxCreateSubscriptionSchema(DummyEnum, DummyEnum);
|
|
37
|
+
const zodUpdateSubscriptionSchema = ZodUpdateSubscriptionSchema(false)(DummyEnum, DummyEnum);
|
|
38
|
+
const typeboxUpdateSubscriptionSchema = TypeboxUpdateSubscriptionSchema(false)(DummyEnum, DummyEnum);
|
|
39
|
+
const zodSubscriptionSchema = ZodSubscriptionSchema(false)(DummyEnum, DummyEnum);
|
|
40
|
+
const typeboxSubscriptionSchema = TypeboxSubscriptionSchema(false)(DummyEnum, DummyEnum);
|
|
41
|
+
describe('schema equality', () => {
|
|
42
|
+
it('should be equal for billing portal', () => {
|
|
43
|
+
expect(isTrue(testSchemaEquality(ZodCreateBillingPortalSchema, TypeboxCreateBillingPortalSchema, {
|
|
44
|
+
customerId: 'test',
|
|
45
|
+
uri: 'https://example.com',
|
|
46
|
+
expiresAt: new Date()
|
|
47
|
+
}))).toBeTruthy();
|
|
48
|
+
expect(isTrue(testSchemaEquality(zodUpdateBillingPortalSchema, typeboxUpdateBillingPortalSchema, {
|
|
49
|
+
id: 'test',
|
|
50
|
+
uri: 'https://example.com',
|
|
51
|
+
expiresAt: new Date()
|
|
52
|
+
}))).toBeTruthy();
|
|
53
|
+
expect(isTrue(testSchemaEquality(zodBillingPortalSchema, typeboxBillingPortalSchema, {
|
|
54
|
+
id: 'test',
|
|
55
|
+
customerId: 'test',
|
|
56
|
+
uri: 'https://example.com',
|
|
57
|
+
expiresAt: new Date(),
|
|
58
|
+
extraFields: {
|
|
59
|
+
test: 'test'
|
|
60
|
+
}
|
|
61
|
+
}))).toBeTruthy();
|
|
62
|
+
});
|
|
63
|
+
it('should be equal for checkout session', () => {
|
|
64
|
+
expect(isTrue(testSchemaEquality(zodCreateCheckoutSessionSchema, typeboxCreateCheckoutSessionSchema, {
|
|
65
|
+
customerId: 'test',
|
|
66
|
+
paymentMethods: [DummyEnum.A, DummyEnum.B],
|
|
67
|
+
successRedirectUri: 'https://example.com',
|
|
68
|
+
cancelRedirectUri: 'https://example.com',
|
|
69
|
+
extraFields: {
|
|
70
|
+
test: 'test'
|
|
71
|
+
}
|
|
72
|
+
}))).toBeTruthy();
|
|
73
|
+
expect(isTrue(testSchemaEquality(zodUpdateCheckoutSessionSchema, typeboxUpdateCheckoutSessionSchema, {
|
|
74
|
+
id: 'test',
|
|
75
|
+
customerId: 'test',
|
|
76
|
+
paymentMethods: [DummyEnum.A, DummyEnum.B],
|
|
77
|
+
successRedirectUri: 'https://example.com',
|
|
78
|
+
cancelRedirectUri: 'https://example.com',
|
|
79
|
+
extraFields: {
|
|
80
|
+
test: 'test'
|
|
81
|
+
}
|
|
82
|
+
}))).toBeTruthy();
|
|
83
|
+
expect(isTrue(testSchemaEquality(zodCheckoutSessionSchema, typeboxCheckoutSessionSchema, {
|
|
84
|
+
id: 'test',
|
|
85
|
+
customerId: 'test',
|
|
86
|
+
paymentMethods: [DummyEnum.A, DummyEnum.B],
|
|
87
|
+
successRedirectUri: 'https://example.com',
|
|
88
|
+
cancelRedirectUri: 'https://example.com',
|
|
89
|
+
extraFields: {
|
|
90
|
+
test: 'test'
|
|
91
|
+
}
|
|
92
|
+
}))).toBeTruthy();
|
|
93
|
+
});
|
|
94
|
+
it('should be equal for payment link', () => {
|
|
95
|
+
expect(isTrue(testSchemaEquality(zodCreatePaymentLinkSchema, typeboxCreatePaymentLinkSchema, {
|
|
96
|
+
amount: 100,
|
|
97
|
+
currency: DummyEnum.A,
|
|
98
|
+
successRedirectUri: 'https://example.com',
|
|
99
|
+
cancelRedirectUri: 'https://example.com',
|
|
100
|
+
description: 'test',
|
|
101
|
+
metadata: {
|
|
102
|
+
test: 'test'
|
|
103
|
+
},
|
|
104
|
+
extraFields: {
|
|
105
|
+
test: 'test'
|
|
106
|
+
}
|
|
107
|
+
}))).toBeTruthy();
|
|
108
|
+
expect(isTrue(testSchemaEquality(zodUpdatePaymentLinkSchema, typeboxUpdatePaymentLinkSchema, {
|
|
109
|
+
id: 'test',
|
|
110
|
+
amount: 100,
|
|
111
|
+
currency: DummyEnum.A,
|
|
112
|
+
successRedirectUri: 'https://example.com',
|
|
113
|
+
cancelRedirectUri: 'https://example.com',
|
|
114
|
+
description: 'test',
|
|
115
|
+
metadata: {
|
|
116
|
+
test: 'test'
|
|
117
|
+
},
|
|
118
|
+
extraFields: {
|
|
119
|
+
test: 'test'
|
|
120
|
+
}
|
|
121
|
+
})));
|
|
122
|
+
expect(isTrue(testSchemaEquality(zodPaymentLinkSchema, typeboxPaymentLinkSchema, {
|
|
123
|
+
id: 'test',
|
|
124
|
+
amount: 100,
|
|
125
|
+
currency: DummyEnum.A,
|
|
126
|
+
successRedirectUri: 'https://example.com',
|
|
127
|
+
cancelRedirectUri: 'https://example.com',
|
|
128
|
+
description: 'test',
|
|
129
|
+
metadata: {
|
|
130
|
+
test: 'test'
|
|
131
|
+
},
|
|
132
|
+
extraFields: {
|
|
133
|
+
test: 'test'
|
|
134
|
+
}
|
|
135
|
+
}))).toBeTruthy();
|
|
136
|
+
});
|
|
137
|
+
it('should be equal for plan', () => {
|
|
138
|
+
expect(isTrue(testSchemaEquality(zodCreatePlanSchema, typeboxCreatePlanSchema, {
|
|
139
|
+
name: 'test',
|
|
140
|
+
price: 100,
|
|
141
|
+
cadence: DummyEnum.A,
|
|
142
|
+
features: [DummyEnum.A, DummyEnum.B],
|
|
143
|
+
externalId: 'test',
|
|
144
|
+
active: true,
|
|
145
|
+
description: 'test',
|
|
146
|
+
extraFields: {
|
|
147
|
+
test: 'test'
|
|
148
|
+
}
|
|
149
|
+
}))).toBeTruthy();
|
|
150
|
+
expect(isTrue(testSchemaEquality(zodUpdatePlanSchema, typeboxUpdatePlanSchema, {
|
|
151
|
+
id: 'test',
|
|
152
|
+
name: 'test',
|
|
153
|
+
price: 100,
|
|
154
|
+
cadence: DummyEnum.A,
|
|
155
|
+
features: [DummyEnum.A, DummyEnum.B],
|
|
156
|
+
externalId: 'test',
|
|
157
|
+
active: true,
|
|
158
|
+
description: 'test',
|
|
159
|
+
extraFields: {
|
|
160
|
+
test: 'test'
|
|
161
|
+
}
|
|
162
|
+
}))).toBeTruthy();
|
|
163
|
+
expect(isTrue(testSchemaEquality(zodPlanSchema, typeboxPlanSchema, {
|
|
164
|
+
id: 'test',
|
|
165
|
+
name: 'test',
|
|
166
|
+
price: 100,
|
|
167
|
+
cadence: DummyEnum.A,
|
|
168
|
+
features: [DummyEnum.A, DummyEnum.B],
|
|
169
|
+
externalId: 'test',
|
|
170
|
+
active: true,
|
|
171
|
+
description: 'test',
|
|
172
|
+
extraFields: {
|
|
173
|
+
test: 'test'
|
|
174
|
+
}
|
|
175
|
+
}))).toBeTruthy();
|
|
176
|
+
});
|
|
177
|
+
it('should be equal for subscription', () => {
|
|
178
|
+
expect(isTrue(testSchemaEquality(zodCreateSubscriptionSchema, typeboxCreateSubscriptionSchema, {
|
|
179
|
+
partyId: 'test',
|
|
180
|
+
partyType: DummyEnum.A,
|
|
181
|
+
productId: 'test',
|
|
182
|
+
status: DummyEnum.A,
|
|
183
|
+
active: true,
|
|
184
|
+
externalId: 'test',
|
|
185
|
+
startDate: new Date(),
|
|
186
|
+
endDate: new Date(),
|
|
187
|
+
description: 'test',
|
|
188
|
+
extraFields: {
|
|
189
|
+
test: 'test'
|
|
190
|
+
},
|
|
191
|
+
billingProvider: DummyEnum.A
|
|
192
|
+
}))).toBeTruthy();
|
|
193
|
+
expect(isTrue(testSchemaEquality(zodUpdateSubscriptionSchema, typeboxUpdateSubscriptionSchema, {
|
|
194
|
+
id: 'test',
|
|
195
|
+
partyId: 'test',
|
|
196
|
+
partyType: DummyEnum.A,
|
|
197
|
+
productId: 'test',
|
|
198
|
+
status: DummyEnum.A,
|
|
199
|
+
active: true,
|
|
200
|
+
externalId: 'test',
|
|
201
|
+
startDate: new Date(),
|
|
202
|
+
endDate: new Date(),
|
|
203
|
+
description: 'test',
|
|
204
|
+
extraFields: {
|
|
205
|
+
test: 'test'
|
|
206
|
+
},
|
|
207
|
+
billingProvider: DummyEnum.A
|
|
208
|
+
}))).toBeTruthy();
|
|
209
|
+
expect(isTrue(testSchemaEquality(zodSubscriptionSchema, typeboxSubscriptionSchema, {
|
|
210
|
+
id: 'test',
|
|
211
|
+
partyId: 'test',
|
|
212
|
+
partyType: DummyEnum.A,
|
|
213
|
+
productId: 'test',
|
|
214
|
+
status: DummyEnum.A,
|
|
215
|
+
active: true,
|
|
216
|
+
externalId: 'test',
|
|
217
|
+
startDate: new Date(),
|
|
218
|
+
endDate: new Date(),
|
|
219
|
+
description: 'test',
|
|
220
|
+
extraFields: {
|
|
221
|
+
test: 'test'
|
|
222
|
+
},
|
|
223
|
+
billingProvider: DummyEnum.A
|
|
224
|
+
}))).toBeTruthy();
|
|
225
|
+
});
|
|
226
|
+
});
|
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
string,
|
|
8
8
|
unknown,
|
|
9
9
|
uuid
|
|
10
|
-
} from '@forklaunch/validator/
|
|
10
|
+
} from '@forklaunch/validator/zod';
|
|
11
11
|
|
|
12
12
|
export const CreateCheckoutSessionSchema = <
|
|
13
13
|
T extends Record<string, LiteralSchema>
|
|
@@ -37,7 +37,6 @@ export const CheckoutSessionSchema =
|
|
|
37
37
|
<T extends Record<string, LiteralSchema>>(PaymentMethodEnum: T) => ({
|
|
38
38
|
id: uuidId ? uuid : string,
|
|
39
39
|
customerId: string,
|
|
40
|
-
metadata: optional(unknown),
|
|
41
40
|
paymentMethods: array(enum_(PaymentMethodEnum)),
|
|
42
41
|
successRedirectUri: string,
|
|
43
42
|
cancelRedirectUri: string,
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BillingPortalDto,
|
|
3
|
+
BillingPortalService,
|
|
4
|
+
CreateBillingPortalDto,
|
|
5
|
+
UpdateBillingPortalDto
|
|
6
|
+
} from '@forklaunch/interfaces-billing';
|
|
7
|
+
import { IdDto, InstanceTypeRecord } from '@forklaunch/common';
|
|
8
|
+
import { createCacheKey, TtlCache } from '@forklaunch/core/cache';
|
|
9
|
+
import {
|
|
10
|
+
InternalDtoMapper,
|
|
11
|
+
RequestDtoMapperConstructor,
|
|
12
|
+
ResponseDtoMapperConstructor,
|
|
13
|
+
transformIntoInternalDtoMapper
|
|
14
|
+
} from '@forklaunch/core/dtoMapper';
|
|
15
|
+
import {
|
|
16
|
+
MetricsDefinition,
|
|
17
|
+
OpenTelemetryCollector
|
|
18
|
+
} from '@forklaunch/core/http';
|
|
19
|
+
import { AnySchemaValidator } from '@forklaunch/validator';
|
|
20
|
+
|
|
21
|
+
export class BaseBillingPortalService<
|
|
22
|
+
SchemaValidator extends AnySchemaValidator,
|
|
23
|
+
Metrics extends MetricsDefinition = MetricsDefinition,
|
|
24
|
+
Dto extends {
|
|
25
|
+
BillingPortalDtoMapper: BillingPortalDto;
|
|
26
|
+
CreateBillingPortalDtoMapper: CreateBillingPortalDto;
|
|
27
|
+
UpdateBillingPortalDtoMapper: UpdateBillingPortalDto;
|
|
28
|
+
} = {
|
|
29
|
+
BillingPortalDtoMapper: BillingPortalDto;
|
|
30
|
+
CreateBillingPortalDtoMapper: CreateBillingPortalDto;
|
|
31
|
+
UpdateBillingPortalDtoMapper: UpdateBillingPortalDto;
|
|
32
|
+
},
|
|
33
|
+
Entities extends {
|
|
34
|
+
BillingPortalDtoMapper: BillingPortalDto;
|
|
35
|
+
CreateBillingPortalDtoMapper: BillingPortalDto;
|
|
36
|
+
UpdateBillingPortalDtoMapper: BillingPortalDto;
|
|
37
|
+
} = {
|
|
38
|
+
BillingPortalDtoMapper: BillingPortalDto;
|
|
39
|
+
CreateBillingPortalDtoMapper: BillingPortalDto;
|
|
40
|
+
UpdateBillingPortalDtoMapper: BillingPortalDto;
|
|
41
|
+
}
|
|
42
|
+
> implements BillingPortalService
|
|
43
|
+
{
|
|
44
|
+
#dtoMappers: InternalDtoMapper<
|
|
45
|
+
InstanceTypeRecord<typeof this.dtoMappers>,
|
|
46
|
+
Entities,
|
|
47
|
+
Dto
|
|
48
|
+
>;
|
|
49
|
+
|
|
50
|
+
constructor(
|
|
51
|
+
protected cache: TtlCache,
|
|
52
|
+
protected openTelemetryCollector: OpenTelemetryCollector<Metrics>,
|
|
53
|
+
protected schemaValidator: SchemaValidator,
|
|
54
|
+
protected dtoMappers: {
|
|
55
|
+
BillingPortalDtoMapper: ResponseDtoMapperConstructor<
|
|
56
|
+
SchemaValidator,
|
|
57
|
+
Dto['BillingPortalDtoMapper'],
|
|
58
|
+
Entities['BillingPortalDtoMapper']
|
|
59
|
+
>;
|
|
60
|
+
CreateBillingPortalDtoMapper: RequestDtoMapperConstructor<
|
|
61
|
+
SchemaValidator,
|
|
62
|
+
Dto['CreateBillingPortalDtoMapper'],
|
|
63
|
+
Entities['CreateBillingPortalDtoMapper']
|
|
64
|
+
>;
|
|
65
|
+
UpdateBillingPortalDtoMapper: RequestDtoMapperConstructor<
|
|
66
|
+
SchemaValidator,
|
|
67
|
+
Dto['UpdateBillingPortalDtoMapper'],
|
|
68
|
+
Entities['UpdateBillingPortalDtoMapper']
|
|
69
|
+
>;
|
|
70
|
+
}
|
|
71
|
+
) {
|
|
72
|
+
this.#dtoMappers = transformIntoInternalDtoMapper(
|
|
73
|
+
dtoMappers,
|
|
74
|
+
schemaValidator
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
protected createCacheKey = createCacheKey('billing_portal_session');
|
|
79
|
+
|
|
80
|
+
async createBillingPortalSession(
|
|
81
|
+
billingPortalDto: Dto['CreateBillingPortalDtoMapper']
|
|
82
|
+
): Promise<Dto['BillingPortalDtoMapper']> {
|
|
83
|
+
const billingPortalSession =
|
|
84
|
+
this.#dtoMappers.CreateBillingPortalDtoMapper.deserializeDtoToEntity(
|
|
85
|
+
billingPortalDto
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
this.openTelemetryCollector.debug(
|
|
89
|
+
'Create billing portal session',
|
|
90
|
+
billingPortalSession
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
await this.cache.putRecord({
|
|
94
|
+
key: this.createCacheKey(billingPortalSession.id),
|
|
95
|
+
value: billingPortalSession,
|
|
96
|
+
ttlMilliseconds: this.cache.getTtlMilliseconds()
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
return this.#dtoMappers.BillingPortalDtoMapper.serializeEntityToDto(
|
|
100
|
+
billingPortalSession
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async getBillingPortalSession(
|
|
105
|
+
idDto: IdDto
|
|
106
|
+
): Promise<Dto['BillingPortalDtoMapper']> {
|
|
107
|
+
const billingPortalSessionDetails = await this.cache.readRecord<
|
|
108
|
+
Entities['BillingPortalDtoMapper']
|
|
109
|
+
>(this.createCacheKey(idDto.id));
|
|
110
|
+
if (!billingPortalSessionDetails) {
|
|
111
|
+
throw new Error('Session not found');
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return this.#dtoMappers.BillingPortalDtoMapper.serializeEntityToDto(
|
|
115
|
+
billingPortalSessionDetails.value
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async updateBillingPortalSession(
|
|
120
|
+
billingPortalDto: UpdateBillingPortalDto
|
|
121
|
+
): Promise<Dto['BillingPortalDtoMapper']> {
|
|
122
|
+
const billingPortalSession =
|
|
123
|
+
this.#dtoMappers.UpdateBillingPortalDtoMapper.deserializeDtoToEntity(
|
|
124
|
+
billingPortalDto
|
|
125
|
+
);
|
|
126
|
+
// Save the updated session to your database or external service
|
|
127
|
+
await this.cache.putRecord({
|
|
128
|
+
key: this.createCacheKey(billingPortalSession.id),
|
|
129
|
+
value: billingPortalSession,
|
|
130
|
+
ttlMilliseconds: this.cache.getTtlMilliseconds()
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
return this.#dtoMappers.BillingPortalDtoMapper.serializeEntityToDto(
|
|
134
|
+
billingPortalSession
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async expireBillingPortalSession(idDto: IdDto): Promise<void> {
|
|
139
|
+
const sessionExists = await this.cache.readRecord(
|
|
140
|
+
this.createCacheKey(idDto.id)
|
|
141
|
+
);
|
|
142
|
+
if (!sessionExists) {
|
|
143
|
+
throw new Error('Session not found');
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
await this.cache.deleteRecord(this.createCacheKey(idDto.id));
|
|
147
|
+
}
|
|
148
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CheckoutSessionDto,
|
|
3
|
+
CheckoutSessionService,
|
|
4
|
+
CreateCheckoutSessionDto,
|
|
5
|
+
UpdateCheckoutSessionDto
|
|
6
|
+
} from '@forklaunch/interfaces-billing';
|
|
7
|
+
import { IdDto, InstanceTypeRecord } from '@forklaunch/common';
|
|
8
|
+
import { createCacheKey, TtlCache } from '@forklaunch/core/cache';
|
|
9
|
+
import {
|
|
10
|
+
InternalDtoMapper,
|
|
11
|
+
RequestDtoMapperConstructor,
|
|
12
|
+
ResponseDtoMapperConstructor,
|
|
13
|
+
transformIntoInternalDtoMapper
|
|
14
|
+
} from '@forklaunch/core/dtoMapper';
|
|
15
|
+
import {
|
|
16
|
+
MetricsDefinition,
|
|
17
|
+
OpenTelemetryCollector
|
|
18
|
+
} from '@forklaunch/core/http';
|
|
19
|
+
import { AnySchemaValidator } from '@forklaunch/validator';
|
|
20
|
+
|
|
21
|
+
export class BaseCheckoutSessionService<
|
|
22
|
+
SchemaValidator extends AnySchemaValidator,
|
|
23
|
+
PaymentMethodEnum,
|
|
24
|
+
Metrics extends MetricsDefinition = MetricsDefinition,
|
|
25
|
+
Dto extends {
|
|
26
|
+
CheckoutSessionDtoMapper: CheckoutSessionDto<PaymentMethodEnum>;
|
|
27
|
+
CreateCheckoutSessionDtoMapper: CreateCheckoutSessionDto<PaymentMethodEnum>;
|
|
28
|
+
UpdateCheckoutSessionDtoMapper: UpdateCheckoutSessionDto<PaymentMethodEnum>;
|
|
29
|
+
} = {
|
|
30
|
+
CheckoutSessionDtoMapper: CheckoutSessionDto<PaymentMethodEnum>;
|
|
31
|
+
CreateCheckoutSessionDtoMapper: CreateCheckoutSessionDto<PaymentMethodEnum>;
|
|
32
|
+
UpdateCheckoutSessionDtoMapper: UpdateCheckoutSessionDto<PaymentMethodEnum>;
|
|
33
|
+
},
|
|
34
|
+
Entities extends {
|
|
35
|
+
CheckoutSessionDtoMapper: CheckoutSessionDto<PaymentMethodEnum>;
|
|
36
|
+
CreateCheckoutSessionDtoMapper: CheckoutSessionDto<PaymentMethodEnum>;
|
|
37
|
+
UpdateCheckoutSessionDtoMapper: CheckoutSessionDto<PaymentMethodEnum>;
|
|
38
|
+
} = {
|
|
39
|
+
CheckoutSessionDtoMapper: CheckoutSessionDto<PaymentMethodEnum>;
|
|
40
|
+
CreateCheckoutSessionDtoMapper: CheckoutSessionDto<PaymentMethodEnum>;
|
|
41
|
+
UpdateCheckoutSessionDtoMapper: CheckoutSessionDto<PaymentMethodEnum>;
|
|
42
|
+
}
|
|
43
|
+
> implements CheckoutSessionService<PaymentMethodEnum>
|
|
44
|
+
{
|
|
45
|
+
#dtoMappers: InternalDtoMapper<
|
|
46
|
+
InstanceTypeRecord<typeof this.dtoMappers>,
|
|
47
|
+
Entities,
|
|
48
|
+
Dto
|
|
49
|
+
>;
|
|
50
|
+
|
|
51
|
+
constructor(
|
|
52
|
+
protected readonly cache: TtlCache,
|
|
53
|
+
protected readonly openTelemetryCollector: OpenTelemetryCollector<Metrics>,
|
|
54
|
+
protected readonly schemaValidator: SchemaValidator,
|
|
55
|
+
protected readonly dtoMappers: {
|
|
56
|
+
CheckoutSessionDtoMapper: ResponseDtoMapperConstructor<
|
|
57
|
+
SchemaValidator,
|
|
58
|
+
Dto['CheckoutSessionDtoMapper'],
|
|
59
|
+
Entities['CheckoutSessionDtoMapper']
|
|
60
|
+
>;
|
|
61
|
+
CreateCheckoutSessionDtoMapper: RequestDtoMapperConstructor<
|
|
62
|
+
SchemaValidator,
|
|
63
|
+
Dto['CreateCheckoutSessionDtoMapper'],
|
|
64
|
+
Entities['CreateCheckoutSessionDtoMapper']
|
|
65
|
+
>;
|
|
66
|
+
UpdateCheckoutSessionDtoMapper: RequestDtoMapperConstructor<
|
|
67
|
+
SchemaValidator,
|
|
68
|
+
Dto['UpdateCheckoutSessionDtoMapper'],
|
|
69
|
+
Entities['UpdateCheckoutSessionDtoMapper']
|
|
70
|
+
>;
|
|
71
|
+
}
|
|
72
|
+
) {
|
|
73
|
+
this.#dtoMappers = transformIntoInternalDtoMapper(
|
|
74
|
+
dtoMappers,
|
|
75
|
+
schemaValidator
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
protected createCacheKey = createCacheKey('checkout_session');
|
|
80
|
+
|
|
81
|
+
async createCheckoutSession(
|
|
82
|
+
checkoutSessionDto: Dto['CreateCheckoutSessionDtoMapper']
|
|
83
|
+
): Promise<Dto['CheckoutSessionDtoMapper']> {
|
|
84
|
+
const checkoutSession =
|
|
85
|
+
this.#dtoMappers.CreateCheckoutSessionDtoMapper.deserializeDtoToEntity(
|
|
86
|
+
checkoutSessionDto
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
// Store the checkoutSession details in the cache
|
|
90
|
+
await this.cache.putRecord({
|
|
91
|
+
key: this.createCacheKey(checkoutSession.id),
|
|
92
|
+
value: checkoutSession,
|
|
93
|
+
ttlMilliseconds: this.cache.getTtlMilliseconds()
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
return this.#dtoMappers.CheckoutSessionDtoMapper.serializeEntityToDto(
|
|
97
|
+
checkoutSession
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async getCheckoutSession({
|
|
102
|
+
id
|
|
103
|
+
}: IdDto): Promise<Dto['CheckoutSessionDtoMapper']> {
|
|
104
|
+
const checkoutSessionDetails = await this.cache.readRecord<
|
|
105
|
+
Entities['CheckoutSessionDtoMapper']
|
|
106
|
+
>(this.createCacheKey(id));
|
|
107
|
+
if (!checkoutSessionDetails) {
|
|
108
|
+
throw new Error('Session not found');
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return this.#dtoMappers.CheckoutSessionDtoMapper.serializeEntityToDto(
|
|
112
|
+
checkoutSessionDetails.value
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
async expireCheckoutSession({ id }: IdDto): Promise<void> {
|
|
117
|
+
const checkoutSessionDetails = await this.cache.readRecord(
|
|
118
|
+
this.createCacheKey(id)
|
|
119
|
+
);
|
|
120
|
+
if (!checkoutSessionDetails) {
|
|
121
|
+
throw new Error('Session not found');
|
|
122
|
+
}
|
|
123
|
+
await this.cache.deleteRecord(this.createCacheKey(id));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async handleCheckoutSuccess({ id }: IdDto): Promise<void> {
|
|
127
|
+
this.openTelemetryCollector.info('Checkout success', { id });
|
|
128
|
+
await this.cache.deleteRecord(this.createCacheKey(id));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async handleCheckoutFailure({ id }: IdDto): Promise<void> {
|
|
132
|
+
this.openTelemetryCollector.info('Checkout failure', { id });
|
|
133
|
+
await this.cache.deleteRecord(this.createCacheKey(id));
|
|
134
|
+
}
|
|
135
|
+
}
|