refactored webhook realted to coupon 3

This commit is contained in:
rizwanisready
2024-08-05 19:19:20 +05:30
parent 3ac7e0d522
commit fccd7378f7
2 changed files with 43 additions and 41 deletions

View File

@@ -1,3 +1,4 @@
from venv import logger
from django.db import transaction from django.db import transaction
from manage_wallets.models import ( from manage_wallets.models import (
@@ -75,40 +76,51 @@ class PaymentProcessingService:
def handle_success(self, transactio): def handle_success(self, transactio):
with transaction.atomic(): with transaction.atomic():
# Create or update the principal subscription # Create or update the principal subscription
self.principal_subscription = ( transactio = self.create_transaction()
self.subscription_service.create_principal_subscription( try:
principal=self.principal, self.principal_subscription = (
subscription=self.subscription, self.subscription_service.create_principal_subscription(
stripe_subscription=self.stripe_subscription, principal=self.principal,
order_id=self.order_id, subscription=self.subscription,
current_period_start=self.current_period_start, stripe_subscription=self.stripe_subscription,
current_period_end=self.current_period_end, order_id=self.order_id,
coupon=self.coupon, 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 # Update transaction status to success
self.subscription_service.update_transaction_success( self.subscription_service.update_transaction_success(
transactio, self.principal_subscription transactio, self.principal_subscription
) )
print("Second Part Done....!!!!!") print("Second Part Done....!!!!!")
# Handle referral rewards # Handle referral rewards
referral_service = ReferralRewardService( referral_service = ReferralRewardService(
self.principal, self.principal_subscription, self.subscription self.principal, self.principal_subscription, self.subscription
) )
print("Third Part Done...!!!!!!!!!!!") print("Third Part Done...!!!!!!!!!!!")
referral_service.credit_referral_reward_if_applicable() referral_service.credit_referral_reward_if_applicable()
print("Fourth Part Done....!!!!!") print("Fourth Part Done....!!!!!")
# Send payment success notification # Send payment success notification
self.notification_service.payment_success_notification( self.notification_service.payment_success_notification(
self.principal, self.principal,
self.subscription, self.subscription,
self.principal_subscription, self.principal_subscription,
transactio.amount, 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): def handle_failure(self, transactio):
self.subscription_service.update_transaction_failure(transactio) self.subscription_service.update_transaction_failure(transactio)

View File

@@ -219,18 +219,8 @@ class StripeWebhookTest(APIView):
current_period_start=current_period_start, current_period_start=current_period_start,
current_period_end=current_period_end, 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.status = "processed"
webhook_event.processed_at = timezone.now() webhook_event.processed_at = timezone.now()
webhook_event.save() webhook_event.save()