153 lines
4.5 KiB
Python
153 lines
4.5 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
易收米支付二维码演示
|
|
|
|
生成微信扫码支付二维码,真实支付场景演示
|
|
"""
|
|
|
|
import asyncio
|
|
import time
|
|
import qrcode
|
|
from io import BytesIO
|
|
from PIL import Image
|
|
from pay import create_payment
|
|
|
|
async def create_qr_payment():
|
|
"""创建二维码支付"""
|
|
print("易收米微信扫码支付演示")
|
|
print("=" * 50)
|
|
|
|
# 配置信息
|
|
appid = 'YSMcd16b45d'
|
|
appsecret = '899850e778e8d2b53e4c4a4e88695688'
|
|
|
|
# 生成订单号
|
|
order_id = f"qr_{int(time.time())}"
|
|
|
|
print(f"订单号: {order_id}")
|
|
print(f"商品: 测试商品 - 数据线")
|
|
print(f"金额: 0.01元")
|
|
print(f"支付方式: 微信扫码支付")
|
|
print()
|
|
print("正在生成支付二维码...")
|
|
|
|
try:
|
|
# 创建扫码支付订单 - 使用 pay_type=2
|
|
pay_url = await create_payment(
|
|
appid=appid,
|
|
appsecret=appsecret,
|
|
order_id=order_id,
|
|
description="测试商品 - 数据线",
|
|
amount=1, # 1分钱
|
|
notify_url="http://your-domain.com/notify",
|
|
nopay_url="http://your-domain.com/cancel",
|
|
callback_url="http://your-domain.com/success",
|
|
pay_type=2 # 微信扫码支付
|
|
)
|
|
|
|
if pay_url:
|
|
print("支付订单创建成功!")
|
|
print(f"支付链接: {pay_url}")
|
|
print()
|
|
|
|
# 生成二维码
|
|
qr = qrcode.QRCode(
|
|
version=1,
|
|
error_correction=qrcode.constants.ERROR_CORRECT_L,
|
|
box_size=10,
|
|
border=4,
|
|
)
|
|
qr.add_data(pay_url)
|
|
qr.make(fit=True)
|
|
|
|
# 创建二维码图片
|
|
qr_img = qr.make_image(fill_color="black", back_color="white")
|
|
|
|
# 保存二维码
|
|
qr_filename = f"payment_qr_{order_id}.png"
|
|
qr_img.save(qr_filename)
|
|
|
|
print(f"二维码已生成: {qr_filename}")
|
|
print()
|
|
print("使用方法:")
|
|
print("1. 打开微信扫一扫")
|
|
print(f"2. 扫描生成的二维码文件: {qr_filename}")
|
|
print("3. 完成0.01元支付")
|
|
print("4. 支付完成后可以查询订单状态")
|
|
print()
|
|
print("=" * 50)
|
|
print("二维码图片已保存到当前目录")
|
|
print("请使用微信扫描二维码文件进行支付")
|
|
print("=" * 50)
|
|
|
|
return order_id, pay_url, qr_filename
|
|
|
|
else:
|
|
print("支付订单创建失败")
|
|
return None, None, None
|
|
|
|
except Exception as e:
|
|
print(f"创建支付时出错: {str(e)}")
|
|
return None, None, None
|
|
|
|
async def check_payment_status(order_id):
|
|
"""检查支付状态"""
|
|
if not order_id:
|
|
return
|
|
|
|
print(f"\n检查订单状态: {order_id}")
|
|
|
|
from query import query_order
|
|
|
|
try:
|
|
# 查询订单状态
|
|
order_info = await query_order(
|
|
appid='YSMcd16b45d',
|
|
mch_orderid=order_id
|
|
)
|
|
|
|
if order_info:
|
|
state = order_info.get('state', 'UNKNOWN')
|
|
|
|
if state == 'SUCCESS':
|
|
print("支付状态: 支付成功! 🎉")
|
|
elif state == 'NOTPAY':
|
|
print("支付状态: 等待支付...")
|
|
elif 'code' in order_info and order_info.get('code') == 'ORDER_NOT_EXIST':
|
|
print("支付状态: 订单查询中...")
|
|
else:
|
|
print(f"支付状态: {state}")
|
|
|
|
print(f"详细信息: {order_info}")
|
|
else:
|
|
print("查询失败")
|
|
|
|
except Exception as e:
|
|
print(f"查询订单时出错: {str(e)}")
|
|
|
|
async def main():
|
|
"""主函数"""
|
|
# 生成支付二维码
|
|
order_id, pay_url, qr_file = await create_qr_payment()
|
|
|
|
if order_id:
|
|
# 等待用户扫码支付
|
|
input("\n按回车键查询支付状态...")
|
|
|
|
# 检查支付状态
|
|
await check_payment_status(order_id)
|
|
|
|
# 可以持续查询
|
|
while True:
|
|
check_again = input("\n是否再次查询订单状态? (y/n): ")
|
|
if check_again.lower() == 'y':
|
|
await check_payment_status(order_id)
|
|
else:
|
|
break
|
|
|
|
if __name__ == "__main__":
|
|
print("请确保已安装依赖: pip install qrcode[pil]")
|
|
print()
|
|
asyncio.run(main()) |