#!/usr/bin/env python
"""
Production Fix M-Pesa Callback URLs
Run this script on your cPanel production server to fix callback URLs
"""
import os
import sys
import django

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

from payments.models import MpesaConfiguration

def fix_production_callback_urls():
    """Fix production M-Pesa callback URLs"""
    print("Fixing Production M-Pesa Callback URLs")
    print("=" * 50)
    
    # Your production domain
    production_domain = "branchbusinessadvance.co.ke"
    
    print(f"Production domain: {production_domain}")
    print()
    
    # Fix all M-Pesa configurations
    configs = MpesaConfiguration.objects.all()
    
    if not configs.exists():
        print("❌ No M-Pesa configurations found!")
        return
    
    for config in configs:
        print(f"Processing configuration for shortcode: {config.business_short_code}")
        print(f"Environment: {config.environment}")
        print(f"Current validation URL: {config.validation_url}")
        print(f"Current confirmation URL: {config.confirmation_url}")
        
        # Update URLs to use correct domain
        new_validation_url = f"https://{production_domain}/payments/callback/validation/"
        new_confirmation_url = f"https://{production_domain}/payments/callback/confirmation/"
        
        config.validation_url = new_validation_url
        config.confirmation_url = new_confirmation_url
        config.save()
        
        print(f"✅ Updated to:")
        print(f"   Validation URL: {config.validation_url}")
        print(f"   Confirmation URL: {config.confirmation_url}")
        print()
    
    print("✅ All callback URLs have been fixed!")
    print()
    print("NEXT STEPS:")
    print("1. Go to your M-Pesa Daraja portal")
    print("2. Re-register these callback URLs:")
    print(f"   - Validation: https://{production_domain}/payments/callback/validation/")
    print(f"   - Confirmation: https://{production_domain}/payments/callback/confirmation/")
    print("3. Test with a small payment")
    print("4. Check that callbacks are received")

if __name__ == "__main__":
    fix_production_callback_urls()
