From 4816e3040ee6858fd3d4b791d7ede5da72ab28bf Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 12 Jul 2025 16:44:18 +0530 Subject: [PATCH] Add comprehensive Stripe debugging system MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ๐Ÿ” DEBUGGING TOOLS ADDED: 1. Enhanced Payment Creation Logging: - Show API key, version, environment details - Log complete session creation response - Immediate session verification after creation - Detailed session metadata and status 2. Enhanced Payment Verification Logging: - Complete session retrieval details - Payment intent information if available - Full session status and metadata - Account verification details 3. Stripe Debug Endpoint (/stripe/debug/): - Test API connectivity - Show account information - List recent checkout sessions - List recent charges/payments - Identify which Stripe account we're connected to This will help identify: - If payments are going to wrong Stripe account - If API keys are correct - If sessions are being created properly - Where the disconnect between payments and dashboard is happening ๐Ÿค– Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- core/urls.py | 1 + core/views.py | 92 ++++++++++++++++++++++++++++++++++++++++ wallet/stripe_handler.py | 71 ++++++++++++++++++++++++++++--- 3 files changed, 157 insertions(+), 7 deletions(-) diff --git a/core/urls.py b/core/urls.py index 705276b..32f693d 100644 --- a/core/urls.py +++ b/core/urls.py @@ -16,6 +16,7 @@ urlpatterns = [ path('wallet/demo/test-payment/', views.wallet_demo_test_payment, name='wallet_demo_test_payment'), path('wallet/demo/check-balance/', views.wallet_demo_check_balance, name='wallet_demo_check_balance'), path('wallet/verify-payment/', views.verify_payment_view, name='verify_payment'), + path('stripe/debug/', views.stripe_debug_view, name='stripe_debug'), path('stripe/webhook/', views.stripe_webhook_view, name='stripe_webhook'), path('webhook-test/', views.webhook_test_view, name='webhook_test'), path('stripe-webhook-test/', views.stripe_webhook_test_view, name='stripe_webhook_test'), diff --git a/core/views.py b/core/views.py index bbdd2ed..a63c45f 100644 --- a/core/views.py +++ b/core/views.py @@ -290,6 +290,98 @@ def verify_payment_view(request): return JsonResponse({'error': 'Method not allowed'}, status=405) +@login_required +def stripe_debug_view(request): + """Debug endpoint to show Stripe API configuration and test connectivity""" + import stripe + from django.conf import settings + + debug_info = { + 'timestamp': datetime.datetime.now().isoformat(), + 'user_id': request.user.id, + 'user_email': request.user.email, + } + + try: + # Test Stripe API connectivity + print(f"๐Ÿ” [STRIPE DEBUG] Testing Stripe API connectivity...") + + # Get API key info (masked) + api_key = settings.STRIPE_SECRET_KEY + debug_info['stripe_api_key_last4'] = api_key[-4:] if api_key else 'Not set' + debug_info['stripe_api_key_prefix'] = api_key[:7] if api_key else 'Not set' + debug_info['stripe_api_version'] = stripe.api_version + + # Test account connectivity + try: + account = stripe.Account.retrieve() + debug_info['stripe_account'] = { + 'id': account.id, + 'email': account.email, + 'display_name': account.display_name, + 'country': account.country, + 'default_currency': account.default_currency, + 'business_profile': account.business_profile, + 'charges_enabled': account.charges_enabled, + 'payouts_enabled': account.payouts_enabled, + } + print(f"โœ… [STRIPE DEBUG] Account connected: {account.id}") + except Exception as account_error: + debug_info['stripe_account_error'] = str(account_error) + print(f"โŒ [STRIPE DEBUG] Account error: {account_error}") + + # Test recent checkout sessions + try: + sessions = stripe.checkout.Session.list(limit=5) + debug_info['recent_sessions'] = [] + for session in sessions.data: + debug_info['recent_sessions'].append({ + 'id': session.id, + 'status': session.status, + 'payment_status': session.payment_status, + 'amount_total': session.amount_total, + 'currency': session.currency, + 'customer_email': session.customer_email, + 'client_reference_id': session.client_reference_id, + 'created': session.created, + 'metadata': session.metadata, + }) + print(f"โœ… [STRIPE DEBUG] Retrieved {len(sessions.data)} recent sessions") + except Exception as sessions_error: + debug_info['sessions_error'] = str(sessions_error) + print(f"โŒ [STRIPE DEBUG] Sessions error: {sessions_error}") + + # Test recent payments + try: + charges = stripe.Charge.list(limit=5) + debug_info['recent_charges'] = [] + for charge in charges.data: + debug_info['recent_charges'].append({ + 'id': charge.id, + 'amount': charge.amount, + 'currency': charge.currency, + 'status': charge.status, + 'paid': charge.paid, + 'customer': charge.customer, + 'description': charge.description, + 'created': charge.created, + 'metadata': charge.metadata, + }) + print(f"โœ… [STRIPE DEBUG] Retrieved {len(charges.data)} recent charges") + except Exception as charges_error: + debug_info['charges_error'] = str(charges_error) + print(f"โŒ [STRIPE DEBUG] Charges error: {charges_error}") + + debug_info['status'] = 'success' + + except Exception as e: + debug_info['error'] = str(e) + debug_info['status'] = 'error' + print(f"โŒ [STRIPE DEBUG] General error: {e}") + + return JsonResponse(debug_info, indent=2) + + # Simple webhook test page def webhook_test_view(request): """Simple HTML page for webhook testing""" diff --git a/wallet/stripe_handler.py b/wallet/stripe_handler.py index 619450b..12543ec 100644 --- a/wallet/stripe_handler.py +++ b/wallet/stripe_handler.py @@ -32,10 +32,15 @@ class StripePaymentHandler: cancel_url = 'https://netcop.up.railway.app/wallet/top-up/cancel/' try: - print(f"๐Ÿš€ [MODERN] Creating checkout session for user {user.id} ({user.email}), amount: {amount} AED") + print(f"๐Ÿš€ [STRIPE DEBUG] Starting checkout session creation...") + print(f"๐Ÿ‘ค User: {user.id} ({user.email})") + print(f"๐Ÿ’ฐ Amount: {amount} AED") + print(f"๐Ÿ”‘ Stripe API Key (last 4): ...{settings.STRIPE_SECRET_KEY[-4:]}") + print(f"๐Ÿ”‘ API Version: {stripe.api_version}") print(f"๐Ÿ“ Success URL: {success_url}") print(f"๐Ÿ“ Cancel URL: {cancel_url}") - print(f"๐Ÿ“ Webhook URL: https://netcop.up.railway.app/stripe/webhook/") + print(f"๐Ÿ“ Expected Webhook URL: https://netcop.up.railway.app/stripe/webhook/") + print(f"๐ŸŒ Environment: {'production' if 'railway.app' in (request.get_host() if request else '') else 'development'}") # Create session with modern Stripe practices session = stripe.checkout.Session.create( @@ -99,12 +104,32 @@ class StripePaymentHandler: expires_at=int(time.time()) + (30 * 60), # 30 minutes from now ) - print(f"โœ… [MODERN] Session created successfully:") + print(f"โœ… [STRIPE DEBUG] Session created successfully!") print(f" ๐Ÿ’ณ Session ID: {session.id}") print(f" ๐Ÿ‘ค Client Reference: {session.client_reference_id}") - print(f" ๐Ÿ’ฐ Amount: {amount} AED ({int(amount * 100)} fils)") + print(f" ๐Ÿ‘ค Customer Email: {session.customer_email}") + print(f" ๐Ÿ’ฐ Amount Total: {session.amount_total} fils ({session.amount_total / 100} AED)") + print(f" ๐Ÿ’ฑ Currency: {session.currency}") print(f" ๐Ÿ”— Payment URL: {session.url}") + print(f" ๐Ÿ“Š Status: {session.status}") + print(f" ๐Ÿ’ณ Payment Status: {session.payment_status}") + print(f" โฐ Created: {session.created}") print(f" โฐ Expires: {session.expires_at}") + print(f" ๐Ÿท๏ธ Mode: {session.mode}") + print(f" ๐Ÿ†” Object Type: {session.object}") + print(f" ๐Ÿ“ Metadata: {session.metadata}") + + # CRITICAL: Verify session was created in correct Stripe account + print(f"๐Ÿ” [STRIPE DEBUG] Verifying session exists immediately...") + try: + verification_session = stripe.checkout.Session.retrieve(session.id) + print(f"โœ… [STRIPE DEBUG] Session verification successful!") + print(f" ๐Ÿ”— Retrieved Session ID: {verification_session.id}") + print(f" ๐Ÿ“Š Retrieved Status: {verification_session.status}") + print(f" ๐Ÿ‘ค Retrieved Customer Email: {verification_session.customer_email}") + except Exception as verify_error: + print(f"โŒ [STRIPE DEBUG] Session verification FAILED: {verify_error}") + print(f"โŒ This means the session was NOT created in the expected Stripe account!") return { 'payment_url': session.url, @@ -121,10 +146,42 @@ class StripePaymentHandler: def verify_payment(self, session_id): """Verify payment directly from Stripe (bypasses webhook issues)""" try: + print(f"๐Ÿ” [STRIPE DEBUG] Starting payment verification...") + print(f"๐Ÿ”‘ Using Stripe API Key (last 4): ...{settings.STRIPE_SECRET_KEY[-4:]}") + print(f"๐Ÿ”‘ API Version: {stripe.api_version}") + print(f"๐Ÿ’ณ Session ID to verify: {session_id}") + session = stripe.checkout.Session.retrieve(session_id) - print(f"๐Ÿ” VERIFY: Checking session {session_id}") - print(f"๐Ÿ” VERIFY: Payment status: {session.payment_status}") - print(f"๐Ÿ” VERIFY: Session status: {session.status}") + + print(f"โœ… [STRIPE DEBUG] Session retrieved successfully!") + print(f" ๐Ÿ’ณ Session ID: {session.id}") + print(f" ๐Ÿ“Š Session Status: {session.status}") + print(f" ๐Ÿ’ณ Payment Status: {session.payment_status}") + print(f" ๐Ÿ‘ค Client Reference ID: {session.client_reference_id}") + print(f" ๐Ÿ‘ค Customer Email: {session.customer_email}") + print(f" ๐Ÿ’ฐ Amount Total: {session.amount_total} fils ({session.amount_total / 100} AED)") + print(f" ๐Ÿ’ฑ Currency: {session.currency}") + print(f" โฐ Created: {session.created}") + print(f" โฐ Expires At: {session.expires_at}") + print(f" ๐Ÿท๏ธ Mode: {session.mode}") + print(f" ๐Ÿ“ Metadata: {session.metadata}") + print(f" ๐Ÿ’ณ Payment Intent: {getattr(session, 'payment_intent', 'None')}") + print(f" ๐Ÿงพ Invoice: {getattr(session, 'invoice', 'None')}") + print(f" ๐ŸŽฏ Success URL: {getattr(session, 'success_url', 'None')}") + + # Check if payment was actually completed + if hasattr(session, 'payment_intent') and session.payment_intent: + try: + payment_intent = stripe.PaymentIntent.retrieve(session.payment_intent) + print(f"๐Ÿ’ณ [STRIPE DEBUG] Payment Intent Details:") + print(f" ๐Ÿ†” Payment Intent ID: {payment_intent.id}") + print(f" ๐Ÿ“Š Status: {payment_intent.status}") + print(f" ๐Ÿ’ฐ Amount: {payment_intent.amount} fils ({payment_intent.amount / 100} AED)") + print(f" ๐Ÿ’ฑ Currency: {payment_intent.currency}") + print(f" โฐ Created: {payment_intent.created}") + print(f" ๐Ÿ“ Description: {payment_intent.description}") + except Exception as pi_error: + print(f"โŒ [STRIPE DEBUG] Could not retrieve Payment Intent: {pi_error}") if session.payment_status == 'paid' and session.status == 'complete': user_id = session.client_reference_id