From 6557b7157f880e775fbdf5db8a78bcc240abd9f6 Mon Sep 17 00:00:00 2001 From: Son NK <> Date: Mon, 12 Oct 2020 17:37:04 +0200 Subject: [PATCH] handle the Paddle 147 error --- app/dashboard/views/billing.py | 30 ++++++++++++++++++------------ app/paddle_utils.py | 14 ++++++++++++-- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/app/dashboard/views/billing.py b/app/dashboard/views/billing.py index 9f8def67..de08c434 100644 --- a/app/dashboard/views/billing.py +++ b/app/dashboard/views/billing.py @@ -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")) diff --git a/app/paddle_utils.py b/app/paddle_utils.py index ed953440..20d04042 100644 --- a/app/paddle_utils.py +++ b/app/paddle_utils.py @@ -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"], ""