mirror of
https://github.com/thecyberlearn/quantum-ai-v3.git
synced 2026-08-18 19:52:57 +00:00
Implement webhook-free Stripe payment verification system
✅ SOLUTION: Bypass webhook delivery issues entirely Changes: - Enhanced checkout session creation with session_id in success URL - Updated success page to automatically verify payments via Stripe API - Added manual payment verification modal on wallet page - Users can verify payments manually if automatic verification fails - Real-time balance updates without depending on webhooks Benefits: - Instant payment confirmation upon return from Stripe - No webhook delivery dependency - Manual fallback for edge cases - Better user experience with immediate feedback 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
4b168ec1f0
commit
e7d5ccf017
@ -15,6 +15,7 @@ urlpatterns = [
|
||||
path('wallet/demo/', views.wallet_demo_view, name='wallet_demo'),
|
||||
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/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'),
|
||||
|
||||
@ -132,8 +132,36 @@ def wallet_topup_view(request):
|
||||
|
||||
@login_required
|
||||
def wallet_topup_success_view(request):
|
||||
"""Payment success page"""
|
||||
messages.success(request, 'Payment successful! Your wallet balance has been updated.')
|
||||
"""Payment success page with automatic payment verification (NO WEBHOOKS NEEDED)"""
|
||||
session_id = request.GET.get('session_id')
|
||||
|
||||
if not session_id:
|
||||
messages.error(request, 'No payment session found. Please contact support if you completed a payment.')
|
||||
return redirect('core:wallet')
|
||||
|
||||
# Verify payment directly with Stripe API (bypasses webhook issues)
|
||||
try:
|
||||
from wallet.stripe_handler import StripePaymentHandler
|
||||
stripe_handler = StripePaymentHandler()
|
||||
|
||||
print(f"💳 [SUCCESS PAGE] Verifying payment for session: {session_id}")
|
||||
result = stripe_handler.verify_payment(session_id)
|
||||
|
||||
if result['success']:
|
||||
if result['processed']:
|
||||
messages.success(request, f'Payment successful! {result["amount"]} AED has been added to your wallet.')
|
||||
print(f"✅ [SUCCESS PAGE] Payment verified and wallet updated for user {request.user.id}")
|
||||
else:
|
||||
messages.info(request, 'Payment already processed. Your wallet balance is up to date.')
|
||||
print(f"ℹ️ [SUCCESS PAGE] Payment already processed for session {session_id}")
|
||||
else:
|
||||
messages.warning(request, f'Payment verification failed: {result.get("error", "Unknown error")}. Please contact support.')
|
||||
print(f"❌ [SUCCESS PAGE] Payment verification failed: {result}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ [SUCCESS PAGE] Error verifying payment: {e}")
|
||||
messages.error(request, 'Unable to verify payment. Please contact support if you completed a payment.')
|
||||
|
||||
return redirect('core:wallet')
|
||||
|
||||
|
||||
@ -222,6 +250,46 @@ def wallet_demo_check_balance(request):
|
||||
})
|
||||
|
||||
|
||||
@login_required
|
||||
def verify_payment_view(request):
|
||||
"""Manual payment verification endpoint (fallback for webhook issues)"""
|
||||
if request.method == 'POST':
|
||||
try:
|
||||
data = json.loads(request.body)
|
||||
session_id = data.get('session_id', '').strip()
|
||||
|
||||
if not session_id:
|
||||
return JsonResponse({'error': 'Session ID is required'}, status=400)
|
||||
|
||||
# Verify payment with Stripe
|
||||
from wallet.stripe_handler import StripePaymentHandler
|
||||
stripe_handler = StripePaymentHandler()
|
||||
|
||||
print(f"🔍 [MANUAL VERIFY] User {request.user.id} verifying session: {session_id}")
|
||||
result = stripe_handler.verify_payment(session_id)
|
||||
|
||||
if result['success']:
|
||||
return JsonResponse({
|
||||
'success': True,
|
||||
'processed': result['processed'],
|
||||
'amount': result.get('amount', 0),
|
||||
'message': result['message']
|
||||
})
|
||||
else:
|
||||
return JsonResponse({
|
||||
'success': False,
|
||||
'error': result['error']
|
||||
})
|
||||
|
||||
except json.JSONDecodeError:
|
||||
return JsonResponse({'error': 'Invalid JSON data'}, status=400)
|
||||
except Exception as e:
|
||||
print(f"❌ [MANUAL VERIFY] Error: {e}")
|
||||
return JsonResponse({'error': str(e)}, status=500)
|
||||
|
||||
return JsonResponse({'error': 'Method not allowed'}, status=405)
|
||||
|
||||
|
||||
# Simple webhook test page
|
||||
def webhook_test_view(request):
|
||||
"""Simple HTML page for webhook testing"""
|
||||
|
||||
@ -231,6 +231,120 @@
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
/* Verify payment button */
|
||||
.verify-payment-btn {
|
||||
background: rgba(255, 255, 255, 0.15);
|
||||
color: white;
|
||||
border: 1px solid rgba(255, 255, 255, 0.3);
|
||||
padding: 8px 16px;
|
||||
border-radius: 8px;
|
||||
font-size: 14px;
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
transition: all 0.3s ease;
|
||||
backdrop-filter: blur(10px);
|
||||
}
|
||||
|
||||
.verify-payment-btn:hover {
|
||||
background: rgba(255, 255, 255, 0.25);
|
||||
transform: translateY(-1px);
|
||||
}
|
||||
|
||||
/* Verify modal */
|
||||
.verify-modal {
|
||||
display: none;
|
||||
position: fixed;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
background: rgba(0, 0, 0, 0.5);
|
||||
z-index: 1000;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.verify-modal-content {
|
||||
background: white;
|
||||
border-radius: 16px;
|
||||
padding: 24px;
|
||||
max-width: 500px;
|
||||
width: 90%;
|
||||
max-height: 90vh;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.verify-modal-title {
|
||||
font-size: 20px;
|
||||
font-weight: 700;
|
||||
margin-bottom: 16px;
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.verify-input {
|
||||
width: 100%;
|
||||
padding: 12px;
|
||||
border: 2px solid #e2e8f0;
|
||||
border-radius: 8px;
|
||||
font-size: 14px;
|
||||
margin: 8px 0;
|
||||
font-family: 'Courier New', monospace;
|
||||
}
|
||||
|
||||
.verify-buttons {
|
||||
display: flex;
|
||||
gap: 12px;
|
||||
margin-top: 20px;
|
||||
}
|
||||
|
||||
.verify-btn {
|
||||
flex: 1;
|
||||
padding: 12px 20px;
|
||||
border: none;
|
||||
border-radius: 8px;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s ease;
|
||||
}
|
||||
|
||||
.verify-btn.primary {
|
||||
background: var(--primary-blue);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.verify-btn.primary:hover {
|
||||
background: var(--primary-dark);
|
||||
}
|
||||
|
||||
.verify-btn.secondary {
|
||||
background: #f1f5f9;
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.verify-btn.secondary:hover {
|
||||
background: #e2e8f0;
|
||||
}
|
||||
|
||||
.verify-result {
|
||||
margin-top: 16px;
|
||||
padding: 12px;
|
||||
border-radius: 8px;
|
||||
font-size: 14px;
|
||||
display: none;
|
||||
}
|
||||
|
||||
.verify-result.success {
|
||||
background: #dcfce7;
|
||||
color: #16a34a;
|
||||
border: 1px solid #bbf7d0;
|
||||
}
|
||||
|
||||
.verify-result.error {
|
||||
background: #fef2f2;
|
||||
color: #dc2626;
|
||||
border: 1px solid #fecaca;
|
||||
}
|
||||
|
||||
/* Responsive adjustments */
|
||||
@media (max-width: 768px) {
|
||||
.stats-grid {
|
||||
@ -260,6 +374,11 @@
|
||||
<a href="{% url 'core:wallet_topup' %}" class="topup-btn">
|
||||
💳 Top Up Wallet
|
||||
</a>
|
||||
<div style="margin-top: 16px;">
|
||||
<button class="verify-payment-btn" onclick="showVerifyModal()">
|
||||
🔍 Verify Payment
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Statistics -->
|
||||
@ -311,7 +430,126 @@
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Verify Payment Modal -->
|
||||
<div class="verify-modal" id="verifyModal">
|
||||
<div class="verify-modal-content">
|
||||
<h3 class="verify-modal-title">🔍 Verify Payment</h3>
|
||||
<p style="margin-bottom: 16px; color: #666; font-size: 14px;">
|
||||
If you completed a payment but your wallet balance wasn't updated, enter the Stripe session ID below to verify it manually.
|
||||
</p>
|
||||
|
||||
<label for="sessionIdInput" style="display: block; margin-bottom: 8px; font-weight: 600; color: var(--text-primary);">
|
||||
Stripe Session ID:
|
||||
</label>
|
||||
<input type="text" id="sessionIdInput" class="verify-input" placeholder="cs_test_..." />
|
||||
|
||||
<p style="margin: 12px 0; font-size: 13px; color: #666;">
|
||||
💡 You can find the session ID in your browser URL after completing payment, or in your email receipt.
|
||||
</p>
|
||||
|
||||
<div class="verify-buttons">
|
||||
<button class="verify-btn secondary" onclick="hideVerifyModal()">Cancel</button>
|
||||
<button class="verify-btn primary" onclick="verifyPayment()">Verify Payment</button>
|
||||
</div>
|
||||
|
||||
<div class="verify-result" id="verifyResult"></div>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
||||
{% block extra_js %}
|
||||
<script>
|
||||
function showVerifyModal() {
|
||||
document.getElementById('verifyModal').style.display = 'flex';
|
||||
document.getElementById('sessionIdInput').focus();
|
||||
}
|
||||
|
||||
function hideVerifyModal() {
|
||||
document.getElementById('verifyModal').style.display = 'none';
|
||||
document.getElementById('sessionIdInput').value = '';
|
||||
document.getElementById('verifyResult').style.display = 'none';
|
||||
}
|
||||
|
||||
function verifyPayment() {
|
||||
const sessionId = document.getElementById('sessionIdInput').value.trim();
|
||||
const resultDiv = document.getElementById('verifyResult');
|
||||
|
||||
if (!sessionId) {
|
||||
showResult('Please enter a session ID', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!sessionId.startsWith('cs_')) {
|
||||
showResult('Invalid session ID format. It should start with "cs_"', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
// Show loading
|
||||
showResult('🔍 Verifying payment...', 'success');
|
||||
|
||||
// Get CSRF token
|
||||
const csrfToken = getCsrfToken();
|
||||
|
||||
fetch('/wallet/verify-payment/', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-CSRFToken': csrfToken
|
||||
},
|
||||
body: JSON.stringify({ session_id: sessionId })
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
if (data.processed) {
|
||||
showResult(`✅ Payment verified! ${data.amount} AED has been added to your wallet.`, 'success');
|
||||
// Refresh page after 2 seconds
|
||||
setTimeout(() => {
|
||||
window.location.reload();
|
||||
}, 2000);
|
||||
} else {
|
||||
showResult(`ℹ️ Payment already processed. ${data.message}`, 'success');
|
||||
}
|
||||
} else {
|
||||
showResult(`❌ Verification failed: ${data.error}`, 'error');
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
showResult(`❌ Error: ${error.message}`, 'error');
|
||||
});
|
||||
}
|
||||
|
||||
function showResult(message, type) {
|
||||
const resultDiv = document.getElementById('verifyResult');
|
||||
resultDiv.textContent = message;
|
||||
resultDiv.className = `verify-result ${type}`;
|
||||
resultDiv.style.display = 'block';
|
||||
}
|
||||
|
||||
function getCsrfToken() {
|
||||
const cookies = document.cookie.split(';');
|
||||
for (let cookie of cookies) {
|
||||
const [name, value] = cookie.trim().split('=');
|
||||
if (name === 'csrftoken') {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
// Close modal when clicking outside
|
||||
document.getElementById('verifyModal').addEventListener('click', function(e) {
|
||||
if (e.target === this) {
|
||||
hideVerifyModal();
|
||||
}
|
||||
});
|
||||
|
||||
// Handle Enter key in input
|
||||
document.getElementById('sessionIdInput').addEventListener('keypress', function(e) {
|
||||
if (e.key === 'Enter') {
|
||||
verifyPayment();
|
||||
}
|
||||
});
|
||||
</script>
|
||||
{% endblock %}
|
||||
62
test_webhook_external.py
Normal file
62
test_webhook_external.py
Normal file
@ -0,0 +1,62 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test external webhook accessibility
|
||||
"""
|
||||
import requests
|
||||
import json
|
||||
from datetime import datetime
|
||||
|
||||
def test_webhook_accessibility():
|
||||
"""Test if Railway webhook endpoints are accessible externally"""
|
||||
|
||||
base_url = "https://netcop.up.railway.app"
|
||||
|
||||
endpoints = [
|
||||
"/simple-webhook-test/",
|
||||
"/stripe/webhook/",
|
||||
]
|
||||
|
||||
print("🧪 Testing Railway webhook endpoint accessibility...")
|
||||
print(f"📍 Base URL: {base_url}")
|
||||
print("-" * 60)
|
||||
|
||||
for endpoint in endpoints:
|
||||
url = base_url + endpoint
|
||||
|
||||
try:
|
||||
print(f"\n🔍 Testing: {url}")
|
||||
|
||||
# Test with POST request
|
||||
response = requests.post(
|
||||
url,
|
||||
json={
|
||||
"test": True,
|
||||
"source": "external_test_script",
|
||||
"timestamp": datetime.now().isoformat()
|
||||
},
|
||||
headers={
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": "WebhookTester/1.0",
|
||||
"X-Test-Source": "external"
|
||||
},
|
||||
timeout=10
|
||||
)
|
||||
|
||||
print(f"✅ Status: {response.status_code}")
|
||||
print(f"✅ Response: {response.text[:200]}")
|
||||
|
||||
if response.status_code < 400:
|
||||
print(f"✅ SUCCESS: {endpoint} is accessible")
|
||||
else:
|
||||
print(f"❌ FAILED: {endpoint} returned {response.status_code}")
|
||||
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"❌ CONNECTION ERROR: {e}")
|
||||
except Exception as e:
|
||||
print(f"❌ UNEXPECTED ERROR: {e}")
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("🏁 External accessibility test completed")
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_webhook_accessibility()
|
||||
@ -4,6 +4,7 @@ from django.contrib.auth import get_user_model
|
||||
from django.http import JsonResponse
|
||||
from decimal import Decimal
|
||||
import json
|
||||
import time
|
||||
|
||||
User = get_user_model()
|
||||
stripe.api_key = settings.STRIPE_SECRET_KEY
|
||||
@ -17,63 +18,104 @@ class StripePaymentHandler:
|
||||
self.allowed_amounts = [10, 50, 100, 500]
|
||||
|
||||
def create_checkout_session(self, user, amount, request=None):
|
||||
"""Create a Stripe checkout session for wallet top-up"""
|
||||
"""Create a Stripe checkout session for wallet top-up (Modern Integration)"""
|
||||
if amount not in self.allowed_amounts:
|
||||
raise ValueError(f"Invalid amount: {amount}. Allowed amounts: {self.allowed_amounts}")
|
||||
|
||||
# Build URLs based on current request domain
|
||||
# Build URLs with session_id parameter for payment verification
|
||||
if request:
|
||||
success_url = request.build_absolute_uri('/wallet/top-up/success/')
|
||||
success_url = request.build_absolute_uri('/wallet/top-up/success/') + '?session_id={CHECKOUT_SESSION_ID}'
|
||||
cancel_url = request.build_absolute_uri('/wallet/top-up/cancel/')
|
||||
else:
|
||||
# Fallback URLs (shouldn't happen in normal flow)
|
||||
success_url = 'https://netcop.up.railway.app/wallet/top-up/success/'
|
||||
# Fallback URLs
|
||||
success_url = 'https://netcop.up.railway.app/wallet/top-up/success/?session_id={CHECKOUT_SESSION_ID}'
|
||||
cancel_url = 'https://netcop.up.railway.app/wallet/top-up/cancel/'
|
||||
|
||||
try:
|
||||
print(f"🚀 Creating checkout session for user {user.id} ({user.email}), amount: {amount} AED")
|
||||
print(f"🚀 [MODERN] Creating checkout session for user {user.id} ({user.email}), amount: {amount} AED")
|
||||
print(f"📍 Success URL: {success_url}")
|
||||
print(f"📍 Cancel URL: {cancel_url}")
|
||||
print(f"📍 Webhook URL: https://netcop.up.railway.app/stripe/webhook/")
|
||||
|
||||
# Create session with modern Stripe practices
|
||||
session = stripe.checkout.Session.create(
|
||||
# Modern payment method configuration
|
||||
payment_method_types=['card'],
|
||||
|
||||
# Line items configuration
|
||||
line_items=[{
|
||||
'price_data': {
|
||||
'currency': 'aed',
|
||||
'product_data': {
|
||||
'name': f'NetCop Wallet Top-up',
|
||||
'description': f'Add {amount} AED to your wallet balance'
|
||||
'name': 'NetCop Wallet Top-up',
|
||||
'description': f'Add {amount} AED to your wallet balance',
|
||||
'metadata': {
|
||||
'service': 'netcop_wallet',
|
||||
'user_id': str(user.id)
|
||||
}
|
||||
},
|
||||
'unit_amount': int(amount * 100), # Convert to cents
|
||||
'unit_amount': int(amount * 100), # Convert to fils (AED cents)
|
||||
},
|
||||
'quantity': 1,
|
||||
}],
|
||||
|
||||
# Payment configuration
|
||||
mode='payment',
|
||||
|
||||
# URLs with session ID parameter
|
||||
success_url=success_url,
|
||||
cancel_url=cancel_url,
|
||||
|
||||
# Customer and reference data
|
||||
client_reference_id=str(user.id),
|
||||
customer_email=user.email,
|
||||
|
||||
# Comprehensive metadata for webhook processing
|
||||
metadata={
|
||||
'user_id': str(user.id),
|
||||
'user_email': user.email,
|
||||
'amount': str(amount),
|
||||
'type': 'wallet_topup'
|
||||
'currency': 'aed',
|
||||
'type': 'wallet_topup',
|
||||
'service': 'netcop',
|
||||
'environment': 'production' if 'railway.app' in (request.get_host() if request else '') else 'development',
|
||||
'created_at': str(int(time.time())),
|
||||
'app_version': '1.0'
|
||||
},
|
||||
|
||||
# Modern Stripe features
|
||||
payment_intent_data={
|
||||
'metadata': {
|
||||
'user_id': str(user.id),
|
||||
'amount': str(amount),
|
||||
'service': 'netcop_wallet'
|
||||
}
|
||||
},
|
||||
|
||||
# Automatic tax and billing
|
||||
automatic_tax={'enabled': False},
|
||||
|
||||
# Expiration
|
||||
expires_at=int(time.time()) + (30 * 60), # 30 minutes from now
|
||||
)
|
||||
|
||||
print(f"✅ Session created: {session.id}")
|
||||
print(f"💳 Client reference ID: {session.client_reference_id}")
|
||||
print(f"🔗 Payment URL: {session.url}")
|
||||
|
||||
# ✅ WEBHOOK BYPASS: Process payment immediately after session creation
|
||||
# This bypasses webhook delivery issues by simulating the webhook locally
|
||||
print(f"💡 BYPASS: Setting up webhook simulation for session {session.id}")
|
||||
print(f"✅ [MODERN] 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" 🔗 Payment URL: {session.url}")
|
||||
print(f" ⏰ Expires: {session.expires_at}")
|
||||
|
||||
return {
|
||||
'payment_url': session.url,
|
||||
'session_id': session.id
|
||||
'session_id': session.id,
|
||||
'amount': amount,
|
||||
'currency': 'aed',
|
||||
'expires_at': session.expires_at
|
||||
}
|
||||
|
||||
except stripe.error.StripeError as e:
|
||||
print(f"❌ [MODERN] Stripe error: {str(e)}")
|
||||
raise ValueError(f"Failed to create checkout session: {str(e)}")
|
||||
|
||||
def verify_payment(self, session_id):
|
||||
|
||||
Loading…
Reference in New Issue
Block a user