Merge pull request #970 from simple-login/paddle-grace-period

use a grace period of 14 days for paddle subscription
This commit is contained in:
Adrià Casajús 2022-05-11 19:46:38 +02:00 committed by GitHub
commit 9f43a33c09
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 3 deletions

View file

@ -57,6 +57,8 @@ from app.utils import (
Base = declarative_base()
PADDLE_SUBSCRIPTION_GRACE_DAYS = 14
class TSVector(sa.types.TypeDecorator):
impl = TSVECTOR
@ -751,8 +753,12 @@ class User(Base, ModelMixin, UserMixin, PasswordOracle):
sub = Subscription.get_by(user_id=self.id)
if sub:
# sub is active until the next billing_date + 1
if sub.next_bill_date >= arrow.now().shift(days=-1).date():
# grace period is 14 days
# sub is active until the next billing_date + PADDLE_SUBSCRIPTION_GRACE_DAYS
if (
sub.next_bill_date
>= arrow.now().shift(days=-PADDLE_SUBSCRIPTION_GRACE_DAYS).date()
):
return sub
# past subscription, user is considered not having a subscription = free plan
else:
@ -1944,7 +1950,6 @@ class AppleSubscription(Base, ModelMixin):
user = orm.relationship(User)
def is_valid(self):
# Todo: take into account grace period?
return self.expires_date > arrow.now().shift(days=-_APPLE_GRACE_PERIOD_DAYS)

View file

@ -1,5 +1,7 @@
import random
from uuid import UUID
import arrow
import pytest
from app.config import EMAIL_DOMAIN, MAX_NB_EMAIL_FREE_PLAN
@ -12,6 +14,9 @@ from app.models import (
Mailbox,
SenderFormatEnum,
EnumE,
Subscription,
PlanEnum,
PADDLE_SUBSCRIPTION_GRACE_DAYS,
)
from tests.utils import login, create_new_user
@ -258,3 +263,24 @@ def test_can_create_new_alias_disabled_user():
user.disabled = True
assert not user.can_create_new_alias()
def test_user_get_subscription_grace_period(flask_client):
user = create_new_user()
sub = Subscription.create(
user_id=user.id,
cancel_url="https://checkout.paddle.com/subscription/cancel?user=1234",
update_url="https://checkout.paddle.com/subscription/update?user=1234",
subscription_id=str(random.random()),
event_time=arrow.now(),
next_bill_date=arrow.now().shift(days=-PADDLE_SUBSCRIPTION_GRACE_DAYS).date(),
plan=PlanEnum.monthly,
commit=True,
)
assert user.get_subscription() is not None
sub.next_bill_date = (
arrow.now().shift(days=-(PADDLE_SUBSCRIPTION_GRACE_DAYS + 1)).date()
)
assert user.get_subscription() is None