210 lines
6.9 KiB
Python
210 lines
6.9 KiB
Python
#!/usr/bin/env python
|
||
# -*- coding: utf-8 -*-
|
||
|
||
"""
|
||
易收米支付SDK使用演示
|
||
|
||
这个demo展示了如何使用易收米支付SDK的各个功能:
|
||
1. 创建支付订单
|
||
2. 查询订单状态
|
||
3. 处理支付回调
|
||
|
||
运行前请确保安装依赖:
|
||
pip install aiohttp fastapi uvicorn
|
||
|
||
使用方法:
|
||
python demo.py
|
||
"""
|
||
|
||
import json
|
||
import time
|
||
import asyncio
|
||
from pay import create_payment
|
||
from query import query_order
|
||
from notify import PaymentNotify
|
||
|
||
class YsmPayDemo:
|
||
"""易收米支付演示类"""
|
||
|
||
def __init__(self):
|
||
# 配置信息 - 请替换为你的真实配置
|
||
self.appid = 'YSMcd16b45d' # 支付通道ID
|
||
self.appsecret = '899850e778e8d2b53e4c4a4e88695688' # AppSecret
|
||
|
||
# 回调地址配置
|
||
self.notify_url = "http://your-domain.com/notify"
|
||
self.nopay_url = "http://your-domain.com/cancel"
|
||
self.callback_url = "http://your-domain.com/success"
|
||
|
||
async def demo_create_payment(self):
|
||
"""演示创建支付订单"""
|
||
print("\n=== 创建支付订单演示 ===")
|
||
|
||
# 生成唯一订单号
|
||
order_id = f"demo_{int(time.time())}"
|
||
|
||
try:
|
||
# 创建支付
|
||
pay_url = await create_payment(
|
||
appid=self.appid,
|
||
appsecret=self.appsecret,
|
||
order_id=order_id,
|
||
description="演示商品 - 快充数据线",
|
||
amount=100, # 1元 = 100分
|
||
notify_url=self.notify_url,
|
||
nopay_url=self.nopay_url,
|
||
callback_url=self.callback_url,
|
||
pay_type=1 # 微信内支付
|
||
)
|
||
|
||
if pay_url:
|
||
print(f"✅ 支付订单创建成功")
|
||
print(f"📋 订单号: {order_id}")
|
||
print(f"💰 金额: 1.00元")
|
||
print(f"🔗 支付链接: {pay_url}")
|
||
return order_id, pay_url
|
||
else:
|
||
print("❌ 支付订单创建失败")
|
||
return None, None
|
||
|
||
except Exception as e:
|
||
print(f"❌ 创建支付时出错: {str(e)}")
|
||
return None, None
|
||
|
||
async def demo_query_order(self, order_id):
|
||
"""演示查询订单状态"""
|
||
print(f"\n=== 查询订单状态演示 ===")
|
||
print(f"🔍 查询订单: {order_id}")
|
||
|
||
try:
|
||
# 查询订单
|
||
order_info = await query_order(
|
||
appid=self.appid,
|
||
mch_orderid=order_id
|
||
)
|
||
|
||
if order_info:
|
||
print("✅ 订单查询成功")
|
||
print(f"📊 订单信息:")
|
||
print(json.dumps(order_info, ensure_ascii=False, indent=2))
|
||
|
||
# 解析订单状态
|
||
state = order_info.get('state', 'UNKNOWN')
|
||
status_map = {
|
||
'SUCCESS': '✅ 支付成功',
|
||
'REFUND': '🔄 转入退款',
|
||
'NOTPAY': '⏳ 未支付',
|
||
'CLOSED': '❌ 已关闭',
|
||
}
|
||
|
||
# 检查是否是错误响应
|
||
if 'code' in order_info and order_info.get('code') == 'ORDER_NOT_EXIST':
|
||
print(f"💡 订单状态: ⚠️ 订单不存在(演示订单,正常现象)")
|
||
else:
|
||
print(f"💡 订单状态: {status_map.get(state, f'未知状态({state})')}")
|
||
|
||
return order_info
|
||
else:
|
||
print("❌ 订单查询失败")
|
||
return None
|
||
|
||
except Exception as e:
|
||
print(f"❌ 查询订单时出错: {str(e)}")
|
||
return None
|
||
|
||
async def demo_payment_notify(self):
|
||
"""演示支付回调处理"""
|
||
print(f"\n=== 支付回调处理演示 ===")
|
||
|
||
# 模拟支付成功的回调数据
|
||
mock_notify_data = {
|
||
"appid": self.appid,
|
||
"mch_orderid": "demo_1234567890",
|
||
"ysm_orderid": "YSM20240101123456",
|
||
"total": 100,
|
||
"state": "SUCCESS",
|
||
"time": int(time.time()),
|
||
"nonce_str": "abc123456"
|
||
}
|
||
|
||
# 添加签名
|
||
from api import YsmPayApi
|
||
mock_notify_data['hash'] = YsmPayApi.hash_sign(mock_notify_data, self.appsecret)
|
||
|
||
print(f"📨 模拟回调数据:")
|
||
print(json.dumps(mock_notify_data, ensure_ascii=False, indent=2))
|
||
|
||
# 创建通知处理器
|
||
notify_handler = PaymentNotify(self.appid, self.appsecret)
|
||
|
||
# 处理回调
|
||
try:
|
||
# 使用异步调用
|
||
success, message = await notify_handler.process(mock_notify_data)
|
||
|
||
if success:
|
||
print(f"✅ 回调处理成功: {message}")
|
||
else:
|
||
print(f"❌ 回调处理失败: {message}")
|
||
|
||
return success, message
|
||
|
||
except Exception as e:
|
||
print(f"❌ 处理回调时出错: {str(e)}")
|
||
return False, str(e)
|
||
|
||
def demo_payment_types(self):
|
||
"""演示不同支付类型"""
|
||
print(f"\n=== 支付类型说明 ===")
|
||
|
||
payment_types = {
|
||
1: "微信内支付 - 适用于在微信内打开的页面",
|
||
2: "微信扫码支付 - 生成二维码供用户扫码支付",
|
||
3: "微信H5支付 - 适用于手机浏览器(非微信内)",
|
||
11: "支付宝H5支付 - 适用于手机浏览器跳转支付宝"
|
||
}
|
||
|
||
for pay_type, description in payment_types.items():
|
||
print(f"💳 pay_type={pay_type}: {description}")
|
||
|
||
async def run_full_demo(self):
|
||
"""运行完整演示"""
|
||
print("🚀 易收米支付SDK演示开始")
|
||
print("=" * 50)
|
||
|
||
# 显示配置信息
|
||
print(f"📋 当前配置:")
|
||
print(f" AppID: {self.appid}")
|
||
print(f" AppSecret: {self.appsecret[:8]}***{self.appsecret[-8:]}")
|
||
|
||
# 1. 演示支付类型
|
||
self.demo_payment_types()
|
||
|
||
# 2. 创建支付订单
|
||
order_id, pay_url = await self.demo_create_payment()
|
||
|
||
if order_id:
|
||
# 3. 查询订单状态
|
||
await self.demo_query_order(order_id)
|
||
|
||
# 4. 演示回调处理
|
||
await self.demo_payment_notify()
|
||
|
||
print("\n" + "=" * 50)
|
||
print("✅ 演示完成")
|
||
|
||
# 提示用户
|
||
if pay_url:
|
||
print(f"\n💡 提示:")
|
||
print(f" 1. 可以访问支付链接进行测试: {pay_url}")
|
||
print(f" 2. 支付成功后可以再次查询订单状态")
|
||
print(f" 3. 实际使用时请替换为真实的回调地址")
|
||
|
||
async def main():
|
||
"""主函数"""
|
||
demo = YsmPayDemo()
|
||
await demo.run_full_demo()
|
||
|
||
if __name__ == "__main__":
|
||
# 运行演示
|
||
asyncio.run(main()) |