From 1424e6522f910f06bae5d7a8ba2e9cf1b1c8c729 Mon Sep 17 00:00:00 2001 From: rizwanisready Date: Wed, 24 Apr 2024 20:36:33 +0530 Subject: [PATCH] changed webhook 4 --- goodtimes/services.py | 4 ++-- manage_subscriptions/api/views.py | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/goodtimes/services.py b/goodtimes/services.py index 768976d..a886178 100644 --- a/goodtimes/services.py +++ b/goodtimes/services.py @@ -295,7 +295,7 @@ class PaymentProcessingService: .first() ) if active_subscription: - subscription = self._get_subscription() + subscription = self._get_principal_subscription_by_id() if subscription: # Calculate the reward value percentage = ( @@ -325,7 +325,7 @@ class PaymentProcessingService: self._update_reward( referral_record=referral_record, active_subscription=None, - create_subscription_method=self._update_principal_subscription, + create_subscription_method=self._update_principal_subscription(), has_active_subscription=False, ) diff --git a/manage_subscriptions/api/views.py b/manage_subscriptions/api/views.py index e0b8de6..4e92d37 100644 --- a/manage_subscriptions/api/views.py +++ b/manage_subscriptions/api/views.py @@ -177,6 +177,7 @@ class StripeWebhookTest(APIView): event = stripe.Event.construct_from(json.loads(payload), stripe.api_key) event_id = event["id"] event_type = event["type"] + principal_id = event["data"]["object"]["metadata"]["principal"] webhook_event, created = WebhookEvent.objects.get_or_create( event_id=event_id, @@ -192,6 +193,13 @@ class StripeWebhookTest(APIView): message="Event already processed", ) + # Check if there is an active principal subscription + if self._has_active_principal_subscription(principal_id): + return ApiResponse.success( + status=status.HTTP_208_ALREADY_REPORTED, + message="Active principal subscription already exists", + ) + payment_service = services.PaymentProcessingService(webhook_data=event) payment_service.process_event() webhook_event = WebhookEvent.objects.get(event_id=event_id) @@ -231,6 +239,15 @@ class StripeWebhookTest(APIView): errors=str(e), ) + def _has_active_principal_subscription(self, principal_id): + return PrincipalSubscription.objects.filter( + principal__id=principal_id, + active=True, + deleted=False, + is_paid=True, + end_date__gte=timezone.now().date(), + ).exists() + class LastActiveSubscriptionView(APIView): authentication_classes = [JWTAuthentication]