import requests
import time

def test_alipay_connection():
    """测试支付宝沙箱API连接"""
    print("=== 测试支付宝沙箱API连接 ===")
    
    # 支付宝沙箱网关地址
    gateway_url = "https://openapi.alipaydev.com/gateway.do"
    
    # 测试连接
    try:
        print(f"正在测试连接到: {gateway_url}")
        response = requests.get(gateway_url, timeout=10)
        print(f"连接成功! 状态码: {response.status_code}")
        print(f"响应头: {dict(response.headers)}")
        return True
    except requests.exceptions.Timeout:
        print("❌ 连接超时 - 可能是网络问题")
        return False
    except requests.exceptions.ConnectionError as e:
        print(f"❌ 连接错误: {e}")
        return False
    except Exception as e:
        print(f"❌ 其他错误: {e}")
        return False

def test_alternative_gateway():
    """测试备用网关"""
    print("\n=== 测试备用网关 ===")
    
    # 尝试不同的网关地址
    gateways = [
        "https://openapi.alipaydev.com/gateway.do",
        "https://openapi.alipay.com/gateway.do",
        "https://mapi.alipay.com/gateway.do"
    ]
    
    for gateway in gateways:
        try:
            print(f"测试网关: {gateway}")
            response = requests.get(gateway, timeout=5)
            print(f"✅ {gateway} - 状态码: {response.status_code}")
        except Exception as e:
            print(f"❌ {gateway} - 错误: {e}")

def test_network_connectivity():
    """测试基本网络连接"""
    print("\n=== 测试基本网络连接 ===")
    
    test_urls = [
        "https://www.baidu.com",
        "https://www.google.com",
        "https://openapi.alipaydev.com"
    ]
    
    for url in test_urls:
        try:
            response = requests.get(url, timeout=5)
            print(f"✅ {url} - 状态码: {response.status_code}")
        except Exception as e:
            print(f"❌ {url} - 错误: {e}")

if __name__ == "__main__":
    print("开始网络连接测试...")
    
    # 测试基本网络连接
    test_network_connectivity()
    
    # 测试支付宝连接
    alipay_ok = test_alipay_connection()
    
    # 测试备用网关
    test_alternative_gateway()
    
    if alipay_ok:
        print("\n✅ 支付宝沙箱API连接正常")
    else:
        print("\n❌ 支付宝沙箱API连接有问题，建议检查网络或使用模拟支付")





