From 1711af39bc188432d2776ae855ad77addf823f7f Mon Sep 17 00:00:00 2001 From: rizwanisready Date: Fri, 2 Aug 2024 15:30:19 +0530 Subject: [PATCH] auto recurring testing phase 19 --- goodtimes/webhook.py | 7 ++----- manage_subscriptions/api/views.py | 21 ++++++++++----------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/goodtimes/webhook.py b/goodtimes/webhook.py index 97cbbab..1af7e05 100644 --- a/goodtimes/webhook.py +++ b/goodtimes/webhook.py @@ -131,9 +131,6 @@ class WebhookService: logger.error(f"Invalid subscription ID: {subscription_id}") raise ValueError(f"Invalid subscription ID: {subscription_id}") - def get_stripe_subscription(self): - return self.charge_data.get("subscription") - def get_order_id(self): return self.charge_data["metadata"]["order_id"] @@ -292,7 +289,7 @@ class SubscriptionService: class PaymentProcessingService: - def __init__(self, webhook_data, current_period_start, current_period_end): + def __init__(self, webhook_data, stripe_subscription, current_period_start, current_period_end): self.webhook_service = WebhookService(webhook_data) self.notification_service = NotificationService() self.current_period_start = current_period_start @@ -301,7 +298,7 @@ class PaymentProcessingService: self.principal = self.webhook_service.get_principal() self.transaction = self.webhook_service.get_transaction() self.subscription = self.webhook_service.get_subscription() - self.stripe_subscription = self.webhook_service.get_stripe_subscription() + self.stripe_subscription = stripe_subscription self.order_id = self.webhook_service.get_order_id() self.coupon = self.webhook_service.get_coupon() self.subscription_service = SubscriptionService() diff --git a/manage_subscriptions/api/views.py b/manage_subscriptions/api/views.py index b0a01ef..9b46666 100644 --- a/manage_subscriptions/api/views.py +++ b/manage_subscriptions/api/views.py @@ -179,17 +179,7 @@ class StripeWebhookTest(APIView): event_id = event["id"] event_type = event["type"] # principal_id = event["data"]["object"]["metadata"]["principal"] - stripe_subscription_id = stripe.Subscription.retrieve( - event["data"]["object"]["metadata"]["subscription"] - ) - - webhook_event, created = WebhookEvent.objects.get_or_create( - event_id=event_id, - defaults={ - "event_type": event_type, - "event_payload": json.loads(payload), - }, - ) + stripe_subscription_id = event["data"]["object"].get("subscription") if stripe_subscription_id: stripe_subscription = stripe.Subscription.retrieve(stripe_subscription_id) @@ -199,6 +189,14 @@ class StripeWebhookTest(APIView): current_period_start = None current_period_end = None + webhook_event, created = WebhookEvent.objects.get_or_create( + event_id=event_id, + defaults={ + "event_type": event_type, + "event_payload": json.loads(payload), + }, + ) + if not created and webhook_event.status == "processed": return ApiResponse.success( status=status.HTTP_208_ALREADY_REPORTED, @@ -207,6 +205,7 @@ class StripeWebhookTest(APIView): payment_service = PaymentProcessingService( webhook_data=event, + stripe_subscription=stripe_subscription_id, current_period_start=current_period_start, current_period_end=current_period_end, )