servcraft 0.1.0 → 0.1.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/.claude/settings.local.json +29 -0
- package/.github/CODEOWNERS +18 -0
- package/.github/PULL_REQUEST_TEMPLATE.md +46 -0
- package/.github/dependabot.yml +59 -0
- package/.github/workflows/ci.yml +188 -0
- package/.github/workflows/release.yml +195 -0
- package/AUDIT.md +602 -0
- package/README.md +1070 -1
- package/dist/cli/index.cjs +2026 -2168
- package/dist/cli/index.cjs.map +1 -1
- package/dist/cli/index.js +2026 -2168
- package/dist/cli/index.js.map +1 -1
- package/dist/index.cjs +595 -616
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +114 -52
- package/dist/index.d.ts +114 -52
- package/dist/index.js +595 -616
- package/dist/index.js.map +1 -1
- package/docs/CLI-001_MULTI_DB_PLAN.md +546 -0
- package/docs/DATABASE_MULTI_ORM.md +399 -0
- package/docs/PHASE1_BREAKDOWN.md +346 -0
- package/docs/PROGRESS.md +550 -0
- package/docs/modules/ANALYTICS.md +226 -0
- package/docs/modules/API-VERSIONING.md +252 -0
- package/docs/modules/AUDIT.md +192 -0
- package/docs/modules/AUTH.md +431 -0
- package/docs/modules/CACHE.md +346 -0
- package/docs/modules/EMAIL.md +254 -0
- package/docs/modules/FEATURE-FLAG.md +291 -0
- package/docs/modules/I18N.md +294 -0
- package/docs/modules/MEDIA-PROCESSING.md +281 -0
- package/docs/modules/MFA.md +266 -0
- package/docs/modules/NOTIFICATION.md +311 -0
- package/docs/modules/OAUTH.md +237 -0
- package/docs/modules/PAYMENT.md +804 -0
- package/docs/modules/QUEUE.md +540 -0
- package/docs/modules/RATE-LIMIT.md +339 -0
- package/docs/modules/SEARCH.md +288 -0
- package/docs/modules/SECURITY.md +327 -0
- package/docs/modules/SESSION.md +382 -0
- package/docs/modules/SWAGGER.md +305 -0
- package/docs/modules/UPLOAD.md +296 -0
- package/docs/modules/USER.md +505 -0
- package/docs/modules/VALIDATION.md +294 -0
- package/docs/modules/WEBHOOK.md +270 -0
- package/docs/modules/WEBSOCKET.md +691 -0
- package/package.json +53 -38
- package/prisma/schema.prisma +395 -1
- package/src/cli/commands/add-module.ts +520 -87
- package/src/cli/commands/db.ts +3 -4
- package/src/cli/commands/docs.ts +256 -6
- package/src/cli/commands/generate.ts +12 -19
- package/src/cli/commands/init.ts +384 -214
- package/src/cli/index.ts +0 -4
- package/src/cli/templates/repository.ts +6 -1
- package/src/cli/templates/routes.ts +6 -21
- package/src/cli/utils/docs-generator.ts +6 -7
- package/src/cli/utils/env-manager.ts +717 -0
- package/src/cli/utils/field-parser.ts +16 -7
- package/src/cli/utils/interactive-prompt.ts +223 -0
- package/src/cli/utils/template-manager.ts +346 -0
- package/src/config/database.config.ts +183 -0
- package/src/config/env.ts +0 -10
- package/src/config/index.ts +0 -14
- package/src/core/server.ts +1 -1
- package/src/database/adapters/mongoose.adapter.ts +132 -0
- package/src/database/adapters/prisma.adapter.ts +118 -0
- package/src/database/connection.ts +190 -0
- package/src/database/interfaces/database.interface.ts +85 -0
- package/src/database/interfaces/index.ts +7 -0
- package/src/database/interfaces/repository.interface.ts +129 -0
- package/src/database/models/mongoose/index.ts +7 -0
- package/src/database/models/mongoose/payment.schema.ts +347 -0
- package/src/database/models/mongoose/user.schema.ts +154 -0
- package/src/database/prisma.ts +1 -4
- package/src/database/redis.ts +101 -0
- package/src/database/repositories/mongoose/index.ts +7 -0
- package/src/database/repositories/mongoose/payment.repository.ts +380 -0
- package/src/database/repositories/mongoose/user.repository.ts +255 -0
- package/src/database/seed.ts +6 -1
- package/src/index.ts +9 -20
- package/src/middleware/security.ts +2 -6
- package/src/modules/analytics/analytics.routes.ts +80 -0
- package/src/modules/analytics/analytics.service.ts +364 -0
- package/src/modules/analytics/index.ts +18 -0
- package/src/modules/analytics/types.ts +180 -0
- package/src/modules/api-versioning/index.ts +15 -0
- package/src/modules/api-versioning/types.ts +86 -0
- package/src/modules/api-versioning/versioning.middleware.ts +120 -0
- package/src/modules/api-versioning/versioning.routes.ts +54 -0
- package/src/modules/api-versioning/versioning.service.ts +189 -0
- package/src/modules/audit/audit.repository.ts +206 -0
- package/src/modules/audit/audit.service.ts +27 -59
- package/src/modules/auth/auth.controller.ts +2 -2
- package/src/modules/auth/auth.middleware.ts +3 -9
- package/src/modules/auth/auth.routes.ts +10 -107
- package/src/modules/auth/auth.service.ts +126 -23
- package/src/modules/auth/index.ts +3 -4
- package/src/modules/cache/cache.service.ts +367 -0
- package/src/modules/cache/index.ts +10 -0
- package/src/modules/cache/types.ts +44 -0
- package/src/modules/email/email.service.ts +3 -10
- package/src/modules/email/templates.ts +2 -8
- package/src/modules/feature-flag/feature-flag.repository.ts +303 -0
- package/src/modules/feature-flag/feature-flag.routes.ts +247 -0
- package/src/modules/feature-flag/feature-flag.service.ts +566 -0
- package/src/modules/feature-flag/index.ts +20 -0
- package/src/modules/feature-flag/types.ts +192 -0
- package/src/modules/i18n/i18n.middleware.ts +186 -0
- package/src/modules/i18n/i18n.routes.ts +191 -0
- package/src/modules/i18n/i18n.service.ts +456 -0
- package/src/modules/i18n/index.ts +18 -0
- package/src/modules/i18n/types.ts +118 -0
- package/src/modules/media-processing/index.ts +17 -0
- package/src/modules/media-processing/media-processing.routes.ts +111 -0
- package/src/modules/media-processing/media-processing.service.ts +245 -0
- package/src/modules/media-processing/types.ts +156 -0
- package/src/modules/mfa/index.ts +20 -0
- package/src/modules/mfa/mfa.repository.ts +206 -0
- package/src/modules/mfa/mfa.routes.ts +595 -0
- package/src/modules/mfa/mfa.service.ts +572 -0
- package/src/modules/mfa/totp.ts +150 -0
- package/src/modules/mfa/types.ts +57 -0
- package/src/modules/notification/index.ts +20 -0
- package/src/modules/notification/notification.repository.ts +356 -0
- package/src/modules/notification/notification.service.ts +483 -0
- package/src/modules/notification/types.ts +119 -0
- package/src/modules/oauth/index.ts +20 -0
- package/src/modules/oauth/oauth.repository.ts +219 -0
- package/src/modules/oauth/oauth.routes.ts +446 -0
- package/src/modules/oauth/oauth.service.ts +293 -0
- package/src/modules/oauth/providers/apple.provider.ts +250 -0
- package/src/modules/oauth/providers/facebook.provider.ts +181 -0
- package/src/modules/oauth/providers/github.provider.ts +248 -0
- package/src/modules/oauth/providers/google.provider.ts +189 -0
- package/src/modules/oauth/providers/twitter.provider.ts +214 -0
- package/src/modules/oauth/types.ts +94 -0
- package/src/modules/payment/index.ts +19 -0
- package/src/modules/payment/payment.repository.ts +733 -0
- package/src/modules/payment/payment.routes.ts +390 -0
- package/src/modules/payment/payment.service.ts +354 -0
- package/src/modules/payment/providers/mobile-money.provider.ts +274 -0
- package/src/modules/payment/providers/paypal.provider.ts +190 -0
- package/src/modules/payment/providers/stripe.provider.ts +215 -0
- package/src/modules/payment/types.ts +140 -0
- package/src/modules/queue/cron.ts +438 -0
- package/src/modules/queue/index.ts +87 -0
- package/src/modules/queue/queue.routes.ts +600 -0
- package/src/modules/queue/queue.service.ts +842 -0
- package/src/modules/queue/types.ts +222 -0
- package/src/modules/queue/workers.ts +366 -0
- package/src/modules/rate-limit/index.ts +59 -0
- package/src/modules/rate-limit/rate-limit.middleware.ts +134 -0
- package/src/modules/rate-limit/rate-limit.routes.ts +269 -0
- package/src/modules/rate-limit/rate-limit.service.ts +348 -0
- package/src/modules/rate-limit/stores/memory.store.ts +165 -0
- package/src/modules/rate-limit/stores/redis.store.ts +322 -0
- package/src/modules/rate-limit/types.ts +153 -0
- package/src/modules/search/adapters/elasticsearch.adapter.ts +326 -0
- package/src/modules/search/adapters/meilisearch.adapter.ts +261 -0
- package/src/modules/search/adapters/memory.adapter.ts +278 -0
- package/src/modules/search/index.ts +21 -0
- package/src/modules/search/search.service.ts +234 -0
- package/src/modules/search/types.ts +214 -0
- package/src/modules/security/index.ts +40 -0
- package/src/modules/security/sanitize.ts +223 -0
- package/src/modules/security/security-audit.service.ts +388 -0
- package/src/modules/security/security.middleware.ts +398 -0
- package/src/modules/session/index.ts +3 -0
- package/src/modules/session/session.repository.ts +159 -0
- package/src/modules/session/session.service.ts +340 -0
- package/src/modules/session/types.ts +38 -0
- package/src/modules/swagger/index.ts +7 -1
- package/src/modules/swagger/schema-builder.ts +16 -4
- package/src/modules/swagger/swagger.service.ts +9 -10
- package/src/modules/swagger/types.ts +0 -2
- package/src/modules/upload/index.ts +14 -0
- package/src/modules/upload/types.ts +83 -0
- package/src/modules/upload/upload.repository.ts +199 -0
- package/src/modules/upload/upload.routes.ts +311 -0
- package/src/modules/upload/upload.service.ts +448 -0
- package/src/modules/user/index.ts +3 -3
- package/src/modules/user/user.controller.ts +15 -9
- package/src/modules/user/user.repository.ts +237 -113
- package/src/modules/user/user.routes.ts +39 -164
- package/src/modules/user/user.service.ts +4 -3
- package/src/modules/validation/validator.ts +12 -17
- package/src/modules/webhook/index.ts +91 -0
- package/src/modules/webhook/retry.ts +196 -0
- package/src/modules/webhook/signature.ts +135 -0
- package/src/modules/webhook/types.ts +181 -0
- package/src/modules/webhook/webhook.repository.ts +358 -0
- package/src/modules/webhook/webhook.routes.ts +442 -0
- package/src/modules/webhook/webhook.service.ts +457 -0
- package/src/modules/websocket/features.ts +504 -0
- package/src/modules/websocket/index.ts +106 -0
- package/src/modules/websocket/middlewares.ts +298 -0
- package/src/modules/websocket/types.ts +181 -0
- package/src/modules/websocket/websocket.service.ts +692 -0
- package/src/utils/errors.ts +7 -0
- package/src/utils/pagination.ts +4 -1
- package/tests/helpers/db-check.ts +79 -0
- package/tests/integration/auth-redis.test.ts +94 -0
- package/tests/integration/cache-redis.test.ts +387 -0
- package/tests/integration/mongoose-repositories.test.ts +410 -0
- package/tests/integration/payment-prisma.test.ts +637 -0
- package/tests/integration/queue-bullmq.test.ts +417 -0
- package/tests/integration/user-prisma.test.ts +441 -0
- package/tests/integration/websocket-socketio.test.ts +552 -0
- package/tests/setup.ts +11 -9
- package/vitest.config.ts +3 -8
- package/npm-cache/_cacache/content-v2/sha512/1c/d0/03440d500a0487621aad1d6402978340698976602046db8e24fa03c01ee6c022c69b0582f969042d9442ee876ac35c038e960dd427d1e622fa24b8eb7dba +0 -0
- package/npm-cache/_cacache/content-v2/sha512/42/55/28b493ca491833e5aab0e9c3108d29ab3f36c248ca88f45d4630674fce9130959e56ae308797ac2b6328fa7f09a610b9550ed09cb971d039876d293fc69d +0 -0
- package/npm-cache/_cacache/content-v2/sha512/e0/12/f360dc9315ee5f17844a0c8c233ee6bf7c30837c4a02ea0d56c61c7f7ab21c0e958e50ed2c57c59f983c762b93056778c9009b2398ffc26def0183999b13 +0 -0
- package/npm-cache/_cacache/content-v2/sha512/ed/b0/fae1161902898f4c913c67d7f6cdf6be0665aec3b389b9c4f4f0a101ca1da59badf1b59c4e0030f5223023b8d63cfe501c46a32c20c895d4fb3f11ca2232 +0 -0
- package/npm-cache/_cacache/index-v5/58/94/c2cba79e0f16b4c10e95a87e32255741149e8222cc314a476aab67c39cc0 +0 -5
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';
|
|
2
|
+
import type { AuthService } from '../auth/auth.service.js';
|
|
3
|
+
import { createAuthMiddleware } from '../auth/auth.middleware.js';
|
|
4
|
+
import { commonResponses, idParam } from '../swagger/index.js';
|
|
5
|
+
import { getPaymentService } from './payment.service.js';
|
|
6
|
+
import type { CreatePaymentData, PaymentProvider } from './types.js';
|
|
7
|
+
|
|
8
|
+
const paymentTag = 'Payments';
|
|
9
|
+
|
|
10
|
+
const paymentResponse = {
|
|
11
|
+
type: 'object',
|
|
12
|
+
properties: {
|
|
13
|
+
success: { type: 'boolean', example: true },
|
|
14
|
+
data: {
|
|
15
|
+
type: 'object',
|
|
16
|
+
properties: {
|
|
17
|
+
id: { type: 'string', format: 'uuid' },
|
|
18
|
+
status: {
|
|
19
|
+
type: 'string',
|
|
20
|
+
enum: ['pending', 'processing', 'completed', 'failed', 'refunded'],
|
|
21
|
+
},
|
|
22
|
+
amount: { type: 'number' },
|
|
23
|
+
currency: { type: 'string' },
|
|
24
|
+
provider: { type: 'string', enum: ['stripe', 'paypal', 'mobile_money', 'manual'] },
|
|
25
|
+
clientSecret: { type: 'string' },
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const createPaymentBody = {
|
|
32
|
+
type: 'object',
|
|
33
|
+
required: ['amount', 'currency', 'provider'],
|
|
34
|
+
properties: {
|
|
35
|
+
amount: { type: 'number', minimum: 0.01, description: 'Payment amount' },
|
|
36
|
+
currency: { type: 'string', minLength: 3, maxLength: 3, description: 'ISO 4217 currency code' },
|
|
37
|
+
provider: {
|
|
38
|
+
type: 'string',
|
|
39
|
+
enum: ['stripe', 'paypal', 'mobile_money'],
|
|
40
|
+
description: 'Payment provider',
|
|
41
|
+
},
|
|
42
|
+
method: { type: 'string', enum: ['card', 'bank_transfer', 'mobile_money', 'paypal'] },
|
|
43
|
+
description: { type: 'string' },
|
|
44
|
+
metadata: { type: 'object', additionalProperties: true },
|
|
45
|
+
returnUrl: {
|
|
46
|
+
type: 'string',
|
|
47
|
+
format: 'uri',
|
|
48
|
+
description: 'URL to redirect after payment (PayPal)',
|
|
49
|
+
},
|
|
50
|
+
cancelUrl: { type: 'string', format: 'uri', description: 'URL to redirect on cancel (PayPal)' },
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const subscriptionBody = {
|
|
55
|
+
type: 'object',
|
|
56
|
+
required: ['planId'],
|
|
57
|
+
properties: {
|
|
58
|
+
planId: { type: 'string', format: 'uuid' },
|
|
59
|
+
provider: { type: 'string', enum: ['stripe', 'paypal'], default: 'stripe' },
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export function registerPaymentRoutes(app: FastifyInstance, authService: AuthService): void {
|
|
64
|
+
const authenticate = createAuthMiddleware(authService);
|
|
65
|
+
const paymentService = getPaymentService();
|
|
66
|
+
|
|
67
|
+
// Create payment intent
|
|
68
|
+
app.post(
|
|
69
|
+
'/payments',
|
|
70
|
+
{
|
|
71
|
+
preHandler: [authenticate],
|
|
72
|
+
schema: {
|
|
73
|
+
tags: [paymentTag],
|
|
74
|
+
summary: 'Create a payment intent',
|
|
75
|
+
description:
|
|
76
|
+
'Creates a payment intent with the specified provider (Stripe, PayPal, or Mobile Money)',
|
|
77
|
+
security: [{ bearerAuth: [] }],
|
|
78
|
+
body: createPaymentBody,
|
|
79
|
+
response: {
|
|
80
|
+
201: paymentResponse,
|
|
81
|
+
400: commonResponses.error,
|
|
82
|
+
401: commonResponses.unauthorized,
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
async (request: FastifyRequest, reply: FastifyReply) => {
|
|
87
|
+
const userId = (request as FastifyRequest & { user: { id: string } }).user.id;
|
|
88
|
+
const body = request.body as CreatePaymentData;
|
|
89
|
+
const intent = await paymentService.createPayment({ ...body, userId });
|
|
90
|
+
return reply.status(201).send({ success: true, data: intent });
|
|
91
|
+
}
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
// Confirm payment
|
|
95
|
+
app.post(
|
|
96
|
+
'/payments/:id/confirm',
|
|
97
|
+
{
|
|
98
|
+
preHandler: [authenticate],
|
|
99
|
+
schema: {
|
|
100
|
+
tags: [paymentTag],
|
|
101
|
+
summary: 'Confirm a payment',
|
|
102
|
+
security: [{ bearerAuth: [] }],
|
|
103
|
+
params: idParam,
|
|
104
|
+
response: {
|
|
105
|
+
200: paymentResponse,
|
|
106
|
+
400: commonResponses.error,
|
|
107
|
+
401: commonResponses.unauthorized,
|
|
108
|
+
404: commonResponses.notFound,
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
async (request: FastifyRequest, reply: FastifyReply) => {
|
|
113
|
+
const params = request.params as { id: string };
|
|
114
|
+
const payment = await paymentService.confirmPayment(params.id);
|
|
115
|
+
return reply.send({ success: true, data: payment });
|
|
116
|
+
}
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
// Get payment by ID
|
|
120
|
+
app.get(
|
|
121
|
+
'/payments/:id',
|
|
122
|
+
{
|
|
123
|
+
preHandler: [authenticate],
|
|
124
|
+
schema: {
|
|
125
|
+
tags: [paymentTag],
|
|
126
|
+
summary: 'Get payment details',
|
|
127
|
+
security: [{ bearerAuth: [] }],
|
|
128
|
+
params: idParam,
|
|
129
|
+
response: {
|
|
130
|
+
200: paymentResponse,
|
|
131
|
+
401: commonResponses.unauthorized,
|
|
132
|
+
404: commonResponses.notFound,
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
async (request: FastifyRequest, reply: FastifyReply) => {
|
|
137
|
+
const params = request.params as { id: string };
|
|
138
|
+
const payment = await paymentService.getPayment(params.id);
|
|
139
|
+
if (!payment) {
|
|
140
|
+
return reply.status(404).send({ success: false, message: 'Payment not found' });
|
|
141
|
+
}
|
|
142
|
+
return reply.send({ success: true, data: payment });
|
|
143
|
+
}
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
// Get user payments
|
|
147
|
+
app.get(
|
|
148
|
+
'/payments',
|
|
149
|
+
{
|
|
150
|
+
preHandler: [authenticate],
|
|
151
|
+
schema: {
|
|
152
|
+
tags: [paymentTag],
|
|
153
|
+
summary: 'Get user payments',
|
|
154
|
+
security: [{ bearerAuth: [] }],
|
|
155
|
+
response: {
|
|
156
|
+
200: {
|
|
157
|
+
type: 'object',
|
|
158
|
+
properties: {
|
|
159
|
+
success: { type: 'boolean' },
|
|
160
|
+
data: { type: 'array', items: { type: 'object' } },
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
401: commonResponses.unauthorized,
|
|
164
|
+
},
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
async (request: FastifyRequest, reply: FastifyReply) => {
|
|
168
|
+
const userId = (request as FastifyRequest & { user: { id: string } }).user.id;
|
|
169
|
+
const payments = await paymentService.getUserPayments(userId);
|
|
170
|
+
return reply.send({ success: true, data: payments });
|
|
171
|
+
}
|
|
172
|
+
);
|
|
173
|
+
|
|
174
|
+
// Refund payment
|
|
175
|
+
app.post(
|
|
176
|
+
'/payments/:id/refund',
|
|
177
|
+
{
|
|
178
|
+
preHandler: [authenticate],
|
|
179
|
+
schema: {
|
|
180
|
+
tags: [paymentTag],
|
|
181
|
+
summary: 'Refund a payment',
|
|
182
|
+
security: [{ bearerAuth: [] }],
|
|
183
|
+
params: idParam,
|
|
184
|
+
body: {
|
|
185
|
+
type: 'object',
|
|
186
|
+
properties: {
|
|
187
|
+
amount: {
|
|
188
|
+
type: 'number',
|
|
189
|
+
minimum: 0.01,
|
|
190
|
+
description: 'Partial refund amount (optional)',
|
|
191
|
+
},
|
|
192
|
+
},
|
|
193
|
+
},
|
|
194
|
+
response: {
|
|
195
|
+
200: paymentResponse,
|
|
196
|
+
400: commonResponses.error,
|
|
197
|
+
401: commonResponses.unauthorized,
|
|
198
|
+
404: commonResponses.notFound,
|
|
199
|
+
},
|
|
200
|
+
},
|
|
201
|
+
},
|
|
202
|
+
async (request: FastifyRequest, reply: FastifyReply) => {
|
|
203
|
+
const params = request.params as { id: string };
|
|
204
|
+
const body = request.body as { amount?: number };
|
|
205
|
+
const payment = await paymentService.refundPayment(params.id, body.amount);
|
|
206
|
+
return reply.send({ success: true, data: payment });
|
|
207
|
+
}
|
|
208
|
+
);
|
|
209
|
+
|
|
210
|
+
// Subscriptions
|
|
211
|
+
app.post(
|
|
212
|
+
'/subscriptions',
|
|
213
|
+
{
|
|
214
|
+
preHandler: [authenticate],
|
|
215
|
+
schema: {
|
|
216
|
+
tags: [paymentTag],
|
|
217
|
+
summary: 'Create a subscription',
|
|
218
|
+
security: [{ bearerAuth: [] }],
|
|
219
|
+
body: subscriptionBody,
|
|
220
|
+
response: {
|
|
221
|
+
201: {
|
|
222
|
+
type: 'object',
|
|
223
|
+
properties: {
|
|
224
|
+
success: { type: 'boolean' },
|
|
225
|
+
data: { type: 'object' },
|
|
226
|
+
},
|
|
227
|
+
},
|
|
228
|
+
400: commonResponses.error,
|
|
229
|
+
401: commonResponses.unauthorized,
|
|
230
|
+
},
|
|
231
|
+
},
|
|
232
|
+
},
|
|
233
|
+
async (request: FastifyRequest, reply: FastifyReply) => {
|
|
234
|
+
const userId = (request as FastifyRequest & { user: { id: string } }).user.id;
|
|
235
|
+
const body = request.body as { planId: string; provider?: PaymentProvider };
|
|
236
|
+
const subscription = await paymentService.createSubscription(
|
|
237
|
+
userId,
|
|
238
|
+
body.planId,
|
|
239
|
+
body.provider
|
|
240
|
+
);
|
|
241
|
+
return reply.status(201).send({ success: true, data: subscription });
|
|
242
|
+
}
|
|
243
|
+
);
|
|
244
|
+
|
|
245
|
+
app.delete(
|
|
246
|
+
'/subscriptions/:id',
|
|
247
|
+
{
|
|
248
|
+
preHandler: [authenticate],
|
|
249
|
+
schema: {
|
|
250
|
+
tags: [paymentTag],
|
|
251
|
+
summary: 'Cancel a subscription',
|
|
252
|
+
security: [{ bearerAuth: [] }],
|
|
253
|
+
params: idParam,
|
|
254
|
+
response: {
|
|
255
|
+
200: {
|
|
256
|
+
type: 'object',
|
|
257
|
+
properties: { success: { type: 'boolean' }, data: { type: 'object' } },
|
|
258
|
+
},
|
|
259
|
+
401: commonResponses.unauthorized,
|
|
260
|
+
404: commonResponses.notFound,
|
|
261
|
+
},
|
|
262
|
+
},
|
|
263
|
+
},
|
|
264
|
+
async (request: FastifyRequest, reply: FastifyReply) => {
|
|
265
|
+
const params = request.params as { id: string };
|
|
266
|
+
const subscription = await paymentService.cancelSubscription(params.id);
|
|
267
|
+
return reply.send({ success: true, data: subscription });
|
|
268
|
+
}
|
|
269
|
+
);
|
|
270
|
+
|
|
271
|
+
app.get(
|
|
272
|
+
'/subscriptions',
|
|
273
|
+
{
|
|
274
|
+
preHandler: [authenticate],
|
|
275
|
+
schema: {
|
|
276
|
+
tags: [paymentTag],
|
|
277
|
+
summary: 'Get user subscriptions',
|
|
278
|
+
security: [{ bearerAuth: [] }],
|
|
279
|
+
response: {
|
|
280
|
+
200: {
|
|
281
|
+
type: 'object',
|
|
282
|
+
properties: { success: { type: 'boolean' }, data: { type: 'array' } },
|
|
283
|
+
},
|
|
284
|
+
401: commonResponses.unauthorized,
|
|
285
|
+
},
|
|
286
|
+
},
|
|
287
|
+
},
|
|
288
|
+
async (request: FastifyRequest, reply: FastifyReply) => {
|
|
289
|
+
const userId = (request as FastifyRequest & { user: { id: string } }).user.id;
|
|
290
|
+
const subscriptions = await paymentService.getUserSubscriptions(userId);
|
|
291
|
+
return reply.send({ success: true, data: subscriptions });
|
|
292
|
+
}
|
|
293
|
+
);
|
|
294
|
+
|
|
295
|
+
// Plans
|
|
296
|
+
app.get(
|
|
297
|
+
'/plans',
|
|
298
|
+
{
|
|
299
|
+
schema: {
|
|
300
|
+
tags: [paymentTag],
|
|
301
|
+
summary: 'Get available subscription plans',
|
|
302
|
+
response: {
|
|
303
|
+
200: {
|
|
304
|
+
type: 'object',
|
|
305
|
+
properties: {
|
|
306
|
+
success: { type: 'boolean' },
|
|
307
|
+
data: {
|
|
308
|
+
type: 'array',
|
|
309
|
+
items: {
|
|
310
|
+
type: 'object',
|
|
311
|
+
properties: {
|
|
312
|
+
id: { type: 'string' },
|
|
313
|
+
name: { type: 'string' },
|
|
314
|
+
amount: { type: 'number' },
|
|
315
|
+
currency: { type: 'string' },
|
|
316
|
+
interval: { type: 'string' },
|
|
317
|
+
features: { type: 'array', items: { type: 'string' } },
|
|
318
|
+
},
|
|
319
|
+
},
|
|
320
|
+
},
|
|
321
|
+
},
|
|
322
|
+
},
|
|
323
|
+
},
|
|
324
|
+
},
|
|
325
|
+
},
|
|
326
|
+
async (_request: FastifyRequest, reply: FastifyReply) => {
|
|
327
|
+
const plans = await paymentService.getPlans();
|
|
328
|
+
return reply.send({ success: true, data: plans });
|
|
329
|
+
}
|
|
330
|
+
);
|
|
331
|
+
|
|
332
|
+
// Webhooks (no auth - verified by signature)
|
|
333
|
+
app.post(
|
|
334
|
+
'/webhooks/stripe',
|
|
335
|
+
{
|
|
336
|
+
schema: {
|
|
337
|
+
tags: [paymentTag],
|
|
338
|
+
summary: 'Stripe webhook endpoint',
|
|
339
|
+
description: 'Receives Stripe webhook events',
|
|
340
|
+
body: { type: 'string' },
|
|
341
|
+
response: {
|
|
342
|
+
200: { type: 'object', properties: { received: { type: 'boolean' } } },
|
|
343
|
+
},
|
|
344
|
+
},
|
|
345
|
+
config: { rawBody: true } as Record<string, unknown>,
|
|
346
|
+
},
|
|
347
|
+
async (request: FastifyRequest, reply: FastifyReply) => {
|
|
348
|
+
const signature = request.headers['stripe-signature'] as string;
|
|
349
|
+
await paymentService.handleWebhook('stripe', request.body as string, signature);
|
|
350
|
+
return reply.send({ received: true });
|
|
351
|
+
}
|
|
352
|
+
);
|
|
353
|
+
|
|
354
|
+
app.post(
|
|
355
|
+
'/webhooks/paypal',
|
|
356
|
+
{
|
|
357
|
+
schema: {
|
|
358
|
+
tags: [paymentTag],
|
|
359
|
+
summary: 'PayPal webhook endpoint',
|
|
360
|
+
body: { type: 'object' },
|
|
361
|
+
response: {
|
|
362
|
+
200: { type: 'object', properties: { received: { type: 'boolean' } } },
|
|
363
|
+
},
|
|
364
|
+
},
|
|
365
|
+
},
|
|
366
|
+
async (request: FastifyRequest, reply: FastifyReply) => {
|
|
367
|
+
const signature = request.headers['paypal-transmission-sig'] as string;
|
|
368
|
+
await paymentService.handleWebhook('paypal', JSON.stringify(request.body), signature);
|
|
369
|
+
return reply.send({ received: true });
|
|
370
|
+
}
|
|
371
|
+
);
|
|
372
|
+
|
|
373
|
+
app.post(
|
|
374
|
+
'/webhooks/mobile-money',
|
|
375
|
+
{
|
|
376
|
+
schema: {
|
|
377
|
+
tags: [paymentTag],
|
|
378
|
+
summary: 'Mobile Money callback endpoint',
|
|
379
|
+
body: { type: 'object' },
|
|
380
|
+
response: {
|
|
381
|
+
200: { type: 'object', properties: { received: { type: 'boolean' } } },
|
|
382
|
+
},
|
|
383
|
+
},
|
|
384
|
+
},
|
|
385
|
+
async (request: FastifyRequest, reply: FastifyReply) => {
|
|
386
|
+
await paymentService.handleWebhook('mobile_money', JSON.stringify(request.body), '');
|
|
387
|
+
return reply.send({ received: true });
|
|
388
|
+
}
|
|
389
|
+
);
|
|
390
|
+
}
|