Skip to content

Commit

Permalink
fix subscription upgrade on indian cards: cancel & create new subscri…
Browse files Browse the repository at this point in the history
…ption on failure
  • Loading branch information
nikochiko committed Aug 27, 2024
1 parent 8e8bff3 commit 0a0eec8
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions daras_ai_v2/billing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import gooey_gui as gui
import sentry_sdk
import stripe
from django.core.exceptions import ValidationError

Expand Down Expand Up @@ -227,12 +228,22 @@ def _render_plan_action_button(
):
modal.open()
if confirmed:
change_subscription(
user,
plan,
# when upgrading, charge the full new amount today: https://docs.stripe.com/billing/subscriptions/billing-cycle#reset-the-billing-cycle-to-the-current-time
billing_cycle_anchor="now",
)
try:
change_subscription(
user,
plan,
# when upgrading, charge the full new amount today: https://docs.stripe.com/billing/subscriptions/billing-cycle#reset-the-billing-cycle-to-the-current-time
billing_cycle_anchor="now",
payment_behavior="error_if_incomplete",
)
except (stripe.CardError, stripe.InvalidRequestError) as e:
if isinstance(e, stripe.InvalidRequestError):
sentry_sdk.capture_exception(e)

# only handle error if it's related to mandates
# cancel current subscription & redirect user to new subscription page
user.subscription.cancel()
stripe_subscription_create(user=user, plan=plan)
else:
modal, confirmed = confirm_modal(
title="Downgrade Plan",
Expand Down

0 comments on commit 0a0eec8

Please sign in to comment.