refactored webhook realted to coupon 3
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user