Skip to content

Commit

Permalink
cancel subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
odenypeter committed Jun 30, 2024
1 parent 1cffc8e commit 451915c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 51 deletions.
23 changes: 23 additions & 0 deletions core/migrations/0019_auto_20240630_1037.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 2.2.28 on 2024-06-30 10:37

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('core', '0018_auto_20230319_1707'),
]

operations = [
migrations.AddField(
model_name='subscription',
name='cancelled',
field=models.BooleanField(default=False),
),
migrations.AddField(
model_name='subscription',
name='cancelled_date',
field=models.DateTimeField(blank=True, null=True),
),
]
2 changes: 2 additions & 0 deletions core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,5 @@ class Subscription(models.Model):
on_delete=models.CASCADE,
related_name='organization_subscription'
)
cancelled = models.BooleanField(default=False)
cancelled_date = models.DateTimeField(null=True, blank=True)
98 changes: 47 additions & 51 deletions core/views/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class SubscriptionViewSet(viewsets.ModelViewSet):
All the subscriptions related actions
"""
queryset = Subscription.objects.all()
queryset = Subscription.objects.filter(cancelled=False).order_by('-create_date')
serializer_class = SubscriptionSerializer
permission_classes = (AllowAny, IsAuthenticated)

Expand All @@ -54,61 +54,39 @@ def create(self, request, *args, **kwargs):
status=status.HTTP_400_BAD_REQUEST
)

# def update(self, request, *args, **kwargs):
# instance = self.get_object()
# if settings.STRIPE_SECRET:
# stripe.api_key = settings.STRIPE_SECRET
# stripe.api_version = '2022-11-15'
# data = self.get_stripe_details()
# if data:
# serializer = self.get_serializer(instance, data=data, partial=True)
# serializer.is_valid(raise_exception=True)
# self.perform_create(serializer)

# product_info = data.get('stripe_product_info', {})
# # send the email
# context = {
# 'frontend_link': settings.FRONTEND_URL,
# 'product_name': product_info.get('name'),
# 'product_description': product_info.get('description')
# }
# subject = 'Subscription Success'
# template_name = 'email/coreuser/subscription.txt'
# html_template_name = 'email/coreuser/subscription.html'
# send_email(
# self.request.user.email,
# subject,
# context,
# template_name,
# html_template_name
# )

# return Response(
# serializer.data,
# status=status.HTTP_201_CREATED
# )
# return Response(
# dict(
# code='stripe_api_error',
# message='There was an error creating subscription'
# ),
# status=status.HTTP_400_BAD_REQUEST
# )

# return Response(
# dict(
# code='missing_stripe_details',
# message='Please pass valid product/card or stripe secret'
# ),
# status=status.HTTP_400_BAD_REQUEST
# )

def perform_create(self, serializer):
serializer.save(
user=self.request.user,
created_by=self.request.user,
)

def destroy(self, request, *args, **kwargs):
instance = self.get_object()
cancelled = self.perform_destroy(instance)
if not cancelled:
return Response(
dict(
code='stripe_api_error',
message='There was an error cancelling subscription',
),
status=status.HTTP_400_BAD_REQUEST
)
return Response(
dict(code='subscription_cancelled'),
status=status.HTTP_204_NO_CONTENT
)

def perform_destroy(self, instance):
# delete the subscription on stripe
cancelled = self.cancel_subscription_on_stripe(instance)

if cancelled:
# delete the subscription
instance.cancelled = True
instance.cancelled_date = timezone.now()
instance.save()
return cancelled

@action(
detail=False,
methods=['get'],
Expand All @@ -124,7 +102,7 @@ def stripe_products(self, request, *args, **kwargs):
if settings.STRIPE_SECRET:
stripe.api_key = settings.STRIPE_SECRET
stripe.api_version = '2022-11-15'
stripe_products = stripe.Product.search(query="active:'true'",)
stripe_products = stripe.Product.search(query="active:'true'", )
products = stripe_products.data

return Response(
Expand Down Expand Up @@ -198,3 +176,21 @@ def get_stripe_details(self):
return None

return data

@staticmethod
def cancel_subscription_on_stripe(instance):
"""
Cancel a subscription
"""
if settings.STRIPE_SECRET:
stripe.api_key = settings.STRIPE_SECRET
# stripe.api_version = '2022-11-15'
try:
# stripe.Subscription.cancel(instance.stripe_subscription_id)
subscription = stripe.Subscription.retrieve(instance.stripe_subscription_id)
subscription.delete()
return True

except Exception as e:
pass
return False

0 comments on commit 451915c

Please sign in to comment.