331 lines
8.5 KiB
Python
331 lines
8.5 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
Stripe Payment Module Test Script
|
||
测试 stripe_payment.py 模块的功能
|
||
"""
|
||
|
||
import sys
|
||
from pathlib import Path
|
||
|
||
# Add project root to path if needed
|
||
project_root = Path(__file__).parent.parent
|
||
if str(project_root) not in sys.path:
|
||
sys.path.insert(0, str(project_root))
|
||
|
||
from modules.stripe_payment import StripePaymentHandler
|
||
from modules.iban_generator import GermanIbanGenerator
|
||
|
||
def test_basic_flow():
|
||
"""测试基本支付流程"""
|
||
|
||
print("=" * 70)
|
||
print("Stripe Payment Handler - Test Script")
|
||
print("=" * 70)
|
||
print()
|
||
|
||
# 1. 获取用户输入
|
||
print("📋 请提供以下信息:")
|
||
print()
|
||
|
||
checkout_url = input("1. Checkout Session URL (从OpenAI获取的支付链接): ").strip()
|
||
|
||
if not checkout_url:
|
||
print("❌ 错误: 必须提供 Checkout Session URL")
|
||
sys.exit(1)
|
||
|
||
# 检查URL格式
|
||
if "cs_live_" not in checkout_url and "cs_test_" not in checkout_url:
|
||
print("❌ 错误: URL格式不正确,必须包含 cs_live_ 或 cs_test_")
|
||
sys.exit(1)
|
||
|
||
print()
|
||
print("2. 支付信息:")
|
||
print()
|
||
|
||
# IBAN(德国银行账号)
|
||
print(" IBAN 选项:")
|
||
print(" - 直接回车: 自动生成德国 IBAN")
|
||
print(" - 输入具体值: 使用指定的 IBAN")
|
||
iban_input = input(" IBAN: ").strip()
|
||
|
||
if not iban_input:
|
||
# 自动生成 IBAN
|
||
generator = GermanIbanGenerator()
|
||
iban = generator.generate(1)[0]
|
||
print(f" ✨ 已生成: {iban}")
|
||
else:
|
||
iban = iban_input
|
||
print(f" 使用输入: {iban}")
|
||
|
||
# 姓名
|
||
name = input(" 持卡人姓名 (如 John Doe): ").strip()
|
||
if not name:
|
||
name = "John Doe"
|
||
print(f" 使用默认: {name}")
|
||
|
||
# 邮箱
|
||
email = input(" 邮箱地址: ").strip()
|
||
if not email:
|
||
email = "test@example.com"
|
||
print(f" 使用默认: {email}")
|
||
|
||
# 地址
|
||
address_line1 = input(" 街道地址 (如 123 Main Street): ").strip()
|
||
if not address_line1:
|
||
address_line1 = "123 Main Street"
|
||
print(f" 使用默认: {address_line1}")
|
||
|
||
city = input(" 城市 (如 New York): ").strip()
|
||
if not city:
|
||
city = "New York"
|
||
print(f" 使用默认: {city}")
|
||
|
||
postal_code = input(" 邮编 (如 10001): ").strip()
|
||
if not postal_code:
|
||
postal_code = "10001"
|
||
print(f" 使用默认: {postal_code}")
|
||
|
||
state = input(" 州/省 (如 NY): ").strip()
|
||
if not state:
|
||
state = "NY"
|
||
print(f" 使用默认: {state}")
|
||
|
||
country = input(" 国家代码 (如 US): ").strip()
|
||
if not country:
|
||
country = "US"
|
||
print(f" 使用默认: {country}")
|
||
|
||
print()
|
||
print("-" * 70)
|
||
print("📝 确认信息:")
|
||
print(f" Checkout URL: {checkout_url[:60]}...")
|
||
print(f" IBAN: {iban[:8]}****{iban[-4:]}")
|
||
print(f" 姓名: {name}")
|
||
print(f" 邮箱: {email}")
|
||
print(f" 地址: {address_line1}, {city}, {state} {postal_code}, {country}")
|
||
print("-" * 70)
|
||
print()
|
||
|
||
confirm = input("确认开始测试? (y/N): ").strip().lower()
|
||
if confirm != 'y':
|
||
print("❌ 测试取消")
|
||
sys.exit(0)
|
||
|
||
print()
|
||
print("=" * 70)
|
||
print("开始支付流程测试...")
|
||
print("=" * 70)
|
||
print()
|
||
|
||
try:
|
||
# 2. 初始化支付处理器
|
||
print("🔧 初始化支付处理器...")
|
||
handler = StripePaymentHandler(checkout_url)
|
||
print(f"✅ Session ID: {handler.session_id}")
|
||
print(f"✅ GUID: {handler.guid[:20]}...")
|
||
print()
|
||
|
||
# 3. 执行完整支付流程
|
||
success = handler.complete_payment(
|
||
iban=iban,
|
||
name=name,
|
||
email=email,
|
||
address_line1=address_line1,
|
||
city=city,
|
||
postal_code=postal_code,
|
||
state=state,
|
||
country=country
|
||
)
|
||
|
||
print()
|
||
print("=" * 70)
|
||
if success:
|
||
print("✅✅✅ 支付测试成功! ✅✅✅")
|
||
print("=" * 70)
|
||
return True
|
||
else:
|
||
print("❌❌❌ 支付测试失败! ❌❌❌")
|
||
print("=" * 70)
|
||
return False
|
||
|
||
except Exception as e:
|
||
print()
|
||
print("=" * 70)
|
||
print(f"❌❌❌ 测试过程中发生异常! ❌❌❌")
|
||
print(f"错误信息: {e}")
|
||
print("=" * 70)
|
||
import traceback
|
||
traceback.print_exc()
|
||
return False
|
||
|
||
|
||
def test_step_by_step():
|
||
"""分步测试(用于调试)"""
|
||
|
||
print("=" * 70)
|
||
print("Stripe Payment Handler - Step by Step Test")
|
||
print("=" * 70)
|
||
print()
|
||
|
||
checkout_url = input("Checkout Session URL: ").strip()
|
||
|
||
if not checkout_url:
|
||
print("❌ 错误: 必须提供 URL")
|
||
sys.exit(1)
|
||
|
||
try:
|
||
# 初始化
|
||
handler = StripePaymentHandler(checkout_url)
|
||
print(f"✅ Initialized with session: {handler.session_id}")
|
||
print()
|
||
|
||
# 生成 IBAN
|
||
generator = GermanIbanGenerator()
|
||
iban = generator.generate(1)[0]
|
||
print(f"✨ 已自动生成 IBAN: {iban}")
|
||
print()
|
||
|
||
# Step 1: 创建支付方式
|
||
print("=" * 70)
|
||
print("Step 1: Creating Payment Method...")
|
||
print("=" * 70)
|
||
|
||
payment_method_id = handler.create_payment_method(
|
||
iban=iban,
|
||
name="Test User",
|
||
email="test@example.com",
|
||
address_line1="123 Test St",
|
||
city="Test City",
|
||
postal_code="12345",
|
||
state="TS",
|
||
country="US"
|
||
)
|
||
|
||
if not payment_method_id:
|
||
print("❌ Failed to create payment method")
|
||
return False
|
||
|
||
print(f"✅ Payment method created: {payment_method_id}")
|
||
print()
|
||
|
||
# Step 2: 确认支付
|
||
print("=" * 70)
|
||
print("Step 2: Confirming Payment...")
|
||
print("=" * 70)
|
||
|
||
confirmed = handler.confirm_payment(payment_method_id)
|
||
|
||
if not confirmed:
|
||
print("❌ Failed to confirm payment")
|
||
return False
|
||
|
||
print("✅ Payment confirmed")
|
||
print()
|
||
|
||
# Step 3: 轮询状态
|
||
print("=" * 70)
|
||
print("Step 3: Polling Payment Status...")
|
||
print("=" * 70)
|
||
|
||
final_status = handler.poll_payment_status()
|
||
|
||
if final_status.get("state") == "succeeded":
|
||
print("✅ Payment succeeded!")
|
||
return True
|
||
else:
|
||
print(f"❌ Payment ended with state: {final_status.get('state')}")
|
||
return False
|
||
|
||
except Exception as e:
|
||
print(f"❌ Exception: {e}")
|
||
import traceback
|
||
traceback.print_exc()
|
||
return False
|
||
|
||
|
||
def quick_test_with_defaults():
|
||
"""快速测试(使用默认值)"""
|
||
|
||
print("=" * 70)
|
||
print("Quick Test - Using Default Values")
|
||
print("=" * 70)
|
||
print()
|
||
|
||
checkout_url = input("Checkout URL: ").strip()
|
||
|
||
if not checkout_url:
|
||
print("❌ 错误: 必须提供 Checkout Session URL")
|
||
print("示例: https://pay.openai.com/c/pay/cs_live_xxx...")
|
||
sys.exit(1)
|
||
|
||
# 生成 IBAN
|
||
generator = GermanIbanGenerator()
|
||
iban = generator.generate(1)[0]
|
||
print(f"✨ 已自动生成 IBAN: {iban}")
|
||
print()
|
||
|
||
try:
|
||
handler = StripePaymentHandler(checkout_url)
|
||
|
||
success = handler.complete_payment(
|
||
iban=iban,
|
||
name="Test User",
|
||
email="test@example.com",
|
||
address_line1="123 Main Street",
|
||
city="New York",
|
||
postal_code="10001",
|
||
state="NY",
|
||
country="US"
|
||
)
|
||
|
||
if success:
|
||
print()
|
||
print("✅✅✅ SUCCESS ✅✅✅")
|
||
else:
|
||
print()
|
||
print("❌❌❌ FAILED ❌❌❌")
|
||
|
||
return success
|
||
|
||
except Exception as e:
|
||
print(f"❌ Exception: {e}")
|
||
import traceback
|
||
traceback.print_exc()
|
||
return False
|
||
|
||
|
||
def main():
|
||
"""主函数"""
|
||
|
||
print()
|
||
print("=" * 70)
|
||
print("Stripe Payment Module - Test Suite")
|
||
print("=" * 70)
|
||
print()
|
||
print("选择测试模式:")
|
||
print(" 1. 完整测试流程 (推荐)")
|
||
print(" 2. 分步测试 (调试用)")
|
||
print(" 3. 快速测试 (使用默认值)")
|
||
print()
|
||
|
||
choice = input("请选择 (1/2/3): ").strip()
|
||
|
||
print()
|
||
|
||
if choice == "1":
|
||
result = test_basic_flow()
|
||
elif choice == "2":
|
||
result = test_step_by_step()
|
||
elif choice == "3":
|
||
result = quick_test_with_defaults()
|
||
else:
|
||
print("❌ 无效选择")
|
||
sys.exit(1)
|
||
|
||
print()
|
||
sys.exit(0 if result else 1)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|