Files
AutoDoneTeam/test_stripe_payment.py
2026-01-11 18:31:12 +08:00

302 lines
7.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""
Stripe Payment Module Test Script
测试 stripe_payment.py 模块的功能
"""
import sys
from modules.stripe_payment import StripePaymentHandler
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德国银行账号
iban = input(" IBAN (德国银行账号,如 DE89370400440532013000): ").strip()
if not iban:
iban = "DE89370400440532013000" # 默认测试IBAN
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()
# Step 1: 创建支付方式
print("=" * 70)
print("Step 1: Creating Payment Method...")
print("=" * 70)
payment_method_id = handler.create_payment_method(
iban="DE89370400440532013000",
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)
try:
handler = StripePaymentHandler(checkout_url)
success = handler.complete_payment(
iban="DE89370400440532013000",
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()