diff --git a/goodtimes/webhook/payment_processing_service.py b/goodtimes/webhook/payment_processing_service.py index 2359d9a..ce0284b 100644 --- a/goodtimes/webhook/payment_processing_service.py +++ b/goodtimes/webhook/payment_processing_service.py @@ -75,15 +75,22 @@ class PaymentProcessingService: """Process the webhook event.""" with transaction.atomic(): event_type = self.webhook_service.event_type - txn = self.create_transaction() try: - if event_type == "checkout.session.completed": - self.handle_success(txn) - elif event_type == "invoice.payment_succeeded": - print("self.charge_data.billing_reason", self.charge_data.get("billing_reason")) + if event_type == "invoice.payment_succeeded": if self.charge_data.get("billing_reason") != "subscription_create": + txn = self.create_transaction() self.handle_success(txn) - elif event_type == "checkout.session.expired" or event_type == "invoice.payment_failed": + elif event_type == "checkout.session.completed": + txn = self.create_transaction() + self.handle_success(txn) + elif event_type in ["checkout.session.expired", "invoice.payment_failed"]: + order_id = self.order_id + try: + txn = Transaction.objects.get(order_id=order_id) + txn.transaction_status = TransactionStatus.FAIL + return + except Transaction.DoesNotExist: + txn = self.create_transaction() self.handle_failure(txn) except Exception as e: logger.error(f"Transaction Error: {str(e)}") @@ -146,7 +153,7 @@ class PaymentProcessingService: transaction, TransactionStatus.FAIL, error_message=error_message ) self.notification_service.payment_failed_notification( - self.principal, self.subscription, transaction + self.principal, self.subscription, transaction.amount ) print("Payment Failure Notification Sent")