#!/usr/bin/env python
"""
Production Test M-Pesa Callback URLs
Run this script on your cPanel production server to test if callback URLs are accessible
"""
import os
import sys
import django
import requests

# Setup Django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'branch_system.settings')
django.setup()

from payments.models import MpesaConfiguration

def test_production_callback_urls():
    """Test if production callback URLs are accessible"""
    print("Testing Production M-Pesa Callback URLs")
    print("=" * 50)
    
    # Get production configuration
    configs = MpesaConfiguration.objects.all()
    
    if not configs.exists():
        print("❌ No M-Pesa configurations found!")
        return
    
    for config in configs:
        print(f"Testing configuration for shortcode: {config.business_short_code}")
        print(f"Environment: {config.environment}")
        print()
        
        # Test validation URL
        print(f"Testing validation URL: {config.validation_url}")
        try:
            response = requests.get(config.validation_url, timeout=10)
            print(f"   Status Code: {response.status_code}")
            print(f"   Response: {response.text[:200]}...")
            if response.status_code == 200:
                print("   ✅ Validation URL is accessible")
            else:
                print("   ⚠️  Validation URL returned non-200 status")
        except requests.exceptions.RequestException as e:
            print(f"   ❌ Validation URL not accessible: {e}")
        
        print()
        
        # Test confirmation URL
        print(f"Testing confirmation URL: {config.confirmation_url}")
        try:
            response = requests.get(config.confirmation_url, timeout=10)
            print(f"   Status Code: {response.status_code}")
            print(f"   Response: {response.text[:200]}...")
            if response.status_code == 200:
                print("   ✅ Confirmation URL is accessible")
            else:
                print("   ⚠️  Confirmation URL returned non-200 status")
        except requests.exceptions.RequestException as e:
            print(f"   ❌ Confirmation URL not accessible: {e}")
        
        print()
        print("-" * 50)
        print()
    
    print("IMPORTANT NOTES:")
    print("1. These URLs should return HTTP 200 status")
    print("2. If URLs are not accessible, M-Pesa cannot send callbacks")
    print("3. Make sure your server allows POST requests to these URLs")
    print("4. Check your server's firewall and security settings")

if __name__ == "__main__":
    test_production_callback_urls()