@donotdev/functions 0.1.44 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +25 -14
- package/lib/chunks/chunk-2JL3GDTO.js +151 -0
- package/lib/chunks/chunk-2JL3GDTO.js.map +7 -0
- package/lib/chunks/chunk-4TVLH2W2.js +22 -0
- package/lib/chunks/chunk-4TVLH2W2.js.map +7 -0
- package/lib/chunks/chunk-4U7QQWYG.js +22 -0
- package/lib/chunks/chunk-4U7QQWYG.js.map +7 -0
- package/lib/chunks/chunk-5DTPEM64.js +22 -0
- package/lib/chunks/chunk-5DTPEM64.js.map +7 -0
- package/lib/chunks/chunk-6CQXHNVL.js +22 -0
- package/lib/chunks/chunk-6CQXHNVL.js.map +7 -0
- package/lib/chunks/chunk-6DSCPZC6.js +22 -0
- package/lib/chunks/chunk-6DSCPZC6.js.map +7 -0
- package/lib/chunks/chunk-7E6TGWHU.js +22 -0
- package/lib/chunks/chunk-7E6TGWHU.js.map +7 -0
- package/lib/chunks/chunk-7LT32OWE.js +22 -0
- package/lib/chunks/chunk-7LT32OWE.js.map +7 -0
- package/lib/chunks/chunk-7VH2GJXT.js +151 -0
- package/lib/chunks/chunk-7VH2GJXT.js.map +7 -0
- package/lib/chunks/chunk-API5A4BV.js +22 -0
- package/lib/chunks/chunk-API5A4BV.js.map +7 -0
- package/lib/chunks/chunk-AYY7UXNI.js +22 -0
- package/lib/chunks/chunk-AYY7UXNI.js.map +7 -0
- package/lib/chunks/chunk-BUP7SXPV.js +23 -0
- package/lib/chunks/chunk-BUP7SXPV.js.map +7 -0
- package/lib/chunks/chunk-BXKRI3JJ.js +22 -0
- package/lib/chunks/chunk-BXKRI3JJ.js.map +7 -0
- package/lib/chunks/chunk-FSLGZD7A.js +21 -0
- package/lib/chunks/chunk-FSLGZD7A.js.map +7 -0
- package/lib/chunks/chunk-GVLSPWDD.js +151 -0
- package/lib/chunks/chunk-GVLSPWDD.js.map +7 -0
- package/lib/chunks/chunk-GXQVWEFT.js +22 -0
- package/lib/chunks/chunk-GXQVWEFT.js.map +7 -0
- package/lib/chunks/chunk-HB7EGXVI.js +22 -0
- package/lib/chunks/chunk-HB7EGXVI.js.map +7 -0
- package/lib/chunks/chunk-HLNYUAZH.js +22 -0
- package/lib/chunks/chunk-HLNYUAZH.js.map +7 -0
- package/lib/chunks/chunk-HM7XCMWW.js +22 -0
- package/lib/chunks/chunk-HM7XCMWW.js.map +7 -0
- package/lib/chunks/chunk-I7IOIL6U.js +151 -0
- package/lib/chunks/chunk-I7IOIL6U.js.map +7 -0
- package/lib/chunks/chunk-IFOYVSPU.js +22 -0
- package/lib/chunks/chunk-IFOYVSPU.js.map +7 -0
- package/lib/chunks/chunk-JTAGPPPL.js +22 -0
- package/lib/chunks/chunk-JTAGPPPL.js.map +7 -0
- package/lib/chunks/chunk-MZ232FUE.js +22 -0
- package/lib/chunks/chunk-MZ232FUE.js.map +7 -0
- package/lib/chunks/chunk-NE5ZTUUZ.js +22 -0
- package/lib/chunks/chunk-NE5ZTUUZ.js.map +7 -0
- package/lib/chunks/chunk-NP2AN2TV.js +22 -0
- package/lib/chunks/chunk-NP2AN2TV.js.map +7 -0
- package/lib/chunks/chunk-QJAU2MWV.js +22 -0
- package/lib/chunks/chunk-QJAU2MWV.js.map +7 -0
- package/lib/chunks/chunk-RM7DV6IQ.js +23 -0
- package/lib/chunks/chunk-RM7DV6IQ.js.map +7 -0
- package/lib/chunks/chunk-RQZGIZHD.js +151 -0
- package/lib/chunks/chunk-RQZGIZHD.js.map +7 -0
- package/lib/chunks/chunk-TBEDQLOB.js +23 -0
- package/lib/chunks/chunk-TBEDQLOB.js.map +7 -0
- package/lib/chunks/chunk-TMHBP3HB.js +22 -0
- package/lib/chunks/chunk-TMHBP3HB.js.map +7 -0
- package/lib/chunks/chunk-UH4L3EF3.js +151 -0
- package/lib/chunks/chunk-UH4L3EF3.js.map +7 -0
- package/lib/chunks/chunk-UJBIU6TL.js +22 -0
- package/lib/chunks/chunk-UJBIU6TL.js.map +7 -0
- package/lib/chunks/chunk-VKEYW3NT.js +22 -0
- package/lib/chunks/chunk-VKEYW3NT.js.map +7 -0
- package/lib/chunks/chunk-XGXBMXJ6.js +22 -0
- package/lib/chunks/chunk-XGXBMXJ6.js.map +7 -0
- package/lib/chunks/chunk-Y44NEQQG.js +22 -0
- package/lib/chunks/chunk-Y44NEQQG.js.map +7 -0
- package/lib/chunks/chunk-ZVU3KPXI.js +22 -0
- package/lib/chunks/chunk-ZVU3KPXI.js.map +7 -0
- package/lib/chunks/monitoring-AGCSU3FI.js +22 -0
- package/lib/chunks/monitoring-AGCSU3FI.js.map +7 -0
- package/lib/chunks/monitoring-PVQMFL34.js +22 -0
- package/lib/chunks/monitoring-PVQMFL34.js.map +7 -0
- package/lib/chunks/monitoring-V7FZXTTW.js +22 -0
- package/lib/chunks/monitoring-V7FZXTTW.js.map +7 -0
- package/lib/chunks/rateLimiter-6KO7AIZP.js +22 -0
- package/lib/chunks/rateLimiter-6KO7AIZP.js.map +7 -0
- package/lib/chunks/rateLimiter-OXVBFW5C.js +22 -0
- package/lib/chunks/rateLimiter-OXVBFW5C.js.map +7 -0
- package/lib/chunks/rateLimiter-U45OMTWR.js +22 -0
- package/lib/chunks/rateLimiter-U45OMTWR.js.map +7 -0
- package/lib/chunks/server-32A5RH4C.js +22 -0
- package/lib/chunks/server-32A5RH4C.js.map +7 -0
- package/lib/chunks/server-7HDAP5DL.js +22 -0
- package/lib/chunks/server-7HDAP5DL.js.map +7 -0
- package/lib/chunks/server-S2IMFOY7.js +22 -0
- package/lib/chunks/server-S2IMFOY7.js.map +7 -0
- package/lib/firebase/auth/getCustomClaims.d.ts +1 -1
- package/lib/firebase/auth/getUserAuthStatus.d.ts +1 -1
- package/lib/firebase/auth/removeCustomClaims.d.ts +1 -1
- package/lib/firebase/auth/setCustomClaims.d.ts +1 -1
- package/lib/firebase/baseFunction.d.ts +1 -1
- package/lib/firebase/billing/createCheckoutSession.d.ts +2 -2
- package/lib/firebase/billing/createCustomerPortal.d.ts +1 -1
- package/lib/firebase/billing/index.d.ts +2 -1
- package/lib/firebase/billing/refreshSubscriptionStatus.d.ts +0 -8
- package/lib/firebase/billing/webhookHandler.d.ts +17 -2
- package/lib/firebase/crud/aggregate.d.ts +0 -9
- package/lib/firebase/crud/bulk.d.ts +2 -2
- package/lib/firebase/crud/create.d.ts +1 -1
- package/lib/firebase/crud/delete.d.ts +1 -1
- package/lib/firebase/crud/get.d.ts +1 -1
- package/lib/firebase/crud/list.d.ts +1 -1
- package/lib/firebase/crud/update.d.ts +1 -9
- package/lib/firebase/index.js +1 -5
- package/lib/firebase/index.js.map +4 -4
- package/lib/firebase/oauth/githubAccess.d.ts +2 -2
- package/lib/firebase/registerCrudFunctions.d.ts +9 -1
- package/lib/shared/billing/apiVersion.d.ts +22 -0
- package/lib/shared/billing/checkoutPrice.d.ts +27 -0
- package/lib/shared/billing/checkoutValidation.d.ts +89 -0
- package/lib/shared/billing/createCheckout.d.ts +73 -24
- package/lib/shared/billing/helpers/index.d.ts +0 -1
- package/lib/shared/billing/helpers/subscriptionManagement.d.ts +4 -4
- package/lib/shared/billing/helpers/updateUserSubscription.d.ts +2 -29
- package/lib/shared/billing/idempotency.d.ts +32 -10
- package/lib/shared/billing/idempotencyContract.d.ts +121 -0
- package/lib/shared/billing/index.d.ts +2 -0
- package/lib/shared/billing/orderWebhook.d.ts +75 -0
- package/lib/shared/billing/supabaseIdempotency.d.ts +32 -0
- package/lib/shared/billing/tierWebhookEvents.d.ts +54 -0
- package/lib/shared/billing/webhookHandler.d.ts +28 -20
- package/lib/shared/index.js +1 -6
- package/lib/shared/index.js.map +4 -4
- package/lib/shared/utils/functionWrapper.d.ts +1 -1
- package/lib/shared/utils/internal/auth.d.ts +38 -1
- package/lib/shared/utils/internal/rateLimiter.d.ts +8 -0
- package/lib/shared/utils/schemaValidation.d.ts +1 -1
- package/lib/shared/utils/vercelHelpers.d.ts +8 -0
- package/lib/shared/utils.d.ts +4 -17
- package/lib/supabase/baseFunction.d.ts +1 -1
- package/lib/supabase/billing/stripeWebhook.d.ts +34 -0
- package/lib/supabase/crud/bulk.d.ts +1 -1
- package/lib/supabase/crud/entityStore.d.ts +71 -0
- package/lib/supabase/crud/index.d.ts +2 -0
- package/lib/supabase/helpers/authProvider.d.ts +5 -5
- package/lib/supabase/index.d.ts +10 -2
- package/lib/supabase/index.js +1 -3
- package/lib/supabase/index.js.map +4 -4
- package/lib/supabase/utils/rateLimiter.d.ts +1 -1
- package/lib/vercel/api/ai/chat.d.ts +0 -9
- package/lib/vercel/api/auth/get-custom-claims.d.ts +8 -0
- package/lib/vercel/api/auth/get-user-auth-status.d.ts +8 -0
- package/lib/vercel/api/auth/remove-custom-claims.d.ts +8 -0
- package/lib/vercel/api/auth/set-custom-claims.d.ts +8 -0
- package/lib/vercel/api/billing/backend.d.ts +39 -0
- package/lib/vercel/api/billing/cancel.d.ts +8 -0
- package/lib/vercel/api/billing/change-plan.d.ts +9 -1
- package/lib/vercel/api/billing/create-checkout-session.d.ts +25 -6
- package/lib/vercel/api/billing/customer-portal.d.ts +8 -0
- package/lib/vercel/api/billing/index.d.ts +4 -2
- package/lib/vercel/api/billing/refresh-subscription-status.d.ts +19 -3
- package/lib/vercel/api/billing/webhook-handler.d.ts +25 -4
- package/lib/vercel/api/config/firebase-admin.d.ts +8 -0
- package/lib/vercel/api/crud/bulk.d.ts +17 -0
- package/lib/vercel/api/crud/create.d.ts +8 -0
- package/lib/vercel/api/crud/delete.d.ts +8 -0
- package/lib/vercel/api/crud/get.d.ts +8 -0
- package/lib/vercel/api/crud/list.d.ts +8 -0
- package/lib/vercel/api/crud/update.d.ts +8 -0
- package/lib/vercel/api/index.js +1 -3
- package/lib/vercel/api/index.js.map +4 -4
- package/lib/vercel/api/oauth/check-github-access.d.ts +8 -0
- package/lib/vercel/api/oauth/exchange-token.d.ts +8 -0
- package/lib/vercel/api/oauth/grant-github-access.d.ts +8 -0
- package/lib/vercel/api/oauth/revoke-github-access.d.ts +1 -1
- package/lib/vercel/baseFunction.d.ts +4 -1
- package/lib/vercel/supabaseAdmin.d.ts +22 -0
- package/package.json +25 -26
- package/src/firebase/auth/getCustomClaims.ts +4 -5
- package/src/firebase/auth/getUserAuthStatus.ts +4 -5
- package/src/firebase/auth/removeCustomClaims.ts +4 -5
- package/src/firebase/auth/setCustomClaims.ts +4 -5
- package/src/firebase/baseFunction.ts +4 -5
- package/src/firebase/billing/cancelSubscription.ts +1 -2
- package/src/firebase/billing/changePlan.ts +2 -3
- package/src/firebase/billing/createCheckoutSession.ts +30 -56
- package/src/firebase/billing/createCustomerPortal.ts +4 -5
- package/src/firebase/billing/healthCheck.ts +0 -0
- package/src/firebase/billing/index.ts +8 -1
- package/src/firebase/billing/refreshSubscriptionStatus.ts +2 -3
- package/src/firebase/billing/webhookHandler.ts +20 -4
- package/src/firebase/crud/__tests__/bulk.test.ts +11 -1
- package/src/firebase/crud/aggregate.ts +1 -2
- package/src/firebase/crud/bulk.ts +5 -6
- package/src/firebase/crud/create.ts +5 -6
- package/src/firebase/crud/delete.ts +4 -5
- package/src/firebase/crud/get.ts +4 -5
- package/src/firebase/crud/list.ts +4 -5
- package/src/firebase/crud/update.ts +5 -6
- package/src/firebase/oauth/githubAccess.ts +2 -3
- package/src/firebase/registerCrudFunctions.ts +2 -2
- package/src/shared/__tests__/auth.test.ts +0 -3
- package/src/shared/__tests__/bulkCore.test.ts +3 -2
- package/src/shared/__tests__/errorHandling.test.ts +2 -2
- package/src/shared/__tests__/idempotency.test.ts +282 -55
- package/src/shared/__tests__/internalValidation.test.ts +3 -3
- package/src/shared/__tests__/rateLimiter.test.ts +2 -8
- package/src/shared/__tests__/schemaValidation.test.ts +1 -1
- package/src/shared/__tests__/utils.test.ts +2 -1
- package/src/shared/__tests__/validation-env.test.ts +0 -0
- package/src/shared/ai/createAIRoute.ts +53 -30
- package/src/shared/billing/__tests__/checkoutValidation.test.ts +219 -0
- package/src/shared/billing/__tests__/createCheckout.compat.test.ts +349 -0
- package/src/shared/billing/__tests__/createCheckout.test.ts +454 -411
- package/src/shared/billing/__tests__/firestoreIdempotency.emulator.test.ts +92 -0
- package/src/shared/billing/__tests__/webhookCorrectness.test.ts +387 -0
- package/src/shared/billing/__tests__/webhookHandler.test.ts +407 -23
- package/src/shared/billing/apiVersion.ts +25 -0
- package/src/shared/billing/checkoutPrice.ts +125 -0
- package/src/shared/billing/checkoutValidation.ts +284 -0
- package/src/shared/billing/createCheckout.ts +332 -196
- package/src/shared/billing/helpers/index.ts +0 -1
- package/src/shared/billing/helpers/subscriptionManagement.ts +7 -9
- package/src/shared/billing/helpers/updateUserSubscription.ts +3 -36
- package/src/shared/billing/idempotency.ts +253 -150
- package/src/shared/billing/idempotencyContract.ts +135 -0
- package/src/shared/billing/index.ts +2 -0
- package/src/shared/billing/orderWebhook.ts +269 -0
- package/src/shared/billing/supabaseIdempotency.ts +228 -0
- package/src/shared/billing/tierWebhookEvents.ts +432 -0
- package/src/shared/billing/webhookHandler.ts +188 -385
- package/src/shared/utils/external/subscription.ts +0 -0
- package/src/shared/utils/firebaseHelpers.ts +2 -3
- package/src/shared/utils/functionWrapper.ts +1 -2
- package/src/shared/utils/internal/auth.ts +55 -7
- package/src/shared/utils/internal/monitoring.ts +17 -9
- package/src/shared/utils/internal/rateLimiter.ts +14 -5
- package/src/shared/utils/schemaValidation.ts +1 -2
- package/src/shared/utils/vercelHelpers.ts +3 -3
- package/src/shared/utils.ts +14 -31
- package/src/shared/validation.ts +1 -1
- package/src/supabase/__tests__/bulk.test.ts +3 -1
- package/src/supabase/__tests__/entityStore.test.ts +90 -0
- package/src/supabase/baseFunction.ts +1 -2
- package/src/supabase/billing/createCheckoutSession.ts +10 -36
- package/src/supabase/billing/refreshSubscriptionStatus.ts +1 -2
- package/src/supabase/billing/stripeWebhook.ts +79 -0
- package/src/supabase/crud/aggregate.ts +1 -1
- package/src/supabase/crud/bulk.ts +3 -7
- package/src/supabase/crud/create.ts +1 -1
- package/src/supabase/crud/entityStore.ts +240 -0
- package/src/supabase/crud/index.ts +8 -0
- package/src/supabase/crud/update.ts +1 -1
- package/src/supabase/helpers/authProvider.ts +6 -5
- package/src/supabase/index.ts +26 -0
- package/src/supabase/utils/rateLimiter.ts +2 -2
- package/src/vercel/__tests__/bulk.test.ts +24 -21
- package/src/vercel/__tests__/firebaseOptional.test.ts +64 -0
- package/src/vercel/api/ai/chat.ts +2 -1
- package/src/vercel/api/auth/get-custom-claims.ts +2 -3
- package/src/vercel/api/auth/get-user-auth-status.ts +2 -3
- package/src/vercel/api/auth/remove-custom-claims.ts +2 -3
- package/src/vercel/api/auth/set-custom-claims.ts +3 -3
- package/src/vercel/api/billing/backend.ts +72 -0
- package/src/vercel/api/billing/cancel.ts +2 -4
- package/src/vercel/api/billing/change-plan.ts +3 -4
- package/src/vercel/api/billing/create-checkout-session.ts +42 -68
- package/src/vercel/api/billing/customer-portal.ts +2 -4
- package/src/vercel/api/billing/index.ts +4 -2
- package/src/vercel/api/billing/refresh-subscription-status.ts +21 -6
- package/src/vercel/api/billing/webhook-handler.ts +31 -25
- package/src/vercel/api/config/firebase-admin.ts +2 -2
- package/src/vercel/api/crud/bulk.ts +6 -9
- package/src/vercel/api/crud/create.ts +6 -5
- package/src/vercel/api/crud/delete.ts +4 -3
- package/src/vercel/api/crud/get.ts +6 -5
- package/src/vercel/api/crud/list.ts +6 -5
- package/src/vercel/api/crud/update.ts +6 -5
- package/src/vercel/api/oauth/check-github-access.ts +5 -5
- package/src/vercel/api/oauth/exchange-token.ts +3 -3
- package/src/vercel/api/oauth/grant-github-access.ts +5 -5
- package/src/vercel/api/oauth/revoke-github-access.ts +3 -4
- package/src/vercel/baseFunction.ts +23 -8
- package/src/vercel/supabaseAdmin.ts +45 -0
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
// packages/functions/src/shared/billing/checkoutValidation.ts
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @fileoverview Checkout validation helpers
|
|
5
|
+
* @description Fail-loud checks run by `createCheckoutAlgorithm` before calling
|
|
6
|
+
* Stripe: redirect URLs, client metadata, Stripe metadata limits, resolved
|
|
7
|
+
* StripeOrder lines and manual Tax Rates.
|
|
8
|
+
*
|
|
9
|
+
* @version 0.2.0
|
|
10
|
+
* @since 0.2.0
|
|
11
|
+
* @author AMBROISE PARK Consulting
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import type Stripe from 'stripe';
|
|
15
|
+
|
|
16
|
+
import type { ResolvedOrder, ResolvedOrderSession } from '@donotdev/core/server';
|
|
17
|
+
|
|
18
|
+
/** Stripe `client_reference_id` maximum length (Checkout Session create API). */
|
|
19
|
+
const MAX_CLIENT_REFERENCE_ID_LENGTH = 200;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Stripe metadata limits (https://docs.stripe.com/metadata): 50 keys, keys up
|
|
23
|
+
* to 40 characters without square brackets, values up to 500 characters.
|
|
24
|
+
*
|
|
25
|
+
* @version 0.2.0
|
|
26
|
+
* @since 0.2.0
|
|
27
|
+
* @author AMBROISE PARK Consulting
|
|
28
|
+
*/
|
|
29
|
+
export const STRIPE_METADATA_LIMITS = {
|
|
30
|
+
maxKeys: 50,
|
|
31
|
+
maxKeyLength: 40,
|
|
32
|
+
maxValueLength: 500,
|
|
33
|
+
} as const;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Validate URL has http/https protocol.
|
|
37
|
+
* Platform-agnostic (no firebase logger dependency).
|
|
38
|
+
*/
|
|
39
|
+
export function validateCheckoutUrl(url: string, name: string): void {
|
|
40
|
+
try {
|
|
41
|
+
const parsed = new URL(url);
|
|
42
|
+
if (!['http:', 'https:'].includes(parsed.protocol)) {
|
|
43
|
+
throw new Error(`Invalid protocol: ${parsed.protocol}`);
|
|
44
|
+
}
|
|
45
|
+
} catch (error) {
|
|
46
|
+
throw new Error(
|
|
47
|
+
`Invalid ${name} URL: ${url}. ${error instanceof Error ? error.message : 'Invalid URL format'}`
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Sanitize client metadata: enforce string values, strip script injection.
|
|
54
|
+
* Length limits are checked on the final metadata ({@link assertStripeMetadata}).
|
|
55
|
+
*/
|
|
56
|
+
export function sanitizeMetadata(
|
|
57
|
+
metadata: Record<string, any>
|
|
58
|
+
): Record<string, string> {
|
|
59
|
+
const sanitized: Record<string, string> = {};
|
|
60
|
+
|
|
61
|
+
for (const [key, value] of Object.entries(metadata)) {
|
|
62
|
+
if (typeof value !== 'string') {
|
|
63
|
+
throw new Error(
|
|
64
|
+
`Metadata value for key '${key}' must be a string, got ${typeof value}`
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
sanitized[key] = value
|
|
69
|
+
.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '')
|
|
70
|
+
.replace(/javascript:/gi, '')
|
|
71
|
+
.replace(/on\w+\s*=/gi, '');
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return sanitized;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Throw a validation error when metadata exceeds Stripe's limits
|
|
79
|
+
* ({@link STRIPE_METADATA_LIMITS}), instead of letting Stripe answer 400.
|
|
80
|
+
*
|
|
81
|
+
* @param metadata - Metadata as it will be sent to Stripe
|
|
82
|
+
* @param target - Stripe param the metadata goes to (shown in the error)
|
|
83
|
+
*
|
|
84
|
+
* @version 0.2.0
|
|
85
|
+
* @since 0.2.0
|
|
86
|
+
* @author AMBROISE PARK Consulting
|
|
87
|
+
*/
|
|
88
|
+
export function assertStripeMetadata(
|
|
89
|
+
metadata: Record<string, string | number | null>,
|
|
90
|
+
target: string
|
|
91
|
+
): void {
|
|
92
|
+
const { maxKeys, maxKeyLength, maxValueLength } = STRIPE_METADATA_LIMITS;
|
|
93
|
+
const entries = Object.entries(metadata);
|
|
94
|
+
if (entries.length > maxKeys) {
|
|
95
|
+
throw new Error(
|
|
96
|
+
`${target}: ${entries.length} metadata keys, Stripe allows at most ${maxKeys} (server keys included)`
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
for (const [key, value] of entries) {
|
|
100
|
+
if (key.length > maxKeyLength) {
|
|
101
|
+
throw new Error(
|
|
102
|
+
`${target}: metadata key '${key}' is too long (max ${maxKeyLength} characters)`
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
if (/[[\]]/.test(key)) {
|
|
106
|
+
throw new Error(
|
|
107
|
+
`${target}: metadata key '${key}' must not contain square brackets`
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
if (value !== null && String(value).length > maxValueLength) {
|
|
111
|
+
throw new Error(
|
|
112
|
+
`${target}: metadata value for key '${key}' is too long (max ${maxValueLength} characters)`
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Check every metadata map of Checkout Session params (session, subscription,
|
|
120
|
+
* payment intent, invoice, line products) against Stripe's limits.
|
|
121
|
+
*
|
|
122
|
+
* @param params - Session params about to be sent to Stripe
|
|
123
|
+
*
|
|
124
|
+
* @version 0.2.0
|
|
125
|
+
* @since 0.2.0
|
|
126
|
+
* @author AMBROISE PARK Consulting
|
|
127
|
+
*/
|
|
128
|
+
export function assertCheckoutSessionMetadata(
|
|
129
|
+
params: Stripe.Checkout.SessionCreateParams
|
|
130
|
+
): void {
|
|
131
|
+
const targets: Array<[string, Stripe.MetadataParam | '' | undefined]> = [
|
|
132
|
+
['metadata', params.metadata],
|
|
133
|
+
['subscription_data.metadata', params.subscription_data?.metadata],
|
|
134
|
+
['payment_intent_data.metadata', params.payment_intent_data?.metadata],
|
|
135
|
+
[
|
|
136
|
+
'invoice_creation.invoice_data.metadata',
|
|
137
|
+
params.invoice_creation?.invoice_data?.metadata,
|
|
138
|
+
],
|
|
139
|
+
...(params.line_items ?? []).map(
|
|
140
|
+
(line, i): [string, Stripe.MetadataParam | undefined] => [
|
|
141
|
+
`line_items[${i}].price_data.product_data.metadata`,
|
|
142
|
+
line.price_data?.product_data?.metadata,
|
|
143
|
+
]
|
|
144
|
+
),
|
|
145
|
+
];
|
|
146
|
+
for (const [target, metadata] of targets) {
|
|
147
|
+
if (metadata) assertStripeMetadata(metadata, target);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Validate the output of an app `resolveLineItems` hook. Fails loud on any
|
|
153
|
+
* malformed line instead of sending it to Stripe.
|
|
154
|
+
*/
|
|
155
|
+
export function assertResolvedOrder(
|
|
156
|
+
order: ResolvedOrder,
|
|
157
|
+
configKey: string
|
|
158
|
+
): void {
|
|
159
|
+
if (!order.orderId) {
|
|
160
|
+
throw new Error(`resolveLineItems for '${configKey}' returned no orderId`);
|
|
161
|
+
}
|
|
162
|
+
if (order.orderId.length > MAX_CLIENT_REFERENCE_ID_LENGTH) {
|
|
163
|
+
throw new Error(
|
|
164
|
+
`resolveLineItems for '${configKey}': orderId is sent as client_reference_id (max ${MAX_CLIENT_REFERENCE_ID_LENGTH} characters), got ${order.orderId.length}`
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
if (order.lineItems.length === 0) {
|
|
168
|
+
throw new Error(`resolveLineItems for '${configKey}' returned no lines`);
|
|
169
|
+
}
|
|
170
|
+
if (order.session) assertResolvedOrderSession(order.session, configKey);
|
|
171
|
+
for (const line of order.lineItems) {
|
|
172
|
+
if (!line.productId || !line.name) {
|
|
173
|
+
throw new Error(
|
|
174
|
+
`resolveLineItems for '${configKey}' returned a line without productId or name`
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
if (!Number.isInteger(line.unitAmount) || line.unitAmount < 0) {
|
|
178
|
+
throw new Error(
|
|
179
|
+
`Line '${line.productId}': unitAmount must be a non-negative integer (smallest currency unit), got ${line.unitAmount}`
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
if (!Number.isInteger(line.quantity) || line.quantity < 1) {
|
|
183
|
+
throw new Error(
|
|
184
|
+
`Line '${line.productId}': quantity must be a positive integer, got ${line.quantity}`
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Stripe Checkout `expires_at` window (seconds after creation). */
|
|
191
|
+
export const CHECKOUT_EXPIRY_WINDOW = { minSeconds: 30 * 60, maxSeconds: 24 * 60 * 60 } as const;
|
|
192
|
+
|
|
193
|
+
/** Stripe invoice footer maximum length. */
|
|
194
|
+
const MAX_INVOICE_FOOTER_LENGTH = 5000;
|
|
195
|
+
|
|
196
|
+
const ORDER_PAYMENT_METHOD_TYPES = ['card', 'sepa_debit'] as const;
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Validate per-order session options returned by `resolveLineItems`.
|
|
200
|
+
* Item-dependent checks (manual tax, invoice creation) run in `createCheckoutAlgorithm`.
|
|
201
|
+
*
|
|
202
|
+
* @param session - `ResolvedOrder.session`
|
|
203
|
+
* @param configKey - Back config key (shown in errors)
|
|
204
|
+
* @param nowSeconds - Current Unix time (injectable for tests)
|
|
205
|
+
*
|
|
206
|
+
* @version 0.2.0
|
|
207
|
+
* @since 0.2.0
|
|
208
|
+
* @author AMBROISE PARK Consulting
|
|
209
|
+
*/
|
|
210
|
+
export function assertResolvedOrderSession(
|
|
211
|
+
session: ResolvedOrderSession,
|
|
212
|
+
configKey: string,
|
|
213
|
+
nowSeconds: number = Math.floor(Date.now() / 1000)
|
|
214
|
+
): void {
|
|
215
|
+
const { taxRateIds, paymentMethodTypes, expiresAt, invoiceFooter } = session;
|
|
216
|
+
if (taxRateIds && taxRateIds.some((id) => typeof id !== 'string' || id.length === 0)) {
|
|
217
|
+
throw new Error(
|
|
218
|
+
`resolveLineItems for '${configKey}': session.taxRateIds must be non-empty strings`
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
if (paymentMethodTypes) {
|
|
222
|
+
if (paymentMethodTypes.length === 0) {
|
|
223
|
+
throw new Error(
|
|
224
|
+
`resolveLineItems for '${configKey}': session.paymentMethodTypes must not be empty`
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
for (const type of paymentMethodTypes) {
|
|
228
|
+
if (!(ORDER_PAYMENT_METHOD_TYPES as readonly string[]).includes(type)) {
|
|
229
|
+
throw new Error(
|
|
230
|
+
`resolveLineItems for '${configKey}': unsupported payment method type '${type}'`
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
if (expiresAt !== undefined) {
|
|
236
|
+
const { minSeconds, maxSeconds } = CHECKOUT_EXPIRY_WINDOW;
|
|
237
|
+
if (
|
|
238
|
+
!Number.isInteger(expiresAt) ||
|
|
239
|
+
expiresAt < nowSeconds + minSeconds ||
|
|
240
|
+
expiresAt > nowSeconds + maxSeconds
|
|
241
|
+
) {
|
|
242
|
+
throw new Error(
|
|
243
|
+
`resolveLineItems for '${configKey}': session.expiresAt must be a Unix time 30 minutes to 24 hours from now (Stripe), got ${expiresAt}`
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
if (
|
|
248
|
+
invoiceFooter !== undefined &&
|
|
249
|
+
(invoiceFooter.length === 0 || invoiceFooter.length > MAX_INVOICE_FOOTER_LENGTH)
|
|
250
|
+
) {
|
|
251
|
+
throw new Error(
|
|
252
|
+
`resolveLineItems for '${configKey}': session.invoiceFooter must be 1 to ${MAX_INVOICE_FOOTER_LENGTH} characters`
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/** Inclusive flag per Tax Rate ID: one Stripe call per ID per instance (cold start). */
|
|
258
|
+
const taxRateInclusiveCache = new Map<string, Promise<boolean>>();
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* StripeOrder manual tax: unit amounts are excl. VAT, but an inclusive Tax Rate
|
|
262
|
+
* overrides `price_data.tax_behavior: 'exclusive'` and carves the tax out of
|
|
263
|
+
* the amount. Throws when any configured rate is inclusive.
|
|
264
|
+
*/
|
|
265
|
+
export async function assertExclusiveTaxRates(
|
|
266
|
+
retrieveTaxRate: (id: string) => Promise<{ inclusive: boolean }>,
|
|
267
|
+
taxRateIds: string[],
|
|
268
|
+
configKey: string
|
|
269
|
+
): Promise<void> {
|
|
270
|
+
for (const id of taxRateIds) {
|
|
271
|
+
let inclusive = taxRateInclusiveCache.get(id);
|
|
272
|
+
if (!inclusive) {
|
|
273
|
+
inclusive = retrieveTaxRate(id).then((rate) => rate.inclusive);
|
|
274
|
+
taxRateInclusiveCache.set(id, inclusive);
|
|
275
|
+
// Failures are not cached: the next checkout retries
|
|
276
|
+
inclusive.catch(() => taxRateInclusiveCache.delete(id));
|
|
277
|
+
}
|
|
278
|
+
if (await inclusive) {
|
|
279
|
+
throw new Error(
|
|
280
|
+
`'${configKey}': Tax Rate '${id}' is inclusive. StripeOrder unit amounts are excl. VAT: use an exclusive Tax Rate (inclusive: false).`
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|