diff --git a/app/dashboard/views/billing.py b/app/dashboard/views/billing.py index de08c434..4f3b6b01 100644 --- a/app/dashboard/views/billing.py +++ b/app/dashboard/views/billing.py @@ -38,7 +38,9 @@ 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, msg = change_plan(sub.subscription_id, PADDLE_MONTHLY_PRODUCT_ID) + success, msg = change_plan( + current_user, sub.subscription_id, PADDLE_MONTHLY_PRODUCT_ID + ) if success: sub.plan = PlanEnum.monthly @@ -57,7 +59,9 @@ def billing(): 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, msg = change_plan(sub.subscription_id, PADDLE_YEARLY_PRODUCT_ID) + success, msg = change_plan( + current_user, sub.subscription_id, PADDLE_YEARLY_PRODUCT_ID + ) if success: sub.plan = PlanEnum.yearly diff --git a/app/paddle_utils.py b/app/paddle_utils.py index 556900b9..22398cd4 100644 --- a/app/paddle_utils.py +++ b/app/paddle_utils.py @@ -19,6 +19,7 @@ from app.config import PADDLE_PUBLIC_KEY_PATH, PADDLE_VENDOR_ID, PADDLE_AUTH_COD # Your Paddle public key. from app.log import LOG +from app.models import User with open(PADDLE_PUBLIC_KEY_PATH) as f: public_key = f.read() @@ -78,7 +79,7 @@ def cancel_subscription(subscription_id: int) -> bool: return res["success"] -def change_plan(subscription_id: str, plan_id) -> (bool, str): +def change_plan(user: User, 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", @@ -94,6 +95,11 @@ def change_plan(subscription_id: str, plan_id) -> (bool, str): try: # "unable to complete the resubscription because we could not charge the customer for the resubscription" if res["error"]["code"] == 147: + LOG.w( + "could not charge the customer for the resubscription error %s,%s", + subscription_id, + user, + ) return False, "Your card cannot be charged" except KeyError: LOG.exception(