-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an expiry date check subscriptions and remove discounts that expire before next payment date allow auto-applied membership vouchers custom voucher messages
- Loading branch information
Showing
23 changed files
with
386 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
booking/management/commands/remove_expired_subscription_vouchers.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
''' | ||
Find all subscriptions with discounts applied. | ||
Check if discount voucher code expires before next payment date | ||
Remove discount if necessary | ||
''' | ||
import logging | ||
from datetime import timedelta | ||
import stripe | ||
|
||
from django.utils import timezone | ||
from django.conf import settings | ||
from django.core.mail import send_mail | ||
from django.template.loader import get_template | ||
from django.core.management.base import BaseCommand | ||
|
||
from booking.models import UserMembership, StripeSubscriptionVoucher | ||
from booking.views.membership_views import ensure_subscription_up_to_date | ||
from activitylog.models import ActivityLog | ||
from stripe_payments.utils import StripeConnector | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class Command(BaseCommand): | ||
help = 'Remove expired discounts' | ||
|
||
def handle(self, *args, **options): | ||
client = StripeConnector() | ||
active = UserMembership.objects.filter(subscription_status="active") | ||
for user_membership in active: | ||
stripe_subscription = client.get_subscription(user_membership.subscription_id) | ||
if stripe_subscription.discounts: | ||
discount = stripe_subscription.discounts[0] | ||
voucher = StripeSubscriptionVoucher.objects.get(promo_code_id=discount.promotion_code) | ||
if voucher.expires_before_next_payment_date(): | ||
client.remove_discount_from_subscription(stripe_subscription.id) | ||
self.stdout.write( | ||
f"Expired discount code {voucher.code} removed from subscription from user {user_membership.username}" | ||
) |
35 changes: 35 additions & 0 deletions
35
booking/migrations/0105_alter_stripesubscriptionvoucher_options_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Generated by Django 5.1.1 on 2024-11-03 13:25 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("booking", "0104_event_members_only"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterModelOptions( | ||
name="stripesubscriptionvoucher", | ||
options={"ordering": ("-active", "-expiry_date", "-redeem_by")}, | ||
), | ||
migrations.AddField( | ||
model_name="stripesubscriptionvoucher", | ||
name="expiry_date", | ||
field=models.DateTimeField( | ||
blank=True, | ||
help_text="Date after which the code will be removed from any memberships that have currently applied it.", | ||
null=True, | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name="stripesubscriptionvoucher", | ||
name="redeem_by", | ||
field=models.DateTimeField( | ||
blank=True, | ||
help_text="Date after which users can no longer apply the code; note that once applied, it will apply for the voucher duration, even if that duration extends beyond the redeem by date. i.e. if a voucher applies for 2 months, and is redeemed on the redeem by date, it will still apply for the next 2 months' membership. If you want to override this behaviour, set an expiry date as well.", | ||
null=True, | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.