Skip to content

Commit

Permalink
Methods for creating, updating and cancelling subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
rebkwok committed May 19, 2024
1 parent 8eb4abf commit ac3d84b
Show file tree
Hide file tree
Showing 5 changed files with 281 additions and 78 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ accounts/test_file.txt
provision/.vaultpass
pipsevents.log
.venv*/
prices.json
18 changes: 11 additions & 7 deletions booking/models/membership_models.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

import logging
import uuid
import re

from django.db import models
from django.utils.text import slugify

from booking.models import EventType
from stripe_payments.utils import create_stripe_product, update_stripe_product, get_or_create_stripe_price
from stripe_payments.utils import StripeConnector

logger = logging.getLogger(__name__)

Expand All @@ -33,15 +33,17 @@ def generate_stripe_product_id(self):
slug = slugify(self.name)
counter = 1
while Membership.objects.filter(stripe_product_id=slug).exists():
slug = f"{slug}_{counter}"
slug_without_counter = re.sub(r"(_\d+$)", "", slug)
slug = f"{slug_without_counter}_{counter}"
counter += 1
return slug

def save(self, *args, **kwargs):
stripe_client = StripeConnector()
if not self.id:
self.stripe_product_id = self.generate_stripe_product_id()
# create stripe product with price
product = create_stripe_product(
product = stripe_client.create_stripe_product(
product_id=self.stripe_product_id,
name=self.name,
description=self.description,
Expand All @@ -54,19 +56,21 @@ def save(self, *args, **kwargs):
changed = False
if self.price != presaved.price:
# if price has changed, create new Price and update stripe price ID
price_id = get_or_create_stripe_price(self.stripe_product_id, self.price)
price_id = stripe_client.get_or_create_stripe_price(self.stripe_product_id, self.price)
self.stripe_price_id = price_id
changed = True
if self.name != presaved.name or self.description != presaved.description or self.active != presaved.active:
changed = True
if changed:
update_stripe_product(
stripe_client.update_stripe_product(
product_id=self.stripe_product_id,
name=self.name,
description=self.description,
active=self.active,
price_id=self.stripe_price_id,
)
)
# TODO: If price has changed, update UserMemberships with active subscriptions
# beyond this month (with stripe_client.update_subscription_price())
super().save(*args, **kwargs)


Expand Down
2 changes: 1 addition & 1 deletion pipsevents/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
LOCAL=(bool, False),
SHOW_VAT=(bool, True),
TESTING=(bool, False),
PAYMENT_METHOD=(str, "paypal") ,
PAYMENT_METHOD=(str, "stripe"),
ENFORCE_AUTO_CANCELLATION=(bool, False)
)

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ static3==0.7.0
# via
# -r requirements.in
# dj-static
stripe==6.4.0
stripe==9.7.0
# via -r requirements.in
tomli==2.0.1
# via pytest
Expand Down
Loading

0 comments on commit ac3d84b

Please sign in to comment.