mirror of
https://github.com/thecyberlearn/quantum-ai-v2.git
synced 2026-08-18 12:52:59 +00:00
Add webhook debugging to troubleshoot payment balance updates
- Add detailed logging to Stripe webhook handler - Log webhook events, user processing, and balance updates - Help identify if webhooks are being received and processed correctly - Temporary debugging to resolve wallet balance not updating after payment 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
f7cdad4273
commit
bf22a2d21a
@ -147,12 +147,19 @@ def wallet_topup_cancel_view(request):
|
|||||||
@require_http_methods(["POST"])
|
@require_http_methods(["POST"])
|
||||||
def stripe_webhook_view(request):
|
def stripe_webhook_view(request):
|
||||||
"""Handle Stripe webhook events"""
|
"""Handle Stripe webhook events"""
|
||||||
|
print("🎯 Stripe webhook received!")
|
||||||
|
|
||||||
payload = request.body
|
payload = request.body
|
||||||
sig_header = request.META.get('HTTP_STRIPE_SIGNATURE')
|
sig_header = request.META.get('HTTP_STRIPE_SIGNATURE')
|
||||||
|
|
||||||
|
print(f"📦 Payload length: {len(payload)} bytes")
|
||||||
|
print(f"🔐 Signature header: {sig_header is not None}")
|
||||||
|
|
||||||
stripe_handler = StripePaymentHandler()
|
stripe_handler = StripePaymentHandler()
|
||||||
result = stripe_handler.handle_webhook(payload, sig_header)
|
result = stripe_handler.handle_webhook(payload, sig_header)
|
||||||
|
|
||||||
|
print(f"✅ Webhook result: {result}")
|
||||||
|
|
||||||
if result['success']:
|
if result['success']:
|
||||||
return JsonResponse({'status': 'success'})
|
return JsonResponse({'status': 'success'})
|
||||||
else:
|
else:
|
||||||
|
|||||||
@ -81,33 +81,52 @@ class StripePaymentHandler:
|
|||||||
|
|
||||||
def handle_webhook(self, payload, signature):
|
def handle_webhook(self, payload, signature):
|
||||||
"""Handle Stripe webhook events"""
|
"""Handle Stripe webhook events"""
|
||||||
|
print(f"🔍 Processing webhook with signature: {bool(signature)}")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
event = stripe.Webhook.construct_event(
|
event = stripe.Webhook.construct_event(
|
||||||
payload, signature, settings.STRIPE_WEBHOOK_SECRET
|
payload, signature, settings.STRIPE_WEBHOOK_SECRET
|
||||||
)
|
)
|
||||||
except ValueError:
|
print(f"📋 Event type: {event['type']}")
|
||||||
|
except ValueError as e:
|
||||||
|
print(f"❌ Invalid payload: {e}")
|
||||||
return {'success': False, 'error': 'Invalid payload'}
|
return {'success': False, 'error': 'Invalid payload'}
|
||||||
except stripe.error.SignatureVerificationError:
|
except stripe.error.SignatureVerificationError as e:
|
||||||
|
print(f"❌ Invalid signature: {e}")
|
||||||
return {'success': False, 'error': 'Invalid signature'}
|
return {'success': False, 'error': 'Invalid signature'}
|
||||||
|
|
||||||
if event['type'] == 'checkout.session.completed':
|
if event['type'] == 'checkout.session.completed':
|
||||||
session = event['data']['object']
|
session = event['data']['object']
|
||||||
|
print(f"💳 Processing checkout session: {session['id']}")
|
||||||
|
|
||||||
# Process successful payment
|
# Process successful payment
|
||||||
user_id = session.get('client_reference_id')
|
user_id = session.get('client_reference_id')
|
||||||
amount = session['amount_total'] / 100 # Convert from cents
|
amount = session['amount_total'] / 100 # Convert from cents
|
||||||
|
|
||||||
|
print(f"👤 User ID: {user_id}, Amount: {amount} AED")
|
||||||
|
|
||||||
if user_id:
|
if user_id:
|
||||||
try:
|
try:
|
||||||
user = User.objects.get(id=user_id)
|
user = User.objects.get(id=user_id)
|
||||||
|
print(f"✅ Found user: {user.email}, Current balance: {user.wallet_balance}")
|
||||||
|
|
||||||
user.add_balance(
|
user.add_balance(
|
||||||
amount=amount,
|
amount=amount,
|
||||||
description=f"Wallet top-up via Stripe",
|
description=f"Wallet top-up via Stripe",
|
||||||
stripe_session_id=session['id']
|
stripe_session_id=session['id']
|
||||||
)
|
)
|
||||||
|
user.refresh_from_db()
|
||||||
|
print(f"💰 New balance: {user.wallet_balance}")
|
||||||
|
|
||||||
return {'success': True, 'message': 'Payment processed successfully'}
|
return {'success': True, 'message': 'Payment processed successfully'}
|
||||||
except User.DoesNotExist:
|
except User.DoesNotExist:
|
||||||
|
print(f"❌ User not found: {user_id}")
|
||||||
return {'success': False, 'error': 'User not found'}
|
return {'success': False, 'error': 'User not found'}
|
||||||
|
else:
|
||||||
|
print("❌ No user_id in session")
|
||||||
|
return {'success': False, 'error': 'No user reference'}
|
||||||
|
else:
|
||||||
|
print(f"ℹ️ Ignored event type: {event['type']}")
|
||||||
|
|
||||||
return {'success': True, 'message': 'Event processed'}
|
return {'success': True, 'message': 'Event processed'}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user