#madmark00178_api
1 messages ¡ Page 1 of 1 (latest)
đ Welcome to your new thread!
â˛ď¸ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.
âąď¸ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.
đ This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1272958324580548649
đ Have more to share? Add more details, code, screenshots, videos, etc. below.
This is my python code for modifying the subscription:
@app.post("/modify-subscription")
def modify_subscription(data: ModifySubscriptionData, current_user: str = Depends(get_current_user)):
action = data.action
new_plan_id = data.new_plan_id
if action not in ["upgrade", "downgrade", "skip", "cancel"]:
raise HTTPException(status_code=400, detail=f"Invalid action, action was {action}")
user_id = current_user.get("sub")
logger.debug(f"modify-subscription: user {user_id}, new subscription data: {data}")
try:
user = get_user(user_id=user_id, tag="modify-subscription")
if not user:
raise HTTPException(status_code=400, detail="User doesn't exist in database")
if not user.subscription_id:
raise HTTPException(status_code=400, detail="No subscription found for that user")
current_subscription = stripe.Subscription.retrieve(user.subscription_id)
is_trial = current_subscription["status"] == "trialing"
if action == "cancel":
updated_subscription = stripe.Subscription.modify(
user.subscription_id,
items=[
{
"id": user.subscription_item_id,
"price": new_plan_id,
}
],
cancel_at_period_end="true",
proration_behavior="none",
)
Hello! Can you give me the IDs for the two Invoices that were paid?
elif action == "upgrade" or action == "skip":
if not new_plan_id:
raise HTTPException(status_code=400, detail="No new plan ID provided")
updated_subscription = stripe.Subscription.modify(
user.subscription_id,
items=[
{
"id": user.subscription_item_id,
"price": new_plan_id,
}
],
billing_cycle_anchor="now",
proration_behavior="none",
trial_end="now",
)
elif action == "downgrade":
if not new_plan_id:
raise HTTPException(status_code=400, detail="No new plan ID provided")
anchor = "now" if is_trial else "unchanged"
updated_subscription = stripe.Subscription.modify(
user.subscription_id,
items=[{"id": user.subscription_item_id, "price": new_plan_id}],
billing_cycle_anchor=anchor,
proration_behavior="none",
trial_end="now",
)
return updated_subscription
except stripe.error.StripeError as e:
logger.error(f"modify-subscription: StripeError {e}", exc_info=True)
raise HTTPException(status_code=400, detail=str(e))
except Exception as e:
logger.error(f"modify-subscription: Exception {e}", exc_info=True)
raise HTTPException(status_code=500, detail=str(e))
Yes, one sec
in_1PfNRTCAMZtSw1NM8RcBU06z
in_1PfOaDCAMZtSw1NMSzmm8xCv
I'm not sure I understand, in_1PfNRTCAMZtSw1NM8RcBU06z was not paid.
One sec
Sorry it wasn't actually charged, i cancelled it manually, but the point is that it was retrying the person's accout even though they already changed the subscription
I realize its a very specific case, but do i need to modify my upgrade/downgrade function to cancel existing invoices?
Oh, I think I understand. You're saying that you were expecting the unpaid Invoice to void/cancel itself when the Subscription was upgraded, but it didn't, so there were two outstanding Invoices at the same time for the same Subscripton?
Yep
I guess 1) is that not correct to expect that, and if not, 2) whats the best way to handle this
Gotcha. That's not how our Subscriptions system works. Upgrading a Subscription doesn't have any impact on existing outstanding Invoices. If you want to void those, you need to do so yourself during the upgrade process.
Got it
The specifics of how you go about that are up to you, and depend on what exactly you want to happen. It might be as simple as looking at the latest_invoice on the Subscription and voiding that before you upgrade. You could also list oustanding Invoices for the Subscription and loop through them and handle each one as needed for a more robust solution: https://docs.stripe.com/api/invoices/list
Got it thanks!
Happy to help!