allow user with manual or coinbase subscription to switch to paddle

This commit is contained in:
Son 2021-12-01 17:16:01 +01:00
parent 5dab819ac3
commit 4d388a202c
3 changed files with 43 additions and 28 deletions

View File

@ -1,3 +1,4 @@
import arrow
from coinbase_commerce import Client
from flask import render_template, flash, redirect, url_for
from flask_login import login_required, current_user
@ -12,16 +13,33 @@ from app.config import (
)
from app.dashboard.base import dashboard_bp
from app.log import LOG
from app.models import AppleSubscription
from app.models import (
AppleSubscription,
Subscription,
ManualSubscription,
CoinbaseSubscription,
)
@dashboard_bp.route("/pricing", methods=["GET", "POST"])
@login_required
def pricing():
if not current_user.can_upgrade():
flash("You are already a premium user", "warning")
sub: Subscription = current_user.get_subscription()
# user who has canceled can re-subscribe
if sub and not sub.cancelled:
flash("You already have an active subscription", "error")
return redirect(url_for("dashboard.index"))
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()
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")
@ -32,6 +50,9 @@ def pricing():
PADDLE_MONTHLY_PRODUCT_ID=PADDLE_MONTHLY_PRODUCT_ID,
PADDLE_YEARLY_PRODUCT_ID=PADDLE_YEARLY_PRODUCT_ID,
success_url=URL + "/dashboard/subscription_success",
manual_sub=manual_sub,
coinbase_sub=coinbase_sub,
now=now,
)

View File

@ -562,31 +562,6 @@ class User(Base, ModelMixin, UserMixin, PasswordOracle):
return True
def can_upgrade(self) -> bool:
"""
The following users can upgrade:
- have giveaway lifetime licence
- have giveaway manual subscriptions
- have a cancelled Paddle subscription
- have a expired Apple subscription
- have a expired Coinbase subscription
"""
sub: Subscription = self.get_subscription()
# user who has canceled can also re-subscribe
if sub and not sub.cancelled:
return False
manual_sub: ManualSubscription = ManualSubscription.get_by(user_id=self.id)
# user who has giveaway premium can decide to upgrade
if manual_sub and manual_sub.is_active() and not manual_sub.is_giveaway:
return False
coinbase_subscription = CoinbaseSubscription.get_by(user_id=self.id)
if coinbase_subscription and coinbase_subscription.is_active():
return False
return True
def is_premium(self) -> bool:
"""
user is premium if they:

View File

@ -70,6 +70,16 @@
</div>
<div class="col-sm-6 col-lg-6">
{% if manual_sub %}
<div class="alert alert-info">
You currently have a subscription until <b>{{ manual_sub.end_at.format("YYYY-MM-DD") }}</b>
({{ (manual_sub.end_at - now).days }} days left).
<br>
Please note that the time left will <b>not</b> be taken into account in a new subscription.
</div>
<hr>
{% endif %}
<div class="display-6 my-3">
🔐 Secure payments by
<a href="https://paddle.com" target="_blank" rel="noopener">
@ -87,6 +97,15 @@
</div>
{% endif %}
{% if coinbase_sub %}
<div class="alert alert-info">
You currently have a Coinbase subscription until <b>{{ coinbase_sub.end_at.format("YYYY-MM-DD") }}</b>
({{ (coinbase_sub.end_at - now).days }} days left).
<br>
Please note that the time left will <b>not</b> be taken into account in a new Paddle subscription.
</div>
{% endif %}
<div class="mb-3">
Paddle supports bank cards
(Mastercard, Visa, American Express, etc) and PayPal.