managed stripe change in env to trigger db refresh

This commit is contained in:
ValueOn AG 2026-03-31 00:24:56 +02:00
parent 3be0967936
commit 7cbcaacda1

View file

@ -154,6 +154,23 @@ def _createStripePrice(stripe, productId: str, unitAmountCHF: float, interval: s
return price.id
def _validateStripeIdsExist(stripe, mapping: StripePlanPrice) -> bool:
"""Quick check whether at least the stored product IDs still exist in Stripe.
Returns False when running against a different Stripe account or after DB copy."""
try:
if mapping.stripeProductIdUsers:
stripe.Product.retrieve(mapping.stripeProductIdUsers)
if mapping.stripeProductIdInstances:
stripe.Product.retrieve(mapping.stripeProductIdInstances)
return True
except Exception as e:
code = getattr(e, "code", None)
if code == "resource_missing":
return False
logger.debug("Stripe validation check failed (non-critical): %s", e)
return False
def bootstrapStripePrices() -> None:
"""Ensure all paid plans have separate Stripe Products for users and instances."""
try:
@ -183,6 +200,7 @@ def bootstrapStripePrices() -> None:
hasAllPrices = mapping.stripePriceIdUsers and mapping.stripePriceIdInstances
hasAllProducts = mapping.stripeProductIdUsers and mapping.stripeProductIdInstances
if hasAllPrices and hasAllProducts:
if _validateStripeIdsExist(stripe, mapping):
changed = False
reconciledUsers = _reconcilePrice(
stripe, mapping.stripeProductIdUsers, mapping.stripePriceIdUsers,
@ -207,6 +225,12 @@ def bootstrapStripePrices() -> None:
else:
logger.debug("Stripe prices up-to-date for plan %s", planKey)
continue
else:
logger.warning(
"Stored Stripe IDs for plan %s reference unknown objects "
"(likely wrong Stripe account or copied DB) — re-provisioning.",
planKey,
)
productIdUsers = None
productIdInstances = None