changed webhook 4

This commit is contained in:
rizwanisready
2024-04-24 20:36:33 +05:30
parent a12418d640
commit 1424e6522f
2 changed files with 19 additions and 2 deletions

View File

@@ -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,
)

View File

@@ -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]