handle the Paddle 147 error

This commit is contained in:
Son NK 2020-10-12 17:37:04 +02:00
parent 8268568f08
commit 6557b7157f
2 changed files with 30 additions and 14 deletions

View File

@ -38,34 +38,40 @@ def billing():
return redirect(url_for("dashboard.billing"))
elif request.form.get("form-name") == "change-monthly":
LOG.debug(f"User {current_user} changes to monthly plan")
success = change_plan(sub.subscription_id, PADDLE_MONTHLY_PRODUCT_ID)
success, msg = change_plan(sub.subscription_id, PADDLE_MONTHLY_PRODUCT_ID)
if success:
sub.plan = PlanEnum.monthly
db.session.commit()
flash("Your subscription has been updated", "success")
else:
flash(
"Something went wrong, sorry for the inconvenience. Please retry. "
"We are already notified and will be on it asap",
"error",
)
if msg:
flash(msg, "error")
else:
flash(
"Something went wrong, sorry for the inconvenience. Please retry. "
"We are already notified and will be on it asap",
"error",
)
return redirect(url_for("dashboard.billing"))
elif request.form.get("form-name") == "change-yearly":
LOG.debug(f"User {current_user} changes to yearly plan")
success = change_plan(sub.subscription_id, PADDLE_YEARLY_PRODUCT_ID)
success, msg = change_plan(sub.subscription_id, PADDLE_YEARLY_PRODUCT_ID)
if success:
sub.plan = PlanEnum.yearly
db.session.commit()
flash("Your subscription has been updated", "success")
else:
flash(
"Something went wrong, sorry for the inconvenience. Please retry. "
"We are already notified and will be on it asap",
"error",
)
if msg:
flash(msg, "error")
else:
flash(
"Something went wrong, sorry for the inconvenience. Please retry. "
"We are already notified and will be on it asap",
"error",
)
return redirect(url_for("dashboard.billing"))

View File

@ -78,7 +78,8 @@ def cancel_subscription(subscription_id: int) -> bool:
return res["success"]
def change_plan(subscription_id: int, plan_id) -> bool:
def change_plan(subscription_id: str, plan_id) -> (bool, str):
"""return whether the operation is successful and an optional error message"""
r = requests.post(
"https://vendors.paddle.com/api/2.0/subscription/users/update",
data={
@ -93,5 +94,14 @@ def change_plan(subscription_id: int, plan_id) -> bool:
LOG.exception(
f"cannot change subscription {subscription_id} to {plan_id}, paddle response: {res}"
)
try:
# "unable to complete the resubscription because we could not charge the customer for the resubscription"
if res["error"]["code"] == 147:
return False, "Your card cannot be charged"
except:
LOG.warning("Cannot parse error code from %s", res)
return False, ""
return res["success"]
return False, ""
return res["success"], ""