@vandenberghinc/volt 1.2.6 → 1.2.7
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/backend/dist/cjs/backend/src/blacklist.d.ts +12 -0
- package/backend/dist/cjs/backend/src/blacklist.js +78 -0
- package/backend/dist/cjs/backend/src/cli.d.ts +2 -0
- package/backend/dist/cjs/backend/src/cli.js +198 -0
- package/backend/dist/cjs/backend/src/database/collection.d.ts +1765 -0
- package/backend/dist/cjs/backend/src/database/collection.js +3301 -0
- package/backend/dist/cjs/backend/src/database/database.d.ts +92 -0
- package/backend/dist/cjs/backend/src/database/database.js +170 -0
- package/backend/dist/cjs/backend/src/database/document.d.ts +1 -0
- package/backend/dist/cjs/backend/src/database/document.js +15 -0
- package/backend/dist/cjs/backend/src/database/filters/filters.d.ts +6 -0
- package/backend/dist/cjs/backend/src/database/filters/filters.js +15 -0
- package/backend/dist/cjs/backend/src/database/filters/strict_filter.d.ts +223 -0
- package/backend/dist/cjs/backend/src/database/filters/strict_filter.js +15 -0
- package/backend/dist/cjs/backend/src/database/filters/strict_filter_test.d.ts +1 -0
- package/backend/dist/cjs/backend/src/database/filters/strict_filter_test.js +443 -0
- package/backend/dist/cjs/backend/src/database/filters/strict_filter_test_v0.d.ts +1 -0
- package/backend/dist/cjs/backend/src/database/filters/strict_filter_test_v0.js +15 -0
- package/backend/dist/cjs/backend/src/database/filters/strict_filter_v0.d.ts +50 -0
- package/backend/dist/cjs/backend/src/database/filters/strict_filter_v0.js +15 -0
- package/backend/dist/cjs/backend/src/database/filters/strict_filter_v1.d.ts +76 -0
- package/backend/dist/cjs/backend/src/database/filters/strict_filter_v1.js +15 -0
- package/backend/dist/cjs/backend/src/database/filters/strict_filter_v2.d.ts +75 -0
- package/backend/dist/cjs/backend/src/database/filters/strict_filter_v2.js +15 -0
- package/backend/dist/cjs/backend/src/database/filters/strict_filter_v3.d.ts +219 -0
- package/backend/dist/cjs/backend/src/database/filters/strict_filter_v3.js +15 -0
- package/backend/dist/cjs/backend/src/database/filters/strict_update_filter.d.ts +165 -0
- package/backend/dist/cjs/backend/src/database/filters/strict_update_filter.js +15 -0
- package/backend/dist/cjs/backend/src/database/filters/strict_update_filter_test.d.ts +5 -0
- package/backend/dist/cjs/backend/src/database/filters/strict_update_filter_test.js +355 -0
- package/backend/dist/cjs/backend/src/database/flatten.d.ts +78 -0
- package/backend/dist/cjs/backend/src/database/flatten.js +53 -0
- package/backend/dist/cjs/backend/src/database/flatten_test.d.ts +1 -0
- package/backend/dist/cjs/backend/src/database/flatten_test.js +175 -0
- package/backend/dist/cjs/backend/src/database/quota/quoata_v2.d.ts +533 -0
- package/backend/dist/cjs/backend/src/database/quota/quoata_v2.js +1046 -0
- package/backend/dist/cjs/backend/src/database/quota/quota.d.ts +551 -0
- package/backend/dist/cjs/backend/src/database/quota/quota.js +1108 -0
- package/backend/dist/cjs/backend/src/database/quota/quota_v1.d.ts +534 -0
- package/backend/dist/cjs/backend/src/database/quota/quota_v1.js +1087 -0
- package/backend/dist/cjs/backend/src/database/quota/safe_int.d.ts +412 -0
- package/backend/dist/cjs/backend/src/database/quota/safe_int.js +745 -0
- package/backend/dist/cjs/backend/src/endpoint.d.ts +346 -0
- package/backend/dist/cjs/backend/src/endpoint.js +468 -0
- package/backend/dist/cjs/backend/src/errors/index.d.ts +7 -0
- package/backend/dist/cjs/backend/src/errors/index.js +25 -0
- package/backend/dist/cjs/backend/src/errors/internal_external.d.ts +52 -0
- package/backend/dist/cjs/backend/src/errors/internal_external.js +95 -0
- package/backend/dist/cjs/backend/src/errors/invalid_usage_error.d.ts +41 -0
- package/backend/dist/cjs/backend/src/errors/invalid_usage_error.js +47 -0
- package/backend/dist/cjs/backend/src/errors/system_error.d.ts +261 -0
- package/backend/dist/cjs/backend/src/errors/system_error.js +436 -0
- package/backend/dist/cjs/backend/src/events.d.ts +97 -0
- package/backend/dist/cjs/backend/src/events.js +15 -0
- package/backend/dist/cjs/backend/src/frontend.d.ts +13 -0
- package/backend/dist/cjs/backend/src/frontend.js +56 -0
- package/backend/dist/cjs/backend/src/image_endpoint.d.ts +44 -0
- package/backend/dist/cjs/backend/src/image_endpoint.js +185 -0
- package/backend/dist/cjs/backend/src/index.d.ts +23 -0
- package/backend/dist/cjs/backend/src/index.js +70 -0
- package/backend/dist/cjs/backend/src/logger.d.ts +5 -0
- package/backend/dist/cjs/backend/src/logger.js +15 -0
- package/backend/dist/cjs/backend/src/meta.d.ts +112 -0
- package/backend/dist/cjs/backend/src/meta.js +181 -0
- package/backend/dist/cjs/backend/src/payments/paddle.d.ts +329 -0
- package/backend/dist/cjs/backend/src/payments/paddle.js +1996 -0
- package/backend/dist/cjs/backend/src/payments/stripe/checkout.d.ts +113 -0
- package/backend/dist/cjs/backend/src/payments/stripe/checkout.js +295 -0
- package/backend/dist/cjs/backend/src/payments/stripe/customers.d.ts +17 -0
- package/backend/dist/cjs/backend/src/payments/stripe/customers.js +164 -0
- package/backend/dist/cjs/backend/src/payments/stripe/error.d.ts +74 -0
- package/backend/dist/cjs/backend/src/payments/stripe/error.js +64 -0
- package/backend/dist/cjs/backend/src/payments/stripe/events.d.ts +155 -0
- package/backend/dist/cjs/backend/src/payments/stripe/events.js +15 -0
- package/backend/dist/cjs/backend/src/payments/stripe/meters.d.ts +105 -0
- package/backend/dist/cjs/backend/src/payments/stripe/meters.js +230 -0
- package/backend/dist/cjs/backend/src/payments/stripe/payment_methods.d.ts +58 -0
- package/backend/dist/cjs/backend/src/payments/stripe/payment_methods.js +109 -0
- package/backend/dist/cjs/backend/src/payments/stripe/products.d.ts +519 -0
- package/backend/dist/cjs/backend/src/payments/stripe/products.js +650 -0
- package/backend/dist/cjs/backend/src/payments/stripe/stripe.d.ts +215 -0
- package/backend/dist/cjs/backend/src/payments/stripe/stripe.js +468 -0
- package/backend/dist/cjs/backend/src/payments/stripe/subscriptions.d.ts +172 -0
- package/backend/dist/cjs/backend/src/payments/stripe/subscriptions.js +557 -0
- package/backend/dist/cjs/backend/src/payments/stripe/utils.d.ts +63 -0
- package/backend/dist/cjs/backend/src/payments/stripe/utils.js +118 -0
- package/backend/dist/cjs/backend/src/payments/stripe/webhooks.d.ts +105 -0
- package/backend/dist/cjs/backend/src/payments/stripe/webhooks.js +627 -0
- package/backend/dist/cjs/backend/src/plugins/browser.d.ts +1 -0
- package/backend/dist/cjs/backend/src/plugins/browser.js +15 -0
- package/backend/dist/cjs/backend/src/plugins/communication.d.ts +70 -0
- package/backend/dist/cjs/backend/src/plugins/communication.js +196 -0
- package/backend/dist/cjs/backend/src/plugins/mail/mail.d.ts +255 -0
- package/backend/dist/cjs/backend/src/plugins/mail/mail.js +381 -0
- package/backend/dist/cjs/backend/src/plugins/mail/ui.d.ts +297 -0
- package/backend/dist/cjs/backend/src/plugins/mail/ui.js +1370 -0
- package/backend/dist/cjs/backend/src/plugins/pdf.d.ts +1 -0
- package/backend/dist/cjs/backend/src/plugins/pdf.js +1456 -0
- package/backend/dist/cjs/backend/src/plugins/thread_monitor.d.ts +18 -0
- package/backend/dist/cjs/backend/src/plugins/thread_monitor.js +116 -0
- package/backend/dist/cjs/backend/src/rate_limit.d.ts +148 -0
- package/backend/dist/cjs/backend/src/rate_limit.js +543 -0
- package/backend/dist/cjs/backend/src/route.d.ts +39 -0
- package/backend/dist/cjs/backend/src/route.js +172 -0
- package/backend/dist/cjs/backend/src/server.d.ts +502 -0
- package/backend/dist/cjs/backend/src/server.js +1710 -0
- package/backend/dist/cjs/backend/src/server.old.d.ts +594 -0
- package/backend/dist/cjs/backend/src/server.old.js +2058 -0
- package/backend/dist/cjs/backend/src/splash_screen.d.ts +93 -0
- package/backend/dist/cjs/backend/src/splash_screen.js +119 -0
- package/backend/dist/cjs/backend/src/status.d.ts +89 -0
- package/backend/dist/cjs/backend/src/status.js +211 -0
- package/backend/dist/cjs/backend/src/stream.d.ts +494 -0
- package/backend/dist/cjs/backend/src/stream.js +1370 -0
- package/backend/dist/cjs/backend/src/users.d.ts +926 -0
- package/backend/dist/cjs/backend/src/users.js +2223 -0
- package/backend/dist/cjs/backend/src/utils.d.ts +22 -0
- package/backend/dist/cjs/backend/src/utils.js +626 -0
- package/backend/dist/cjs/backend/src/view.d.ts +115 -0
- package/backend/dist/cjs/backend/src/view.js +519 -0
- package/backend/dist/cjs/backend/src/vinc.d.ts +6 -0
- package/backend/dist/cjs/backend/src/vinc.js +40 -0
- package/backend/dist/cjs/backend/src/volt.d.ts +24 -0
- package/backend/dist/cjs/backend/src/volt.js +72 -0
- package/backend/dist/cjs/frontend/src/modules/request.d.ts +70 -0
- package/backend/dist/cjs/frontend/src/modules/request.js +99 -0
- package/backend/dist/cjs/package.json +1 -0
- package/backend/dist/esm/backend/src/blacklist.d.ts +12 -0
- package/backend/dist/esm/backend/src/blacklist.js +52 -0
- package/backend/dist/esm/backend/src/cli.d.ts +2 -0
- package/backend/dist/esm/backend/src/cli.js +211 -0
- package/backend/dist/esm/backend/src/database/collection.d.ts +1765 -0
- package/backend/dist/esm/backend/src/database/collection.js +3779 -0
- package/backend/dist/esm/backend/src/database/database.d.ts +92 -0
- package/backend/dist/esm/backend/src/database/database.js +214 -0
- package/backend/dist/esm/backend/src/database/document.d.ts +1 -0
- package/backend/dist/esm/backend/src/database/document.js +558 -0
- package/backend/dist/esm/backend/src/database/filters/filters.d.ts +6 -0
- package/backend/dist/esm/backend/src/database/filters/filters.js +1 -0
- package/backend/dist/esm/backend/src/database/filters/strict_filter.d.ts +223 -0
- package/backend/dist/esm/backend/src/database/filters/strict_filter.js +3 -0
- package/backend/dist/esm/backend/src/database/filters/strict_filter_test.d.ts +1 -0
- package/backend/dist/esm/backend/src/database/filters/strict_filter_test.js +505 -0
- package/backend/dist/esm/backend/src/database/filters/strict_filter_test_v0.d.ts +1 -0
- package/backend/dist/esm/backend/src/database/filters/strict_filter_test_v0.js +712 -0
- package/backend/dist/esm/backend/src/database/filters/strict_filter_v0.d.ts +50 -0
- package/backend/dist/esm/backend/src/database/filters/strict_filter_v0.js +5 -0
- package/backend/dist/esm/backend/src/database/filters/strict_filter_v1.d.ts +76 -0
- package/backend/dist/esm/backend/src/database/filters/strict_filter_v1.js +44 -0
- package/backend/dist/esm/backend/src/database/filters/strict_filter_v2.d.ts +75 -0
- package/backend/dist/esm/backend/src/database/filters/strict_filter_v2.js +5 -0
- package/backend/dist/esm/backend/src/database/filters/strict_filter_v3.d.ts +219 -0
- package/backend/dist/esm/backend/src/database/filters/strict_filter_v3.js +1 -0
- package/backend/dist/esm/backend/src/database/filters/strict_update_filter.d.ts +165 -0
- package/backend/dist/esm/backend/src/database/filters/strict_update_filter.js +5 -0
- package/backend/dist/esm/backend/src/database/filters/strict_update_filter_test.d.ts +5 -0
- package/backend/dist/esm/backend/src/database/filters/strict_update_filter_test.js +415 -0
- package/backend/dist/esm/backend/src/database/flatten.d.ts +78 -0
- package/backend/dist/esm/backend/src/database/flatten.js +22 -0
- package/backend/dist/esm/backend/src/database/flatten_test.d.ts +1 -0
- package/backend/dist/esm/backend/src/database/flatten_test.js +174 -0
- package/backend/dist/esm/backend/src/database/quota/quoata_v2.d.ts +533 -0
- package/backend/dist/esm/backend/src/database/quota/quoata_v2.js +1155 -0
- package/backend/dist/esm/backend/src/database/quota/quota.d.ts +551 -0
- package/backend/dist/esm/backend/src/database/quota/quota.js +1219 -0
- package/backend/dist/esm/backend/src/database/quota/quota_v1.d.ts +534 -0
- package/backend/dist/esm/backend/src/database/quota/quota_v1.js +1242 -0
- package/backend/dist/esm/backend/src/database/quota/safe_int.d.ts +412 -0
- package/backend/dist/esm/backend/src/database/quota/safe_int.js +810 -0
- package/backend/dist/esm/backend/src/endpoint.d.ts +346 -0
- package/backend/dist/esm/backend/src/endpoint.js +479 -0
- package/backend/dist/esm/backend/src/errors/index.d.ts +7 -0
- package/backend/dist/esm/backend/src/errors/index.js +7 -0
- package/backend/dist/esm/backend/src/errors/internal_external.d.ts +52 -0
- package/backend/dist/esm/backend/src/errors/internal_external.js +86 -0
- package/backend/dist/esm/backend/src/errors/invalid_usage_error.d.ts +41 -0
- package/backend/dist/esm/backend/src/errors/invalid_usage_error.js +33 -0
- package/backend/dist/esm/backend/src/errors/system_error.d.ts +261 -0
- package/backend/dist/esm/backend/src/errors/system_error.js +444 -0
- package/backend/dist/esm/backend/src/events.d.ts +97 -0
- package/backend/dist/esm/backend/src/events.js +5 -0
- package/backend/dist/esm/backend/src/frontend.d.ts +13 -0
- package/backend/dist/esm/backend/src/frontend.js +23 -0
- package/backend/dist/esm/backend/src/image_endpoint.d.ts +44 -0
- package/backend/dist/esm/backend/src/image_endpoint.js +196 -0
- package/backend/dist/esm/backend/src/index.d.ts +23 -0
- package/backend/dist/esm/backend/src/index.js +26 -0
- package/backend/dist/esm/backend/src/logger.d.ts +5 -0
- package/backend/dist/esm/backend/src/logger.js +8 -0
- package/backend/dist/esm/backend/src/meta.d.ts +112 -0
- package/backend/dist/esm/backend/src/meta.js +152 -0
- package/backend/dist/esm/backend/src/payments/paddle.d.ts +329 -0
- package/backend/dist/esm/backend/src/payments/paddle.js +2276 -0
- package/backend/dist/esm/backend/src/payments/stripe/checkout.d.ts +113 -0
- package/backend/dist/esm/backend/src/payments/stripe/checkout.js +356 -0
- package/backend/dist/esm/backend/src/payments/stripe/customers.d.ts +17 -0
- package/backend/dist/esm/backend/src/payments/stripe/customers.js +193 -0
- package/backend/dist/esm/backend/src/payments/stripe/error.d.ts +74 -0
- package/backend/dist/esm/backend/src/payments/stripe/error.js +51 -0
- package/backend/dist/esm/backend/src/payments/stripe/events.d.ts +155 -0
- package/backend/dist/esm/backend/src/payments/stripe/events.js +5 -0
- package/backend/dist/esm/backend/src/payments/stripe/meters.d.ts +105 -0
- package/backend/dist/esm/backend/src/payments/stripe/meters.js +318 -0
- package/backend/dist/esm/backend/src/payments/stripe/payment_methods.d.ts +58 -0
- package/backend/dist/esm/backend/src/payments/stripe/payment_methods.js +135 -0
- package/backend/dist/esm/backend/src/payments/stripe/products.d.ts +519 -0
- package/backend/dist/esm/backend/src/payments/stripe/products.js +896 -0
- package/backend/dist/esm/backend/src/payments/stripe/stripe.d.ts +215 -0
- package/backend/dist/esm/backend/src/payments/stripe/stripe.js +464 -0
- package/backend/dist/esm/backend/src/payments/stripe/subscriptions.d.ts +172 -0
- package/backend/dist/esm/backend/src/payments/stripe/subscriptions.js +754 -0
- package/backend/dist/esm/backend/src/payments/stripe/utils.d.ts +63 -0
- package/backend/dist/esm/backend/src/payments/stripe/utils.js +131 -0
- package/backend/dist/esm/backend/src/payments/stripe/webhooks.d.ts +105 -0
- package/backend/dist/esm/backend/src/payments/stripe/webhooks.js +752 -0
- package/backend/dist/esm/backend/src/plugins/browser.d.ts +1 -0
- package/backend/dist/esm/backend/src/plugins/browser.js +170 -0
- package/backend/dist/esm/backend/src/plugins/communication.d.ts +70 -0
- package/backend/dist/esm/backend/src/plugins/communication.js +169 -0
- package/backend/dist/esm/backend/src/plugins/mail/mail.d.ts +255 -0
- package/backend/dist/esm/backend/src/plugins/mail/mail.js +396 -0
- package/backend/dist/esm/backend/src/plugins/mail/ui.d.ts +297 -0
- package/backend/dist/esm/backend/src/plugins/mail/ui.js +1400 -0
- package/backend/dist/esm/backend/src/plugins/pdf.d.ts +1 -0
- package/backend/dist/esm/backend/src/plugins/pdf.js +1694 -0
- package/backend/dist/esm/backend/src/plugins/thread_monitor.d.ts +18 -0
- package/backend/dist/esm/backend/src/plugins/thread_monitor.js +120 -0
- package/backend/dist/esm/backend/src/rate_limit.d.ts +148 -0
- package/backend/dist/esm/backend/src/rate_limit.js +667 -0
- package/backend/dist/esm/backend/src/route.d.ts +39 -0
- package/backend/dist/esm/backend/src/route.js +222 -0
- package/backend/dist/esm/backend/src/server.d.ts +502 -0
- package/backend/dist/esm/backend/src/server.js +2031 -0
- package/backend/dist/esm/backend/src/server.old.d.ts +594 -0
- package/backend/dist/esm/backend/src/server.old.js +2630 -0
- package/backend/dist/esm/backend/src/splash_screen.d.ts +93 -0
- package/backend/dist/esm/backend/src/splash_screen.js +156 -0
- package/backend/dist/esm/backend/src/status.d.ts +89 -0
- package/backend/dist/esm/backend/src/status.js +213 -0
- package/backend/dist/esm/backend/src/stream.d.ts +494 -0
- package/backend/dist/esm/backend/src/stream.js +1611 -0
- package/backend/dist/esm/backend/src/users.d.ts +926 -0
- package/backend/dist/esm/backend/src/users.js +2423 -0
- package/backend/dist/esm/backend/src/utils.d.ts +22 -0
- package/backend/dist/esm/backend/src/utils.js +463 -0
- package/backend/dist/esm/backend/src/view.d.ts +115 -0
- package/backend/dist/esm/backend/src/view.js +584 -0
- package/backend/dist/esm/backend/src/vinc.d.ts +6 -0
- package/backend/dist/esm/backend/src/vinc.js +6 -0
- package/backend/dist/esm/backend/src/volt.d.ts +24 -0
- package/backend/dist/esm/backend/src/volt.js +27 -0
- package/backend/dist/esm/frontend/src/modules/request.d.ts +70 -0
- package/backend/dist/esm/frontend/src/modules/request.js +117 -0
- package/frontend/dist/backend/src/database/collection.d.ts +1765 -0
- package/frontend/dist/backend/src/database/collection.js +3779 -0
- package/frontend/dist/backend/src/database/database.d.ts +92 -0
- package/frontend/dist/backend/src/database/database.js +214 -0
- package/frontend/dist/backend/src/database/filters/filters.d.ts +6 -0
- package/frontend/dist/backend/src/database/filters/filters.js +1 -0
- package/frontend/dist/backend/src/database/filters/strict_filter.d.ts +223 -0
- package/frontend/dist/backend/src/database/filters/strict_filter.js +3 -0
- package/frontend/dist/backend/src/database/filters/strict_update_filter.d.ts +165 -0
- package/frontend/dist/backend/src/database/filters/strict_update_filter.js +5 -0
- package/frontend/dist/backend/src/database/flatten.d.ts +78 -0
- package/frontend/dist/backend/src/database/flatten.js +22 -0
- package/frontend/dist/backend/src/endpoint.d.ts +346 -0
- package/frontend/dist/backend/src/endpoint.js +479 -0
- package/frontend/dist/backend/src/errors/index.d.ts +7 -0
- package/frontend/dist/backend/src/errors/index.js +7 -0
- package/frontend/dist/backend/src/errors/internal_external.d.ts +52 -0
- package/frontend/dist/backend/src/errors/internal_external.js +86 -0
- package/frontend/dist/backend/src/errors/invalid_usage_error.d.ts +41 -0
- package/frontend/dist/backend/src/errors/invalid_usage_error.js +33 -0
- package/frontend/dist/backend/src/errors/system_error.d.ts +261 -0
- package/frontend/dist/backend/src/errors/system_error.js +444 -0
- package/frontend/dist/backend/src/events.d.ts +97 -0
- package/frontend/dist/backend/src/events.js +5 -0
- package/frontend/dist/backend/src/frontend.d.ts +13 -0
- package/frontend/dist/backend/src/frontend.js +23 -0
- package/frontend/dist/backend/src/image_endpoint.d.ts +44 -0
- package/frontend/dist/backend/src/image_endpoint.js +196 -0
- package/frontend/dist/backend/src/meta.d.ts +112 -0
- package/frontend/dist/backend/src/meta.js +152 -0
- package/frontend/dist/backend/src/payments/paddle.d.ts +329 -0
- package/frontend/dist/backend/src/payments/paddle.js +2276 -0
- package/frontend/dist/backend/src/payments/stripe/checkout.d.ts +113 -0
- package/frontend/dist/backend/src/payments/stripe/checkout.js +356 -0
- package/frontend/dist/backend/src/payments/stripe/customers.d.ts +17 -0
- package/frontend/dist/backend/src/payments/stripe/customers.js +193 -0
- package/frontend/dist/backend/src/payments/stripe/error.d.ts +74 -0
- package/frontend/dist/backend/src/payments/stripe/error.js +51 -0
- package/frontend/dist/backend/src/payments/stripe/events.d.ts +155 -0
- package/frontend/dist/backend/src/payments/stripe/events.js +5 -0
- package/frontend/dist/backend/src/payments/stripe/meters.d.ts +105 -0
- package/frontend/dist/backend/src/payments/stripe/meters.js +318 -0
- package/frontend/dist/backend/src/payments/stripe/payment_methods.d.ts +58 -0
- package/frontend/dist/backend/src/payments/stripe/payment_methods.js +135 -0
- package/frontend/dist/backend/src/payments/stripe/products.d.ts +519 -0
- package/frontend/dist/backend/src/payments/stripe/products.js +896 -0
- package/frontend/dist/backend/src/payments/stripe/stripe.d.ts +215 -0
- package/frontend/dist/backend/src/payments/stripe/stripe.js +464 -0
- package/frontend/dist/backend/src/payments/stripe/subscriptions.d.ts +172 -0
- package/frontend/dist/backend/src/payments/stripe/subscriptions.js +754 -0
- package/frontend/dist/backend/src/payments/stripe/utils.d.ts +63 -0
- package/frontend/dist/backend/src/payments/stripe/utils.js +131 -0
- package/frontend/dist/backend/src/payments/stripe/webhooks.d.ts +105 -0
- package/frontend/dist/backend/src/payments/stripe/webhooks.js +752 -0
- package/frontend/dist/backend/src/plugins/mail/mail.d.ts +255 -0
- package/frontend/dist/backend/src/plugins/mail/mail.js +396 -0
- package/frontend/dist/backend/src/plugins/mail/ui.d.ts +297 -0
- package/frontend/dist/backend/src/plugins/mail/ui.js +1400 -0
- package/frontend/dist/backend/src/rate_limit.d.ts +148 -0
- package/frontend/dist/backend/src/rate_limit.js +667 -0
- package/frontend/dist/backend/src/route.d.ts +39 -0
- package/frontend/dist/backend/src/route.js +222 -0
- package/frontend/dist/backend/src/server.d.ts +502 -0
- package/frontend/dist/backend/src/server.js +2031 -0
- package/frontend/dist/backend/src/splash_screen.d.ts +93 -0
- package/frontend/dist/backend/src/splash_screen.js +156 -0
- package/frontend/dist/backend/src/status.d.ts +89 -0
- package/frontend/dist/backend/src/status.js +213 -0
- package/frontend/dist/backend/src/stream.d.ts +494 -0
- package/frontend/dist/backend/src/stream.js +1611 -0
- package/frontend/dist/backend/src/users.d.ts +926 -0
- package/frontend/dist/backend/src/users.js +2423 -0
- package/frontend/dist/backend/src/utils.d.ts +22 -0
- package/frontend/dist/backend/src/utils.js +463 -0
- package/frontend/dist/backend/src/view.d.ts +115 -0
- package/frontend/dist/backend/src/view.js +584 -0
- package/frontend/dist/frontend/src/css/adyen.css +92 -0
- package/frontend/dist/frontend/src/css/volt.css +75 -0
- package/frontend/dist/frontend/src/elements/base.d.ts +3743 -0
- package/frontend/dist/frontend/src/elements/base.js +12151 -0
- package/frontend/dist/frontend/src/elements/module.d.ts +95 -0
- package/frontend/dist/frontend/src/elements/module.js +216 -0
- package/frontend/dist/frontend/src/elements/register_element.d.ts +3 -0
- package/frontend/dist/frontend/src/elements/register_element.js +22 -0
- package/frontend/dist/frontend/src/elements/resize_query_manager.d.ts +0 -0
- package/frontend/dist/frontend/src/elements/resize_query_manager.js +150 -0
- package/frontend/dist/frontend/src/elements/types.d.ts +52 -0
- package/frontend/dist/frontend/src/elements/types.js +5 -0
- package/frontend/dist/frontend/src/index.d.ts +21 -0
- package/frontend/dist/frontend/src/index.js +29 -0
- package/frontend/dist/frontend/src/modules/attachment.d.ts +126 -0
- package/frontend/dist/frontend/src/modules/attachment.js +306 -0
- package/frontend/dist/frontend/src/modules/auth.d.ts +44 -0
- package/frontend/dist/frontend/src/modules/auth.js +80 -0
- package/frontend/dist/frontend/src/modules/color.d.ts +160 -0
- package/frontend/dist/frontend/src/modules/color.js +316 -0
- package/frontend/dist/frontend/src/modules/compression.d.ts +39 -0
- package/frontend/dist/frontend/src/modules/compression.js +102 -0
- package/frontend/dist/frontend/src/modules/cookies.d.ts +44 -0
- package/frontend/dist/frontend/src/modules/cookies.js +143 -0
- package/frontend/dist/frontend/src/modules/events.d.ts +31 -0
- package/frontend/dist/frontend/src/modules/events.js +79 -0
- package/frontend/dist/frontend/src/modules/google.d.ts +23 -0
- package/frontend/dist/frontend/src/modules/google.js +52 -0
- package/frontend/dist/frontend/src/modules/meta.d.ts +14 -0
- package/frontend/dist/frontend/src/modules/meta.js +48 -0
- package/frontend/dist/frontend/src/modules/paddle.d.ts +1207 -0
- package/frontend/dist/frontend/src/modules/paddle.js +2594 -0
- package/frontend/dist/frontend/src/modules/request.d.ts +70 -0
- package/frontend/dist/frontend/src/modules/request.js +117 -0
- package/frontend/dist/frontend/src/modules/settings.d.ts +3 -0
- package/frontend/dist/frontend/src/modules/settings.js +5 -0
- package/frontend/dist/frontend/src/modules/statics.d.ts +21 -0
- package/frontend/dist/frontend/src/modules/statics.js +43 -0
- package/frontend/dist/frontend/src/modules/stripe/cart.d.ts +112 -0
- package/frontend/dist/frontend/src/modules/stripe/cart.js +321 -0
- package/frontend/dist/frontend/src/modules/stripe/checkout.d.ts +7 -0
- package/frontend/dist/frontend/src/modules/stripe/checkout.js +37 -0
- package/frontend/dist/frontend/src/modules/stripe/index.m.d.ts +6 -0
- package/frontend/dist/frontend/src/modules/stripe/index.m.js +6 -0
- package/frontend/dist/frontend/src/modules/stripe/payments.d.ts +58 -0
- package/frontend/dist/frontend/src/modules/stripe/payments.js +92 -0
- package/frontend/dist/frontend/src/modules/support.d.ts +30 -0
- package/frontend/dist/frontend/src/modules/support.js +53 -0
- package/frontend/dist/frontend/src/modules/theme.d.ts +133 -0
- package/frontend/dist/frontend/src/modules/theme.js +406 -0
- package/frontend/dist/frontend/src/modules/themes.d.ts +12 -0
- package/frontend/dist/frontend/src/modules/themes.js +22 -0
- package/frontend/dist/frontend/src/modules/user.d.ts +164 -0
- package/frontend/dist/frontend/src/modules/user.js +270 -0
- package/frontend/dist/frontend/src/modules/utils.d.ts +176 -0
- package/frontend/dist/frontend/src/modules/utils.js +569 -0
- package/frontend/dist/frontend/src/types/gradient.d.ts +29 -0
- package/frontend/dist/frontend/src/types/gradient.js +79 -0
- package/frontend/dist/frontend/src/ui/border_button.d.ts +94 -0
- package/frontend/dist/frontend/src/ui/border_button.js +228 -0
- package/frontend/dist/frontend/src/ui/button.d.ts +241 -0
- package/frontend/dist/frontend/src/ui/button.js +682 -0
- package/frontend/dist/frontend/src/ui/canvas.d.ts +138 -0
- package/frontend/dist/frontend/src/ui/canvas.js +444 -0
- package/frontend/dist/frontend/src/ui/checkbox.d.ts +74 -0
- package/frontend/dist/frontend/src/ui/checkbox.js +321 -0
- package/frontend/dist/frontend/src/ui/code.d.ts +235 -0
- package/frontend/dist/frontend/src/ui/code.js +1007 -0
- package/frontend/dist/frontend/src/ui/context_menu.d.ts +36 -0
- package/frontend/dist/frontend/src/ui/context_menu.js +205 -0
- package/frontend/dist/frontend/src/ui/css.d.ts +16 -0
- package/frontend/dist/frontend/src/ui/css.js +48 -0
- package/frontend/dist/frontend/src/ui/divider.d.ts +15 -0
- package/frontend/dist/frontend/src/ui/divider.js +78 -0
- package/frontend/dist/frontend/src/ui/dropdown.d.ts +176 -0
- package/frontend/dist/frontend/src/ui/dropdown.js +481 -0
- package/frontend/dist/frontend/src/ui/for_each.d.ts +37 -0
- package/frontend/dist/frontend/src/ui/for_each.js +92 -0
- package/frontend/dist/frontend/src/ui/form.d.ts +34 -0
- package/frontend/dist/frontend/src/ui/form.js +233 -0
- package/frontend/dist/frontend/src/ui/frame_modes.d.ts +37 -0
- package/frontend/dist/frontend/src/ui/frame_modes.js +108 -0
- package/frontend/dist/frontend/src/ui/google_map.d.ts +24 -0
- package/frontend/dist/frontend/src/ui/google_map.js +106 -0
- package/frontend/dist/frontend/src/ui/gradient.d.ts +25 -0
- package/frontend/dist/frontend/src/ui/gradient.js +131 -0
- package/frontend/dist/frontend/src/ui/image.d.ts +111 -0
- package/frontend/dist/frontend/src/ui/image.js +576 -0
- package/frontend/dist/frontend/src/ui/input.d.ts +392 -0
- package/frontend/dist/frontend/src/ui/input.js +1201 -0
- package/frontend/dist/frontend/src/ui/link.d.ts +25 -0
- package/frontend/dist/frontend/src/ui/link.js +140 -0
- package/frontend/dist/frontend/src/ui/list.d.ts +37 -0
- package/frontend/dist/frontend/src/ui/list.js +170 -0
- package/frontend/dist/frontend/src/ui/loader_button.d.ts +80 -0
- package/frontend/dist/frontend/src/ui/loader_button.js +193 -0
- package/frontend/dist/frontend/src/ui/loaders.d.ts +57 -0
- package/frontend/dist/frontend/src/ui/loaders.js +157 -0
- package/frontend/dist/frontend/src/ui/popup.d.ts +94 -0
- package/frontend/dist/frontend/src/ui/popup.js +510 -0
- package/frontend/dist/frontend/src/ui/pseudo.d.ts +44 -0
- package/frontend/dist/frontend/src/ui/pseudo.js +154 -0
- package/frontend/dist/frontend/src/ui/scroller.d.ts +105 -0
- package/frontend/dist/frontend/src/ui/scroller.js +1253 -0
- package/frontend/dist/frontend/src/ui/slider.d.ts +45 -0
- package/frontend/dist/frontend/src/ui/slider.js +217 -0
- package/frontend/dist/frontend/src/ui/spacer.d.ts +15 -0
- package/frontend/dist/frontend/src/ui/spacer.js +78 -0
- package/frontend/dist/frontend/src/ui/span.d.ts +15 -0
- package/frontend/dist/frontend/src/ui/span.js +73 -0
- package/frontend/dist/frontend/src/ui/stack.d.ts +66 -0
- package/frontend/dist/frontend/src/ui/stack.js +335 -0
- package/frontend/dist/frontend/src/ui/steps.d.ts +131 -0
- package/frontend/dist/frontend/src/ui/steps.js +308 -0
- package/frontend/dist/frontend/src/ui/style.d.ts +17 -0
- package/frontend/dist/frontend/src/ui/style.js +73 -0
- package/frontend/dist/frontend/src/ui/switch.d.ts +69 -0
- package/frontend/dist/frontend/src/ui/switch.js +357 -0
- package/frontend/dist/frontend/src/ui/table.d.ts +100 -0
- package/frontend/dist/frontend/src/ui/table.js +405 -0
- package/frontend/dist/frontend/src/ui/tabs.d.ts +111 -0
- package/frontend/dist/frontend/src/ui/tabs.js +424 -0
- package/frontend/dist/frontend/src/ui/text.d.ts +15 -0
- package/frontend/dist/frontend/src/ui/text.js +83 -0
- package/frontend/dist/frontend/src/ui/title.d.ts +91 -0
- package/frontend/dist/frontend/src/ui/title.js +272 -0
- package/frontend/dist/frontend/src/ui/ui.d.ts +35 -0
- package/frontend/dist/frontend/src/ui/ui.js +38 -0
- package/frontend/dist/frontend/src/ui/view.d.ts +15 -0
- package/frontend/dist/frontend/src/ui/view.js +88 -0
- package/frontend/dist/frontend/src/volt.d.ts +20 -0
- package/frontend/dist/frontend/src/volt.js +27 -0
- package/package.json +7 -2
|
@@ -0,0 +1,754 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author Daan van den Bergh
|
|
3
|
+
* @copyright © 2026 - 2026 Daan van den Bergh. All rights reserved
|
|
4
|
+
*/
|
|
5
|
+
import * as vlib from "@vandenberghinc/vlib";
|
|
6
|
+
import { ExternalStripeError, InternalStripeError } from "./error.js";
|
|
7
|
+
import { ensure_stripe_customer } from "./customers.js";
|
|
8
|
+
import { assert, public_assert, stripe_api_call, is_non_empty_string, stable_idempotency_key } from "./utils.js";
|
|
9
|
+
import { Collection } from "../../database/collection.js";
|
|
10
|
+
// ----------------------------------------------------------------------------
|
|
11
|
+
// Caching.
|
|
12
|
+
/**
|
|
13
|
+
* Cache for loading the subscription record of a user.
|
|
14
|
+
*
|
|
15
|
+
* This is a short lived cache for hot paths,
|
|
16
|
+
* since changes accross multiple forks & multi-load server
|
|
17
|
+
* are not persisted to this cache.
|
|
18
|
+
*/
|
|
19
|
+
const subscription_record_cache = new vlib.Cache({
|
|
20
|
+
max_size: 250_000,
|
|
21
|
+
ttl: {
|
|
22
|
+
sliding: true,
|
|
23
|
+
// Short lived cache, see docstring why
|
|
24
|
+
duration: 1000 * 60 * 5,
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
/**
|
|
28
|
+
* List all subscriptions for a user.
|
|
29
|
+
*
|
|
30
|
+
* We do not use any caching here by design, dont change this since it will break current implementations.
|
|
31
|
+
*
|
|
32
|
+
* Stripe docs:
|
|
33
|
+
* - List subscriptions: https://docs.stripe.com/api/subscriptions/list
|
|
34
|
+
* - Expand: https://docs.stripe.com/expand
|
|
35
|
+
*/
|
|
36
|
+
async function list_all_customer_subscriptions(client, server, uid, customer_id) {
|
|
37
|
+
/** Validate input early to avoid cache poisoning with ambiguous keys. */
|
|
38
|
+
assert(uid.trim().length > 0, "invalid_argument", "Uid must be a non-empty string.", { uid });
|
|
39
|
+
// Ensure customer.
|
|
40
|
+
const ensured_customer_id = customer_id ?? await ensure_stripe_customer(client, server, uid);
|
|
41
|
+
// Fetch subscriptions.
|
|
42
|
+
const subscriptions = [];
|
|
43
|
+
let starting_after;
|
|
44
|
+
for (;;) {
|
|
45
|
+
const page = await stripe_api_call(() => client.subscriptions.list({
|
|
46
|
+
customer: ensured_customer_id,
|
|
47
|
+
status: "all",
|
|
48
|
+
limit: 100,
|
|
49
|
+
starting_after,
|
|
50
|
+
// ALWAYS expand, since the callee's expect this.
|
|
51
|
+
expand: ["data.items.data.price"],
|
|
52
|
+
}), { operation: "subscriptions.list", customer_id: ensured_customer_id, starting_after });
|
|
53
|
+
subscriptions.push(...page.data);
|
|
54
|
+
if (!page.has_more || page.data.length === 0) {
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
const last = page.data[page.data.length - 1];
|
|
58
|
+
assert(last !== undefined, "api_error", "Stripe subscriptions pagination returned an empty last item.", {
|
|
59
|
+
customer_id: ensured_customer_id,
|
|
60
|
+
returned: page.data.length,
|
|
61
|
+
});
|
|
62
|
+
starting_after = last.id;
|
|
63
|
+
}
|
|
64
|
+
return subscriptions;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Resolve the parent subscription product for a plan.
|
|
68
|
+
* This is needed to apply subscription-level rules (trial_days, billing_anchor).
|
|
69
|
+
*/
|
|
70
|
+
function resolve_plan_parent_subscription(opts) {
|
|
71
|
+
for (const product of opts.all_products) {
|
|
72
|
+
if (product.type === "subscription" && product.id === opts.plan.subscription_id) {
|
|
73
|
+
return product;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
// If we cannot find it, this indicates a corrupted initialization state.
|
|
77
|
+
throw new InternalStripeError("invalid_product", "Subscription plan refers to a missing parent subscription product.", { plan_id: opts.plan.id, subscription_id: opts.plan.subscription_id });
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Retrieve a Stripe customer and safely resolve the default payment method id for invoices/subscriptions.
|
|
81
|
+
*
|
|
82
|
+
* Stripe docs:
|
|
83
|
+
* - Retrieve customer: https://docs.stripe.com/api/customers/retrieve
|
|
84
|
+
* - invoice_settings.default_payment_method: https://docs.stripe.com/api/customers/object#customer_object-invoice_settings
|
|
85
|
+
*/
|
|
86
|
+
async function resolve_default_payment_method_id(client, opts) {
|
|
87
|
+
// We expand default_payment_method so we can always read `.id` without extra requests.
|
|
88
|
+
const customer = await stripe_api_call(() => client.customers.retrieve(opts.stripe_customer_id, {
|
|
89
|
+
expand: ["invoice_settings.default_payment_method"],
|
|
90
|
+
}), { operation: "customers.retrieve", uid: opts.uid, stripe_customer_id: opts.stripe_customer_id });
|
|
91
|
+
// Stripe can return DeletedCustomer; we must never proceed if the customer is deleted.
|
|
92
|
+
public_assert(customer.deleted !== true, "customer_not_found", "Stripe customer was not found.", { uid: opts.uid, stripe_customer_id: opts.stripe_customer_id });
|
|
93
|
+
// We avoid any unsafe casts by narrowing through `unknown` and structural checks.
|
|
94
|
+
const invoice_settings = customer.invoice_settings;
|
|
95
|
+
const default_payment_method = invoice_settings?.default_payment_method;
|
|
96
|
+
// invoice_settings.default_payment_method is string | PaymentMethod | null.
|
|
97
|
+
if (typeof default_payment_method === "string") {
|
|
98
|
+
return default_payment_method;
|
|
99
|
+
}
|
|
100
|
+
if (default_payment_method && typeof default_payment_method === "object") {
|
|
101
|
+
const id = default_payment_method.id;
|
|
102
|
+
if (typeof id === "string" && id.trim().length > 0) {
|
|
103
|
+
return id;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
// User-facing: without a default payment method, we cannot bill off-session securely.
|
|
107
|
+
throw new ExternalStripeError("payment_method_missing", "No default payment method on file. Please add a payment method before subscribing.", { uid: opts.uid, stripe_customer_id: opts.stripe_customer_id });
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Extract a PaymentIntent from an expanded subscription.latest_invoice.payment_intent.
|
|
111
|
+
*
|
|
112
|
+
* Stripe docs:
|
|
113
|
+
* - Subscription object: https://docs.stripe.com/api/subscriptions/object
|
|
114
|
+
* - Invoice.payment_intent: https://docs.stripe.com/api/invoices/object#invoice_object-payment_intent
|
|
115
|
+
* - PaymentIntent statuses: https://docs.stripe.com/api/payment_intents/object#payment_intent_object-status
|
|
116
|
+
*/
|
|
117
|
+
function resolve_payment_intent_from_subscription(subscription) {
|
|
118
|
+
const latest_invoice = subscription.latest_invoice;
|
|
119
|
+
// latest_invoice is string | Invoice | null.
|
|
120
|
+
if (!latest_invoice || typeof latest_invoice === "string") {
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
const payment_intent = latest_invoice.payment_intent;
|
|
124
|
+
// payment_intent is string | PaymentIntent | null.
|
|
125
|
+
if (!payment_intent || typeof payment_intent === "string") {
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
return payment_intent;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Build SubscriptionCreateParams.SubscriptionData from our initialized product configuration.
|
|
132
|
+
*/
|
|
133
|
+
function build_subscription_create_params_from_product(opts) {
|
|
134
|
+
const parent = opts.parent_subscription;
|
|
135
|
+
const subscription_params = {
|
|
136
|
+
// Safe metadata for reconciliation and security auditing.
|
|
137
|
+
metadata: {
|
|
138
|
+
__volt_uid: opts.uid,
|
|
139
|
+
__volt_subscription_id: parent.id,
|
|
140
|
+
},
|
|
141
|
+
};
|
|
142
|
+
// Apply trial days if configured (Stripe requires integer days).
|
|
143
|
+
if (parent.trial_days !== undefined) {
|
|
144
|
+
subscription_params.trial_period_days = parent.trial_days;
|
|
145
|
+
}
|
|
146
|
+
// Apply billing anchor strategy.
|
|
147
|
+
//
|
|
148
|
+
// NOTE:
|
|
149
|
+
// - We only support "immediately" and "first_of_month" because those are the only ones we model.
|
|
150
|
+
// - For "first_of_month", we disable proration to avoid pro-rated invoices on anchor changes.
|
|
151
|
+
const billing_anchor = parent.billing_anchor ?? "immediately";
|
|
152
|
+
if (billing_anchor === "immediately") {
|
|
153
|
+
return subscription_params;
|
|
154
|
+
}
|
|
155
|
+
if (billing_anchor === "first_of_month") {
|
|
156
|
+
// We keep this logic consistent with checkout.ts: anchor to first day of next month
|
|
157
|
+
// after the trial ends (or now if no trial).
|
|
158
|
+
const now = new Date();
|
|
159
|
+
const trial_days = parent.trial_days;
|
|
160
|
+
const trial_end_reference = trial_days !== undefined ? new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate() + trial_days, now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds(), now.getUTCMilliseconds())) : now;
|
|
161
|
+
const anchor_date = new Date(Date.UTC(trial_end_reference.getUTCFullYear(), trial_end_reference.getUTCMonth() + 1, 1, 0, 0, 0, 0));
|
|
162
|
+
// Stripe expects unix timestamp seconds.
|
|
163
|
+
subscription_params.billing_cycle_anchor = Math.floor(anchor_date.getTime() / 1000);
|
|
164
|
+
subscription_params.proration_behavior = "none";
|
|
165
|
+
return subscription_params;
|
|
166
|
+
}
|
|
167
|
+
// Future-proof exhaustive safety.
|
|
168
|
+
throw new InternalStripeError("invalid_product", "Unsupported billing_anchor value.", { billing_anchor, subscription_id: parent.id });
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Create the stripe subscriptions database collection.
|
|
172
|
+
*/
|
|
173
|
+
function create_subscriptions_db(server) {
|
|
174
|
+
// Initialize the database connection.
|
|
175
|
+
return server.db.collection({
|
|
176
|
+
name: "Volt.Stripe.Subscriptions",
|
|
177
|
+
indexes: [
|
|
178
|
+
{
|
|
179
|
+
keys: { uid: 1 },
|
|
180
|
+
unique: true,
|
|
181
|
+
},
|
|
182
|
+
],
|
|
183
|
+
// Ensure its not unique so we retrieve the cached collection if already created.
|
|
184
|
+
unique: false,
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
// ----------------------------------------------------------------------------
|
|
188
|
+
// Internal but exported API.
|
|
189
|
+
/**
|
|
190
|
+
* Update the subscriptions record for a user after a subscription mutation.
|
|
191
|
+
* @note This includes the meter subscriptions.
|
|
192
|
+
* @internal
|
|
193
|
+
*/
|
|
194
|
+
export async function update_subscription_record(client, server, opts) {
|
|
195
|
+
// List all subscriptions.
|
|
196
|
+
const subscriptions = await list_all_customer_subscriptions(client, server, opts.uid, undefined);
|
|
197
|
+
// Map subscription plan price ids to their plan ids for quick lookup.
|
|
198
|
+
const sub_price_id_to_plan_id = new Map();
|
|
199
|
+
const meter_price_id_to_product_id = new Map();
|
|
200
|
+
for (const product of opts.all_products) {
|
|
201
|
+
if (product.type === "subscription") {
|
|
202
|
+
for (const plan of product.plans) {
|
|
203
|
+
sub_price_id_to_plan_id.set(plan.stripe_price_id, plan.id);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
else if (product.type === "meter") {
|
|
207
|
+
meter_price_id_to_product_id.set(product.stripe_price_id, product.id);
|
|
208
|
+
}
|
|
209
|
+
else if (product.type !== "one_time") {
|
|
210
|
+
// @ts-expect-error Future-proof exhaustive safety.
|
|
211
|
+
product.toString();
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
// Create record.
|
|
215
|
+
const record = {
|
|
216
|
+
uid: opts.uid,
|
|
217
|
+
subscriptions: {},
|
|
218
|
+
meters: {},
|
|
219
|
+
};
|
|
220
|
+
// Iterate.
|
|
221
|
+
for (const subscription of subscriptions) {
|
|
222
|
+
switch (subscription.status) {
|
|
223
|
+
// Store all semi-active statuses, they can later be filtered.
|
|
224
|
+
case "active":
|
|
225
|
+
case "trialing":
|
|
226
|
+
case "past_due": {
|
|
227
|
+
for (const item of subscription.items.data) {
|
|
228
|
+
const price = item.price;
|
|
229
|
+
if (!price) {
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
if (sub_price_id_to_plan_id.has(price.id)) {
|
|
233
|
+
const plan_id = sub_price_id_to_plan_id.get(price.id);
|
|
234
|
+
if (!plan_id) {
|
|
235
|
+
continue;
|
|
236
|
+
}
|
|
237
|
+
record.subscriptions[plan_id] = subscription.status;
|
|
238
|
+
}
|
|
239
|
+
if (meter_price_id_to_product_id.has(price.id)) {
|
|
240
|
+
const product_id = meter_price_id_to_product_id.get(price.id);
|
|
241
|
+
if (!product_id) {
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
record.meters[product_id] = subscription.status;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
break;
|
|
248
|
+
}
|
|
249
|
+
default:
|
|
250
|
+
break;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
// Save the record.
|
|
254
|
+
const db = create_subscriptions_db(server);
|
|
255
|
+
await db.set({ uid: opts.uid }, record, {
|
|
256
|
+
// ensure we do not flatten the subscriptions object so we can remove old plans that are no longer active.
|
|
257
|
+
flatten: false,
|
|
258
|
+
upsert: true,
|
|
259
|
+
retry: 3,
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Load a subscription record with caching.
|
|
264
|
+
*/
|
|
265
|
+
async function load_subscription_record(server, opts) {
|
|
266
|
+
const use_cache = opts.cache ?? true;
|
|
267
|
+
// Check cache first.
|
|
268
|
+
if (use_cache) {
|
|
269
|
+
const cached = subscription_record_cache.get(opts.uid);
|
|
270
|
+
if (cached != null) {
|
|
271
|
+
return cached;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
// Load.
|
|
275
|
+
const db = create_subscriptions_db(server);
|
|
276
|
+
const record = await db.load({ uid: opts.uid }, { throw: false });
|
|
277
|
+
if (record instanceof Error) {
|
|
278
|
+
if (record instanceof Collection.NotFoundError) {
|
|
279
|
+
// Cache empty record to avoid repeated db hits for users without subscriptions.
|
|
280
|
+
const empty_record = { uid: opts.uid, subscriptions: {}, meters: {} };
|
|
281
|
+
subscription_record_cache.set(opts.uid, empty_record);
|
|
282
|
+
return empty_record;
|
|
283
|
+
}
|
|
284
|
+
throw record;
|
|
285
|
+
}
|
|
286
|
+
// Cache always and return.
|
|
287
|
+
subscription_record_cache.set(opts.uid, record);
|
|
288
|
+
return record;
|
|
289
|
+
}
|
|
290
|
+
// ----------------------------------------------------------------------------
|
|
291
|
+
// Public API.
|
|
292
|
+
/**
|
|
293
|
+
* Delete a user from the subscription caches.
|
|
294
|
+
* Should be called after any mutation to the user's subscriptions to avoid stale cache entries.
|
|
295
|
+
*/
|
|
296
|
+
export function delete_subscription_caches(uid) {
|
|
297
|
+
subscription_record_cache.delete(uid);
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* List all subscribed product plans for a given user.
|
|
301
|
+
*/
|
|
302
|
+
export async function list_subscribed_plans(client, server, opts) {
|
|
303
|
+
/** Validate input early to avoid cache poisoning with ambiguous keys. */
|
|
304
|
+
assert(opts.uid.trim().length > 0, "invalid_argument", "Uid must be a non-empty string.", { uid: opts.uid });
|
|
305
|
+
// Load record.
|
|
306
|
+
const record = await load_subscription_record(server, { uid: opts.uid });
|
|
307
|
+
// Subscription statuses that represent a live (entitlement-granting) subscription.
|
|
308
|
+
const active_statuses = new Set(opts.status ?? [
|
|
309
|
+
"active",
|
|
310
|
+
"trialing",
|
|
311
|
+
"past_due",
|
|
312
|
+
]);
|
|
313
|
+
// Walk all subscriptions.
|
|
314
|
+
const active = {};
|
|
315
|
+
for (const [id, status] of Object.entries(record.subscriptions)) {
|
|
316
|
+
if (!active_statuses.has(status)) {
|
|
317
|
+
continue;
|
|
318
|
+
}
|
|
319
|
+
active[id] = status;
|
|
320
|
+
}
|
|
321
|
+
return active;
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* List all meter product id's that a customer is currently subscribed (entitled) to.
|
|
325
|
+
*
|
|
326
|
+
* We require an active-ish subscription that contains a subscription item for the meter product's price id.
|
|
327
|
+
* This prevents accidental/abusive usage reporting for users who aren't subscribed.
|
|
328
|
+
*
|
|
329
|
+
* Stripe docs:
|
|
330
|
+
* - List subscriptions: https://docs.stripe.com/api/subscriptions/list
|
|
331
|
+
* - Expand: https://docs.stripe.com/expand
|
|
332
|
+
*/
|
|
333
|
+
export async function list_subscribed_meters(client, server, opts) {
|
|
334
|
+
// Load record.
|
|
335
|
+
const record = await load_subscription_record(server, { uid: opts.uid });
|
|
336
|
+
// Subscription statuses that represent a live (entitlement-granting) subscription.
|
|
337
|
+
const active_statuses = new Set(opts.status ?? [
|
|
338
|
+
"active",
|
|
339
|
+
// We dont allow `trialing` and `past_due` to reduce risk of accidental/abusive access.
|
|
340
|
+
]);
|
|
341
|
+
// Walk all subscriptions.
|
|
342
|
+
const active = {};
|
|
343
|
+
for (const [id, status] of Object.entries(record.meters)) {
|
|
344
|
+
if (!active_statuses.has(status)) {
|
|
345
|
+
continue;
|
|
346
|
+
}
|
|
347
|
+
active[id] = status;
|
|
348
|
+
}
|
|
349
|
+
return active;
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* Check whether a user (by uid) is subscribed to a specific subscription (plan) or meter product.
|
|
353
|
+
*
|
|
354
|
+
* @returns `true` if the user has an active subscription to the subscription (plan) or meter product, `false` otherwise.
|
|
355
|
+
*/
|
|
356
|
+
export async function is_user_subscribed_to(client, server, opts) {
|
|
357
|
+
/** Validate inputs early. */
|
|
358
|
+
assert(opts.plan.id.trim().length > 0, "invalid_argument", "Plan.id must be a non-empty string.", { plan_id: opts.plan.id });
|
|
359
|
+
// Meter product path.
|
|
360
|
+
if (opts.plan.type === "meter") {
|
|
361
|
+
const subscribed_plans = await list_subscribed_meters(client, server, {
|
|
362
|
+
uid: opts.uid,
|
|
363
|
+
stripe_customer_id: opts.customer_id ?? await ensure_stripe_customer(client, server, opts.uid),
|
|
364
|
+
all_products: opts.all_products,
|
|
365
|
+
status: opts.status,
|
|
366
|
+
});
|
|
367
|
+
return Object.keys(subscribed_plans).includes(opts.plan.id);
|
|
368
|
+
}
|
|
369
|
+
// Subscription (plan) path.
|
|
370
|
+
else {
|
|
371
|
+
// Retrieve subscribed plans.
|
|
372
|
+
const subscribed_plans = await list_subscribed_plans(client, server, {
|
|
373
|
+
uid: opts.uid,
|
|
374
|
+
customer_id: opts.customer_id,
|
|
375
|
+
all_products: opts.all_products,
|
|
376
|
+
status: opts.status,
|
|
377
|
+
});
|
|
378
|
+
return Object.keys(subscribed_plans).includes(opts.plan.id);
|
|
379
|
+
}
|
|
380
|
+
// Not subscribed.
|
|
381
|
+
return false;
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* Create a subscription for either:
|
|
385
|
+
* - a subscription plan (licensed recurring)
|
|
386
|
+
* - a meter product (metered recurring)
|
|
387
|
+
*
|
|
388
|
+
* This uses the customer's default invoice payment method for off-session billing and
|
|
389
|
+
* returns a "requires_action" payload when SCA/3DS confirmation is needed in the UI.
|
|
390
|
+
*
|
|
391
|
+
* Stripe docs:
|
|
392
|
+
* - Create subscription: https://docs.stripe.com/api/subscriptions/create
|
|
393
|
+
* - Payment behavior (default_incomplete): https://docs.stripe.com/billing/subscriptions/overview#handling-incomplete-subscriptions
|
|
394
|
+
* - Expand: https://docs.stripe.com/expand
|
|
395
|
+
* - PaymentIntents: https://docs.stripe.com/api/payment_intents
|
|
396
|
+
*
|
|
397
|
+
* @warning This function intentionally creates subscriptions with `payment_behavior="default_incomplete"`
|
|
398
|
+
* so we can reliably detect and surface required customer actions (SCA).
|
|
399
|
+
*/
|
|
400
|
+
export async function create_user_subscription(client, server, opts) {
|
|
401
|
+
// -------------------------------------------------------------------------
|
|
402
|
+
// Validate inputs (user-facing where appropriate).
|
|
403
|
+
public_assert(is_non_empty_string(opts.uid), "invalid_argument", "Property 'uid' must be a non-empty string.");
|
|
404
|
+
assert(Array.isArray(opts.all_products), "invalid_argument", "Property 'all_products' must be an array.");
|
|
405
|
+
assert(opts.target.id.trim().length > 0, "invalid_argument", "Target.id must be non-empty.", { target_id: opts.target.id });
|
|
406
|
+
// Any mutation can change entitlements; invalidate cache up-front to reduce stale reads.
|
|
407
|
+
delete_subscription_caches(opts.uid);
|
|
408
|
+
// -------------------------------------------------------------------------
|
|
409
|
+
// Ensure customer + default payment method.
|
|
410
|
+
const stripe_customer_id = await ensure_stripe_customer(client, server, opts.uid);
|
|
411
|
+
const default_payment_method_id = await resolve_default_payment_method_id(client, {
|
|
412
|
+
uid: opts.uid,
|
|
413
|
+
stripe_customer_id,
|
|
414
|
+
});
|
|
415
|
+
// -------------------------------------------------------------------------
|
|
416
|
+
// Build subscription create params.
|
|
417
|
+
const idempotency_key = stable_idempotency_key(`sub_create:${opts.uid}:${opts.idempotency_key}`);
|
|
418
|
+
// Base params shared for both plan and meter.
|
|
419
|
+
const base_params = {
|
|
420
|
+
customer: stripe_customer_id,
|
|
421
|
+
// We always charge automatically for subscriptions.
|
|
422
|
+
// Docs: https://docs.stripe.com/api/subscriptions/create#create_subscription-collection_method
|
|
423
|
+
collection_method: "charge_automatically",
|
|
424
|
+
// Use the default invoice payment method for off-session charges.
|
|
425
|
+
// Docs: https://docs.stripe.com/api/subscriptions/create#create_subscription-default_payment_method
|
|
426
|
+
default_payment_method: default_payment_method_id,
|
|
427
|
+
// Crucial: create an incomplete subscription when payment can't be completed immediately,
|
|
428
|
+
// so we can surface the PaymentIntent client_secret to the UI for SCA/3DS.
|
|
429
|
+
// Docs: https://docs.stripe.com/billing/subscriptions/overview#handling-incomplete-subscriptions
|
|
430
|
+
payment_behavior: "default_incomplete",
|
|
431
|
+
// Safe metadata only.
|
|
432
|
+
metadata: {
|
|
433
|
+
__volt_uid: opts.uid,
|
|
434
|
+
__volt_target_id: opts.target.id,
|
|
435
|
+
__volt_target_type: opts.target.type,
|
|
436
|
+
},
|
|
437
|
+
};
|
|
438
|
+
// Items differ per target type.
|
|
439
|
+
if (opts.target.type === "subscription_plan") {
|
|
440
|
+
// Resolve parent subscription product to apply trial/billing anchor coherently.
|
|
441
|
+
const parent_subscription = resolve_plan_parent_subscription({
|
|
442
|
+
plan: opts.target,
|
|
443
|
+
all_products: opts.all_products,
|
|
444
|
+
});
|
|
445
|
+
const subscription_level_params = build_subscription_create_params_from_product({
|
|
446
|
+
uid: opts.uid,
|
|
447
|
+
parent_subscription,
|
|
448
|
+
});
|
|
449
|
+
// Stripe docs: items.price https://docs.stripe.com/api/subscriptions/create#create_subscription-items-price
|
|
450
|
+
base_params.items = [{ price: opts.target.stripe_price_id, quantity: 1 }];
|
|
451
|
+
// Apply subscription-level options (trial, anchor, metadata).
|
|
452
|
+
base_params.trial_period_days = subscription_level_params.trial_period_days;
|
|
453
|
+
base_params.billing_cycle_anchor = subscription_level_params.billing_cycle_anchor;
|
|
454
|
+
base_params.proration_behavior = subscription_level_params.proration_behavior;
|
|
455
|
+
// Merge metadata (preserve base keys).
|
|
456
|
+
base_params.metadata = {
|
|
457
|
+
...(base_params.metadata ?? {}),
|
|
458
|
+
...(subscription_level_params.metadata ?? {}),
|
|
459
|
+
__volt_subscription_id: parent_subscription.id,
|
|
460
|
+
__volt_plan_id: opts.target.id,
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
else if (opts.target.type === "meter") {
|
|
464
|
+
// Meter products are metered recurring prices already linked to a Stripe Billing Meter during initialization.
|
|
465
|
+
// Stripe docs: https://docs.stripe.com/api/prices/create (recurring.meter)
|
|
466
|
+
base_params.items = [{ price: opts.target.stripe_price_id, quantity: 1 }];
|
|
467
|
+
// Useful context for reconciliation.
|
|
468
|
+
base_params.metadata = {
|
|
469
|
+
...(base_params.metadata ?? {}),
|
|
470
|
+
__volt_meter_product_id: opts.target.id,
|
|
471
|
+
__volt_meter_event_name: opts.target.meter_event_name,
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
else {
|
|
475
|
+
// Exhaustive safety for future target types.
|
|
476
|
+
// @ts-expect-error should be never
|
|
477
|
+
opts.target.toString();
|
|
478
|
+
throw new InternalStripeError("invalid_argument", "Unsupported subscription target type.", { target_type: opts.target.type });
|
|
479
|
+
}
|
|
480
|
+
// -------------------------------------------------------------------------
|
|
481
|
+
// Create subscription (expand latest_invoice.payment_intent so we can surface UI actions).
|
|
482
|
+
const subscription = await stripe_api_call(() => client.subscriptions.create({
|
|
483
|
+
...base_params,
|
|
484
|
+
// Expand payment intent so we can return client_secret for SCA, if required.
|
|
485
|
+
// Docs: https://docs.stripe.com/expand
|
|
486
|
+
expand: ["latest_invoice.payment_intent"],
|
|
487
|
+
}, { idempotencyKey: idempotency_key }), {
|
|
488
|
+
operation: "subscriptions.create",
|
|
489
|
+
uid: opts.uid,
|
|
490
|
+
stripe_customer_id,
|
|
491
|
+
target_id: opts.target.id,
|
|
492
|
+
target_type: opts.target.type,
|
|
493
|
+
});
|
|
494
|
+
// Mutation happened: invalidate cache again post-mutation to reduce stale reads.
|
|
495
|
+
delete_subscription_caches(opts.uid);
|
|
496
|
+
// -------------------------------------------------------------------------
|
|
497
|
+
// Handle outcomes:
|
|
498
|
+
// - active/trialing/past_due => success
|
|
499
|
+
// - incomplete + requires_action => UI must confirm the PaymentIntent
|
|
500
|
+
// - incomplete + requires_payment_method => user must add/replace payment method
|
|
501
|
+
const payment_intent = resolve_payment_intent_from_subscription(subscription);
|
|
502
|
+
// If Stripe indicates the PaymentIntent needs customer action, return client_secret to the UI.
|
|
503
|
+
if (payment_intent && payment_intent.status === "requires_action") {
|
|
504
|
+
const client_secret = payment_intent.client_secret;
|
|
505
|
+
public_assert(is_non_empty_string(client_secret), "subscription_payment_action_required", "Additional payment verification is required, but Stripe did not return a client secret.", { subscription_id: subscription.id, payment_intent_id: payment_intent.id });
|
|
506
|
+
return {
|
|
507
|
+
type: "requires_action",
|
|
508
|
+
subscription_id: subscription.id,
|
|
509
|
+
stripe_customer_id,
|
|
510
|
+
payment_intent_id: payment_intent.id,
|
|
511
|
+
client_secret,
|
|
512
|
+
status: subscription.status,
|
|
513
|
+
};
|
|
514
|
+
}
|
|
515
|
+
// If payment method was rejected/invalid, surface a user-actionable error.
|
|
516
|
+
if (payment_intent && payment_intent.status === "requires_payment_method") {
|
|
517
|
+
throw new ExternalStripeError("payment_method_missing", "Your default payment method could not be charged. Please update your payment method and try again.", { subscription_id: subscription.id, payment_intent_id: payment_intent.id });
|
|
518
|
+
}
|
|
519
|
+
// Some accounts/flows may produce an "incomplete" subscription without an expanded PI; treat as internal inconsistency.
|
|
520
|
+
if (subscription.status === "incomplete" || subscription.status === "incomplete_expired") {
|
|
521
|
+
throw new InternalStripeError("subscription_create_error", "Subscription was created in an incomplete state without a resolvable PaymentIntent.", {
|
|
522
|
+
uid: opts.uid,
|
|
523
|
+
stripe_customer_id,
|
|
524
|
+
subscription_id: subscription.id,
|
|
525
|
+
status: subscription.status,
|
|
526
|
+
has_latest_invoice: subscription.latest_invoice != null,
|
|
527
|
+
has_payment_intent: payment_intent != null,
|
|
528
|
+
});
|
|
529
|
+
}
|
|
530
|
+
// Otherwise, consider it created (active/trialing/past_due/etc).
|
|
531
|
+
return {
|
|
532
|
+
type: "created",
|
|
533
|
+
subscription_id: subscription.id,
|
|
534
|
+
stripe_customer_id,
|
|
535
|
+
status: subscription.status,
|
|
536
|
+
};
|
|
537
|
+
}
|
|
538
|
+
/**
|
|
539
|
+
* Cancel a user's Stripe subscription(s) matching a specific subscription plan.
|
|
540
|
+
*
|
|
541
|
+
* Important behavior change:
|
|
542
|
+
* - If the matched Stripe Subscription contains multiple subscription items (multiple plans),
|
|
543
|
+
* we still cancel the *entire* subscription (all items) to keep semantics simple and consistent.
|
|
544
|
+
*
|
|
545
|
+
* Stripe docs:
|
|
546
|
+
* - Cancel a subscription: https://docs.stripe.com/api/subscriptions/cancel
|
|
547
|
+
* - Update a subscription (cancel_at_period_end): https://docs.stripe.com/api/subscriptions/update
|
|
548
|
+
* - List subscriptions: https://docs.stripe.com/api/subscriptions/list
|
|
549
|
+
*
|
|
550
|
+
* @note If a user is not subscribed to the specified plan, this function succeeds without doing anything (idempotent).
|
|
551
|
+
*
|
|
552
|
+
* @returns The affected subscriptions which were canceled/updated.
|
|
553
|
+
*/
|
|
554
|
+
export async function cancel_user_subscription(client, server, opts) {
|
|
555
|
+
// -------------------------------------------------------------------------
|
|
556
|
+
// Validate inputs.
|
|
557
|
+
const uid = opts.uid;
|
|
558
|
+
/** Validate uid so we never act on an ambiguous identity. */
|
|
559
|
+
assert(uid.trim().length > 0, "invalid_argument", "Uid must be a non-empty string.", { uid });
|
|
560
|
+
/** Validate plan fields so we only match the intended Stripe price. */
|
|
561
|
+
assert(opts.plan.id.trim().length > 0, "invalid_argument", "Plan.id must be non-empty.", { plan_id: opts.plan.id });
|
|
562
|
+
assert(opts.plan.stripe_price_id.trim().length > 0, "invalid_argument", "Plan.stripe_price_id must be non-empty.", { plan_id: opts.plan.id });
|
|
563
|
+
const cancel_at_period_end = opts.cancel_at_period_end ?? true;
|
|
564
|
+
// Any mutation can change entitlements; invalidate cache up-front to reduce stale reads.
|
|
565
|
+
delete_subscription_caches(uid);
|
|
566
|
+
// -------------------------------------------------------------------------
|
|
567
|
+
// Fetch subscriptions.
|
|
568
|
+
/** Fetch all subscriptions (paginated). */
|
|
569
|
+
const subscriptions = await list_all_customer_subscriptions(client, server, uid, opts.customer_id);
|
|
570
|
+
// -------------------------------------------------------------------------
|
|
571
|
+
// Identify matching subscriptions.
|
|
572
|
+
// Subscription statuses that represent a live (entitlement-granting) subscription.
|
|
573
|
+
const active_sub_status = new Set([
|
|
574
|
+
"active",
|
|
575
|
+
"trialing",
|
|
576
|
+
"past_due", // since Stripe can keep subscriptions in past_due while retrying payment.
|
|
577
|
+
]);
|
|
578
|
+
const affected_subscriptions = [];
|
|
579
|
+
for (const subscription of subscriptions) {
|
|
580
|
+
// We only operate on active-ish subscriptions; canceled/unpaid ones are ignored.
|
|
581
|
+
if (!active_sub_status.has(subscription.status)) {
|
|
582
|
+
continue;
|
|
583
|
+
}
|
|
584
|
+
// Match if any subscription item uses the plan's Stripe price id.
|
|
585
|
+
let matches_plan = false;
|
|
586
|
+
for (const item of subscription.items.data) {
|
|
587
|
+
const price = item.price;
|
|
588
|
+
if (!price) {
|
|
589
|
+
continue;
|
|
590
|
+
}
|
|
591
|
+
if (price.id === opts.plan.stripe_price_id) {
|
|
592
|
+
matches_plan = true;
|
|
593
|
+
break;
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
if (!matches_plan) {
|
|
597
|
+
continue;
|
|
598
|
+
}
|
|
599
|
+
affected_subscriptions.push(subscription);
|
|
600
|
+
}
|
|
601
|
+
// Idempotent semantics: if nothing matches, succeed without doing anything.
|
|
602
|
+
if (affected_subscriptions.length === 0) {
|
|
603
|
+
// Best-effort cache invalidation so the next read doesn't serve stale positives.
|
|
604
|
+
delete_subscription_caches(uid);
|
|
605
|
+
return [];
|
|
606
|
+
}
|
|
607
|
+
// -------------------------------------------------------------------------
|
|
608
|
+
// Cancel/update subscriptions.
|
|
609
|
+
//
|
|
610
|
+
// NOTE: Even if a subscription contains multiple items (multiple plans),
|
|
611
|
+
// we cancel the entire subscription to avoid partial-cancel surprises.
|
|
612
|
+
await Promise.all(affected_subscriptions.map(async (subscription) => {
|
|
613
|
+
if (cancel_at_period_end) {
|
|
614
|
+
// Non-destructive cancel: the subscription stays active until end of period, then cancels.
|
|
615
|
+
await stripe_api_call(() => client.subscriptions.update(subscription.id, { cancel_at_period_end: true }, { idempotencyKey: stable_idempotency_key(`sub_update_cancel_at_period_end:${subscription.id}`) }), {
|
|
616
|
+
operation: "subscriptions.update",
|
|
617
|
+
action: "cancel_at_period_end",
|
|
618
|
+
uid,
|
|
619
|
+
subscription_id: subscription.id,
|
|
620
|
+
plan_id: opts.plan.id,
|
|
621
|
+
stripe_price_id: opts.plan.stripe_price_id,
|
|
622
|
+
});
|
|
623
|
+
}
|
|
624
|
+
else {
|
|
625
|
+
// Immediate cancellation.
|
|
626
|
+
await stripe_api_call(() => client.subscriptions.cancel(subscription.id, undefined, { idempotencyKey: stable_idempotency_key(`sub_cancel_immediate:${subscription.id}`) }), {
|
|
627
|
+
operation: "subscriptions.cancel",
|
|
628
|
+
action: "cancel_immediately",
|
|
629
|
+
uid,
|
|
630
|
+
subscription_id: subscription.id,
|
|
631
|
+
plan_id: opts.plan.id,
|
|
632
|
+
stripe_price_id: opts.plan.stripe_price_id,
|
|
633
|
+
});
|
|
634
|
+
}
|
|
635
|
+
}));
|
|
636
|
+
// Mutation happened: invalidate cache again post-mutation to reduce stale reads.
|
|
637
|
+
delete_subscription_caches(uid);
|
|
638
|
+
// Response.
|
|
639
|
+
return affected_subscriptions;
|
|
640
|
+
}
|
|
641
|
+
/**
|
|
642
|
+
* Enforce that a user can only have **one active subscription plan per SubscriptionProduct**.
|
|
643
|
+
*
|
|
644
|
+
* Workflow:
|
|
645
|
+
* 1. Accepts the newly created/updated Stripe.Subscription (expanded items.price).
|
|
646
|
+
* 2. Resolves which internal SubscriptionProduct it belongs to by matching *all* known plan price IDs.
|
|
647
|
+
* 3. Lists all customer subscriptions.
|
|
648
|
+
* 4. Cancels (immediately) all other active subscriptions that contain *any* plan
|
|
649
|
+
* from the same SubscriptionProduct, excluding the newly created subscription.
|
|
650
|
+
*
|
|
651
|
+
* Safety guarantees:
|
|
652
|
+
* - Does nothing if the new subscription is not live.
|
|
653
|
+
* - Does nothing if the subscription does not map to any known SubscriptionProduct.
|
|
654
|
+
* - Cancels only subscriptions that clearly overlap with the same SubscriptionProduct.
|
|
655
|
+
*/
|
|
656
|
+
export async function enforce_single_subscription_plan(client, server, opts) {
|
|
657
|
+
assert(opts.uid.length > 0, "invalid_argument", "uid must be provided");
|
|
658
|
+
assert(opts.stripe_customer_id.length > 0, "invalid_argument", "stripe_customer_id must be provided");
|
|
659
|
+
const { new_subscription, all_products } = opts;
|
|
660
|
+
// Subscription statuses that represent a live (entitlement-granting) subscription.
|
|
661
|
+
// Keep inline to avoid confusion with `active_subscription_statuses`.
|
|
662
|
+
const active_sub_status = new Set([
|
|
663
|
+
"active",
|
|
664
|
+
"trialing",
|
|
665
|
+
"past_due",
|
|
666
|
+
]);
|
|
667
|
+
// Only enforce for live subscriptions
|
|
668
|
+
if (!active_sub_status.has(new_subscription.status)) {
|
|
669
|
+
return [];
|
|
670
|
+
}
|
|
671
|
+
// Build a map of price_id -> SubscriptionProduct
|
|
672
|
+
const price_to_subscription_product = new Map();
|
|
673
|
+
for (const product of all_products) {
|
|
674
|
+
if (product.type !== "subscription")
|
|
675
|
+
continue;
|
|
676
|
+
for (const plan of product.plans) {
|
|
677
|
+
price_to_subscription_product.set(plan.stripe_price_id, product);
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
// Resolve which SubscriptionProduct this new subscription belongs to
|
|
681
|
+
const resolved_products = new Set();
|
|
682
|
+
for (const item of new_subscription.items.data) {
|
|
683
|
+
const price = item.price;
|
|
684
|
+
if (!price)
|
|
685
|
+
continue;
|
|
686
|
+
// Enforce expanded price object, otherwise we can't safely map price ids.
|
|
687
|
+
assert(typeof price !== "string", "invalid_argument", "new_subscription.items.data.price must be expanded.", {
|
|
688
|
+
subscription_id: new_subscription.id,
|
|
689
|
+
});
|
|
690
|
+
const sub_product = price_to_subscription_product.get(price.id);
|
|
691
|
+
if (sub_product) {
|
|
692
|
+
resolved_products.add(sub_product);
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
// If we can't resolve exactly one subscription product, do nothing
|
|
696
|
+
if (resolved_products.size === 0) {
|
|
697
|
+
return [];
|
|
698
|
+
}
|
|
699
|
+
if (resolved_products.size > 1) {
|
|
700
|
+
throw new InternalStripeError("subscription_resolution_error", "Subscription resolves to multiple subscription products.", {
|
|
701
|
+
subscription_id: new_subscription.id,
|
|
702
|
+
subscription_product_ids: [...resolved_products].map((p) => p.id),
|
|
703
|
+
});
|
|
704
|
+
}
|
|
705
|
+
const [subscription_product] = [...resolved_products];
|
|
706
|
+
// All Stripe price ids belonging to this SubscriptionProduct
|
|
707
|
+
const product_price_ids = new Set(subscription_product.plans.map((p) => p.stripe_price_id));
|
|
708
|
+
// Fetch all customer subscriptions
|
|
709
|
+
const all_subscriptions = await list_all_customer_subscriptions(client, server, opts.uid, opts.stripe_customer_id);
|
|
710
|
+
const canceled = [];
|
|
711
|
+
for (const sub of all_subscriptions) {
|
|
712
|
+
// Skip the newly created subscription
|
|
713
|
+
if (sub.id === new_subscription.id)
|
|
714
|
+
continue;
|
|
715
|
+
// Only cancel live subscriptions
|
|
716
|
+
if (!active_sub_status.has(sub.status))
|
|
717
|
+
continue;
|
|
718
|
+
// Check if this subscription contains *any* plan from the same SubscriptionProduct
|
|
719
|
+
let overlaps = false;
|
|
720
|
+
for (const item of sub.items.data) {
|
|
721
|
+
const price = item.price;
|
|
722
|
+
if (!price)
|
|
723
|
+
continue;
|
|
724
|
+
assert(typeof price !== "string", "api_error", "Expected expanded item.price from subscriptions.list expand.", {
|
|
725
|
+
subscription_id: sub.id,
|
|
726
|
+
});
|
|
727
|
+
if (product_price_ids.has(price.id)) {
|
|
728
|
+
overlaps = true;
|
|
729
|
+
break;
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
if (!overlaps)
|
|
733
|
+
continue;
|
|
734
|
+
// Cancel immediately (idempotent)
|
|
735
|
+
await stripe_api_call(() => client.subscriptions.cancel(sub.id, undefined, {
|
|
736
|
+
idempotencyKey: opts.idempotency_key ?? stable_idempotency_key(`enforce_single_plan:${opts.stripe_customer_id}:${new_subscription.id}:${sub.id}`),
|
|
737
|
+
}), {
|
|
738
|
+
operation: "subscriptions.cancel",
|
|
739
|
+
action: "enforce_single_plan",
|
|
740
|
+
uid: opts.uid,
|
|
741
|
+
stripe_customer_id: opts.stripe_customer_id,
|
|
742
|
+
kept_subscription_id: new_subscription.id,
|
|
743
|
+
canceled_subscription_id: sub.id,
|
|
744
|
+
subscription_product_id: subscription_product.id,
|
|
745
|
+
});
|
|
746
|
+
canceled.push(sub);
|
|
747
|
+
}
|
|
748
|
+
// Invalidate cache.
|
|
749
|
+
if (canceled.length > 0) {
|
|
750
|
+
delete_subscription_caches(opts.uid);
|
|
751
|
+
}
|
|
752
|
+
// Return.
|
|
753
|
+
return canceled;
|
|
754
|
+
}
|