2021-12-01 17:16:01 +01:00
|
|
|
import arrow
|
2020-12-14 11:34:59 +01:00
|
|
|
from coinbase_commerce import Client
|
2019-11-14 15:05:20 +01:00
|
|
|
from flask import render_template, flash, redirect, url_for
|
|
|
|
from flask_login import login_required, current_user
|
|
|
|
|
|
|
|
from app.config import (
|
|
|
|
PADDLE_VENDOR_ID,
|
|
|
|
PADDLE_MONTHLY_PRODUCT_ID,
|
|
|
|
PADDLE_YEARLY_PRODUCT_ID,
|
|
|
|
URL,
|
2020-12-14 11:34:59 +01:00
|
|
|
COINBASE_YEARLY_PRICE,
|
|
|
|
COINBASE_API_KEY,
|
2019-11-14 15:05:20 +01:00
|
|
|
)
|
|
|
|
from app.dashboard.base import dashboard_bp
|
2022-06-08 17:36:03 +02:00
|
|
|
from app.extensions import limiter
|
2020-12-14 11:34:59 +01:00
|
|
|
from app.log import LOG
|
2021-12-01 17:16:01 +01:00
|
|
|
from app.models import (
|
|
|
|
AppleSubscription,
|
|
|
|
Subscription,
|
|
|
|
ManualSubscription,
|
|
|
|
CoinbaseSubscription,
|
2022-06-23 12:26:02 +02:00
|
|
|
PartnerUser,
|
|
|
|
PartnerSubscription,
|
2021-12-01 17:16:01 +01:00
|
|
|
)
|
2022-06-23 12:26:02 +02:00
|
|
|
from app.proton.utils import get_proton_partner
|
2019-11-14 15:05:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
@dashboard_bp.route("/pricing", methods=["GET", "POST"])
|
|
|
|
@login_required
|
|
|
|
def pricing():
|
2021-12-01 17:41:20 +01:00
|
|
|
if current_user.lifetime:
|
|
|
|
flash("You already have a lifetime subscription", "error")
|
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
|
2022-06-28 17:21:23 +02:00
|
|
|
paddle_sub: Subscription = current_user.get_paddle_subscription()
|
2021-12-01 17:16:01 +01:00
|
|
|
# user who has canceled can re-subscribe
|
2022-06-28 17:21:23 +02:00
|
|
|
if paddle_sub and not paddle_sub.cancelled:
|
2021-12-01 17:16:01 +01:00
|
|
|
flash("You already have an active subscription", "error")
|
2019-11-14 15:05:20 +01:00
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
|
2021-12-01 17:16:01 +01:00
|
|
|
now = arrow.now()
|
|
|
|
manual_sub: ManualSubscription = ManualSubscription.filter(
|
|
|
|
ManualSubscription.user_id == current_user.id, ManualSubscription.end_at > now
|
|
|
|
).first()
|
|
|
|
|
|
|
|
coinbase_sub = CoinbaseSubscription.filter(
|
|
|
|
CoinbaseSubscription.user_id == current_user.id,
|
|
|
|
CoinbaseSubscription.end_at > now,
|
|
|
|
).first()
|
|
|
|
|
2021-09-27 09:59:33 +02:00
|
|
|
apple_sub: AppleSubscription = AppleSubscription.get_by(user_id=current_user.id)
|
|
|
|
if apple_sub and apple_sub.is_valid():
|
|
|
|
flash("Please make sure to cancel your subscription on Apple first", "warning")
|
|
|
|
|
2022-06-23 12:26:02 +02:00
|
|
|
proton_upgrade = False
|
|
|
|
partner_user = PartnerUser.get_by(user_id=current_user.id)
|
|
|
|
if partner_user:
|
|
|
|
partner_sub = PartnerSubscription.get_by(partner_user_id=partner_user.id)
|
|
|
|
if partner_sub and partner_sub.is_active():
|
|
|
|
flash(
|
|
|
|
f"You already have a subscription provided by {partner_user.partner.name}",
|
|
|
|
"error",
|
|
|
|
)
|
|
|
|
return redirect(url_for("dashboard.index"))
|
2022-06-28 12:36:21 +02:00
|
|
|
proton_upgrade = partner_user.partner_id == get_proton_partner().id
|
2022-06-23 12:26:02 +02:00
|
|
|
|
2019-11-14 15:05:20 +01:00
|
|
|
return render_template(
|
|
|
|
"dashboard/pricing.html",
|
|
|
|
PADDLE_VENDOR_ID=PADDLE_VENDOR_ID,
|
|
|
|
PADDLE_MONTHLY_PRODUCT_ID=PADDLE_MONTHLY_PRODUCT_ID,
|
|
|
|
PADDLE_YEARLY_PRODUCT_ID=PADDLE_YEARLY_PRODUCT_ID,
|
|
|
|
success_url=URL + "/dashboard/subscription_success",
|
2021-12-01 17:16:01 +01:00
|
|
|
manual_sub=manual_sub,
|
|
|
|
coinbase_sub=coinbase_sub,
|
|
|
|
now=now,
|
2022-06-23 12:26:02 +02:00
|
|
|
proton_upgrade=proton_upgrade,
|
2019-11-14 15:05:20 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@dashboard_bp.route("/subscription_success")
|
|
|
|
@login_required
|
|
|
|
def subscription_success():
|
2023-03-23 21:11:05 +01:00
|
|
|
return render_template(
|
|
|
|
"dashboard/thank-you.html",
|
|
|
|
)
|
2020-12-14 11:34:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
@dashboard_bp.route("/coinbase_checkout")
|
|
|
|
@login_required
|
2022-06-08 17:36:03 +02:00
|
|
|
@limiter.limit("5/minute")
|
2020-12-14 11:34:59 +01:00
|
|
|
def coinbase_checkout_route():
|
|
|
|
client = Client(api_key=COINBASE_API_KEY)
|
|
|
|
charge = client.charge.create(
|
2020-12-14 11:52:35 +01:00
|
|
|
name="1 Year SimpleLogin Premium Subscription",
|
2020-12-14 11:34:59 +01:00
|
|
|
local_price={"amount": str(COINBASE_YEARLY_PRICE), "currency": "USD"},
|
|
|
|
pricing_type="fixed_price",
|
|
|
|
metadata={"user_id": current_user.id},
|
|
|
|
)
|
|
|
|
|
|
|
|
LOG.d("Create coinbase charge %s", charge)
|
|
|
|
|
|
|
|
return redirect(charge["hosted_url"])
|