[PR #374] [MERGED] Load Stripe plan-price mapping from env with startup validation #680

Closed
opened 2026-03-13 21:03:27 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/AJaySi/ALwrity/pull/374
Author: @AJaySi
Created: 3/4/2026
Status: Merged
Merged: 3/5/2026
Merged by: @AJaySi

Base: mainHead: codex/move-stripe_plan_price_mapping-to-env-driven-config


📝 Commits (1)

  • fc96e12 Move Stripe plan price mapping to env with startup validation

📊 Changes

4 files changed (+120 additions, -12 deletions)

View changed files

📝 backend/README.md (+20 -0)
📝 backend/services/subscription/stripe_service.py (+77 -3)
📝 docs/Billing_Subscription/stripe-dev-guide.md (+16 -6)
📝 docs/Billing_Subscription/stripe-go-live-checklist.md (+7 -3)

📄 Description

Motivation

  • Replace hardcoded Stripe price IDs with environment-driven configuration so test and live deployments can use different mappings without code edits.
  • Fail fast at startup when mapping is missing, malformed, or missing required tier/cycle keys to avoid runtime checkout/webhook errors.

Description

  • Replaced the in-code STRIPE_PLAN_PRICE_MAPPING with an environment-driven loader in backend/services/subscription/stripe_service.py that: detects mode via STRIPE_MODE or STRIPE_SECRET_KEY prefix, prefers STRIPE_PLAN_PRICE_MAPPING_LIVE/_TEST and falls back to STRIPE_PLAN_PRICE_MAPPING, parses JSON, normalizes keys, and builds the runtime mapping and reverse map (STRIPE_PRICE_TO_PLAN).
  • Added strict validation (REQUIRED_STRIPE_PLAN_KEYS) to ensure required combinations (currently basic.monthly and pro.monthly) exist and that price IDs are non-empty, raising RuntimeError on failure.
  • Kept existing runtime lookup behavior (_get_price_id_for_plan, _get_plan_for_price_id) intact but now backed by the validated mapping.
  • Updated documentation: docs/Billing_Subscription/stripe-go-live-checklist.md, docs/Billing_Subscription/stripe-dev-guide.md, and backend/README.md to document the new env vars and expected JSON mapping format and required keys.

Testing

  • Compiled the modified module: python -m py_compile backend/services/subscription/stripe_service.py (succeeded).
  • Verified successful import with a valid test mapping by setting STRIPE_PLAN_PRICE_MAPPING_TEST='{"basic":{"monthly":"price_basic"},"pro":{"monthly":"price_pro"}}' and importing STRIPE_PLAN_PRICE_MAPPING (succeeded and returned expected price id).
  • Verified fail-fast behaviour by setting STRIPE_PLAN_PRICE_MAPPING_TEST='{"basic":{"monthly":"price_basic"}}' and importing the module, which raised the expected RuntimeError for the missing required pro.monthly mapping.

Codex Task


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/AJaySi/ALwrity/pull/374 **Author:** [@AJaySi](https://github.com/AJaySi) **Created:** 3/4/2026 **Status:** ✅ Merged **Merged:** 3/5/2026 **Merged by:** [@AJaySi](https://github.com/AJaySi) **Base:** `main` ← **Head:** `codex/move-stripe_plan_price_mapping-to-env-driven-config` --- ### 📝 Commits (1) - [`fc96e12`](https://github.com/AJaySi/ALwrity/commit/fc96e1218a83df452df51fd38b06d2fb752bea37) Move Stripe plan price mapping to env with startup validation ### 📊 Changes **4 files changed** (+120 additions, -12 deletions) <details> <summary>View changed files</summary> 📝 `backend/README.md` (+20 -0) 📝 `backend/services/subscription/stripe_service.py` (+77 -3) 📝 `docs/Billing_Subscription/stripe-dev-guide.md` (+16 -6) 📝 `docs/Billing_Subscription/stripe-go-live-checklist.md` (+7 -3) </details> ### 📄 Description ### Motivation - Replace hardcoded Stripe price IDs with environment-driven configuration so test and live deployments can use different mappings without code edits. - Fail fast at startup when mapping is missing, malformed, or missing required tier/cycle keys to avoid runtime checkout/webhook errors. ### Description - Replaced the in-code `STRIPE_PLAN_PRICE_MAPPING` with an environment-driven loader in `backend/services/subscription/stripe_service.py` that: detects mode via `STRIPE_MODE` or `STRIPE_SECRET_KEY` prefix, prefers `STRIPE_PLAN_PRICE_MAPPING_LIVE`/`_TEST` and falls back to `STRIPE_PLAN_PRICE_MAPPING`, parses JSON, normalizes keys, and builds the runtime mapping and reverse map (`STRIPE_PRICE_TO_PLAN`). - Added strict validation (`REQUIRED_STRIPE_PLAN_KEYS`) to ensure required combinations (currently `basic.monthly` and `pro.monthly`) exist and that price IDs are non-empty, raising `RuntimeError` on failure. - Kept existing runtime lookup behavior (`_get_price_id_for_plan`, `_get_plan_for_price_id`) intact but now backed by the validated mapping. - Updated documentation: `docs/Billing_Subscription/stripe-go-live-checklist.md`, `docs/Billing_Subscription/stripe-dev-guide.md`, and `backend/README.md` to document the new env vars and expected JSON mapping format and required keys. ### Testing - Compiled the modified module: `python -m py_compile backend/services/subscription/stripe_service.py` (succeeded). - Verified successful import with a valid test mapping by setting `STRIPE_PLAN_PRICE_MAPPING_TEST='{"basic":{"monthly":"price_basic"},"pro":{"monthly":"price_pro"}}'` and importing `STRIPE_PLAN_PRICE_MAPPING` (succeeded and returned expected price id). - Verified fail-fast behaviour by setting `STRIPE_PLAN_PRICE_MAPPING_TEST='{"basic":{"monthly":"price_basic"}}'` and importing the module, which raised the expected `RuntimeError` for the missing required `pro.monthly` mapping. ------ [Codex Task](https://chatgpt.com/codex/tasks/task_e_69a8468ba6588328aa3eb7e5de9f4fe2) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
kerem 2026-03-13 21:03:27 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/ALwrity#680
No description provided.