@donotdev/functions 0.0.20 → 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/LICENSE.md +3 -3
- package/guidelines/FUNCTIONS.md +9 -0
- package/guidelines/VERCEL.md +260 -0
- package/lib/firebase/auth/getCustomClaims.d.ts +2 -2
- package/lib/firebase/auth/getUserAuthStatus.d.ts +2 -2
- package/lib/firebase/auth/index.d.ts +1 -1
- package/lib/firebase/auth/removeCustomClaims.d.ts +2 -2
- package/lib/firebase/auth/setCustomClaims.d.ts +2 -2
- package/lib/firebase/baseFunction.d.ts +1 -1
- package/lib/firebase/billing/cancelSubscription.d.ts +1 -1
- package/lib/firebase/billing/changePlan.d.ts +1 -1
- package/lib/firebase/billing/createCheckoutSession.d.ts +10 -13
- package/lib/firebase/billing/createCustomerPortal.d.ts +1 -1
- package/lib/firebase/billing/index.d.ts +1 -1
- package/lib/firebase/billing/refreshSubscriptionStatus.d.ts +2 -2
- package/lib/firebase/config/constants.d.ts +4 -4
- package/lib/firebase/config/index.d.ts +1 -1
- package/lib/firebase/crud/aggregate.d.ts +2 -2
- package/lib/firebase/crud/delete.d.ts +1 -1
- package/lib/firebase/crud/get.d.ts +1 -1
- package/lib/firebase/crud/index.d.ts +1 -1
- package/lib/firebase/crud/update.d.ts +1 -1
- package/lib/firebase/helpers/githubAccessHelper.d.ts +3 -3
- package/lib/firebase/helpers/githubTeamAccessHelper.d.ts +2 -2
- package/lib/firebase/helpers/index.d.ts +1 -1
- package/lib/firebase/index.d.ts +1 -1
- package/lib/firebase/index.js +3 -3
- package/lib/firebase/index.js.map +4 -4
- package/lib/firebase/oauth/disconnect.d.ts +1 -1
- package/lib/firebase/oauth/exchangeToken.d.ts +1 -1
- package/lib/firebase/oauth/getConnections.d.ts +1 -1
- package/lib/firebase/oauth/githubAccess.d.ts +6 -6
- package/lib/firebase/oauth/index.d.ts +1 -1
- package/lib/firebase/oauth/refreshToken.d.ts +1 -1
- package/lib/firebase/scheduled/index.d.ts +1 -1
- package/lib/shared/ai/classifyAIError.d.ts +35 -0
- package/lib/shared/ai/costTracker.d.ts +20 -0
- package/lib/shared/ai/createAIRoute.d.ts +20 -0
- package/lib/shared/ai/index.d.ts +6 -0
- package/lib/shared/ai/resolveModelTier.d.ts +14 -0
- package/lib/shared/billing/createCheckout.d.ts +11 -5
- package/lib/shared/billing/helpers/index.d.ts +1 -1
- package/lib/shared/billing/helpers/subscriptionManagement.d.ts +3 -3
- package/lib/shared/billing/helpers/updateUserSubscription.d.ts +4 -4
- package/lib/shared/billing/idempotency.d.ts +2 -2
- package/lib/shared/billing/index.d.ts +1 -1
- package/lib/shared/billing/webhookHandler.d.ts +1 -1
- package/lib/shared/email/createEmailRoute.d.ts +30 -0
- package/lib/shared/email/index.d.ts +3 -0
- package/lib/shared/email/provider.d.ts +30 -0
- package/lib/shared/email/resendProvider.d.ts +22 -0
- package/lib/shared/errorHandling.d.ts +1 -1
- package/lib/shared/firebase.d.ts +7 -7
- package/lib/shared/github.d.ts +16 -11
- package/lib/shared/index.d.ts +3 -1
- package/lib/shared/index.js +4 -3
- package/lib/shared/index.js.map +4 -4
- package/lib/shared/logger.d.ts +8 -6
- package/lib/shared/metadata.d.ts +3 -3
- package/lib/shared/oauth/exchangeToken.d.ts +3 -2
- package/lib/shared/oauth/grantAccess.d.ts +3 -2
- package/lib/shared/oauth/index.d.ts +1 -1
- package/lib/shared/schema.d.ts +2 -2
- package/lib/shared/utils/detectFirestore.d.ts +3 -3
- package/lib/shared/utils/external/date.d.ts +2 -2
- package/lib/shared/utils/external/index.d.ts +1 -1
- package/lib/shared/utils/external/references.d.ts +1 -1
- package/lib/shared/utils/external/subscription.d.ts +5 -5
- package/lib/shared/utils/firebaseHelpers.d.ts +2 -2
- package/lib/shared/utils/functionWrapper.d.ts +4 -4
- package/lib/shared/utils/index.d.ts +1 -1
- package/lib/shared/utils/internal/auth.d.ts +4 -4
- package/lib/shared/utils/internal/idempotency.d.ts +3 -2
- package/lib/shared/utils/internal/index.d.ts +1 -1
- package/lib/shared/utils/internal/monitoring.d.ts +7 -7
- package/lib/shared/utils/internal/rateLimiter.d.ts +5 -5
- package/lib/shared/utils/internal/validation.d.ts +4 -4
- package/lib/shared/utils/schemaValidation.d.ts +6 -6
- package/lib/shared/utils/validation.d.ts +9 -9
- package/lib/shared/utils/vercelHelpers.d.ts +2 -2
- package/lib/shared/utils.d.ts +19 -19
- package/lib/shared/validation.d.ts +2 -2
- package/lib/supabase/ai/createAIFunction.d.ts +30 -0
- package/lib/supabase/auth/deleteAccount.d.ts +1 -1
- package/lib/supabase/auth/getCustomClaims.d.ts +1 -1
- package/lib/supabase/auth/getUserAuthStatus.d.ts +1 -1
- package/lib/supabase/auth/removeCustomClaims.d.ts +1 -1
- package/lib/supabase/auth/setCustomClaims.d.ts +12 -4
- package/lib/supabase/baseFunction.d.ts +2 -2
- package/lib/supabase/billing/cancelSubscription.d.ts +1 -1
- package/lib/supabase/billing/changePlan.d.ts +2 -2
- package/lib/supabase/billing/createCheckoutSession.d.ts +1 -1
- package/lib/supabase/billing/createCustomerPortal.d.ts +1 -1
- package/lib/supabase/billing/refreshSubscriptionStatus.d.ts +1 -1
- package/lib/supabase/crud/aggregate.d.ts +2 -1
- package/lib/supabase/crud/create.d.ts +2 -1
- package/lib/supabase/crud/delete.d.ts +1 -0
- package/lib/supabase/crud/get.d.ts +2 -1
- package/lib/supabase/crud/index.d.ts +1 -1
- package/lib/supabase/crud/list.d.ts +2 -1
- package/lib/supabase/crud/update.d.ts +2 -1
- package/lib/supabase/edgeFunction.d.ts +56 -0
- package/lib/supabase/email/createEmailFunction.d.ts +31 -0
- package/lib/supabase/helpers/authProvider.d.ts +2 -2
- package/lib/supabase/index.d.ts +11 -2
- package/lib/supabase/index.js +3 -2
- package/lib/supabase/index.js.map +4 -4
- package/lib/supabase/oauth/disconnect.d.ts +13 -0
- package/lib/supabase/oauth/exchangeToken.d.ts +13 -0
- package/lib/supabase/oauth/getConnections.d.ts +13 -0
- package/lib/supabase/oauth/index.d.ts +12 -0
- package/lib/supabase/oauth/refreshToken.d.ts +13 -0
- package/lib/supabase/utils/idempotency.d.ts +1 -1
- package/lib/supabase/utils/monitoring.d.ts +1 -1
- package/lib/supabase/utils/rateLimiter.d.ts +1 -1
- package/lib/vercel/api/ai/chat.d.ts +29 -0
- package/lib/vercel/api/ai/index.d.ts +1 -0
- package/lib/vercel/api/auth/index.d.ts +1 -1
- package/lib/vercel/api/billing/cancel.d.ts +1 -1
- package/lib/vercel/api/billing/change-plan.d.ts +1 -1
- package/lib/vercel/api/billing/create-checkout-session.d.ts +1 -1
- package/lib/vercel/api/billing/index.d.ts +1 -1
- package/lib/vercel/api/billing/refresh-subscription-status.d.ts +1 -1
- package/lib/vercel/api/billing/webhook-handler.d.ts +2 -2
- package/lib/vercel/api/config/constants.d.ts +9 -9
- package/lib/vercel/api/config/firebase-admin.d.ts +2 -2
- package/lib/vercel/api/config/index.d.ts +1 -1
- package/lib/vercel/api/crud/index.d.ts +1 -1
- package/lib/vercel/api/index.d.ts +2 -1
- package/lib/vercel/api/index.js +3 -2
- package/lib/vercel/api/index.js.map +4 -4
- package/lib/vercel/api/oauth/check-github-access.d.ts +1 -1
- package/lib/vercel/api/oauth/grant-github-access.d.ts +1 -1
- package/lib/vercel/api/oauth/index.d.ts +1 -1
- package/lib/vercel/api/oauth/revoke-github-access.d.ts +1 -1
- package/lib/vercel/api/utils/cors.d.ts +4 -4
- package/lib/vercel/baseFunction.d.ts +2 -2
- package/package.json +38 -5
- package/src/firebase/auth/getCustomClaims.ts +3 -3
- package/src/firebase/auth/getUserAuthStatus.ts +3 -3
- package/src/firebase/auth/index.ts +1 -1
- package/src/firebase/auth/removeCustomClaims.ts +3 -3
- package/src/firebase/auth/setCustomClaims.ts +70 -69
- package/src/firebase/baseFunction.ts +2 -2
- package/src/firebase/billing/cancelSubscription.ts +3 -3
- package/src/firebase/billing/changePlan.ts +3 -3
- package/src/firebase/billing/createCheckoutSession.ts +79 -187
- package/src/firebase/billing/createCustomerPortal.ts +1 -1
- package/src/firebase/billing/healthCheck.ts +1 -1
- package/src/firebase/billing/index.d.ts +11 -11
- package/src/firebase/billing/index.ts +1 -1
- package/src/firebase/billing/refreshSubscriptionStatus.ts +3 -3
- package/src/firebase/billing/webhookHandler.ts +1 -1
- package/src/firebase/config/constants.ts +4 -4
- package/src/firebase/config/index.ts +1 -1
- package/src/firebase/config/secrets.ts +1 -1
- package/src/firebase/crud/aggregate.ts +2 -2
- package/src/firebase/crud/create.ts +2 -2
- package/src/firebase/crud/delete.ts +3 -3
- package/src/firebase/crud/get.ts +1 -1
- package/src/firebase/crud/index.ts +1 -1
- package/src/firebase/crud/list.ts +1 -1
- package/src/firebase/crud/update.ts +2 -2
- package/src/firebase/helpers/githubAccessHelper.ts +4 -4
- package/src/firebase/helpers/githubTeamAccessHelper.ts +3 -3
- package/src/firebase/helpers/index.ts +1 -1
- package/src/firebase/index.d.ts +9 -9
- package/src/firebase/index.ts +1 -1
- package/src/firebase/oauth/disconnect.ts +2 -2
- package/src/firebase/oauth/exchangeToken.ts +6 -6
- package/src/firebase/oauth/getConnections.ts +2 -2
- package/src/firebase/oauth/githubAccess.ts +10 -10
- package/src/firebase/oauth/index.ts +1 -1
- package/src/firebase/oauth/refreshToken.ts +6 -6
- package/src/firebase/registerCrudFunctions.ts +1 -1
- package/src/firebase/scheduled/checkExpiredSubscriptions.ts +1 -1
- package/src/firebase/scheduled/index.ts +1 -1
- package/src/shared/__tests__/auth.test.ts +46 -0
- package/src/shared/__tests__/errorHandling.test.ts +66 -1
- package/src/shared/__tests__/firebase.test.ts +293 -0
- package/src/shared/__tests__/internalValidation.test.ts +190 -0
- package/src/shared/__tests__/metadata.test.ts +66 -0
- package/src/shared/__tests__/schema.test.ts +219 -0
- package/src/shared/__tests__/schemaValidation.test.ts +72 -0
- package/src/shared/__tests__/subscription.test.ts +45 -0
- package/src/shared/__tests__/utils.test.ts +281 -0
- package/src/shared/__tests__/validation-env.test.ts +73 -0
- package/src/shared/ai/__tests__/costTracker.test.ts +121 -0
- package/src/shared/ai/classifyAIError.ts +163 -0
- package/src/shared/ai/costTracker.ts +51 -0
- package/src/shared/ai/createAIRoute.ts +397 -0
- package/src/shared/ai/index.ts +8 -0
- package/src/shared/ai/resolveModelTier.ts +58 -0
- package/src/shared/billing/__tests__/createCheckout.test.ts +1 -1
- package/src/shared/billing/__tests__/webhookHandler.test.ts +1 -1
- package/src/shared/billing/createCheckout.ts +156 -11
- package/src/shared/billing/helpers/index.ts +1 -1
- package/src/shared/billing/helpers/subscriptionManagement.ts +13 -4
- package/src/shared/billing/helpers/updateUserSubscription.ts +4 -4
- package/src/shared/billing/idempotency.ts +9 -4
- package/src/shared/billing/index.ts +1 -1
- package/src/shared/billing/webhookHandler.ts +7 -7
- package/src/shared/email/createEmailRoute.ts +81 -0
- package/src/shared/email/index.ts +5 -0
- package/src/shared/email/provider.ts +31 -0
- package/src/shared/email/resendProvider.ts +56 -0
- package/src/shared/errorHandling.ts +12 -6
- package/src/shared/firebase.ts +7 -7
- package/src/shared/github.ts +49 -11
- package/src/shared/index.d.ts +9 -9
- package/src/shared/index.ts +3 -1
- package/src/shared/logger.ts +10 -8
- package/src/shared/metadata.ts +3 -3
- package/src/shared/oauth/__tests__/exchangeToken.test.ts +1 -1
- package/src/shared/oauth/exchangeToken.ts +3 -2
- package/src/shared/oauth/grantAccess.ts +3 -2
- package/src/shared/oauth/index.ts +1 -1
- package/src/shared/schema.ts +4 -4
- package/src/shared/utils/__tests__/functionWrapper.test.ts +1 -1
- package/src/shared/utils/detectFirestore.ts +3 -3
- package/src/shared/utils/external/date.ts +5 -5
- package/src/shared/utils/external/index.ts +1 -1
- package/src/shared/utils/external/references.ts +2 -2
- package/src/shared/utils/external/subscription.ts +5 -5
- package/src/shared/utils/firebaseHelpers.ts +3 -3
- package/src/shared/utils/functionWrapper.ts +4 -4
- package/src/shared/utils/index.ts +1 -1
- package/src/shared/utils/internal/auth.ts +7 -7
- package/src/shared/utils/internal/firebase.ts +1 -1
- package/src/shared/utils/internal/idempotency.ts +4 -3
- package/src/shared/utils/internal/index.ts +1 -1
- package/src/shared/utils/internal/monitoring.ts +8 -8
- package/src/shared/utils/internal/rateLimiter.ts +12 -9
- package/src/shared/utils/internal/validation.ts +5 -5
- package/src/shared/utils/schemaValidation.ts +6 -6
- package/src/shared/utils/validation.ts +10 -10
- package/src/shared/utils/vercelHelpers.ts +3 -3
- package/src/shared/utils.ts +19 -19
- package/src/shared/validation.ts +3 -3
- package/src/supabase/ai/createAIFunction.ts +75 -0
- package/src/supabase/auth/deleteAccount.ts +2 -2
- package/src/supabase/auth/getCustomClaims.ts +2 -2
- package/src/supabase/auth/getUserAuthStatus.ts +2 -2
- package/src/supabase/auth/removeCustomClaims.ts +38 -2
- package/src/supabase/auth/setCustomClaims.ts +66 -5
- package/src/supabase/baseFunction.ts +29 -5
- package/src/supabase/billing/cancelSubscription.ts +2 -2
- package/src/supabase/billing/changePlan.ts +8 -3
- package/src/supabase/billing/createCheckoutSession.ts +18 -4
- package/src/supabase/billing/createCustomerPortal.ts +2 -2
- package/src/supabase/billing/refreshSubscriptionStatus.ts +2 -2
- package/src/supabase/crud/aggregate.ts +2 -1
- package/src/supabase/crud/create.ts +2 -1
- package/src/supabase/crud/delete.ts +2 -1
- package/src/supabase/crud/get.ts +2 -1
- package/src/supabase/crud/index.ts +1 -1
- package/src/supabase/crud/list.ts +2 -1
- package/src/supabase/crud/update.ts +2 -1
- package/src/supabase/edgeFunction.ts +195 -0
- package/src/supabase/email/createEmailFunction.ts +75 -0
- package/src/supabase/helpers/authProvider.ts +2 -2
- package/src/supabase/index.ts +22 -2
- package/src/supabase/oauth/disconnect.ts +104 -0
- package/src/supabase/oauth/exchangeToken.ts +183 -0
- package/src/supabase/oauth/getConnections.ts +85 -0
- package/src/supabase/oauth/index.ts +15 -0
- package/src/supabase/oauth/refreshToken.ts +130 -0
- package/src/supabase/registerCrudFunctions.ts +30 -42
- package/src/supabase/utils/idempotency.ts +7 -3
- package/src/supabase/utils/monitoring.ts +1 -1
- package/src/supabase/utils/rateLimiter.ts +1 -1
- package/src/vercel/api/ai/chat.ts +77 -0
- package/src/vercel/api/ai/index.ts +1 -0
- package/src/vercel/api/auth/get-custom-claims.ts +1 -1
- package/src/vercel/api/auth/get-user-auth-status.ts +1 -1
- package/src/vercel/api/auth/index.ts +1 -1
- package/src/vercel/api/auth/remove-custom-claims.ts +1 -1
- package/src/vercel/api/auth/set-custom-claims.ts +2 -4
- package/src/vercel/api/billing/cancel.ts +3 -3
- package/src/vercel/api/billing/change-plan.ts +3 -3
- package/src/vercel/api/billing/create-checkout-session.ts +30 -7
- package/src/vercel/api/billing/customer-portal.ts +1 -1
- package/src/vercel/api/billing/index.ts +1 -1
- package/src/vercel/api/billing/refresh-subscription-status.ts +1 -1
- package/src/vercel/api/billing/webhook-handler.ts +11 -4
- package/src/vercel/api/config/constants.ts +9 -9
- package/src/vercel/api/config/firebase-admin.ts +3 -3
- package/src/vercel/api/config/index.ts +1 -1
- package/src/vercel/api/crud/create.ts +1 -1
- package/src/vercel/api/crud/delete.ts +1 -1
- package/src/vercel/api/crud/get.ts +1 -1
- package/src/vercel/api/crud/index.ts +1 -1
- package/src/vercel/api/crud/list.ts +1 -1
- package/src/vercel/api/crud/update.ts +1 -1
- package/src/vercel/api/index.d.ts +1 -1
- package/src/vercel/api/index.ts +2 -1
- package/src/vercel/api/oauth/check-github-access.ts +3 -3
- package/src/vercel/api/oauth/exchange-token.ts +25 -2
- package/src/vercel/api/oauth/grant-github-access.ts +3 -3
- package/src/vercel/api/oauth/index.ts +1 -1
- package/src/vercel/api/oauth/revoke-github-access.ts +1 -1
- package/src/vercel/api/utils/cors.ts +10 -4
- package/src/vercel/baseFunction.ts +2 -2
|
@@ -2,33 +2,26 @@
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* @fileoverview Create checkout session Firebase function
|
|
5
|
-
* @description Firebase callable function for creating Stripe checkout sessions
|
|
5
|
+
* @description Firebase callable function for creating Stripe checkout sessions.
|
|
6
|
+
* Delegates to shared createCheckoutAlgorithm (SSOT).
|
|
6
7
|
*
|
|
7
|
-
* @version 0.0
|
|
8
|
+
* @version 0.2.0
|
|
8
9
|
* @since 0.0.1
|
|
9
10
|
* @author AMBROISE PARK Consulting
|
|
10
11
|
*/
|
|
11
12
|
|
|
12
|
-
import { logger } from 'firebase-functions/v2';
|
|
13
|
-
|
|
14
13
|
import type {
|
|
15
14
|
CreateCheckoutSessionRequest,
|
|
16
15
|
StripeBackConfig,
|
|
17
16
|
} from '@donotdev/core/server';
|
|
18
|
-
import { STRIPE_MODES } from '@donotdev/core/server';
|
|
19
17
|
import { CreateCheckoutSessionRequestSchema } from '@donotdev/core/server';
|
|
20
18
|
import { getFirebaseAdminAuth } from '@donotdev/firebase/server';
|
|
21
19
|
|
|
22
|
-
import {
|
|
23
|
-
import {
|
|
24
|
-
validateUrl,
|
|
25
|
-
validateMetadata,
|
|
26
|
-
} from '../../shared/utils/validation.js';
|
|
27
|
-
import { stripe, validateStripeEnvironment } from '../../shared/utils.js';
|
|
28
|
-
import { initStripe } from '../../shared/utils.js'; // ✅ IMPORT INIT
|
|
20
|
+
import { createCheckoutAlgorithm } from '../../shared/billing/createCheckout.js';
|
|
21
|
+
import { initStripe, stripe, validateStripeEnvironment } from '../../shared/utils.js';
|
|
29
22
|
import { createBaseFunction } from '../baseFunction.js';
|
|
30
23
|
import { STRIPE_CONFIG } from '../config/constants.js';
|
|
31
|
-
import { stripeSecretKey } from '../config/secrets.js';
|
|
24
|
+
import { stripeSecretKey } from '../config/secrets.js';
|
|
32
25
|
|
|
33
26
|
import type {
|
|
34
27
|
CallableFunction,
|
|
@@ -36,7 +29,8 @@ import type {
|
|
|
36
29
|
} from 'firebase-functions/v2/https';
|
|
37
30
|
|
|
38
31
|
/**
|
|
39
|
-
* Business logic for creating Stripe checkout session
|
|
32
|
+
* Business logic for creating Stripe checkout session.
|
|
33
|
+
* Uses shared algorithm - same as Supabase and Vercel handlers.
|
|
40
34
|
*/
|
|
41
35
|
async function createCheckoutSessionLogic(
|
|
42
36
|
data: CreateCheckoutSessionRequest,
|
|
@@ -46,200 +40,98 @@ async function createCheckoutSessionLogic(
|
|
|
46
40
|
},
|
|
47
41
|
billingConfig: StripeBackConfig
|
|
48
42
|
) {
|
|
49
|
-
//
|
|
43
|
+
// Initialize Stripe with Firebase secret
|
|
50
44
|
initStripe(stripeSecretKey.value());
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
metadata,
|
|
89
|
-
});
|
|
90
|
-
throw handleError(new Error('Missing billingConfigKey in metadata'));
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
const billingItem = billingConfig[configKey];
|
|
94
|
-
if (!billingItem) {
|
|
95
|
-
logger.error('[createCheckoutSession] Invalid billing config key', {
|
|
96
|
-
configKey,
|
|
97
|
-
availableKeys: Object.keys(billingConfig),
|
|
98
|
-
});
|
|
99
|
-
throw handleError(new Error(`Invalid billing config key: ${configKey}`));
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// ✅ VALIDATE: priceId matches config
|
|
103
|
-
if (billingItem.priceId !== priceId) {
|
|
104
|
-
logger.error('[createCheckoutSession] Price ID mismatch', {
|
|
105
|
-
requested: priceId,
|
|
106
|
-
expected: billingItem.priceId,
|
|
107
|
-
configKey,
|
|
108
|
-
});
|
|
109
|
-
throw handleError(new Error('Price ID mismatch with configuration'));
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// Verify user exists
|
|
113
|
-
let user;
|
|
114
|
-
try {
|
|
115
|
-
user = await getFirebaseAdminAuth().getUser(userId);
|
|
116
|
-
} catch (error) {
|
|
117
|
-
logger.error('[createCheckoutSession] Failed to get user', {
|
|
118
|
-
userId,
|
|
119
|
-
error: error instanceof Error ? error.message : String(error),
|
|
120
|
-
});
|
|
121
|
-
throw handleError(new Error(`User not found: ${userId}`));
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
// Validate URLs from request parameters (frontend responsibility)
|
|
125
|
-
try {
|
|
126
|
-
validateUrl(successUrl, 'success');
|
|
127
|
-
validateUrl(cancelUrl, 'cancel');
|
|
128
|
-
} catch (error) {
|
|
129
|
-
logger.error('[createCheckoutSession] URL validation failed', {
|
|
130
|
-
successUrl,
|
|
131
|
-
cancelUrl,
|
|
132
|
-
error: error instanceof Error ? error.message : String(error),
|
|
133
|
-
});
|
|
134
|
-
throw error;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
// Validate and sanitize metadata
|
|
138
|
-
let sanitizedMetadata: Record<string, string>;
|
|
139
|
-
try {
|
|
140
|
-
sanitizedMetadata = validateMetadata(metadata);
|
|
141
|
-
} catch (error) {
|
|
142
|
-
logger.error('[createCheckoutSession] Metadata validation failed', {
|
|
143
|
-
metadata,
|
|
144
|
-
error: error instanceof Error ? error.message : String(error),
|
|
145
|
-
});
|
|
146
|
-
throw error;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
// Create checkout session with Stripe
|
|
150
|
-
let session;
|
|
151
|
-
try {
|
|
152
|
-
session = await stripe.checkout.sessions.create({
|
|
153
|
-
mode,
|
|
154
|
-
line_items: [
|
|
155
|
-
{
|
|
156
|
-
price: priceId,
|
|
157
|
-
quantity: 1,
|
|
45
|
+
validateStripeEnvironment();
|
|
46
|
+
|
|
47
|
+
// Create Stripe provider
|
|
48
|
+
const stripeProvider = {
|
|
49
|
+
async createCheckoutSession(params: {
|
|
50
|
+
priceId?: string;
|
|
51
|
+
unitAmount?: number;
|
|
52
|
+
productName?: string;
|
|
53
|
+
currency?: string;
|
|
54
|
+
customerEmail?: string;
|
|
55
|
+
metadata: Record<string, string>;
|
|
56
|
+
allowPromotionCodes: boolean;
|
|
57
|
+
successUrl: string;
|
|
58
|
+
cancelUrl: string;
|
|
59
|
+
mode?: 'payment' | 'subscription';
|
|
60
|
+
}) {
|
|
61
|
+
const session = await stripe.checkout.sessions.create({
|
|
62
|
+
line_items: [
|
|
63
|
+
params.priceId
|
|
64
|
+
? { price: params.priceId, quantity: 1 }
|
|
65
|
+
: {
|
|
66
|
+
price_data: {
|
|
67
|
+
currency: params.currency || 'eur',
|
|
68
|
+
unit_amount: params.unitAmount!,
|
|
69
|
+
product_data: { name: params.productName! },
|
|
70
|
+
},
|
|
71
|
+
quantity: 1,
|
|
72
|
+
},
|
|
73
|
+
],
|
|
74
|
+
mode: params.mode || 'payment',
|
|
75
|
+
allow_promotion_codes: params.allowPromotionCodes,
|
|
76
|
+
success_url: params.successUrl,
|
|
77
|
+
cancel_url: params.cancelUrl,
|
|
78
|
+
customer_email: params.customerEmail,
|
|
79
|
+
metadata: {
|
|
80
|
+
...params.metadata,
|
|
81
|
+
firebaseUid: params.metadata.userId || '',
|
|
158
82
|
},
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
...sanitizedMetadata,
|
|
166
|
-
userId,
|
|
167
|
-
firebaseUid: userId,
|
|
168
|
-
billingConfigKey: configKey, // ✅ Server values AFTER spread — cannot be overwritten by client
|
|
169
|
-
},
|
|
170
|
-
// For subscriptions, automatically create customer in Stripe
|
|
171
|
-
...(mode === 'subscription' && {
|
|
172
|
-
subscription_data: {
|
|
173
|
-
metadata: {
|
|
174
|
-
...sanitizedMetadata,
|
|
175
|
-
userId,
|
|
176
|
-
firebaseUid: userId,
|
|
177
|
-
billingConfigKey: configKey, // ✅ Server values AFTER spread — cannot be overwritten by client
|
|
83
|
+
...(params.mode === 'subscription' && {
|
|
84
|
+
subscription_data: {
|
|
85
|
+
metadata: {
|
|
86
|
+
...params.metadata,
|
|
87
|
+
firebaseUid: params.metadata.userId || '',
|
|
88
|
+
},
|
|
178
89
|
},
|
|
179
|
-
},
|
|
180
|
-
})
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
priceId,
|
|
185
|
-
userId,
|
|
186
|
-
error: error instanceof Error ? error.message : String(error),
|
|
187
|
-
stripeError: (error as any).type || (error as any).code,
|
|
188
|
-
});
|
|
189
|
-
throw handleError(
|
|
190
|
-
new Error(
|
|
191
|
-
`Stripe checkout session creation failed: ${error instanceof Error ? error.message : String(error)}`
|
|
192
|
-
)
|
|
193
|
-
);
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
if (!session.url) {
|
|
197
|
-
logger.error('[createCheckoutSession] Session created but no URL', {
|
|
198
|
-
sessionId: session.id,
|
|
199
|
-
});
|
|
200
|
-
throw handleError(new Error('Failed to create checkout session URL'));
|
|
201
|
-
}
|
|
90
|
+
}),
|
|
91
|
+
});
|
|
92
|
+
return { id: session.id, url: session.url };
|
|
93
|
+
},
|
|
94
|
+
};
|
|
202
95
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
userId
|
|
206
|
-
|
|
207
|
-
|
|
96
|
+
// Create auth provider
|
|
97
|
+
const authProvider = {
|
|
98
|
+
async getUser(userId: string) {
|
|
99
|
+
const user = await getFirebaseAdminAuth().getUser(userId);
|
|
100
|
+
return { customClaims: user.customClaims };
|
|
101
|
+
},
|
|
102
|
+
};
|
|
208
103
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
104
|
+
// Inject userId from authenticated context (not from client)
|
|
105
|
+
const requestWithUserId = {
|
|
106
|
+
...data,
|
|
107
|
+
userId: context.uid,
|
|
212
108
|
};
|
|
109
|
+
|
|
110
|
+
// Delegate to shared algorithm (SSOT)
|
|
111
|
+
return createCheckoutAlgorithm(
|
|
112
|
+
requestWithUserId,
|
|
113
|
+
stripeProvider,
|
|
114
|
+
authProvider,
|
|
115
|
+
billingConfig
|
|
116
|
+
);
|
|
213
117
|
}
|
|
214
118
|
|
|
215
119
|
/**
|
|
216
120
|
* Create Firebase function for checkout session creation
|
|
217
|
-
*
|
|
218
|
-
* @param billingConfig - Billing configuration with product definitions
|
|
219
|
-
* @returns Firebase function for checkout session creation
|
|
220
|
-
*
|
|
221
|
-
* @example
|
|
222
|
-
* const createCheckout = createCheckoutSession(billingConfig);
|
|
223
|
-
* const result = await createCheckout({
|
|
224
|
-
* priceId: 'price_xxx',
|
|
225
|
-
* userId: 'user123',
|
|
226
|
-
* metadata: { billingConfigKey: 'early_bird' }
|
|
227
|
-
* });
|
|
228
|
-
* window.location.href = result.data.sessionUrl;
|
|
229
121
|
*/
|
|
230
122
|
export function createCheckoutSession(
|
|
231
123
|
billingConfig: StripeBackConfig
|
|
232
124
|
): CallableFunction<
|
|
233
125
|
CreateCheckoutSessionRequest,
|
|
234
|
-
Promise<{ sessionId: string; sessionUrl: string }>
|
|
126
|
+
Promise<{ sessionId: string; sessionUrl: string | null }>
|
|
235
127
|
> {
|
|
236
128
|
return createBaseFunction<
|
|
237
129
|
CreateCheckoutSessionRequest,
|
|
238
|
-
{ sessionId: string; sessionUrl: string }
|
|
130
|
+
{ sessionId: string; sessionUrl: string | null }
|
|
239
131
|
>(
|
|
240
132
|
STRIPE_CONFIG,
|
|
241
133
|
CreateCheckoutSessionRequestSchema,
|
|
242
134
|
'create_checkout_session',
|
|
243
|
-
(data, context) => createCheckoutSessionLogic(data, context, billingConfig)
|
|
135
|
+
(data, context) => createCheckoutSessionLogic(data, context, billingConfig)
|
|
244
136
|
);
|
|
245
137
|
}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @fileoverview Firebase Billing Functions
|
|
5
5
|
* @description Type definitions for Firebase Cloud Functions related to billing, payment processing, and subscription management.
|
|
6
6
|
*
|
|
7
|
-
* @version 0.0
|
|
7
|
+
* @version 0.1.0
|
|
8
8
|
* @since 0.0.1
|
|
9
9
|
* @author AMBROISE PARK Consulting
|
|
10
10
|
*/
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
/**
|
|
13
13
|
* Creates a Stripe checkout session
|
|
14
14
|
*
|
|
15
|
-
* @version 0.0
|
|
15
|
+
* @version 0.1.0
|
|
16
16
|
* @since 0.0.1
|
|
17
17
|
* @author AMBROISE PARK Consulting
|
|
18
18
|
*/
|
|
@@ -21,7 +21,7 @@ export { createCheckoutSession } from './createCheckoutSession.js';
|
|
|
21
21
|
/**
|
|
22
22
|
* Handles Stripe webhook events
|
|
23
23
|
*
|
|
24
|
-
* @version 0.0
|
|
24
|
+
* @version 0.1.0
|
|
25
25
|
* @since 0.0.1
|
|
26
26
|
* @author AMBROISE PARK Consulting
|
|
27
27
|
*/
|
|
@@ -30,7 +30,7 @@ export { createStripeWebhook } from './webhookHandler.js';
|
|
|
30
30
|
/**
|
|
31
31
|
* Billing helper functions (via shared)
|
|
32
32
|
*
|
|
33
|
-
* @version 0.0
|
|
33
|
+
* @version 0.1.0
|
|
34
34
|
* @since 0.0.1
|
|
35
35
|
* @author AMBROISE PARK Consulting
|
|
36
36
|
*/
|
|
@@ -39,7 +39,7 @@ export * from '../../shared/billing/helpers/index.js';
|
|
|
39
39
|
/**
|
|
40
40
|
* Idempotency utilities for advanced users
|
|
41
41
|
*
|
|
42
|
-
* @version 0.0
|
|
42
|
+
* @version 0.1.0
|
|
43
43
|
* @since 0.0.1
|
|
44
44
|
* @author AMBROISE PARK Consulting
|
|
45
45
|
*/
|
|
@@ -51,7 +51,7 @@ export {
|
|
|
51
51
|
/**
|
|
52
52
|
* Firestore detection utilities
|
|
53
53
|
*
|
|
54
|
-
* @version 0.0
|
|
54
|
+
* @version 0.1.0
|
|
55
55
|
* @since 0.0.1
|
|
56
56
|
* @author AMBROISE PARK Consulting
|
|
57
57
|
*/
|
|
@@ -63,7 +63,7 @@ export {
|
|
|
63
63
|
/**
|
|
64
64
|
* Billing health check function
|
|
65
65
|
*
|
|
66
|
-
* @version 0.0
|
|
66
|
+
* @version 0.1.0
|
|
67
67
|
* @since 0.0.1
|
|
68
68
|
* @author AMBROISE PARK Consulting
|
|
69
69
|
*/
|
|
@@ -72,7 +72,7 @@ export { billingHealth } from './healthCheck.js';
|
|
|
72
72
|
/**
|
|
73
73
|
* Payment processing functions
|
|
74
74
|
*
|
|
75
|
-
* @version 0.0
|
|
75
|
+
* @version 0.1.0
|
|
76
76
|
* @since 0.0.1
|
|
77
77
|
* @author AMBROISE PARK Consulting
|
|
78
78
|
*/
|
|
@@ -81,7 +81,7 @@ export * from './refreshSubscriptionStatus.js';
|
|
|
81
81
|
/**
|
|
82
82
|
* Cancels a subscription
|
|
83
83
|
*
|
|
84
|
-
* @version 0.0
|
|
84
|
+
* @version 0.1.0
|
|
85
85
|
* @since 0.0.1
|
|
86
86
|
* @author AMBROISE PARK Consulting
|
|
87
87
|
*/
|
|
@@ -90,7 +90,7 @@ export { cancelSubscription } from './cancelSubscription.js';
|
|
|
90
90
|
/**
|
|
91
91
|
* Changes a subscription plan
|
|
92
92
|
*
|
|
93
|
-
* @version 0.0
|
|
93
|
+
* @version 0.1.0
|
|
94
94
|
* @since 0.0.1
|
|
95
95
|
* @author AMBROISE PARK Consulting
|
|
96
96
|
*/
|
|
@@ -99,7 +99,7 @@ export { changePlan } from './changePlan.js';
|
|
|
99
99
|
/**
|
|
100
100
|
* Creates a Stripe customer portal session
|
|
101
101
|
*
|
|
102
|
-
* @version 0.0
|
|
102
|
+
* @version 0.1.0
|
|
103
103
|
* @since 0.0.1
|
|
104
104
|
* @author AMBROISE PARK Consulting
|
|
105
105
|
*/
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @fileoverview Refresh subscription status Firebase function
|
|
5
5
|
* @description Firebase callable function for refreshing user subscription status
|
|
6
6
|
*
|
|
7
|
-
* @version 0.0
|
|
7
|
+
* @version 0.1.0
|
|
8
8
|
* @since 0.0.1
|
|
9
9
|
* @author AMBROISE PARK Consulting
|
|
10
10
|
*/
|
|
@@ -32,7 +32,7 @@ const refreshSubscriptionStatusSchema = v.object({
|
|
|
32
32
|
* Business logic for refreshing subscription status
|
|
33
33
|
* Base function handles: validation, auth, rate limiting, monitoring
|
|
34
34
|
*
|
|
35
|
-
* @version 0.0
|
|
35
|
+
* @version 0.1.0
|
|
36
36
|
* @since 0.0.1
|
|
37
37
|
* @author AMBROISE PARK Consulting
|
|
38
38
|
*/
|
|
@@ -97,7 +97,7 @@ async function refreshSubscriptionStatusLogic(
|
|
|
97
97
|
* Refreshes user's subscription status from Stripe
|
|
98
98
|
* Base function handles all common concerns automatically
|
|
99
99
|
*
|
|
100
|
-
* @version 0.0
|
|
100
|
+
* @version 0.1.0
|
|
101
101
|
* @since 0.0.1
|
|
102
102
|
* @author AMBROISE PARK Consulting
|
|
103
103
|
*/
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @fileoverview Firebase Functions configuration constants
|
|
5
5
|
* @description DRY configuration for regions, timeouts, and other function settings
|
|
6
6
|
*
|
|
7
|
-
* @version 0.0
|
|
7
|
+
* @version 0.1.0
|
|
8
8
|
* @since 0.0.1
|
|
9
9
|
* @author AMBROISE PARK Consulting
|
|
10
10
|
*/
|
|
@@ -66,7 +66,7 @@ export const CRUD_READ_CONFIG = {
|
|
|
66
66
|
/**
|
|
67
67
|
* Available Firebase regions for reference
|
|
68
68
|
*
|
|
69
|
-
* @version 0.0
|
|
69
|
+
* @version 0.1.0
|
|
70
70
|
* @since 0.0.1
|
|
71
71
|
* @author AMBROISE PARK Consulting
|
|
72
72
|
*/
|
|
@@ -93,7 +93,7 @@ export const AVAILABLE_REGIONS = {
|
|
|
93
93
|
/**
|
|
94
94
|
* Environment validation
|
|
95
95
|
*
|
|
96
|
-
* @version 0.0
|
|
96
|
+
* @version 0.1.0
|
|
97
97
|
* @since 0.0.1
|
|
98
98
|
* @author AMBROISE PARK Consulting
|
|
99
99
|
*/
|
|
@@ -115,7 +115,7 @@ export const validateEnvironment = () => {
|
|
|
115
115
|
/**
|
|
116
116
|
* Type for Firebase region keys
|
|
117
117
|
*
|
|
118
|
-
* @version 0.0
|
|
118
|
+
* @version 0.1.0
|
|
119
119
|
* @since 0.0.1
|
|
120
120
|
* @author AMBROISE PARK Consulting
|
|
121
121
|
*/
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @description Provides a reusable implementation for computing aggregations on Firestore collections.
|
|
6
6
|
* Returns only computed metrics, never raw data - optimized for analytics dashboards.
|
|
7
7
|
*
|
|
8
|
-
* @version 0.0
|
|
8
|
+
* @version 0.1.0
|
|
9
9
|
* @since 0.0.1
|
|
10
10
|
* @author AMBROISE PARK Consulting
|
|
11
11
|
*/
|
|
@@ -385,7 +385,7 @@ function aggregateEntitiesLogicFactory(
|
|
|
385
385
|
* // }
|
|
386
386
|
* ```
|
|
387
387
|
*
|
|
388
|
-
* @version 0.0
|
|
388
|
+
* @version 0.1.0
|
|
389
389
|
* @since 0.0.1
|
|
390
390
|
* @author AMBROISE PARK Consulting
|
|
391
391
|
*/
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @fileoverview Generic function to create an entity.
|
|
5
5
|
* @description Provides a reusable implementation for creating documents in Firestore with validation.
|
|
6
6
|
*
|
|
7
|
-
* @version 0.0
|
|
7
|
+
* @version 0.1.0
|
|
8
8
|
* @since 0.0.1
|
|
9
9
|
* @author AMBROISE PARK Consulting
|
|
10
10
|
*/
|
|
@@ -144,7 +144,7 @@ async function checkUniqueKeys(
|
|
|
144
144
|
* Generic business logic for creating entities
|
|
145
145
|
* Base function handles: validation, auth, rate limiting, monitoring
|
|
146
146
|
*
|
|
147
|
-
* @version 0.0
|
|
147
|
+
* @version 0.1.0
|
|
148
148
|
* @since 0.0.1
|
|
149
149
|
* @author AMBROISE PARK Consulting
|
|
150
150
|
*/
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @fileoverview Generic function to delete an entity.
|
|
5
5
|
* @description Provides a reusable implementation for deleting documents in Firestore with reference checking.
|
|
6
6
|
*
|
|
7
|
-
* @version 0.0
|
|
7
|
+
* @version 0.1.0
|
|
8
8
|
* @since 0.0.1
|
|
9
9
|
* @author AMBROISE PARK Consulting
|
|
10
10
|
*/
|
|
@@ -30,7 +30,7 @@ export type DeleteEntityRequest = { id: string };
|
|
|
30
30
|
* Generic business logic for deleting entities
|
|
31
31
|
* Base function handles: validation, auth, rate limiting, monitoring
|
|
32
32
|
*
|
|
33
|
-
* @version 0.0
|
|
33
|
+
* @version 0.1.0
|
|
34
34
|
* @since 0.0.1
|
|
35
35
|
* @author AMBROISE PARK Consulting
|
|
36
36
|
*/
|
|
@@ -68,7 +68,7 @@ function deleteEntityLogicFactory(collection: string) {
|
|
|
68
68
|
* @param customSchema - Optional custom request schema
|
|
69
69
|
* @returns Firebase callable function
|
|
70
70
|
*
|
|
71
|
-
* @version 0.0
|
|
71
|
+
* @version 0.1.0
|
|
72
72
|
* @since 0.0.1
|
|
73
73
|
* @author AMBROISE PARK Consulting
|
|
74
74
|
*/
|
package/src/firebase/crud/get.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @fileoverview Generic function to retrieve a single entity.
|
|
5
5
|
* @description Provides a reusable implementation for retrieving documents from Firestore with visibility filtering.
|
|
6
6
|
*
|
|
7
|
-
* @version 0.0
|
|
7
|
+
* @version 0.1.0
|
|
8
8
|
* @since 0.0.1
|
|
9
9
|
* @author AMBROISE PARK Consulting
|
|
10
10
|
*/
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @fileoverview Generic function to list entities with pagination, filtering, and sorting.
|
|
5
5
|
* @description Provides a reusable implementation for listing documents from Firestore with advanced query capabilities.
|
|
6
6
|
*
|
|
7
|
-
* @version 0.0
|
|
7
|
+
* @version 0.1.0
|
|
8
8
|
* @since 0.0.1
|
|
9
9
|
* @author AMBROISE PARK Consulting
|
|
10
10
|
*/
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @fileoverview Generic function to update an entity.
|
|
5
5
|
* @description Provides a reusable implementation for updating documents in Firestore with validation.
|
|
6
6
|
*
|
|
7
|
-
* @version 0.0
|
|
7
|
+
* @version 0.1.0
|
|
8
8
|
* @since 0.0.1
|
|
9
9
|
* @author AMBROISE PARK Consulting
|
|
10
10
|
*/
|
|
@@ -136,7 +136,7 @@ async function checkUniqueKeysForUpdate(
|
|
|
136
136
|
* Generic business logic for updating entities
|
|
137
137
|
* Base function handles: validation, auth, rate limiting, monitoring
|
|
138
138
|
*
|
|
139
|
-
* @version 0.0
|
|
139
|
+
* @version 0.1.0
|
|
140
140
|
* @since 0.0.1
|
|
141
141
|
* @author AMBROISE PARK Consulting
|
|
142
142
|
*/
|