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 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: def bootstrapStripePrices() -> None:
"""Ensure all paid plans have separate Stripe Products for users and instances.""" """Ensure all paid plans have separate Stripe Products for users and instances."""
try: try:
@ -183,30 +200,37 @@ def bootstrapStripePrices() -> None:
hasAllPrices = mapping.stripePriceIdUsers and mapping.stripePriceIdInstances hasAllPrices = mapping.stripePriceIdUsers and mapping.stripePriceIdInstances
hasAllProducts = mapping.stripeProductIdUsers and mapping.stripeProductIdInstances hasAllProducts = mapping.stripeProductIdUsers and mapping.stripeProductIdInstances
if hasAllPrices and hasAllProducts: if hasAllPrices and hasAllProducts:
changed = False if _validateStripeIdsExist(stripe, mapping):
reconciledUsers = _reconcilePrice( changed = False
stripe, mapping.stripeProductIdUsers, mapping.stripePriceIdUsers, reconciledUsers = _reconcilePrice(
plan.pricePerUserCHF, interval, f"{planKey} — Benutzer-Lizenz", stripe, mapping.stripeProductIdUsers, mapping.stripePriceIdUsers,
) plan.pricePerUserCHF, interval, f"{planKey} — Benutzer-Lizenz",
if reconciledUsers != mapping.stripePriceIdUsers: )
changed = True if reconciledUsers != mapping.stripePriceIdUsers:
changed = True
reconciledInstances = _reconcilePrice( reconciledInstances = _reconcilePrice(
stripe, mapping.stripeProductIdInstances, mapping.stripePriceIdInstances, stripe, mapping.stripeProductIdInstances, mapping.stripePriceIdInstances,
plan.pricePerFeatureInstanceCHF, interval, f"{planKey} — Feature-Instanz", plan.pricePerFeatureInstanceCHF, interval, f"{planKey} — Feature-Instanz",
) )
if reconciledInstances != mapping.stripePriceIdInstances: if reconciledInstances != mapping.stripePriceIdInstances:
changed = True changed = True
if changed: if changed:
db.recordModify(StripePlanPrice, mapping.id, { db.recordModify(StripePlanPrice, mapping.id, {
"stripePriceIdUsers": reconciledUsers, "stripePriceIdUsers": reconciledUsers,
"stripePriceIdInstances": reconciledInstances, "stripePriceIdInstances": reconciledInstances,
}) })
logger.info("Reconciled Stripe prices for plan %s: users=%s, instances=%s", planKey, reconciledUsers, reconciledInstances) logger.info("Reconciled Stripe prices for plan %s: users=%s, instances=%s", planKey, reconciledUsers, reconciledInstances)
else:
logger.debug("Stripe prices up-to-date for plan %s", planKey)
continue
else: else:
logger.debug("Stripe prices up-to-date for plan %s", planKey) logger.warning(
continue "Stored Stripe IDs for plan %s reference unknown objects "
"(likely wrong Stripe account or copied DB) — re-provisioning.",
planKey,
)
productIdUsers = None productIdUsers = None
productIdInstances = None productIdInstances = None