app-MAIL-temp/app/dashboard/views/billing.py

79 lines
3.1 KiB
Python
Raw Normal View History

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
2020-04-25 11:30:09 +02:00
from app.extensions import db
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
2020-03-08 10:28:13 +01:00
sub: Subscription = current_user.get_subscription()
2019-11-14 15:05:20 +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":
2020-03-09 09:48:33 +01:00
LOG.warning(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
db.session.commit()
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":
LOG.debug(f"User {current_user} changes to monthly plan")
2020-10-12 17:37:04 +02:00
success, msg = change_plan(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
db.session.commit()
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":
LOG.debug(f"User {current_user} changes to yearly plan")
2020-10-12 17:37:04 +02:00
success, msg = change_plan(sub.subscription_id, PADDLE_YEARLY_PRODUCT_ID)
2020-04-12 19:43:07 +02:00
if success:
sub.plan = PlanEnum.yearly
db.session.commit()
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)