2020-03-08 10:28:13 +01:00
|
|
|
from flask import render_template, flash, redirect, url_for, request
|
2019-11-14 15:05:20 +01:00
|
|
|
from flask_login import login_required, current_user
|
|
|
|
|
2020-04-12 19:43:07 +02:00
|
|
|
from app.config import PADDLE_MONTHLY_PRODUCT_ID, PADDLE_YEARLY_PRODUCT_ID
|
2019-11-14 15:05:20 +01:00
|
|
|
from app.dashboard.base import dashboard_bp
|
2021-10-12 14:36:47 +02:00
|
|
|
from app.db import Session
|
2020-03-08 10:28:13 +01:00
|
|
|
from app.log import LOG
|
2020-04-12 19:43:07 +02:00
|
|
|
from app.models import Subscription, PlanEnum
|
|
|
|
from app.paddle_utils import cancel_subscription, change_plan
|
2019-11-14 15:05:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
@dashboard_bp.route("/billing", methods=["GET", "POST"])
|
|
|
|
@login_required
|
|
|
|
def billing():
|
|
|
|
# sanity check: make sure this page is only for user who has paddle subscription
|
2022-06-20 14:34:20 +02:00
|
|
|
sub: Subscription = current_user.get_paddle_subscription()
|
2019-11-14 15:05:20 +01:00
|
|
|
|
2020-01-01 19:46:35 +01:00
|
|
|
if not sub:
|
|
|
|
flash("You don't have any active subscription", "warning")
|
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
|
2020-03-08 10:28:13 +01:00
|
|
|
if request.method == "POST":
|
|
|
|
if request.form.get("form-name") == "cancel":
|
2021-09-08 11:29:55 +02:00
|
|
|
LOG.w(f"User {current_user} cancels their subscription")
|
2020-03-08 10:28:13 +01:00
|
|
|
success = cancel_subscription(sub.subscription_id)
|
|
|
|
|
|
|
|
if success:
|
|
|
|
sub.cancelled = True
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2020-03-08 10:28:13 +01:00
|
|
|
flash("Your subscription has been canceled successfully", "success")
|
|
|
|
else:
|
|
|
|
flash(
|
2020-04-12 19:43:07 +02:00
|
|
|
"Something went wrong, sorry for the inconvenience. Please retry. "
|
|
|
|
"We are already notified and will be on it asap",
|
2020-03-08 10:28:13 +01:00
|
|
|
"error",
|
|
|
|
)
|
|
|
|
|
|
|
|
return redirect(url_for("dashboard.billing"))
|
2020-04-12 19:43:07 +02:00
|
|
|
elif request.form.get("form-name") == "change-monthly":
|
2021-09-08 11:29:55 +02:00
|
|
|
LOG.d(f"User {current_user} changes to monthly plan")
|
2021-03-22 15:52:48 +01:00
|
|
|
success, msg = change_plan(
|
|
|
|
current_user, sub.subscription_id, PADDLE_MONTHLY_PRODUCT_ID
|
|
|
|
)
|
2020-03-08 10:28:13 +01:00
|
|
|
|
2020-04-12 19:43:07 +02:00
|
|
|
if success:
|
|
|
|
sub.plan = PlanEnum.monthly
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2020-04-12 19:43:07 +02:00
|
|
|
flash("Your subscription has been updated", "success")
|
|
|
|
else:
|
2020-10-12 17:37:04 +02:00
|
|
|
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",
|
|
|
|
)
|
2020-04-12 19:43:07 +02:00
|
|
|
|
|
|
|
return redirect(url_for("dashboard.billing"))
|
|
|
|
elif request.form.get("form-name") == "change-yearly":
|
2021-09-08 11:29:55 +02:00
|
|
|
LOG.d(f"User {current_user} changes to yearly plan")
|
2021-03-22 15:52:48 +01:00
|
|
|
success, msg = change_plan(
|
|
|
|
current_user, sub.subscription_id, PADDLE_YEARLY_PRODUCT_ID
|
|
|
|
)
|
2020-04-12 19:43:07 +02:00
|
|
|
|
|
|
|
if success:
|
|
|
|
sub.plan = PlanEnum.yearly
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2020-04-12 19:43:07 +02:00
|
|
|
flash("Your subscription has been updated", "success")
|
|
|
|
else:
|
2020-10-12 17:37:04 +02:00
|
|
|
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",
|
|
|
|
)
|
2020-04-12 19:43:07 +02:00
|
|
|
|
|
|
|
return redirect(url_for("dashboard.billing"))
|
|
|
|
|
|
|
|
return render_template("dashboard/billing.html", sub=sub, PlanEnum=PlanEnum)
|