From fccd7378f7c309d7f5825bce553c3af94dd8e320 Mon Sep 17 00:00:00 2001 From: rizwanisready Date: Mon, 5 Aug 2024 19:19:20 +0530 Subject: [PATCH] refactored webhook realted to coupon 3 --- .../webhook/payment_processing_service.py | 72 +++++++++++-------- manage_subscriptions/api/views.py | 12 +--- 2 files changed, 43 insertions(+), 41 deletions(-) diff --git a/goodtimes/webhook/payment_processing_service.py b/goodtimes/webhook/payment_processing_service.py index c9ba26f..e333f43 100644 --- a/goodtimes/webhook/payment_processing_service.py +++ b/goodtimes/webhook/payment_processing_service.py @@ -1,3 +1,4 @@ +from venv import logger from django.db import transaction from manage_wallets.models import ( @@ -75,40 +76,51 @@ class PaymentProcessingService: def handle_success(self, transactio): with transaction.atomic(): # Create or update the principal subscription - self.principal_subscription = ( - self.subscription_service.create_principal_subscription( - principal=self.principal, - subscription=self.subscription, - stripe_subscription=self.stripe_subscription, - order_id=self.order_id, - current_period_start=self.current_period_start, - current_period_end=self.current_period_end, - coupon=self.coupon, + transactio = self.create_transaction() + try: + self.principal_subscription = ( + self.subscription_service.create_principal_subscription( + principal=self.principal, + subscription=self.subscription, + stripe_subscription=self.stripe_subscription, + order_id=self.order_id, + current_period_start=self.current_period_start, + current_period_end=self.current_period_end, + coupon=self.coupon, + ) ) - ) - print("First Part Done....!!!!!") + print("First Part Done....!!!!!") - # Update transaction status to success - self.subscription_service.update_transaction_success( - transactio, self.principal_subscription - ) - print("Second Part Done....!!!!!") + # Update transaction status to success + self.subscription_service.update_transaction_success( + transactio, self.principal_subscription + ) + print("Second Part Done....!!!!!") - # Handle referral rewards - referral_service = ReferralRewardService( - self.principal, self.principal_subscription, self.subscription - ) - print("Third Part Done...!!!!!!!!!!!") - referral_service.credit_referral_reward_if_applicable() - print("Fourth Part Done....!!!!!") + # Handle referral rewards + referral_service = ReferralRewardService( + self.principal, self.principal_subscription, self.subscription + ) + print("Third Part Done...!!!!!!!!!!!") + referral_service.credit_referral_reward_if_applicable() + print("Fourth Part Done....!!!!!") - # Send payment success notification - self.notification_service.payment_success_notification( - self.principal, - self.subscription, - self.principal_subscription, - transactio.amount, - ) + # Send payment success notification + self.notification_service.payment_success_notification( + self.principal, + self.subscription, + self.principal_subscription, + transactio.amount, + ) + except Exception as e: + transactio.transaction_status = TransactionStatus.FAIL + transactio.error_message = str(e) + logger.error(f"Transaction Error: {str(e)}") + transactio.save() + raise e + else: + transactio.transaction_status = TransactionStatus.SUCCESS + transactio.save() def handle_failure(self, transactio): self.subscription_service.update_transaction_failure(transactio) diff --git a/manage_subscriptions/api/views.py b/manage_subscriptions/api/views.py index cd89315..63f4321 100644 --- a/manage_subscriptions/api/views.py +++ b/manage_subscriptions/api/views.py @@ -219,18 +219,8 @@ class StripeWebhookTest(APIView): current_period_start=current_period_start, current_period_end=current_period_end, ) - transaction = payment_service.create_transaction() - try: - payment_service.process_event(transaction) - transaction.transaction_status = TransactionStatus.SUCCESS - except Exception as e: - transaction.transaction_status = TransactionStatus.FAIL - transaction.error_message = str(e) - logger.error(f"Transaction Error: {str(e)}") - raise e - finally: - transaction.save() + payment_service.process_event() webhook_event.status = "processed" webhook_event.processed_at = timezone.now() webhook_event.save()