mirror of
https://github.com/thecyberlearn/quantum-ai-v3.git
synced 2026-08-18 10:12:58 +00:00
Add comprehensive Stripe debugging system
🔍 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 <noreply@anthropic.com>
This commit is contained in:
parent
e7d5ccf017
commit
4816e3040e
@ -16,6 +16,7 @@ urlpatterns = [
|
|||||||
path('wallet/demo/test-payment/', views.wallet_demo_test_payment, name='wallet_demo_test_payment'),
|
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/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('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('stripe/webhook/', views.stripe_webhook_view, name='stripe_webhook'),
|
||||||
path('webhook-test/', views.webhook_test_view, name='webhook_test'),
|
path('webhook-test/', views.webhook_test_view, name='webhook_test'),
|
||||||
path('stripe-webhook-test/', views.stripe_webhook_test_view, name='stripe_webhook_test'),
|
path('stripe-webhook-test/', views.stripe_webhook_test_view, name='stripe_webhook_test'),
|
||||||
|
|||||||
@ -290,6 +290,98 @@ def verify_payment_view(request):
|
|||||||
return JsonResponse({'error': 'Method not allowed'}, status=405)
|
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
|
# Simple webhook test page
|
||||||
def webhook_test_view(request):
|
def webhook_test_view(request):
|
||||||
"""Simple HTML page for webhook testing"""
|
"""Simple HTML page for webhook testing"""
|
||||||
|
|||||||
@ -32,10 +32,15 @@ class StripePaymentHandler:
|
|||||||
cancel_url = 'https://netcop.up.railway.app/wallet/top-up/cancel/'
|
cancel_url = 'https://netcop.up.railway.app/wallet/top-up/cancel/'
|
||||||
|
|
||||||
try:
|
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"📍 Success URL: {success_url}")
|
||||||
print(f"📍 Cancel URL: {cancel_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
|
# Create session with modern Stripe practices
|
||||||
session = stripe.checkout.Session.create(
|
session = stripe.checkout.Session.create(
|
||||||
@ -99,12 +104,32 @@ class StripePaymentHandler:
|
|||||||
expires_at=int(time.time()) + (30 * 60), # 30 minutes from now
|
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" 💳 Session ID: {session.id}")
|
||||||
print(f" 👤 Client Reference: {session.client_reference_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" 🔗 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" ⏰ 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 {
|
return {
|
||||||
'payment_url': session.url,
|
'payment_url': session.url,
|
||||||
@ -121,10 +146,42 @@ class StripePaymentHandler:
|
|||||||
def verify_payment(self, session_id):
|
def verify_payment(self, session_id):
|
||||||
"""Verify payment directly from Stripe (bypasses webhook issues)"""
|
"""Verify payment directly from Stripe (bypasses webhook issues)"""
|
||||||
try:
|
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)
|
session = stripe.checkout.Session.retrieve(session_id)
|
||||||
print(f"🔍 VERIFY: Checking session {session_id}")
|
|
||||||
print(f"🔍 VERIFY: Payment status: {session.payment_status}")
|
print(f"✅ [STRIPE DEBUG] Session retrieved successfully!")
|
||||||
print(f"🔍 VERIFY: Session status: {session.status}")
|
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':
|
if session.payment_status == 'paid' and session.status == 'complete':
|
||||||
user_id = session.client_reference_id
|
user_id = session.client_reference_id
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user