add more logging

This commit is contained in:
Son NK 2021-03-22 15:52:48 +01:00
parent 2ad1b75e45
commit 7ab64d9768
2 changed files with 13 additions and 3 deletions

View File

@ -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

View File

@ -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(