@donotdev/functions 0.2.0 → 0.2.2
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 +1 -1
- package/lib/chunks/{chunk-JTAGPPPL.js → chunk-2A4FPYNB.js} +2 -2
- package/lib/chunks/chunk-2IFFIUFV.js +23 -0
- package/lib/chunks/{chunk-RM7DV6IQ.js.map → chunk-2IFFIUFV.js.map} +3 -3
- package/lib/chunks/chunk-FQSQGOBN.js +22 -0
- package/lib/chunks/chunk-FQSQGOBN.js.map +7 -0
- package/lib/chunks/chunk-G4FO76EA.js +22 -0
- package/lib/chunks/{chunk-5DTPEM64.js.map → chunk-G4FO76EA.js.map} +3 -3
- package/lib/chunks/{chunk-QJAU2MWV.js → chunk-LSM5Y2E5.js} +2 -2
- package/lib/chunks/{chunk-QJAU2MWV.js.map → chunk-LSM5Y2E5.js.map} +3 -3
- package/lib/chunks/{chunk-4U7QQWYG.js → chunk-M4JV2JCF.js} +2 -2
- package/lib/chunks/chunk-OZX5ZG44.js +22 -0
- package/lib/chunks/chunk-OZX5ZG44.js.map +7 -0
- package/lib/chunks/{chunk-IFOYVSPU.js → chunk-PJCJ5H2O.js} +2 -2
- package/lib/chunks/chunk-PWAGW4R6.js +22 -0
- package/lib/chunks/chunk-PWAGW4R6.js.map +7 -0
- package/lib/chunks/chunk-UYI56E6Q.js +151 -0
- package/lib/chunks/chunk-UYI56E6Q.js.map +7 -0
- package/lib/chunks/chunk-ZVHC2CAV.js +22 -0
- package/lib/chunks/chunk-ZVHC2CAV.js.map +7 -0
- package/lib/chunks/{monitoring-AGCSU3FI.js → monitoring-UIDJS2KA.js} +2 -2
- package/lib/chunks/{rateLimiter-6KO7AIZP.js → rateLimiter-WNWHI3B4.js} +2 -2
- package/lib/chunks/server-RYGEBS2B.js +22 -0
- package/lib/firebase/auth/getCustomClaims.d.ts +0 -0
- package/lib/firebase/auth/getUserAuthStatus.d.ts +0 -0
- package/lib/firebase/auth/index.d.ts +0 -0
- package/lib/firebase/auth/removeCustomClaims.d.ts +0 -0
- package/lib/firebase/auth/setCustomClaims.d.ts +0 -0
- package/lib/firebase/baseFunction.d.ts +0 -0
- 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 +0 -0
- package/lib/firebase/billing/createCustomerPortal.d.ts +0 -0
- package/lib/firebase/billing/healthCheck.d.ts +0 -0
- package/lib/firebase/billing/index.d.ts +2 -2
- package/lib/firebase/billing/refreshSubscriptionStatus.d.ts +0 -0
- package/lib/firebase/billing/webhookHandler.d.ts +0 -0
- 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 -0
- package/lib/firebase/crud/create.d.ts +0 -0
- package/lib/firebase/crud/delete.d.ts +0 -0
- package/lib/firebase/crud/get.d.ts +0 -0
- package/lib/firebase/crud/index.d.ts +0 -0
- package/lib/firebase/crud/list.d.ts +0 -0
- package/lib/firebase/crud/update.d.ts +0 -0
- 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 +1 -1
- package/lib/firebase/index.js +1 -1
- 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 +0 -0
- package/lib/firebase/oauth/index.d.ts +0 -0
- package/lib/firebase/oauth/refreshToken.d.ts +0 -0
- package/lib/firebase/registerCrudFunctions.d.ts +0 -5
- 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/checkoutPrice.d.ts +3 -6
- package/lib/shared/billing/createCheckout.d.ts +3 -3
- package/lib/shared/billing/firestoreIdempotency.d.ts +13 -0
- package/lib/shared/billing/helpers/index.d.ts +0 -0
- package/lib/shared/billing/helpers/subscriptionManagement.d.ts +0 -0
- package/lib/shared/billing/helpers/updateUserSubscription.d.ts +0 -0
- package/lib/shared/billing/idempotency.d.ts +4 -35
- package/lib/shared/billing/idempotencyContract.d.ts +0 -20
- package/lib/shared/billing/index.d.ts +0 -0
- package/lib/shared/billing/stripeClient.d.ts +45 -0
- package/lib/shared/billing/webhookHandler.d.ts +5 -4
- 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 -1
- package/lib/shared/index.js.map +0 -0
- 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 +0 -0
- package/lib/shared/utils/index.d.ts +0 -0
- package/lib/shared/utils/internal/auth.d.ts +1 -5
- 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 +0 -0
- package/lib/shared/utils/internal/validation.d.ts +0 -8
- package/lib/shared/utils/schemaValidation.d.ts +0 -0
- package/lib/shared/utils/validation.d.ts +0 -0
- package/lib/shared/utils/vercelHelpers.d.ts +0 -0
- package/lib/shared/utils.d.ts +0 -126
- 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 +0 -0
- 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/crud/aggregate.d.ts +0 -0
- package/lib/supabase/crud/create.d.ts +0 -0
- package/lib/supabase/crud/delete.d.ts +0 -0
- package/lib/supabase/crud/get.d.ts +0 -0
- package/lib/supabase/crud/index.d.ts +0 -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 +0 -0
- package/lib/supabase/index.d.ts +3 -2
- package/lib/supabase/index.js +1 -1
- package/lib/supabase/index.js.map +3 -3
- 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 +0 -0
- package/lib/vercel/api/ai/chat.d.ts +0 -0
- package/lib/vercel/api/ai/index.d.ts +0 -0
- package/lib/vercel/api/auth/get-custom-claims.d.ts +0 -0
- package/lib/vercel/api/auth/get-user-auth-status.d.ts +0 -0
- package/lib/vercel/api/auth/index.d.ts +0 -0
- package/lib/vercel/api/auth/remove-custom-claims.d.ts +0 -0
- package/lib/vercel/api/auth/set-custom-claims.d.ts +0 -0
- package/lib/vercel/api/billing/cancel.d.ts +0 -0
- package/lib/vercel/api/billing/change-plan.d.ts +0 -0
- package/lib/vercel/api/billing/create-checkout-session.d.ts +0 -0
- package/lib/vercel/api/billing/customer-portal.d.ts +0 -0
- package/lib/vercel/api/billing/index.d.ts +0 -0
- package/lib/vercel/api/billing/refresh-subscription-status.d.ts +0 -0
- package/lib/vercel/api/billing/webhook-handler.d.ts +0 -0
- package/lib/vercel/api/config/constants.d.ts +0 -0
- package/lib/vercel/api/config/firebase-admin.d.ts +0 -0
- package/lib/vercel/api/config/index.d.ts +0 -0
- package/lib/vercel/api/crud/create.d.ts +0 -0
- package/lib/vercel/api/crud/delete.d.ts +0 -0
- package/lib/vercel/api/crud/get.d.ts +0 -0
- package/lib/vercel/api/crud/index.d.ts +0 -0
- package/lib/vercel/api/crud/list.d.ts +0 -0
- package/lib/vercel/api/crud/update.d.ts +0 -0
- package/lib/vercel/api/index.d.ts +0 -0
- package/lib/vercel/api/index.js +1 -1
- package/lib/vercel/api/index.js.map +2 -2
- package/lib/vercel/api/oauth/check-github-access.d.ts +0 -0
- package/lib/vercel/api/oauth/exchange-token.d.ts +0 -0
- package/lib/vercel/api/oauth/grant-github-access.d.ts +0 -0
- package/lib/vercel/api/oauth/index.d.ts +0 -0
- package/lib/vercel/api/oauth/revoke-github-access.d.ts +0 -0
- package/lib/vercel/api/utils/cors.d.ts +0 -0
- package/lib/vercel/baseFunction.d.ts +0 -0
- package/package.json +2 -2
- package/src/firebase/baseFunction.ts +3 -2
- package/src/firebase/billing/cancelSubscription.ts +1 -1
- package/src/firebase/billing/changePlan.ts +1 -5
- package/src/firebase/billing/createCheckoutSession.ts +1 -5
- package/src/firebase/billing/createCustomerPortal.ts +1 -1
- package/src/firebase/billing/healthCheck.ts +1 -1
- package/src/firebase/billing/index.d.ts +0 -12
- package/src/firebase/billing/index.ts +0 -3
- package/src/firebase/billing/refreshSubscriptionStatus.ts +1 -1
- package/src/firebase/billing/webhookHandler.ts +2 -2
- package/src/firebase/index.ts +0 -1
- package/src/firebase/oauth/disconnect.ts +2 -2
- package/src/firebase/oauth/exchangeToken.ts +2 -2
- package/src/firebase/oauth/getConnections.ts +2 -2
- package/src/firebase/oauth/githubAccess.ts +4 -4
- package/src/firebase/oauth/refreshToken.ts +2 -2
- package/src/firebase/registerCrudFunctions.ts +0 -19
- package/src/shared/__tests__/idempotency.test.ts +0 -32
- package/src/shared/__tests__/internalValidation.test.ts +1 -40
- package/src/shared/__tests__/logger.test.ts +31 -0
- package/src/shared/__tests__/utils.test.ts +1 -26
- package/src/shared/billing/__tests__/createCheckout.compat.test.ts +4 -110
- package/src/shared/billing/__tests__/webhookHandler.test.ts +1 -38
- package/src/shared/billing/checkoutPrice.ts +7 -42
- package/src/shared/billing/createCheckout.ts +3 -19
- package/src/shared/billing/firestoreIdempotency.ts +170 -0
- package/src/shared/billing/helpers/subscriptionManagement.ts +1 -1
- package/src/shared/billing/idempotency.ts +8 -225
- package/src/shared/billing/idempotencyContract.ts +0 -21
- package/src/shared/billing/stripeClient.ts +91 -0
- package/src/shared/billing/webhookHandler.ts +5 -5
- package/src/shared/logger.ts +4 -6
- package/src/shared/utils/internal/auth.ts +1 -6
- package/src/shared/utils/internal/validation.ts +0 -18
- package/src/shared/utils.ts +0 -208
- package/src/supabase/billing/cancelSubscription.ts +1 -1
- package/src/supabase/billing/changePlan.ts +1 -1
- package/src/supabase/billing/createCheckoutSession.ts +1 -5
- package/src/supabase/billing/createCustomerPortal.ts +1 -1
- package/src/supabase/billing/refreshSubscriptionStatus.ts +1 -5
- package/src/supabase/billing/stripeWebhook.ts +2 -2
- package/src/supabase/crud/list.ts +2 -0
- package/src/supabase/index.ts +3 -3
- package/src/vercel/api/billing/change-plan.ts +1 -1
- package/src/vercel/api/billing/create-checkout-session.ts +1 -5
- package/src/vercel/api/billing/customer-portal.ts +1 -1
- package/src/vercel/api/billing/refresh-subscription-status.ts +1 -1
- package/src/vercel/api/billing/webhook-handler.ts +1 -1
- package/lib/chunks/chunk-2JL3GDTO.js +0 -151
- package/lib/chunks/chunk-2JL3GDTO.js.map +0 -7
- package/lib/chunks/chunk-4TVLH2W2.js +0 -22
- package/lib/chunks/chunk-5DTPEM64.js +0 -22
- package/lib/chunks/chunk-6CQXHNVL.js +0 -22
- package/lib/chunks/chunk-6CQXHNVL.js.map +0 -7
- package/lib/chunks/chunk-6DSCPZC6.js +0 -22
- package/lib/chunks/chunk-6DSCPZC6.js.map +0 -7
- package/lib/chunks/chunk-7E6TGWHU.js +0 -22
- package/lib/chunks/chunk-7E6TGWHU.js.map +0 -7
- package/lib/chunks/chunk-7LT32OWE.js +0 -22
- package/lib/chunks/chunk-7VH2GJXT.js +0 -151
- package/lib/chunks/chunk-7VH2GJXT.js.map +0 -7
- package/lib/chunks/chunk-API5A4BV.js +0 -22
- package/lib/chunks/chunk-API5A4BV.js.map +0 -7
- package/lib/chunks/chunk-BUP7SXPV.js +0 -23
- package/lib/chunks/chunk-BUP7SXPV.js.map +0 -7
- package/lib/chunks/chunk-BXKRI3JJ.js +0 -22
- package/lib/chunks/chunk-BXKRI3JJ.js.map +0 -7
- package/lib/chunks/chunk-FSLGZD7A.js +0 -21
- package/lib/chunks/chunk-GVLSPWDD.js +0 -151
- package/lib/chunks/chunk-GVLSPWDD.js.map +0 -7
- package/lib/chunks/chunk-GXQVWEFT.js +0 -22
- package/lib/chunks/chunk-GXQVWEFT.js.map +0 -7
- package/lib/chunks/chunk-HB7EGXVI.js +0 -22
- package/lib/chunks/chunk-HB7EGXVI.js.map +0 -7
- package/lib/chunks/chunk-HM7XCMWW.js +0 -22
- package/lib/chunks/chunk-HM7XCMWW.js.map +0 -7
- package/lib/chunks/chunk-I7IOIL6U.js +0 -151
- package/lib/chunks/chunk-I7IOIL6U.js.map +0 -7
- package/lib/chunks/chunk-IFOYVSPU.js.map +0 -7
- package/lib/chunks/chunk-JTAGPPPL.js.map +0 -7
- package/lib/chunks/chunk-MZ232FUE.js +0 -22
- package/lib/chunks/chunk-MZ232FUE.js.map +0 -7
- package/lib/chunks/chunk-NE5ZTUUZ.js +0 -22
- package/lib/chunks/chunk-NE5ZTUUZ.js.map +0 -7
- package/lib/chunks/chunk-NP2AN2TV.js +0 -22
- package/lib/chunks/chunk-NP2AN2TV.js.map +0 -7
- package/lib/chunks/chunk-RM7DV6IQ.js +0 -23
- package/lib/chunks/chunk-RQZGIZHD.js +0 -151
- package/lib/chunks/chunk-RQZGIZHD.js.map +0 -7
- package/lib/chunks/chunk-TBEDQLOB.js +0 -23
- package/lib/chunks/chunk-TBEDQLOB.js.map +0 -7
- package/lib/chunks/chunk-TMHBP3HB.js +0 -22
- package/lib/chunks/chunk-TMHBP3HB.js.map +0 -7
- package/lib/chunks/chunk-UH4L3EF3.js +0 -151
- package/lib/chunks/chunk-UH4L3EF3.js.map +0 -7
- package/lib/chunks/chunk-UJBIU6TL.js +0 -22
- package/lib/chunks/chunk-UJBIU6TL.js.map +0 -7
- package/lib/chunks/chunk-VKEYW3NT.js +0 -22
- package/lib/chunks/chunk-VKEYW3NT.js.map +0 -7
- package/lib/chunks/chunk-XGXBMXJ6.js +0 -22
- package/lib/chunks/chunk-XGXBMXJ6.js.map +0 -7
- package/lib/chunks/chunk-Y44NEQQG.js +0 -22
- package/lib/chunks/chunk-Y44NEQQG.js.map +0 -7
- package/lib/chunks/chunk-ZVU3KPXI.js +0 -22
- package/lib/chunks/chunk-ZVU3KPXI.js.map +0 -7
- package/lib/chunks/monitoring-PVQMFL34.js +0 -22
- package/lib/chunks/monitoring-V7FZXTTW.js +0 -22
- package/lib/chunks/monitoring-V7FZXTTW.js.map +0 -7
- package/lib/chunks/rateLimiter-6KO7AIZP.js.map +0 -7
- package/lib/chunks/rateLimiter-OXVBFW5C.js +0 -22
- package/lib/chunks/rateLimiter-OXVBFW5C.js.map +0 -7
- package/lib/chunks/rateLimiter-U45OMTWR.js +0 -22
- package/lib/chunks/rateLimiter-U45OMTWR.js.map +0 -7
- package/lib/chunks/server-32A5RH4C.js +0 -22
- package/lib/chunks/server-32A5RH4C.js.map +0 -7
- package/lib/chunks/server-7HDAP5DL.js +0 -22
- package/lib/chunks/server-7HDAP5DL.js.map +0 -7
- package/lib/chunks/server-S2IMFOY7.js +0 -22
- package/lib/chunks/server-S2IMFOY7.js.map +0 -7
- 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
- /package/lib/chunks/{chunk-4TVLH2W2.js.map → chunk-2A4FPYNB.js.map} +0 -0
- /package/lib/chunks/{chunk-4U7QQWYG.js.map → chunk-M4JV2JCF.js.map} +0 -0
- /package/lib/chunks/{chunk-7LT32OWE.js.map → chunk-PJCJ5H2O.js.map} +0 -0
- /package/lib/chunks/{chunk-FSLGZD7A.js.map → monitoring-UIDJS2KA.js.map} +0 -0
- /package/lib/chunks/{monitoring-AGCSU3FI.js.map → rateLimiter-WNWHI3B4.js.map} +0 -0
- /package/lib/chunks/{monitoring-PVQMFL34.js.map → server-RYGEBS2B.js.map} +0 -0
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @fileoverview Create Checkout Session Algorithm
|
|
5
5
|
* @description Platform-agnostic algorithm for creating Stripe checkout sessions.
|
|
6
6
|
* StripePayment/StripeSubscription items keep the 0.1.x behaviour (request or
|
|
7
|
-
* back-config priceId
|
|
7
|
+
* back-config priceId). StripeOrder items are priced on
|
|
8
8
|
* the server (`resolveLineItems`) and need a {@link StripeSessionProvider}.
|
|
9
9
|
*
|
|
10
10
|
* @version 0.2.0
|
|
@@ -206,8 +206,8 @@ type CheckoutArgs =
|
|
|
206
206
|
* Create checkout session algorithm
|
|
207
207
|
*
|
|
208
208
|
* 1. Resolve the billing config item and verify the user
|
|
209
|
-
* 2. Single-price items: 0.1.x pricing (request/back-config priceId
|
|
210
|
-
* 3. `beforeCheckout` hook (block/allow
|
|
209
|
+
* 2. Single-price items: 0.1.x pricing (request/back-config priceId)
|
|
210
|
+
* 3. `beforeCheckout` hook (block/allow)
|
|
211
211
|
* 4. StripeOrder: reject inclusive manual Tax Rates, `resolveLineItems` prices every line
|
|
212
212
|
* and may set per-order session options (`ResolvedOrder.session`: tax rates,
|
|
213
213
|
* payment methods, expiry, invoice footer)
|
|
@@ -301,8 +301,6 @@ export async function createCheckoutAlgorithm(
|
|
|
301
301
|
);
|
|
302
302
|
|
|
303
303
|
const allowPromotionCodes = request.allowPromotionCodes ?? true;
|
|
304
|
-
let unitAmount = request.unitAmount;
|
|
305
|
-
let productName = request.productName;
|
|
306
304
|
|
|
307
305
|
if (billingItem.beforeCheckout) {
|
|
308
306
|
const options: BeforeCheckoutOptions = {
|
|
@@ -323,11 +321,6 @@ export async function createCheckoutAlgorithm(
|
|
|
323
321
|
hookResult.reason || 'Checkout blocked by beforeCheckout hook'
|
|
324
322
|
);
|
|
325
323
|
}
|
|
326
|
-
// 0.1.x: overrides apply to dynamic pricing only
|
|
327
|
-
if (plan?.dynamic && hookResult.overrides) {
|
|
328
|
-
unitAmount = hookResult.overrides.unitAmount ?? unitAmount;
|
|
329
|
-
productName = hookResult.overrides.productName ?? productName;
|
|
330
|
-
}
|
|
331
324
|
}
|
|
332
325
|
|
|
333
326
|
const successUrl = resolveRedirectUrl(request.successUrl, 'success');
|
|
@@ -382,17 +375,8 @@ export async function createCheckoutAlgorithm(
|
|
|
382
375
|
assertCheckoutSessionMetadata(params);
|
|
383
376
|
session = await stripeProvider.createStripeSession(params);
|
|
384
377
|
} else {
|
|
385
|
-
const dynamic = !!plan?.dynamic && !!unitAmount && !!productName;
|
|
386
|
-
// 0.1.x: an overridden amount still respects the security cap
|
|
387
|
-
if (dynamic && billingItem.maxUnitAmount && unitAmount! > billingItem.maxUnitAmount) {
|
|
388
|
-
throw new Error(
|
|
389
|
-
`Hook-overridden unit amount ${unitAmount} exceeds maximum allowed: ${billingItem.maxUnitAmount}`
|
|
390
|
-
);
|
|
391
|
-
}
|
|
392
378
|
const params: CheckoutSessionParams = {
|
|
393
379
|
priceId: plan?.priceId,
|
|
394
|
-
unitAmount: dynamic ? unitAmount : undefined,
|
|
395
|
-
productName: dynamic ? productName : undefined,
|
|
396
380
|
currency: request.currency || billingItem.currency || 'eur',
|
|
397
381
|
customerEmail: request.customerEmail,
|
|
398
382
|
metadata,
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
// packages/functions/src/shared/billing/firestoreIdempotency.ts
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @fileoverview Firestore webhook idempotency store
|
|
5
|
+
* @description `webhook_idempotency` collection, transactional reservations.
|
|
6
|
+
* Used by the Firebase and Vercel (Firestore backend) webhook entry points.
|
|
7
|
+
* Re-exported from `idempotency.ts`. Loads `@donotdev/firebase/server` on
|
|
8
|
+
* first use only.
|
|
9
|
+
*
|
|
10
|
+
* @version 0.2.1
|
|
11
|
+
* @since 0.2.0
|
|
12
|
+
* @author AMBROISE PARK Consulting
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import * as v from 'valibot';
|
|
16
|
+
|
|
17
|
+
import { handleError } from '../errorHandling.js';
|
|
18
|
+
import {
|
|
19
|
+
RESERVATION_LEASE_MS,
|
|
20
|
+
ReservationLostError,
|
|
21
|
+
WEBHOOK_EVENT_RETENTION_MS,
|
|
22
|
+
newOwnerToken,
|
|
23
|
+
} from './idempotencyContract.js';
|
|
24
|
+
import type {
|
|
25
|
+
IdempotencyStore,
|
|
26
|
+
ReservationResult,
|
|
27
|
+
} from './idempotencyContract.js';
|
|
28
|
+
|
|
29
|
+
/** Firestore collection holding webhook event reservations (name kept from 0.1.x). */
|
|
30
|
+
const FIRESTORE_COLLECTION = 'webhook_idempotency';
|
|
31
|
+
|
|
32
|
+
/** Millisecond reading of an `expiresAt` value: Timestamp (0.2.x) or number (0.1.x). */
|
|
33
|
+
const ExpiresAtSchema = v.union([
|
|
34
|
+
v.number(),
|
|
35
|
+
v.pipe(
|
|
36
|
+
v.custom<{ toMillis(): number }>(
|
|
37
|
+
(value) =>
|
|
38
|
+
typeof value === 'object' &&
|
|
39
|
+
value !== null &&
|
|
40
|
+
typeof (value as { toMillis?: unknown }).toMillis === 'function',
|
|
41
|
+
'expiresAt must be a Firestore Timestamp or a number'
|
|
42
|
+
),
|
|
43
|
+
v.transform((timestamp) => timestamp.toMillis())
|
|
44
|
+
),
|
|
45
|
+
]);
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Stored reservation document. 0.1.x documents carry no `status` and mean processed.
|
|
49
|
+
*/
|
|
50
|
+
const FirestoreEventDocSchema = v.object({
|
|
51
|
+
status: v.optional(v.picklist(['processing', 'processed'])),
|
|
52
|
+
ownerToken: v.optional(v.string()),
|
|
53
|
+
reservedAt: v.optional(v.number()),
|
|
54
|
+
expiresAt: v.optional(ExpiresAtSchema),
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
function parseEventDoc(eventId: string, data: unknown) {
|
|
58
|
+
const result = v.safeParse(FirestoreEventDocSchema, data);
|
|
59
|
+
if (!result.success) {
|
|
60
|
+
throw new Error(
|
|
61
|
+
`Malformed ${FIRESTORE_COLLECTION}/${eventId}: ${result.issues.map((issue) => issue.message).join('; ')}`
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
return result.output;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Firestore-backed store (transactions). Collection: `webhook_idempotency`.
|
|
69
|
+
*
|
|
70
|
+
* `expiresAt` is a Firestore Timestamp {@link WEBHOOK_EVENT_RETENTION_MS} after
|
|
71
|
+
* the last write: enable a TTL policy on `webhook_idempotency.expiresAt` so
|
|
72
|
+
* Firestore deletes old documents.
|
|
73
|
+
*
|
|
74
|
+
* @version 0.2.0
|
|
75
|
+
* @since 0.2.0
|
|
76
|
+
* @author AMBROISE PARK Consulting
|
|
77
|
+
*/
|
|
78
|
+
export function createFirestoreIdempotencyStore(): IdempotencyStore {
|
|
79
|
+
let firebase: typeof import('@donotdev/firebase/server') | null = null;
|
|
80
|
+
const load = async () => {
|
|
81
|
+
if (!firebase) firebase = await import('@donotdev/firebase/server');
|
|
82
|
+
const firestore = firebase.getFirebaseAdminFirestore();
|
|
83
|
+
return {
|
|
84
|
+
firestore,
|
|
85
|
+
Timestamp: firebase.Timestamp,
|
|
86
|
+
refOf: (eventId: string) =>
|
|
87
|
+
firestore.collection(FIRESTORE_COLLECTION).doc(eventId),
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const reserve = async (eventId: string): Promise<ReservationResult> => {
|
|
92
|
+
const { firestore, Timestamp, refOf } = await load();
|
|
93
|
+
const ref = refOf(eventId);
|
|
94
|
+
try {
|
|
95
|
+
return await firestore.runTransaction(async (tx) => {
|
|
96
|
+
const snapshot = await tx.get(ref);
|
|
97
|
+
const now = Date.now();
|
|
98
|
+
if (snapshot.exists) {
|
|
99
|
+
const data = parseEventDoc(eventId, snapshot.data());
|
|
100
|
+
if (data.status === 'processing') {
|
|
101
|
+
if (now - (data.reservedAt ?? 0) < RESERVATION_LEASE_MS) {
|
|
102
|
+
return { state: 'in_progress' } as const;
|
|
103
|
+
}
|
|
104
|
+
} else if ((data.expiresAt ?? 0) >= now) {
|
|
105
|
+
return { state: 'processed' } as const;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
const ownerToken = newOwnerToken();
|
|
109
|
+
tx.set(ref, {
|
|
110
|
+
eventId,
|
|
111
|
+
status: 'processing',
|
|
112
|
+
ownerToken,
|
|
113
|
+
reservedAt: now,
|
|
114
|
+
expiresAt: Timestamp.fromMillis(now + WEBHOOK_EVENT_RETENTION_MS),
|
|
115
|
+
});
|
|
116
|
+
return { state: 'reserved', ownerToken } as const;
|
|
117
|
+
});
|
|
118
|
+
} catch (error) {
|
|
119
|
+
throw handleError(error);
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
return {
|
|
124
|
+
reserve,
|
|
125
|
+
async markProcessed(eventId, ownerToken) {
|
|
126
|
+
const { firestore, Timestamp, refOf } = await load();
|
|
127
|
+
const ref = refOf(eventId);
|
|
128
|
+
const now = Date.now();
|
|
129
|
+
const processed = {
|
|
130
|
+
eventId,
|
|
131
|
+
status: 'processed',
|
|
132
|
+
processedAt: now,
|
|
133
|
+
expiresAt: Timestamp.fromMillis(now + WEBHOOK_EVENT_RETENTION_MS),
|
|
134
|
+
};
|
|
135
|
+
try {
|
|
136
|
+
await firestore.runTransaction(async (tx) => {
|
|
137
|
+
const snapshot = await tx.get(ref);
|
|
138
|
+
const data = snapshot.exists
|
|
139
|
+
? parseEventDoc(eventId, snapshot.data())
|
|
140
|
+
: null;
|
|
141
|
+
if (data?.ownerToken !== ownerToken) {
|
|
142
|
+
throw new ReservationLostError(eventId);
|
|
143
|
+
}
|
|
144
|
+
tx.set(ref, { ...processed, ownerToken });
|
|
145
|
+
});
|
|
146
|
+
} catch (error) {
|
|
147
|
+
if (error instanceof ReservationLostError) throw error;
|
|
148
|
+
throw handleError(error);
|
|
149
|
+
}
|
|
150
|
+
},
|
|
151
|
+
async release(eventId, ownerToken) {
|
|
152
|
+
const { firestore, refOf } = await load();
|
|
153
|
+
const ref = refOf(eventId);
|
|
154
|
+
try {
|
|
155
|
+
return await firestore.runTransaction(async (tx) => {
|
|
156
|
+
const snapshot = await tx.get(ref);
|
|
157
|
+
if (!snapshot.exists) return false;
|
|
158
|
+
const data = parseEventDoc(eventId, snapshot.data());
|
|
159
|
+
if (data.status !== 'processing' || data.ownerToken !== ownerToken) {
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
tx.delete(ref);
|
|
163
|
+
return true;
|
|
164
|
+
});
|
|
165
|
+
} catch (error) {
|
|
166
|
+
throw handleError(error);
|
|
167
|
+
}
|
|
168
|
+
},
|
|
169
|
+
};
|
|
170
|
+
}
|
|
@@ -16,7 +16,7 @@ import type { BillingAuthProvider } from '@donotdev/core/server';
|
|
|
16
16
|
|
|
17
17
|
import { updateUserSubscription } from './updateUserSubscription.js';
|
|
18
18
|
import { handleError } from '../../errorHandling.js';
|
|
19
|
-
import { stripe, validateStripeEnvironment } from '
|
|
19
|
+
import { stripe, validateStripeEnvironment } from '../stripeClient.js';
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
22
|
* Cancel user subscription at period end
|
|
@@ -4,8 +4,9 @@
|
|
|
4
4
|
* @fileoverview Webhook idempotency stores
|
|
5
5
|
* @description Durable, atomic event reservation for Stripe webhooks. Every
|
|
6
6
|
* webhook entry point passes the store that matches its backend: Firestore
|
|
7
|
-
* (Firebase
|
|
8
|
-
* implicit fallback.
|
|
7
|
+
* (Firebase, Vercel: `firestoreIdempotency.ts`) or Postgres (Supabase:
|
|
8
|
+
* `supabaseIdempotency.ts`). There is no implicit fallback. This module
|
|
9
|
+
* aggregates the stores; backend entry points import their own store file.
|
|
9
10
|
*
|
|
10
11
|
* Lifecycle per event: `reserve` → process → `markProcessed(ownerToken)`, or
|
|
11
12
|
* `release(ownerToken)` when processing failed (so the provider's retry is
|
|
@@ -17,18 +18,13 @@
|
|
|
17
18
|
* @author AMBROISE PARK Consulting
|
|
18
19
|
*/
|
|
19
20
|
|
|
20
|
-
import * as v from 'valibot';
|
|
21
|
-
|
|
22
|
-
import { handleError } from '../errorHandling.js';
|
|
23
|
-
import { isFirestoreConfigured } from '../utils/detectFirestore.js';
|
|
24
21
|
import {
|
|
25
22
|
RESERVATION_LEASE_MS,
|
|
26
23
|
ReservationLostError,
|
|
27
|
-
WEBHOOK_EVENT_RETENTION_MS,
|
|
28
24
|
newOwnerToken,
|
|
29
25
|
} from './idempotencyContract.js';
|
|
30
26
|
import type {
|
|
31
|
-
|
|
27
|
+
IdempotencyStore,
|
|
32
28
|
ReservationResult,
|
|
33
29
|
} from './idempotencyContract.js';
|
|
34
30
|
|
|
@@ -39,12 +35,9 @@ export {
|
|
|
39
35
|
} from './idempotencyContract.js';
|
|
40
36
|
export type {
|
|
41
37
|
IdempotencyStore,
|
|
42
|
-
LegacyIdempotencyStore,
|
|
43
38
|
ReservationResult,
|
|
44
39
|
} from './idempotencyContract.js';
|
|
45
|
-
|
|
46
|
-
/** Firestore collection holding webhook event reservations (name kept from 0.1.x). */
|
|
47
|
-
const FIRESTORE_COLLECTION = 'webhook_idempotency';
|
|
40
|
+
export { createFirestoreIdempotencyStore } from './firestoreIdempotency.js';
|
|
48
41
|
|
|
49
42
|
// =============================================================================
|
|
50
43
|
// In-memory (tests only)
|
|
@@ -57,7 +50,7 @@ const FIRESTORE_COLLECTION = 'webhook_idempotency';
|
|
|
57
50
|
* @since 0.2.0
|
|
58
51
|
* @author AMBROISE PARK Consulting
|
|
59
52
|
*/
|
|
60
|
-
export function createInMemoryIdempotencyStore():
|
|
53
|
+
export function createInMemoryIdempotencyStore(): IdempotencyStore {
|
|
61
54
|
const events = new Map<
|
|
62
55
|
string,
|
|
63
56
|
{ status: 'processing' | 'processed'; ownerToken: string; at: number }
|
|
@@ -75,17 +68,14 @@ export function createInMemoryIdempotencyStore(): LegacyIdempotencyStore {
|
|
|
75
68
|
};
|
|
76
69
|
return {
|
|
77
70
|
reserve,
|
|
78
|
-
async checkAndReserve(eventId) {
|
|
79
|
-
return (await reserve(eventId)).state !== 'reserved';
|
|
80
|
-
},
|
|
81
71
|
async markProcessed(eventId, ownerToken) {
|
|
82
72
|
const entry = events.get(eventId);
|
|
83
|
-
if (
|
|
73
|
+
if (entry?.ownerToken !== ownerToken) {
|
|
84
74
|
throw new ReservationLostError(eventId);
|
|
85
75
|
}
|
|
86
76
|
events.set(eventId, {
|
|
87
77
|
status: 'processed',
|
|
88
|
-
ownerToken
|
|
78
|
+
ownerToken,
|
|
89
79
|
at: Date.now(),
|
|
90
80
|
});
|
|
91
81
|
},
|
|
@@ -97,214 +87,7 @@ export function createInMemoryIdempotencyStore(): LegacyIdempotencyStore {
|
|
|
97
87
|
events.delete(eventId);
|
|
98
88
|
return true;
|
|
99
89
|
},
|
|
100
|
-
async isProcessed(eventId) {
|
|
101
|
-
return events.get(eventId)?.status === 'processed';
|
|
102
|
-
},
|
|
103
|
-
};
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
// =============================================================================
|
|
107
|
-
// Firestore
|
|
108
|
-
// =============================================================================
|
|
109
|
-
|
|
110
|
-
/** Millisecond reading of an `expiresAt` value: Timestamp (0.2.x) or number (0.1.x). */
|
|
111
|
-
const ExpiresAtSchema = v.union([
|
|
112
|
-
v.number(),
|
|
113
|
-
v.pipe(
|
|
114
|
-
v.custom<{ toMillis(): number }>(
|
|
115
|
-
(value) =>
|
|
116
|
-
typeof value === 'object' &&
|
|
117
|
-
value !== null &&
|
|
118
|
-
typeof (value as { toMillis?: unknown }).toMillis === 'function',
|
|
119
|
-
'expiresAt must be a Firestore Timestamp or a number'
|
|
120
|
-
),
|
|
121
|
-
v.transform((timestamp) => timestamp.toMillis())
|
|
122
|
-
),
|
|
123
|
-
]);
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Stored reservation document. 0.1.x documents carry no `status` and mean processed.
|
|
127
|
-
*/
|
|
128
|
-
const FirestoreEventDocSchema = v.object({
|
|
129
|
-
status: v.optional(v.picklist(['processing', 'processed'])),
|
|
130
|
-
ownerToken: v.optional(v.string()),
|
|
131
|
-
reservedAt: v.optional(v.number()),
|
|
132
|
-
expiresAt: v.optional(ExpiresAtSchema),
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
function parseEventDoc(eventId: string, data: unknown) {
|
|
136
|
-
const result = v.safeParse(FirestoreEventDocSchema, data);
|
|
137
|
-
if (!result.success) {
|
|
138
|
-
throw new Error(
|
|
139
|
-
`Malformed ${FIRESTORE_COLLECTION}/${eventId}: ${result.issues.map((issue) => issue.message).join('; ')}`
|
|
140
|
-
);
|
|
141
|
-
}
|
|
142
|
-
return result.output;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* Firestore-backed store (transactions). Collection: `webhook_idempotency`.
|
|
147
|
-
*
|
|
148
|
-
* `expiresAt` is a Firestore Timestamp {@link WEBHOOK_EVENT_RETENTION_MS} after
|
|
149
|
-
* the last write: enable a TTL policy on `webhook_idempotency.expiresAt` so
|
|
150
|
-
* Firestore deletes old documents.
|
|
151
|
-
*
|
|
152
|
-
* @version 0.2.0
|
|
153
|
-
* @since 0.2.0
|
|
154
|
-
* @author AMBROISE PARK Consulting
|
|
155
|
-
*/
|
|
156
|
-
export function createFirestoreIdempotencyStore(): LegacyIdempotencyStore {
|
|
157
|
-
let firebase: typeof import('@donotdev/firebase/server') | null = null;
|
|
158
|
-
const load = async () => {
|
|
159
|
-
if (!firebase) firebase = await import('@donotdev/firebase/server');
|
|
160
|
-
const firestore = firebase.getFirebaseAdminFirestore();
|
|
161
|
-
return {
|
|
162
|
-
firestore,
|
|
163
|
-
Timestamp: firebase.Timestamp,
|
|
164
|
-
refOf: (eventId: string) =>
|
|
165
|
-
firestore.collection(FIRESTORE_COLLECTION).doc(eventId),
|
|
166
|
-
};
|
|
167
|
-
};
|
|
168
|
-
|
|
169
|
-
const reserve = async (eventId: string): Promise<ReservationResult> => {
|
|
170
|
-
const { firestore, Timestamp, refOf } = await load();
|
|
171
|
-
const ref = refOf(eventId);
|
|
172
|
-
try {
|
|
173
|
-
return await firestore.runTransaction(async (tx) => {
|
|
174
|
-
const snapshot = await tx.get(ref);
|
|
175
|
-
const now = Date.now();
|
|
176
|
-
if (snapshot.exists) {
|
|
177
|
-
const data = parseEventDoc(eventId, snapshot.data());
|
|
178
|
-
if (data.status === 'processing') {
|
|
179
|
-
if (now - (data.reservedAt ?? 0) < RESERVATION_LEASE_MS) {
|
|
180
|
-
return { state: 'in_progress' } as const;
|
|
181
|
-
}
|
|
182
|
-
} else if ((data.expiresAt ?? 0) >= now) {
|
|
183
|
-
return { state: 'processed' } as const;
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
const ownerToken = newOwnerToken();
|
|
187
|
-
tx.set(ref, {
|
|
188
|
-
eventId,
|
|
189
|
-
status: 'processing',
|
|
190
|
-
ownerToken,
|
|
191
|
-
reservedAt: now,
|
|
192
|
-
expiresAt: Timestamp.fromMillis(now + WEBHOOK_EVENT_RETENTION_MS),
|
|
193
|
-
});
|
|
194
|
-
return { state: 'reserved', ownerToken } as const;
|
|
195
|
-
});
|
|
196
|
-
} catch (error) {
|
|
197
|
-
throw handleError(error);
|
|
198
|
-
}
|
|
199
90
|
};
|
|
200
|
-
|
|
201
|
-
return {
|
|
202
|
-
reserve,
|
|
203
|
-
async checkAndReserve(eventId) {
|
|
204
|
-
return (await reserve(eventId)).state !== 'reserved';
|
|
205
|
-
},
|
|
206
|
-
async markProcessed(eventId, ownerToken) {
|
|
207
|
-
const { firestore, Timestamp, refOf } = await load();
|
|
208
|
-
const ref = refOf(eventId);
|
|
209
|
-
const now = Date.now();
|
|
210
|
-
const processed = {
|
|
211
|
-
eventId,
|
|
212
|
-
status: 'processed',
|
|
213
|
-
processedAt: now,
|
|
214
|
-
expiresAt: Timestamp.fromMillis(now + WEBHOOK_EVENT_RETENTION_MS),
|
|
215
|
-
};
|
|
216
|
-
try {
|
|
217
|
-
if (ownerToken === undefined) {
|
|
218
|
-
// 0.1.x call: unconditional write
|
|
219
|
-
await ref.set(processed);
|
|
220
|
-
return;
|
|
221
|
-
}
|
|
222
|
-
await firestore.runTransaction(async (tx) => {
|
|
223
|
-
const snapshot = await tx.get(ref);
|
|
224
|
-
const data = snapshot.exists
|
|
225
|
-
? parseEventDoc(eventId, snapshot.data())
|
|
226
|
-
: null;
|
|
227
|
-
if (data?.ownerToken !== ownerToken) {
|
|
228
|
-
throw new ReservationLostError(eventId);
|
|
229
|
-
}
|
|
230
|
-
tx.set(ref, { ...processed, ownerToken });
|
|
231
|
-
});
|
|
232
|
-
} catch (error) {
|
|
233
|
-
if (error instanceof ReservationLostError) throw error;
|
|
234
|
-
throw handleError(error);
|
|
235
|
-
}
|
|
236
|
-
},
|
|
237
|
-
async release(eventId, ownerToken) {
|
|
238
|
-
const { firestore, refOf } = await load();
|
|
239
|
-
const ref = refOf(eventId);
|
|
240
|
-
try {
|
|
241
|
-
return await firestore.runTransaction(async (tx) => {
|
|
242
|
-
const snapshot = await tx.get(ref);
|
|
243
|
-
if (!snapshot.exists) return false;
|
|
244
|
-
const data = parseEventDoc(eventId, snapshot.data());
|
|
245
|
-
if (data.status !== 'processing' || data.ownerToken !== ownerToken) {
|
|
246
|
-
return false;
|
|
247
|
-
}
|
|
248
|
-
tx.delete(ref);
|
|
249
|
-
return true;
|
|
250
|
-
});
|
|
251
|
-
} catch (error) {
|
|
252
|
-
throw handleError(error);
|
|
253
|
-
}
|
|
254
|
-
},
|
|
255
|
-
async isProcessed(eventId) {
|
|
256
|
-
const { refOf } = await load();
|
|
257
|
-
try {
|
|
258
|
-
const snapshot = await refOf(eventId).get();
|
|
259
|
-
if (!snapshot.exists) return false;
|
|
260
|
-
const data = parseEventDoc(eventId, snapshot.data());
|
|
261
|
-
return data.status !== 'processing' && (data.expiresAt ?? 0) >= Date.now();
|
|
262
|
-
} catch (error) {
|
|
263
|
-
throw handleError(error);
|
|
264
|
-
}
|
|
265
|
-
},
|
|
266
|
-
};
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
// =============================================================================
|
|
270
|
-
// 0.1.x auto-detection (deprecated)
|
|
271
|
-
// =============================================================================
|
|
272
|
-
|
|
273
|
-
let cachedStore: LegacyIdempotencyStore | null = null;
|
|
274
|
-
|
|
275
|
-
/**
|
|
276
|
-
* 0.1.x store auto-detection: Firestore when configured, else in-memory.
|
|
277
|
-
*
|
|
278
|
-
* @deprecated In-memory is not durable across instances. Pass an explicit store:
|
|
279
|
-
* {@link createFirestoreIdempotencyStore} or `createSupabaseIdempotencyStore`.
|
|
280
|
-
* Framework webhook entry points already do.
|
|
281
|
-
* @version 0.2.0
|
|
282
|
-
* @since 0.0.1
|
|
283
|
-
* @author AMBROISE PARK Consulting
|
|
284
|
-
*/
|
|
285
|
-
export function createIdempotencyStore(): LegacyIdempotencyStore {
|
|
286
|
-
if (cachedStore) return cachedStore;
|
|
287
|
-
if (isFirestoreConfigured()) {
|
|
288
|
-
cachedStore = createFirestoreIdempotencyStore();
|
|
289
|
-
} else {
|
|
290
|
-
console.warn(
|
|
291
|
-
'[Idempotency] Firestore not configured, using in-memory (not durable). Pass an explicit store.'
|
|
292
|
-
);
|
|
293
|
-
cachedStore = createInMemoryIdempotencyStore();
|
|
294
|
-
}
|
|
295
|
-
return cachedStore;
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
/**
|
|
299
|
-
* Reset the store cached by {@link createIdempotencyStore} (tests).
|
|
300
|
-
*
|
|
301
|
-
* @deprecated See {@link createIdempotencyStore}.
|
|
302
|
-
* @version 0.1.0
|
|
303
|
-
* @since 0.0.1
|
|
304
|
-
* @author AMBROISE PARK Consulting
|
|
305
|
-
*/
|
|
306
|
-
export function resetIdempotencyStore() {
|
|
307
|
-
cachedStore = null;
|
|
308
91
|
}
|
|
309
92
|
|
|
310
93
|
export {
|
|
@@ -49,27 +49,6 @@ export interface IdempotencyStore {
|
|
|
49
49
|
release(eventId: string, ownerToken: string): Promise<boolean>;
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
/**
|
|
53
|
-
* Store with the 0.1.x accessors.
|
|
54
|
-
*
|
|
55
|
-
* @deprecated Use {@link IdempotencyStore}: `reserve` returns three states and
|
|
56
|
-
* an owner token; `checkAndReserve` and `isProcessed` do not.
|
|
57
|
-
* @version 0.2.0
|
|
58
|
-
* @since 0.2.0
|
|
59
|
-
* @author AMBROISE PARK Consulting
|
|
60
|
-
*/
|
|
61
|
-
export interface LegacyIdempotencyStore extends IdempotencyStore {
|
|
62
|
-
/** 0.1.x: `true` when the event must be skipped (processed or live lease). */
|
|
63
|
-
checkAndReserve(eventId: string): Promise<boolean>;
|
|
64
|
-
/** Whether the event was processed (non-atomic read). */
|
|
65
|
-
isProcessed(eventId: string): Promise<boolean>;
|
|
66
|
-
/**
|
|
67
|
-
* Mark processed. Without `ownerToken` (0.1.x call) the write is unconditional.
|
|
68
|
-
* @throws {ReservationLostError} When `ownerToken` is given and no longer owns the event
|
|
69
|
-
*/
|
|
70
|
-
markProcessed(eventId: string, ownerToken?: string): Promise<void>;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
52
|
/**
|
|
74
53
|
* Thrown by `markProcessed` when the caller no longer owns the reservation:
|
|
75
54
|
* its lease expired and another delivery retook the event, or the row is gone.
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// packages/functions/src/shared/billing/stripeClient.ts
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @fileoverview Stripe client for billing functions
|
|
5
|
+
* @description Lazily initialized Stripe SDK instance shared by the Firebase,
|
|
6
|
+
* Vercel and Supabase billing handlers. Kept apart from `shared/utils.ts` so
|
|
7
|
+
* code that does not bill (CRUD, auth) never loads the Stripe SDK.
|
|
8
|
+
*
|
|
9
|
+
* @version 0.2.1
|
|
10
|
+
* @since 0.0.1
|
|
11
|
+
* @author AMBROISE PARK Consulting
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import Stripe from 'stripe';
|
|
15
|
+
|
|
16
|
+
import { STRIPE_API_VERSION_PIN } from './apiVersion.js';
|
|
17
|
+
|
|
18
|
+
// Lazy initialization of Stripe
|
|
19
|
+
let _stripe: Stripe | null = null;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Initialize Stripe explicitly (e.g. with a secret from defineSecret).
|
|
23
|
+
* API version: `STRIPE_API_VERSION`, else {@link STRIPE_API_VERSION_PIN}.
|
|
24
|
+
* @param apiKey - Stripe secret key
|
|
25
|
+
*
|
|
26
|
+
* @version 0.2.0
|
|
27
|
+
* @since 0.0.1
|
|
28
|
+
* @author AMBROISE PARK Consulting
|
|
29
|
+
*/
|
|
30
|
+
export function initStripe(apiKey: string) {
|
|
31
|
+
if (!apiKey) throw new Error('Stripe API key is required');
|
|
32
|
+
|
|
33
|
+
// Unset: the framework pin (pre-basil fields are still read by subscription handlers)
|
|
34
|
+
const apiVersion = process.env.STRIPE_API_VERSION || STRIPE_API_VERSION_PIN;
|
|
35
|
+
|
|
36
|
+
_stripe = new Stripe(apiKey, {
|
|
37
|
+
apiVersion: apiVersion as any,
|
|
38
|
+
});
|
|
39
|
+
return _stripe;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Get Stripe instance with lazy initialization
|
|
44
|
+
*
|
|
45
|
+
* @version 0.1.0
|
|
46
|
+
* @since 0.0.1
|
|
47
|
+
* @author AMBROISE PARK Consulting
|
|
48
|
+
*/
|
|
49
|
+
export function getStripe(): Stripe {
|
|
50
|
+
if (!_stripe) {
|
|
51
|
+
// Legacy/Vercel fallback: Try process.env
|
|
52
|
+
const STRIPE_SECRET_KEY = process.env.STRIPE_SECRET_KEY;
|
|
53
|
+
if (STRIPE_SECRET_KEY) {
|
|
54
|
+
initStripe(STRIPE_SECRET_KEY);
|
|
55
|
+
} else {
|
|
56
|
+
throw new Error(
|
|
57
|
+
'Stripe not initialized. Call initStripe() or set STRIPE_SECRET_KEY env var.'
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
// We know _stripe is set here or we threw
|
|
62
|
+
return _stripe!;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Lazy-initialized Stripe instance proxy
|
|
67
|
+
*
|
|
68
|
+
* @version 0.1.0
|
|
69
|
+
* @since 0.0.1
|
|
70
|
+
* @author AMBROISE PARK Consulting
|
|
71
|
+
*/
|
|
72
|
+
export const stripe = new Proxy({} as Stripe, {
|
|
73
|
+
get(target, prop) {
|
|
74
|
+
return getStripe()[prop as keyof Stripe];
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Validates that required Stripe environment variables are set
|
|
80
|
+
*
|
|
81
|
+
* @version 0.1.0
|
|
82
|
+
* @since 0.0.1
|
|
83
|
+
* @author AMBROISE PARK Consulting
|
|
84
|
+
*/
|
|
85
|
+
export function validateStripeEnvironment(): void {
|
|
86
|
+
// Functions v2 automatically injects secrets as process.env when declared in config
|
|
87
|
+
const STRIPE_SECRET_KEY = process.env.STRIPE_SECRET_KEY;
|
|
88
|
+
if (!STRIPE_SECRET_KEY) {
|
|
89
|
+
throw new Error('Missing STRIPE_SECRET_KEY environment variable');
|
|
90
|
+
}
|
|
91
|
+
}
|
|
@@ -18,7 +18,6 @@ import type { BillingAuthProvider, StripeBackConfig, UpdateSubscriptionFn } from
|
|
|
18
18
|
|
|
19
19
|
import { handleError } from '../errorHandling.js';
|
|
20
20
|
import { logger } from '../logger.js';
|
|
21
|
-
import { createIdempotencyStore } from './idempotency.js';
|
|
22
21
|
import { ReservationLostError } from './idempotencyContract.js';
|
|
23
22
|
import type { IdempotencyStore } from './idempotencyContract.js';
|
|
24
23
|
import { handleOrderSessionEvent } from './orderWebhook.js';
|
|
@@ -56,11 +55,12 @@ const MARK_PROCESSED_BACKOFF_MS = 200;
|
|
|
56
55
|
* @param billingConfig - Billing configuration
|
|
57
56
|
* @param updateSubscription - Function to update user subscription
|
|
58
57
|
* @param authProvider - Auth provider for subscription claims (null: subscription events fail)
|
|
59
|
-
* @param idempotencyStore - Durable store matching the backend
|
|
60
|
-
*
|
|
58
|
+
* @param idempotencyStore - Durable store matching the backend
|
|
59
|
+
* (`createFirestoreIdempotencyStore` / `createSupabaseIdempotencyStore`). Required:
|
|
60
|
+
* shared logic never picks a backend.
|
|
61
61
|
* @throws Any failure: the caller must answer non-2xx so Stripe retries
|
|
62
62
|
*
|
|
63
|
-
* @version 0.2.
|
|
63
|
+
* @version 0.2.1
|
|
64
64
|
* @since 0.0.1
|
|
65
65
|
* @author AMBROISE PARK Consulting
|
|
66
66
|
*/
|
|
@@ -72,7 +72,7 @@ export async function processWebhook(
|
|
|
72
72
|
billingConfig: StripeBackConfig,
|
|
73
73
|
updateSubscription: UpdateSubscriptionFn,
|
|
74
74
|
authProvider: BillingAuthProvider | null,
|
|
75
|
-
idempotencyStore: IdempotencyStore
|
|
75
|
+
idempotencyStore: IdempotencyStore
|
|
76
76
|
): Promise<{ success: boolean; message: string }> {
|
|
77
77
|
try {
|
|
78
78
|
// Validate data fields only. Valibot strips unknown keys (the hooks) from its
|
package/src/shared/logger.ts
CHANGED
|
@@ -23,12 +23,10 @@
|
|
|
23
23
|
* @author AMBROISE PARK Consulting
|
|
24
24
|
*/
|
|
25
25
|
|
|
26
|
-
//
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
// ServerShim: Warn if not in Node.js (e.g., Deno) but don't throw — allow fallback logging
|
|
26
|
+
// Node-only metadata (pid, version) is added when running on Node; Deno runtimes
|
|
27
|
+
// (Supabase Edge) log without it. Server-only by package: @donotdev/functions is
|
|
28
|
+
// never imported by client code, so no runtime browser check (Supabase Edge
|
|
29
|
+
// defines `window`, which made such a check throw on every edge function).
|
|
32
30
|
const _isNodeEnv = typeof process !== 'undefined' && !!process.versions?.node;
|
|
33
31
|
|
|
34
32
|
let sentryEnabled = false;
|