157 lines
3.1 KiB
Markdown
157 lines
3.1 KiB
Markdown
# Ysm-SDK-python
|
||
|
||
易收米支付Python SDK,提供支付相关API的Python实现。
|
||
|
||
## 安装依赖
|
||
|
||
```bash
|
||
pip install -r requirements.txt
|
||
```
|
||
|
||
或者手动安装:
|
||
|
||
```bash
|
||
pip install aiohttp fastapi uvicorn jinja2 python-multipart
|
||
```
|
||
|
||
## 文件说明
|
||
|
||
- `api.py` - 基础API工具类,提供HTTP请求和签名方法
|
||
- `pay.py` - 发起支付模块
|
||
- `query.py` - 订单查询模块
|
||
- `notify.py` - 支付回调处理模块
|
||
- `demo.py` - 命令行演示脚本
|
||
- `web_demo.py` - Web服务演示
|
||
- `requirements.txt` - 依赖包列表
|
||
|
||
## 使用示例
|
||
|
||
### 发起支付
|
||
|
||
```python
|
||
from pay import create_payment
|
||
|
||
# 配置信息
|
||
appid = '20********' # 支付通道ID
|
||
appsecret = 'e605ac7******************4af5164' # AppSecret
|
||
|
||
# 创建支付
|
||
pay_url = create_payment(
|
||
appid=appid,
|
||
appsecret=appsecret,
|
||
order_id="123321123321",
|
||
description="快充数据线",
|
||
amount=100, # 1元
|
||
notify_url="http://example.com/notify",
|
||
nopay_url="http://example.com/cancel",
|
||
callback_url="http://example.com/success",
|
||
pay_type=1 # 微信内支付
|
||
)
|
||
|
||
print(f"支付链接: {pay_url}")
|
||
```
|
||
|
||
### 查询订单
|
||
|
||
```python
|
||
from query import query_order
|
||
|
||
# 配置信息
|
||
appid = '20********' # 支付通道ID
|
||
|
||
# 查询订单
|
||
order_info = query_order(
|
||
appid=appid,
|
||
mch_orderid='202305105056', # 商户订单号
|
||
)
|
||
|
||
print(f"订单信息: {order_info}")
|
||
```
|
||
|
||
### 处理支付回调
|
||
|
||
```python
|
||
from flask import Flask, request
|
||
from notify import PaymentNotify
|
||
|
||
app = Flask(__name__)
|
||
|
||
@app.route('/notify', methods=['POST'])
|
||
def payment_notify():
|
||
# 配置信息
|
||
appid = '20********' # 支付通道ID
|
||
appsecret = 'e605ac7******************4af5164' # AppSecret
|
||
|
||
# 创建通知处理器
|
||
notify_handler = PaymentNotify(appid, appsecret)
|
||
|
||
# 获取请求数据
|
||
request_data = request.get_data()
|
||
|
||
# 处理通知
|
||
success, message = notify_handler.process(request_data)
|
||
|
||
if success:
|
||
# 处理成功
|
||
return message
|
||
else:
|
||
# 处理失败
|
||
return {"error": message}, 400
|
||
|
||
if __name__ == '__main__':
|
||
app.run(debug=True)
|
||
```
|
||
|
||
## 支付类型说明
|
||
|
||
- `pay_type=1`: 微信内支付
|
||
- `pay_type=2`: 微信扫码支付
|
||
- `pay_type=3`: 微信H5支付(非原生)
|
||
- `pay_type=11`: 支付宝H5支付
|
||
|
||
## 演示程序
|
||
|
||
### 1. 命令行演示
|
||
|
||
运行完整的SDK功能演示:
|
||
|
||
```bash
|
||
python demo.py
|
||
```
|
||
|
||
这个演示会展示:
|
||
- 创建支付订单
|
||
- 查询订单状态
|
||
- 处理支付回调
|
||
- 不同支付类型说明
|
||
|
||
### 2. Web服务演示
|
||
|
||
启动Web演示服务:
|
||
|
||
```bash
|
||
python web_demo.py
|
||
```
|
||
|
||
然后访问 `http://localhost:8000` 查看:
|
||
- 支付表单页面
|
||
- 订单查询界面
|
||
- 支付成功/取消页面
|
||
- 支付回调处理接口
|
||
|
||
### 配置说明
|
||
|
||
在使用前请修改以下配置:
|
||
|
||
1. 在 `demo.py` 中修改:
|
||
```python
|
||
self.appid = 'your_appid' # 替换为你的AppID
|
||
self.appsecret = 'your_appsecret' # 替换为你的AppSecret
|
||
```
|
||
|
||
2. 在 `web_demo.py` 中修改:
|
||
```python
|
||
APPID = 'your_appid'
|
||
APPSECRET = 'your_appsecret'
|
||
NOTIFY_URL = "http://your-domain.com/notify" # 替换为你的回调地址
|
||
``` |