@donotdev/functions 0.1.44 → 0.2.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/README.md +25 -14
- package/lib/chunks/chunk-3F4BWKQD.js +22 -0
- package/lib/chunks/chunk-3F4BWKQD.js.map +7 -0
- package/lib/chunks/chunk-5IPULSUX.js +22 -0
- package/lib/chunks/chunk-5IPULSUX.js.map +7 -0
- package/lib/chunks/chunk-6USHTJED.js +22 -0
- package/lib/chunks/chunk-6USHTJED.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-CXBSROEW.js +23 -0
- package/lib/chunks/chunk-CXBSROEW.js.map +7 -0
- package/lib/chunks/chunk-GYTCWMXV.js +22 -0
- package/lib/chunks/chunk-GYTCWMXV.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-LCDLG4VG.js +151 -0
- package/lib/chunks/chunk-LCDLG4VG.js.map +7 -0
- package/lib/chunks/chunk-MQANUSY3.js +22 -0
- package/lib/chunks/chunk-MQANUSY3.js.map +7 -0
- package/lib/chunks/chunk-OFQVO3FD.js +22 -0
- package/lib/chunks/chunk-OFQVO3FD.js.map +7 -0
- package/lib/chunks/chunk-PWAGW4R6.js +22 -0
- package/lib/chunks/chunk-PWAGW4R6.js.map +7 -0
- package/lib/chunks/chunk-US2VOP4S.js +22 -0
- package/lib/chunks/chunk-US2VOP4S.js.map +7 -0
- package/lib/chunks/chunk-YTKDNZNW.js +22 -0
- package/lib/chunks/chunk-YTKDNZNW.js.map +7 -0
- package/lib/chunks/monitoring-E7JBYBHQ.js +22 -0
- package/lib/chunks/monitoring-E7JBYBHQ.js.map +7 -0
- package/lib/chunks/rateLimiter-SQISLOP5.js +22 -0
- package/lib/chunks/rateLimiter-SQISLOP5.js.map +7 -0
- package/lib/chunks/server-IQ5JNJOM.js +22 -0
- package/lib/chunks/server-IQ5JNJOM.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/index.d.ts +0 -0
- 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/cancelSubscription.d.ts +0 -0
- package/lib/firebase/billing/changePlan.d.ts +0 -0
- package/lib/firebase/billing/createCheckoutSession.d.ts +2 -2
- package/lib/firebase/billing/createCustomerPortal.d.ts +1 -1
- package/lib/firebase/billing/healthCheck.d.ts +0 -0
- 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/config/constants.d.ts +0 -0
- package/lib/firebase/config/index.d.ts +0 -0
- package/lib/firebase/config/secrets.d.ts +0 -0
- 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/index.d.ts +0 -0
- package/lib/firebase/crud/list.d.ts +1 -1
- package/lib/firebase/crud/update.d.ts +1 -9
- package/lib/firebase/helpers/githubAccessHelper.d.ts +0 -0
- package/lib/firebase/helpers/githubTeamAccessHelper.d.ts +0 -0
- package/lib/firebase/helpers/index.d.ts +0 -0
- package/lib/firebase/index.d.ts +0 -0
- package/lib/firebase/index.js +1 -5
- package/lib/firebase/index.js.map +4 -4
- package/lib/firebase/oauth/disconnect.d.ts +0 -0
- package/lib/firebase/oauth/exchangeToken.d.ts +0 -0
- package/lib/firebase/oauth/getConnections.d.ts +0 -0
- package/lib/firebase/oauth/githubAccess.d.ts +2 -2
- package/lib/firebase/oauth/index.d.ts +0 -0
- package/lib/firebase/oauth/refreshToken.d.ts +0 -0
- package/lib/firebase/registerCrudFunctions.d.ts +9 -1
- package/lib/firebase/scheduled/checkExpiredSubscriptions.d.ts +0 -0
- package/lib/firebase/scheduled/index.d.ts +0 -0
- package/lib/shared/ai/classifyAIError.d.ts +0 -0
- package/lib/shared/ai/costTracker.d.ts +0 -0
- package/lib/shared/ai/createAIRoute.d.ts +0 -0
- package/lib/shared/ai/index.d.ts +0 -0
- package/lib/shared/ai/resolveModelTier.d.ts +0 -0
- 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/firestoreIdempotency.d.ts +13 -0
- 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 +21 -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/stripeClient.d.ts +45 -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 +29 -20
- package/lib/shared/email/createEmailRoute.d.ts +0 -0
- package/lib/shared/email/index.d.ts +0 -0
- package/lib/shared/email/provider.d.ts +0 -0
- package/lib/shared/email/resendProvider.d.ts +0 -0
- package/lib/shared/errorHandling.d.ts +0 -0
- package/lib/shared/firebase.d.ts +0 -0
- package/lib/shared/github.d.ts +0 -0
- package/lib/shared/index.d.ts +0 -0
- package/lib/shared/index.js +1 -6
- package/lib/shared/index.js.map +4 -4
- package/lib/shared/logger.d.ts +0 -0
- package/lib/shared/metadata.d.ts +0 -0
- package/lib/shared/oauth/exchangeToken.d.ts +0 -0
- package/lib/shared/oauth/grantAccess.d.ts +0 -0
- package/lib/shared/oauth/index.d.ts +0 -0
- package/lib/shared/schema.d.ts +0 -0
- package/lib/shared/utils/detectFirestore.d.ts +0 -0
- package/lib/shared/utils/external/date.d.ts +0 -0
- package/lib/shared/utils/external/index.d.ts +0 -0
- package/lib/shared/utils/external/references.d.ts +0 -0
- package/lib/shared/utils/external/subscription.d.ts +0 -0
- package/lib/shared/utils/firebaseHelpers.d.ts +0 -0
- package/lib/shared/utils/functionWrapper.d.ts +1 -1
- package/lib/shared/utils/index.d.ts +0 -0
- package/lib/shared/utils/internal/auth.d.ts +38 -1
- package/lib/shared/utils/internal/firebase.d.ts +0 -0
- package/lib/shared/utils/internal/idempotency.d.ts +0 -0
- package/lib/shared/utils/internal/index.d.ts +0 -0
- package/lib/shared/utils/internal/monitoring.d.ts +0 -0
- package/lib/shared/utils/internal/rateLimiter.d.ts +8 -0
- package/lib/shared/utils/internal/validation.d.ts +0 -8
- package/lib/shared/utils/schemaValidation.d.ts +1 -1
- package/lib/shared/utils/validation.d.ts +0 -0
- package/lib/shared/utils/vercelHelpers.d.ts +8 -0
- package/lib/shared/utils.d.ts +0 -56
- package/lib/shared/validation.d.ts +0 -0
- package/lib/supabase/ai/createAIFunction.d.ts +0 -0
- package/lib/supabase/auth/deleteAccount.d.ts +0 -0
- package/lib/supabase/auth/getCustomClaims.d.ts +0 -0
- package/lib/supabase/auth/getUserAuthStatus.d.ts +0 -0
- package/lib/supabase/auth/removeCustomClaims.d.ts +0 -0
- package/lib/supabase/auth/setCustomClaims.d.ts +0 -0
- package/lib/supabase/baseFunction.d.ts +1 -1
- package/lib/supabase/billing/cancelSubscription.d.ts +0 -0
- package/lib/supabase/billing/changePlan.d.ts +0 -0
- package/lib/supabase/billing/createCheckoutSession.d.ts +0 -0
- package/lib/supabase/billing/createCustomerPortal.d.ts +0 -0
- package/lib/supabase/billing/refreshSubscriptionStatus.d.ts +0 -0
- package/lib/supabase/billing/stripeWebhook.d.ts +34 -0
- package/lib/supabase/crud/aggregate.d.ts +0 -0
- package/lib/supabase/crud/bulk.d.ts +1 -1
- package/lib/supabase/crud/create.d.ts +0 -0
- package/lib/supabase/crud/delete.d.ts +0 -0
- package/lib/supabase/crud/entityStore.d.ts +71 -0
- package/lib/supabase/crud/get.d.ts +0 -0
- package/lib/supabase/crud/index.d.ts +2 -0
- package/lib/supabase/crud/list.d.ts +0 -8
- package/lib/supabase/crud/update.d.ts +0 -0
- package/lib/supabase/edgeFunction.d.ts +0 -0
- package/lib/supabase/email/createEmailFunction.d.ts +0 -0
- package/lib/supabase/helpers/authProvider.d.ts +5 -5
- package/lib/supabase/index.d.ts +11 -2
- package/lib/supabase/index.js +1 -3
- package/lib/supabase/index.js.map +4 -4
- package/lib/supabase/oauth/disconnect.d.ts +0 -0
- package/lib/supabase/oauth/exchangeToken.d.ts +0 -0
- package/lib/supabase/oauth/getConnections.d.ts +0 -0
- package/lib/supabase/oauth/index.d.ts +0 -0
- package/lib/supabase/oauth/refreshToken.d.ts +0 -0
- package/lib/supabase/registerCrudFunctions.d.ts +0 -0
- package/lib/supabase/utils/idempotency.d.ts +0 -0
- package/lib/supabase/utils/monitoring.d.ts +0 -0
- package/lib/supabase/utils/rateLimiter.d.ts +1 -1
- package/lib/vercel/api/ai/chat.d.ts +0 -9
- package/lib/vercel/api/ai/index.d.ts +0 -0
- 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/index.d.ts +0 -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/constants.d.ts +0 -0
- package/lib/vercel/api/config/firebase-admin.d.ts +8 -0
- package/lib/vercel/api/config/index.d.ts +0 -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/index.d.ts +0 -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.d.ts +0 -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/index.d.ts +0 -0
- package/lib/vercel/api/oauth/revoke-github-access.d.ts +1 -1
- package/lib/vercel/api/utils/cors.d.ts +0 -0
- 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 +2 -3
- package/src/firebase/billing/changePlan.ts +3 -8
- package/src/firebase/billing/createCheckoutSession.ts +31 -61
- package/src/firebase/billing/createCustomerPortal.ts +5 -6
- package/src/firebase/billing/healthCheck.ts +1 -1
- package/src/firebase/billing/index.ts +8 -1
- package/src/firebase/billing/refreshSubscriptionStatus.ts +3 -4
- package/src/firebase/billing/webhookHandler.ts +21 -5
- 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 -42
- package/src/shared/__tests__/logger.test.ts +31 -0
- 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 +3 -2
- 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 +370 -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/firestoreIdempotency.ts +189 -0
- package/src/shared/billing/helpers/index.ts +0 -1
- package/src/shared/billing/helpers/subscriptionManagement.ts +8 -10
- package/src/shared/billing/helpers/updateUserSubscription.ts +3 -36
- package/src/shared/billing/idempotency.ts +104 -168
- 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/stripeClient.ts +91 -0
- package/src/shared/billing/supabaseIdempotency.ts +228 -0
- package/src/shared/billing/tierWebhookEvents.ts +432 -0
- package/src/shared/billing/webhookHandler.ts +189 -386
- package/src/shared/logger.ts +4 -6
- 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/internal/validation.ts +0 -18
- package/src/shared/utils/schemaValidation.ts +1 -2
- package/src/shared/utils/vercelHelpers.ts +3 -3
- package/src/shared/utils.ts +5 -99
- 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/cancelSubscription.ts +1 -1
- package/src/supabase/billing/changePlan.ts +1 -1
- package/src/supabase/billing/createCheckoutSession.ts +11 -41
- package/src/supabase/billing/createCustomerPortal.ts +1 -1
- package/src/supabase/billing/refreshSubscriptionStatus.ts +2 -7
- 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/list.ts +2 -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 +4 -5
- package/src/vercel/api/billing/create-checkout-session.ts +43 -73
- package/src/vercel/api/billing/customer-portal.ts +3 -5
- package/src/vercel/api/billing/index.ts +4 -2
- package/src/vercel/api/billing/refresh-subscription-status.ts +22 -7
- package/src/vercel/api/billing/webhook-handler.ts +32 -26
- 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
- package/lib/shared/ai/createAIHandler.d.ts +0 -39
- package/lib/shared/ai/providers/anthropic.d.ts +0 -12
- package/lib/shared/ai/providers/google.d.ts +0 -10
- package/lib/shared/ai/providers/index.d.ts +0 -23
- package/lib/shared/ai/providers/openai.d.ts +0 -10
- package/lib/shared/ai/utils/parseSSE.d.ts +0 -22
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
// packages/functions/src/shared/billing/__tests__/createCheckout.compat.test.ts
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @fileoverview 0.1.x regression tests for createCheckoutAlgorithm
|
|
5
|
+
* @description Reproduces 0.1.x usage: back-config items without priceId
|
|
6
|
+
* (client priceId, dynamic pricing), beforeCheckout guards on 0.1.x options,
|
|
7
|
+
* and the 0.1.x call shape / StripeCheckoutProvider.
|
|
8
|
+
*
|
|
9
|
+
* @version 0.2.0
|
|
10
|
+
* @since 0.2.0
|
|
11
|
+
* @author AMBROISE PARK Consulting
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import * as v from 'valibot';
|
|
15
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
16
|
+
|
|
17
|
+
import { CreateCheckoutSessionRequestSchema } from '@donotdev/core/server';
|
|
18
|
+
import type {
|
|
19
|
+
BeforeCheckoutHook,
|
|
20
|
+
CheckoutOptions,
|
|
21
|
+
StripeBackConfig,
|
|
22
|
+
} from '@donotdev/core/server';
|
|
23
|
+
|
|
24
|
+
import { createCheckoutAlgorithm } from '../createCheckout.js';
|
|
25
|
+
import type { StripeCheckoutProvider } from '@donotdev/core/server';
|
|
26
|
+
|
|
27
|
+
import type { AuthCheckoutProvider, StripeSessionProvider } from '../createCheckout.js';
|
|
28
|
+
|
|
29
|
+
const USER_ID = 'user_001';
|
|
30
|
+
const URLS = {
|
|
31
|
+
successUrl: 'https://app.example.com/billing/success',
|
|
32
|
+
cancelUrl: 'https://app.example.com/billing/cancel',
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/** 0.1.x back config: no priceId, client-supplied price or dynamic amount. */
|
|
36
|
+
function makeLegacyConfig(
|
|
37
|
+
beforeCheckout?: BeforeCheckoutHook
|
|
38
|
+
): StripeBackConfig {
|
|
39
|
+
return {
|
|
40
|
+
credits: {
|
|
41
|
+
type: 'StripePayment',
|
|
42
|
+
name: 'Credits',
|
|
43
|
+
price: 0,
|
|
44
|
+
currency: 'eur',
|
|
45
|
+
maxUnitAmount: 50000,
|
|
46
|
+
tier: 'credits',
|
|
47
|
+
duration: 'lifetime',
|
|
48
|
+
beforeCheckout,
|
|
49
|
+
},
|
|
50
|
+
pro_monthly: {
|
|
51
|
+
type: 'StripeSubscription',
|
|
52
|
+
name: 'Pro Monthly',
|
|
53
|
+
price: 1999,
|
|
54
|
+
currency: 'eur',
|
|
55
|
+
tier: 'pro',
|
|
56
|
+
duration: '1month',
|
|
57
|
+
beforeCheckout,
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** 0.1.x client request (as sent by @donotdev/billing 0.1.x), validated by the real schema. */
|
|
63
|
+
function legacyRequest(fields: Record<string, unknown>) {
|
|
64
|
+
return v.parse(CreateCheckoutSessionRequestSchema, {
|
|
65
|
+
...URLS,
|
|
66
|
+
allowPromotionCodes: true,
|
|
67
|
+
...fields,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function makeLegacyProvider() {
|
|
72
|
+
const createCheckoutSession = vi.fn<
|
|
73
|
+
StripeCheckoutProvider['createCheckoutSession']
|
|
74
|
+
>(async () => ({ id: 'cs_legacy', url: 'https://checkout.stripe.com/c/cs_legacy' }));
|
|
75
|
+
return { createCheckoutSession };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function makeSessionProvider() {
|
|
79
|
+
const createStripeSession = vi.fn<StripeSessionProvider['createStripeSession']>(
|
|
80
|
+
async () => ({ id: 'cs_new', url: 'https://checkout.stripe.com/c/cs_new' })
|
|
81
|
+
);
|
|
82
|
+
const retrieveTaxRate = vi.fn<StripeSessionProvider['retrieveTaxRate']>(
|
|
83
|
+
async (id) => ({ id, inclusive: false })
|
|
84
|
+
);
|
|
85
|
+
return { createStripeSession, retrieveTaxRate };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const auth: AuthCheckoutProvider = {
|
|
89
|
+
getUser: vi.fn(async () => ({ customClaims: {} })),
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
// ---------------------------------------------------------------------------
|
|
93
|
+
// (a) back-config item without priceId
|
|
94
|
+
// ---------------------------------------------------------------------------
|
|
95
|
+
|
|
96
|
+
describe('0.1.x (a): back-config item without priceId', () => {
|
|
97
|
+
it('uses the client priceId', async () => {
|
|
98
|
+
const stripe = makeSessionProvider();
|
|
99
|
+
const request = legacyRequest({
|
|
100
|
+
priceId: 'price_client',
|
|
101
|
+
mode: 'subscription',
|
|
102
|
+
metadata: { billingConfigKey: 'pro_monthly' },
|
|
103
|
+
});
|
|
104
|
+
await createCheckoutAlgorithm(request, USER_ID, stripe, auth, makeLegacyConfig());
|
|
105
|
+
expect(stripe.createStripeSession.mock.calls[0]?.[0]).toMatchObject({
|
|
106
|
+
mode: 'subscription',
|
|
107
|
+
line_items: [{ price: 'price_client', quantity: 1 }],
|
|
108
|
+
subscription_data: { metadata: { userId: USER_ID, billingConfigKey: 'pro_monthly' } },
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('dynamic pricing: unitAmount + productName within maxUnitAmount', async () => {
|
|
113
|
+
const stripe = makeSessionProvider();
|
|
114
|
+
const request = legacyRequest({
|
|
115
|
+
unitAmount: 2500,
|
|
116
|
+
productName: '25 credits',
|
|
117
|
+
mode: 'payment',
|
|
118
|
+
metadata: { billingConfigKey: 'credits' },
|
|
119
|
+
});
|
|
120
|
+
await createCheckoutAlgorithm(request, USER_ID, stripe, auth, makeLegacyConfig());
|
|
121
|
+
expect(stripe.createStripeSession.mock.calls[0]?.[0]).toMatchObject({
|
|
122
|
+
mode: 'payment',
|
|
123
|
+
line_items: [
|
|
124
|
+
{
|
|
125
|
+
price_data: {
|
|
126
|
+
currency: 'eur',
|
|
127
|
+
unit_amount: 2500,
|
|
128
|
+
product_data: { name: '25 credits' },
|
|
129
|
+
},
|
|
130
|
+
quantity: 1,
|
|
131
|
+
},
|
|
132
|
+
],
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it.each([
|
|
137
|
+
['above maxUnitAmount', { unitAmount: 60000 }, /exceeds maximum allowed: 50000/],
|
|
138
|
+
['below 1.00', { unitAmount: 50 }, /at least 100 cents/],
|
|
139
|
+
['subscription mode', { mode: 'subscription' }, /not supported for subscriptions/],
|
|
140
|
+
])('dynamic pricing keeps the 0.1.x guards (%s)', async (_label, patch, error) => {
|
|
141
|
+
const request = legacyRequest({
|
|
142
|
+
unitAmount: 2500,
|
|
143
|
+
productName: '25 credits',
|
|
144
|
+
metadata: { billingConfigKey: 'credits' },
|
|
145
|
+
...patch,
|
|
146
|
+
});
|
|
147
|
+
await expect(
|
|
148
|
+
createCheckoutAlgorithm(request, USER_ID, makeSessionProvider(), auth, makeLegacyConfig())
|
|
149
|
+
).rejects.toThrow(error);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it('dynamic pricing without maxUnitAmount fails as in 0.1.x', async () => {
|
|
153
|
+
const config = makeLegacyConfig();
|
|
154
|
+
const { maxUnitAmount: _omit, ...noCap } = config.credits as any;
|
|
155
|
+
config.credits = noCap;
|
|
156
|
+
const request = legacyRequest({
|
|
157
|
+
unitAmount: 2500,
|
|
158
|
+
productName: '25 credits',
|
|
159
|
+
metadata: { billingConfigKey: 'credits' },
|
|
160
|
+
});
|
|
161
|
+
await expect(
|
|
162
|
+
createCheckoutAlgorithm(request, USER_ID, makeSessionProvider(), auth, config)
|
|
163
|
+
).rejects.toThrow(/requires maxUnitAmount/);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it('beforeCheckout overrides apply to dynamic pricing, still capped', async () => {
|
|
167
|
+
const stripe = makeSessionProvider();
|
|
168
|
+
const config = makeLegacyConfig(async () => ({
|
|
169
|
+
allowed: true,
|
|
170
|
+
overrides: { unitAmount: 4000, productName: 'Server-priced credits' },
|
|
171
|
+
}));
|
|
172
|
+
const request = legacyRequest({
|
|
173
|
+
unitAmount: 100,
|
|
174
|
+
productName: 'client',
|
|
175
|
+
metadata: { billingConfigKey: 'credits' },
|
|
176
|
+
});
|
|
177
|
+
await createCheckoutAlgorithm(request, USER_ID, stripe, auth, config);
|
|
178
|
+
expect(stripe.createStripeSession.mock.calls[0]?.[0].line_items?.[0]).toMatchObject({
|
|
179
|
+
price_data: { unit_amount: 4000, product_data: { name: 'Server-priced credits' } },
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
const capped = makeLegacyConfig(async () => ({
|
|
183
|
+
allowed: true,
|
|
184
|
+
overrides: { unitAmount: 99999 },
|
|
185
|
+
}));
|
|
186
|
+
await expect(
|
|
187
|
+
createCheckoutAlgorithm(request, USER_ID, makeSessionProvider(), auth, capped)
|
|
188
|
+
).rejects.toThrow(/Hook-overridden unit amount 99999/);
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
// ---------------------------------------------------------------------------
|
|
193
|
+
// (b) beforeCheckout second argument
|
|
194
|
+
// ---------------------------------------------------------------------------
|
|
195
|
+
|
|
196
|
+
describe('0.1.x (b): beforeCheckout options', () => {
|
|
197
|
+
it('receives priceId / mode / unitAmount / currency, plus the new fields', async () => {
|
|
198
|
+
const hook = vi.fn<BeforeCheckoutHook>(async () => ({ allowed: true }));
|
|
199
|
+
const request = legacyRequest({
|
|
200
|
+
unitAmount: 2500,
|
|
201
|
+
productName: '25 credits',
|
|
202
|
+
currency: 'usd',
|
|
203
|
+
mode: 'payment',
|
|
204
|
+
metadata: { billingConfigKey: 'credits' },
|
|
205
|
+
});
|
|
206
|
+
await createCheckoutAlgorithm(request, USER_ID, makeSessionProvider(), auth, makeLegacyConfig(hook));
|
|
207
|
+
expect(hook.mock.calls[0]?.[1]).toMatchObject({
|
|
208
|
+
mode: 'payment',
|
|
209
|
+
unitAmount: 2500,
|
|
210
|
+
productName: '25 credits',
|
|
211
|
+
currency: 'usd',
|
|
212
|
+
allowPromotionCodes: true,
|
|
213
|
+
metadata: { billingConfigKey: 'credits' },
|
|
214
|
+
billingConfigKey: 'credits',
|
|
215
|
+
lineItems: [{ productId: 'credits', quantity: 1 }],
|
|
216
|
+
userId: USER_ID,
|
|
217
|
+
});
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it('a 0.1.x guard on options.mode still blocks', async () => {
|
|
221
|
+
// 0.1.x hook typed with CheckoutOptions keeps compiling
|
|
222
|
+
const guard = async (_userId: string, options: CheckoutOptions) =>
|
|
223
|
+
options.mode === 'subscription'
|
|
224
|
+
? { allowed: false, reason: 'Subscriptions closed' }
|
|
225
|
+
: { allowed: true };
|
|
226
|
+
const config = makeLegacyConfig(guard);
|
|
227
|
+
const priced = legacyRequest({
|
|
228
|
+
priceId: 'price_client',
|
|
229
|
+
metadata: { billingConfigKey: 'pro_monthly' },
|
|
230
|
+
});
|
|
231
|
+
// mode omitted: derived from the StripeSubscription item type
|
|
232
|
+
await expect(
|
|
233
|
+
createCheckoutAlgorithm(priced, USER_ID, makeSessionProvider(), auth, config)
|
|
234
|
+
).rejects.toThrow('Subscriptions closed');
|
|
235
|
+
// New-shape request on a configured price: mode + priceId still populated
|
|
236
|
+
const hook = vi.fn<BeforeCheckoutHook>(async () => ({ allowed: true }));
|
|
237
|
+
const withPrice = makeLegacyConfig(hook);
|
|
238
|
+
(withPrice.pro_monthly as { priceId?: string }).priceId = 'price_pro';
|
|
239
|
+
await createCheckoutAlgorithm(
|
|
240
|
+
{ billingConfigKey: 'pro_monthly', ...URLS },
|
|
241
|
+
USER_ID,
|
|
242
|
+
makeSessionProvider(),
|
|
243
|
+
auth,
|
|
244
|
+
withPrice
|
|
245
|
+
);
|
|
246
|
+
expect(hook.mock.calls[0]?.[1]).toMatchObject({
|
|
247
|
+
mode: 'subscription',
|
|
248
|
+
priceId: 'price_pro',
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
// ---------------------------------------------------------------------------
|
|
254
|
+
// (c) 0.1.x call shape and StripeCheckoutProvider
|
|
255
|
+
// ---------------------------------------------------------------------------
|
|
256
|
+
|
|
257
|
+
describe('0.1.x (c): createCheckoutAlgorithm(request, provider, auth, config)', () => {
|
|
258
|
+
it('sends the exact 0.1.x params to a 0.1.x provider, userId from the request', async () => {
|
|
259
|
+
const stripe = makeLegacyProvider();
|
|
260
|
+
const result = await createCheckoutAlgorithm(
|
|
261
|
+
{
|
|
262
|
+
priceId: 'price_client',
|
|
263
|
+
userId: USER_ID,
|
|
264
|
+
mode: 'subscription',
|
|
265
|
+
customerEmail: 'a@b.co',
|
|
266
|
+
metadata: { billingConfigKey: 'pro_monthly', ref: 'x' },
|
|
267
|
+
...URLS,
|
|
268
|
+
},
|
|
269
|
+
stripe,
|
|
270
|
+
auth,
|
|
271
|
+
makeLegacyConfig()
|
|
272
|
+
);
|
|
273
|
+
expect(result).toEqual({
|
|
274
|
+
sessionId: 'cs_legacy',
|
|
275
|
+
sessionUrl: 'https://checkout.stripe.com/c/cs_legacy',
|
|
276
|
+
});
|
|
277
|
+
expect(stripe.createCheckoutSession).toHaveBeenCalledWith({
|
|
278
|
+
priceId: 'price_client',
|
|
279
|
+
unitAmount: undefined,
|
|
280
|
+
productName: undefined,
|
|
281
|
+
currency: 'eur',
|
|
282
|
+
customerEmail: 'a@b.co',
|
|
283
|
+
metadata: { billingConfigKey: 'pro_monthly', ref: 'x', userId: USER_ID },
|
|
284
|
+
allowPromotionCodes: true,
|
|
285
|
+
successUrl: URLS.successUrl,
|
|
286
|
+
cancelUrl: URLS.cancelUrl,
|
|
287
|
+
mode: 'subscription',
|
|
288
|
+
});
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
it('dynamic pricing through a 0.1.x provider', async () => {
|
|
292
|
+
const stripe = makeLegacyProvider();
|
|
293
|
+
await createCheckoutAlgorithm(
|
|
294
|
+
{
|
|
295
|
+
unitAmount: 2500,
|
|
296
|
+
productName: '25 credits',
|
|
297
|
+
userId: USER_ID,
|
|
298
|
+
metadata: { billingConfigKey: 'credits' },
|
|
299
|
+
...URLS,
|
|
300
|
+
},
|
|
301
|
+
stripe,
|
|
302
|
+
auth,
|
|
303
|
+
makeLegacyConfig()
|
|
304
|
+
);
|
|
305
|
+
expect(stripe.createCheckoutSession.mock.calls[0]?.[0]).toMatchObject({
|
|
306
|
+
priceId: undefined,
|
|
307
|
+
unitAmount: 2500,
|
|
308
|
+
productName: '25 credits',
|
|
309
|
+
currency: 'eur',
|
|
310
|
+
mode: 'payment',
|
|
311
|
+
});
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
it('0.1.x call shape requires request.userId', async () => {
|
|
315
|
+
await expect(
|
|
316
|
+
createCheckoutAlgorithm(
|
|
317
|
+
{ priceId: 'price_client', metadata: { billingConfigKey: 'credits' }, ...URLS },
|
|
318
|
+
makeLegacyProvider(),
|
|
319
|
+
auth,
|
|
320
|
+
makeLegacyConfig()
|
|
321
|
+
)
|
|
322
|
+
).rejects.toThrow('Missing userId in request');
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
it('StripeOrder needs a StripeSessionProvider', async () => {
|
|
326
|
+
const config: StripeBackConfig = {
|
|
327
|
+
shop: {
|
|
328
|
+
type: 'StripeOrder',
|
|
329
|
+
name: 'Shop',
|
|
330
|
+
currency: 'eur',
|
|
331
|
+
tax: { mode: 'automatic' },
|
|
332
|
+
taxIdCollection: 'disabled',
|
|
333
|
+
billingAddressCollection: 'auto',
|
|
334
|
+
customerCreation: 'if_required',
|
|
335
|
+
invoiceCreation: false,
|
|
336
|
+
resolveLineItems: vi.fn(),
|
|
337
|
+
onOrderPaid: vi.fn(),
|
|
338
|
+
},
|
|
339
|
+
};
|
|
340
|
+
await expect(
|
|
341
|
+
createCheckoutAlgorithm(
|
|
342
|
+
{ billingConfigKey: 'shop', userId: USER_ID, ...URLS },
|
|
343
|
+
makeLegacyProvider(),
|
|
344
|
+
auth,
|
|
345
|
+
config
|
|
346
|
+
)
|
|
347
|
+
).rejects.toThrow(/StripeSessionProvider/);
|
|
348
|
+
});
|
|
349
|
+
});
|