threeypay-sdk 1.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- threeypay_sdk-1.1.0/LICENSE +21 -0
- threeypay_sdk-1.1.0/PKG-INFO +104 -0
- threeypay_sdk-1.1.0/README.md +75 -0
- threeypay_sdk-1.1.0/pyproject.toml +44 -0
- threeypay_sdk-1.1.0/setup.cfg +4 -0
- threeypay_sdk-1.1.0/tests/test_client.py +169 -0
- threeypay_sdk-1.1.0/tests/test_modules.py +469 -0
- threeypay_sdk-1.1.0/tests/test_signer.py +64 -0
- threeypay_sdk-1.1.0/threeypay/__init__.py +37 -0
- threeypay_sdk-1.1.0/threeypay/account/__init__.py +5 -0
- threeypay_sdk-1.1.0/threeypay/account/account.py +77 -0
- threeypay_sdk-1.1.0/threeypay/balanceshare/__init__.py +5 -0
- threeypay_sdk-1.1.0/threeypay/balanceshare/balance_share.py +86 -0
- threeypay_sdk-1.1.0/threeypay/channel/__init__.py +5 -0
- threeypay_sdk-1.1.0/threeypay/channel/channel.py +65 -0
- threeypay_sdk-1.1.0/threeypay/client.py +146 -0
- threeypay_sdk-1.1.0/threeypay/config.py +68 -0
- threeypay_sdk-1.1.0/threeypay/exceptions/__init__.py +27 -0
- threeypay_sdk-1.1.0/threeypay/merchant/__init__.py +5 -0
- threeypay_sdk-1.1.0/threeypay/merchant/merchant.py +128 -0
- threeypay_sdk-1.1.0/threeypay/nonmch/__init__.py +5 -0
- threeypay_sdk-1.1.0/threeypay/nonmch/non_mch.py +61 -0
- threeypay_sdk-1.1.0/threeypay/payment/__init__.py +5 -0
- threeypay_sdk-1.1.0/threeypay/payment/payment.py +258 -0
- threeypay_sdk-1.1.0/threeypay/payout/__init__.py +5 -0
- threeypay_sdk-1.1.0/threeypay/payout/payout.py +67 -0
- threeypay_sdk-1.1.0/threeypay/profitsharing/__init__.py +5 -0
- threeypay_sdk-1.1.0/threeypay/profitsharing/profit_sharing.py +119 -0
- threeypay_sdk-1.1.0/threeypay/sdk.py +126 -0
- threeypay_sdk-1.1.0/threeypay/transfer/__init__.py +5 -0
- threeypay_sdk-1.1.0/threeypay/transfer/transfer.py +176 -0
- threeypay_sdk-1.1.0/threeypay/utils/signer.py +179 -0
- threeypay_sdk-1.1.0/threeypay_sdk.egg-info/PKG-INFO +104 -0
- threeypay_sdk-1.1.0/threeypay_sdk.egg-info/SOURCES.txt +35 -0
- threeypay_sdk-1.1.0/threeypay_sdk.egg-info/dependency_links.txt +1 -0
- threeypay_sdk-1.1.0/threeypay_sdk.egg-info/requires.txt +6 -0
- threeypay_sdk-1.1.0/threeypay_sdk.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 湖北汇联科技有限公司
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: threeypay-sdk
|
|
3
|
+
Version: 1.1.0
|
|
4
|
+
Summary: 3ypay (汇联支付) Python SDK - 统一支付、查询、退款、转账、分账等
|
|
5
|
+
Author-email: 3YPay Team <support@3ypay.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://gitee.com/threeypay/threeypay-python-sdk
|
|
8
|
+
Project-URL: Source, https://gitee.com/threeypay/threeypay-python-sdk
|
|
9
|
+
Project-URL: Issues, https://gitee.com/threeypay/threeypay-python-sdk/issues
|
|
10
|
+
Project-URL: Documentation, https://doc.3ypay.com/
|
|
11
|
+
Keywords: payment,3ypay,alipay,wechat,unionpay,sdk
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Requires-Python: >=3.9
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
License-File: LICENSE
|
|
23
|
+
Requires-Dist: requests>=2.28.0
|
|
24
|
+
Requires-Dist: cryptography>=38.0.0
|
|
25
|
+
Provides-Extra: dev
|
|
26
|
+
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
27
|
+
Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
|
|
28
|
+
Dynamic: license-file
|
|
29
|
+
|
|
30
|
+
# 3YPay Python SDK
|
|
31
|
+
|
|
32
|
+
[](https://badge.fury.io/py/threeypay-sdk)
|
|
33
|
+
[](https://pypi.org/project/threeypay-sdk/)
|
|
34
|
+
[](LICENSE)
|
|
35
|
+
|
|
36
|
+
### V1.1.0。请务必参照对接文档进行对接
|
|
37
|
+
|
|
38
|
+
1. **3ypay (汇联支付) Python SDK**,支持统一支付、交易查询、退款、转账、分账等功能。
|
|
39
|
+
2. **具体参数说明**,请查看 [API文档](https://doc.3ypay.com/)。
|
|
40
|
+
3. **请务必对照文档进行配置**,并确保代码正确运行。
|
|
41
|
+
|
|
42
|
+
## 安装
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
pip install threeypay-sdk
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## 快速开始
|
|
49
|
+
|
|
50
|
+
### 1. 初始化 SDK
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
from threeypay import ThreeYPay
|
|
54
|
+
|
|
55
|
+
# 配置参数
|
|
56
|
+
app_id = "YOUR_APP_ID" # 应用 APPID
|
|
57
|
+
private_key = "YOUR_PRIVATE_KEY" # 商户私钥 (RSA2)
|
|
58
|
+
platform_public_key = "YOUR_PLATFORM_PUBLIC_KEY" # 平台公钥
|
|
59
|
+
is_sandbox = True # 是否使用测试环境
|
|
60
|
+
|
|
61
|
+
# 初始化 SDK
|
|
62
|
+
pay = ThreeYPay(app_id, private_key, platform_public_key, is_sandbox)
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### 2. 支付宝扫码支付
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
import time
|
|
69
|
+
|
|
70
|
+
result = pay.payment().alipay(
|
|
71
|
+
mch_order_no=f"ORDER_{int(time.time() * 1000)}",
|
|
72
|
+
order_amount="0.01",
|
|
73
|
+
subject="测试商品",
|
|
74
|
+
description="商品描述",
|
|
75
|
+
notify_url="https://yourdomain.com/notify",
|
|
76
|
+
client_ip="127.0.0.1",
|
|
77
|
+
expired_time=int(time.time() * 1000) + 30*60*1000
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
print(result)
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### 3. 交易查询
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
result = pay.payment().query_by_pay_order_no("P202603062034302534880")
|
|
87
|
+
print(result)
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## 功能模块
|
|
91
|
+
|
|
92
|
+
- **Payment** - 支付模块(支付宝、微信、银联等)
|
|
93
|
+
- **Transfer** - 转账模块(安全发、直付通)
|
|
94
|
+
- **ProfitSharing** - 分账模块
|
|
95
|
+
- **Account** - 账户管理模块
|
|
96
|
+
- **Merchant** - 商户管理模块
|
|
97
|
+
|
|
98
|
+
## 文档
|
|
99
|
+
|
|
100
|
+
完整API文档:https://doc.3ypay.com/
|
|
101
|
+
|
|
102
|
+
## License
|
|
103
|
+
|
|
104
|
+
MIT
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# 3YPay Python SDK
|
|
2
|
+
|
|
3
|
+
[](https://badge.fury.io/py/threeypay-sdk)
|
|
4
|
+
[](https://pypi.org/project/threeypay-sdk/)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
|
|
7
|
+
### V1.1.0。请务必参照对接文档进行对接
|
|
8
|
+
|
|
9
|
+
1. **3ypay (汇联支付) Python SDK**,支持统一支付、交易查询、退款、转账、分账等功能。
|
|
10
|
+
2. **具体参数说明**,请查看 [API文档](https://doc.3ypay.com/)。
|
|
11
|
+
3. **请务必对照文档进行配置**,并确保代码正确运行。
|
|
12
|
+
|
|
13
|
+
## 安装
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pip install threeypay-sdk
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## 快速开始
|
|
20
|
+
|
|
21
|
+
### 1. 初始化 SDK
|
|
22
|
+
|
|
23
|
+
```python
|
|
24
|
+
from threeypay import ThreeYPay
|
|
25
|
+
|
|
26
|
+
# 配置参数
|
|
27
|
+
app_id = "YOUR_APP_ID" # 应用 APPID
|
|
28
|
+
private_key = "YOUR_PRIVATE_KEY" # 商户私钥 (RSA2)
|
|
29
|
+
platform_public_key = "YOUR_PLATFORM_PUBLIC_KEY" # 平台公钥
|
|
30
|
+
is_sandbox = True # 是否使用测试环境
|
|
31
|
+
|
|
32
|
+
# 初始化 SDK
|
|
33
|
+
pay = ThreeYPay(app_id, private_key, platform_public_key, is_sandbox)
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### 2. 支付宝扫码支付
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
import time
|
|
40
|
+
|
|
41
|
+
result = pay.payment().alipay(
|
|
42
|
+
mch_order_no=f"ORDER_{int(time.time() * 1000)}",
|
|
43
|
+
order_amount="0.01",
|
|
44
|
+
subject="测试商品",
|
|
45
|
+
description="商品描述",
|
|
46
|
+
notify_url="https://yourdomain.com/notify",
|
|
47
|
+
client_ip="127.0.0.1",
|
|
48
|
+
expired_time=int(time.time() * 1000) + 30*60*1000
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
print(result)
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### 3. 交易查询
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
result = pay.payment().query_by_pay_order_no("P202603062034302534880")
|
|
58
|
+
print(result)
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## 功能模块
|
|
62
|
+
|
|
63
|
+
- **Payment** - 支付模块(支付宝、微信、银联等)
|
|
64
|
+
- **Transfer** - 转账模块(安全发、直付通)
|
|
65
|
+
- **ProfitSharing** - 分账模块
|
|
66
|
+
- **Account** - 账户管理模块
|
|
67
|
+
- **Merchant** - 商户管理模块
|
|
68
|
+
|
|
69
|
+
## 文档
|
|
70
|
+
|
|
71
|
+
完整API文档:https://doc.3ypay.com/
|
|
72
|
+
|
|
73
|
+
## License
|
|
74
|
+
|
|
75
|
+
MIT
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "threeypay-sdk"
|
|
7
|
+
version = "1.1.0"
|
|
8
|
+
requires-python = ">=3.9"
|
|
9
|
+
description = "3ypay (汇联支付) Python SDK - 统一支付、查询、退款、转账、分账等"
|
|
10
|
+
readme = "README.md"
|
|
11
|
+
license = {text = "MIT"}
|
|
12
|
+
authors = [
|
|
13
|
+
{name = "3YPay Team", email = "support@3ypay.com"}
|
|
14
|
+
]
|
|
15
|
+
keywords = ["payment", "3ypay", "alipay", "wechat", "unionpay", "sdk"]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Development Status :: 4 - Beta",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"License :: OSI Approved :: MIT License",
|
|
20
|
+
"Programming Language :: Python :: 3",
|
|
21
|
+
"Programming Language :: Python :: 3.9",
|
|
22
|
+
"Programming Language :: Python :: 3.10",
|
|
23
|
+
"Programming Language :: Python :: 3.11",
|
|
24
|
+
"Programming Language :: Python :: 3.12",
|
|
25
|
+
]
|
|
26
|
+
dependencies = [
|
|
27
|
+
"requests>=2.28.0",
|
|
28
|
+
"cryptography>=38.0.0",
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
[project.optional-dependencies]
|
|
32
|
+
dev = [
|
|
33
|
+
"pytest>=7.0.0",
|
|
34
|
+
"pytest-cov>=4.0.0",
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
[project.urls]
|
|
38
|
+
Homepage = "https://gitee.com/threeypay/threeypay-python-sdk"
|
|
39
|
+
Source = "https://gitee.com/threeypay/threeypay-python-sdk"
|
|
40
|
+
Issues = "https://gitee.com/threeypay/threeypay-python-sdk/issues"
|
|
41
|
+
Documentation = "https://doc.3ypay.com/"
|
|
42
|
+
|
|
43
|
+
[tool.setuptools.packages.find]
|
|
44
|
+
include = ["threeypay*"]
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import json
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
from cryptography.hazmat.primitives import serialization
|
|
5
|
+
from cryptography.hazmat.primitives.asymmetric import rsa
|
|
6
|
+
|
|
7
|
+
from threeypay.client import Client
|
|
8
|
+
from threeypay.config import Config
|
|
9
|
+
from threeypay.exceptions import HttpException
|
|
10
|
+
from threeypay.utils.signer import Signer
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def generate_key_pair():
|
|
14
|
+
private_key = rsa.generate_private_key(public_exponent=65537, key_size=2048)
|
|
15
|
+
private_pem = private_key.private_bytes(
|
|
16
|
+
encoding=serialization.Encoding.PEM,
|
|
17
|
+
format=serialization.PrivateFormat.PKCS8,
|
|
18
|
+
encryption_algorithm=serialization.NoEncryption(),
|
|
19
|
+
).decode("utf-8")
|
|
20
|
+
public_pem = private_key.public_key().public_bytes(
|
|
21
|
+
encoding=serialization.Encoding.PEM,
|
|
22
|
+
format=serialization.PublicFormat.SubjectPublicKeyInfo,
|
|
23
|
+
).decode("utf-8")
|
|
24
|
+
return private_pem, public_pem
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class FakeResponse:
|
|
28
|
+
def __init__(self, payload, status_code=200):
|
|
29
|
+
self.payload = payload
|
|
30
|
+
self.status_code = status_code
|
|
31
|
+
self.text = json.dumps(payload, ensure_ascii=False)
|
|
32
|
+
|
|
33
|
+
def json(self):
|
|
34
|
+
return self.payload
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class BadJsonResponse(FakeResponse):
|
|
38
|
+
def __init__(self, status_code=500):
|
|
39
|
+
self.payload = None
|
|
40
|
+
self.status_code = status_code
|
|
41
|
+
self.text = "<html>error</html>"
|
|
42
|
+
|
|
43
|
+
def json(self):
|
|
44
|
+
raise ValueError("invalid json")
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class FakeSession:
|
|
48
|
+
def __init__(self, response=None, exc=None):
|
|
49
|
+
self.response = response
|
|
50
|
+
self.exc = exc
|
|
51
|
+
self.headers = {}
|
|
52
|
+
self.calls = []
|
|
53
|
+
|
|
54
|
+
def post(self, url, json=None, timeout=None):
|
|
55
|
+
self.calls.append({"url": url, "json": json, "timeout": timeout})
|
|
56
|
+
if self.exc is not None:
|
|
57
|
+
raise self.exc
|
|
58
|
+
return self.response
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def build_client(response=None, exc=None, platform_keys=None):
|
|
62
|
+
merchant_private, merchant_public = generate_key_pair()
|
|
63
|
+
platform_private, platform_public = platform_keys or generate_key_pair()
|
|
64
|
+
config = Config("app-1", merchant_private, platform_public, True)
|
|
65
|
+
config.set_gateway_url("https://unit.test")
|
|
66
|
+
session = FakeSession(response, exc)
|
|
67
|
+
return Client(config, session=session), session, merchant_public, platform_private
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def signed(payload, platform_private):
|
|
71
|
+
payload = dict(payload)
|
|
72
|
+
payload["sign"] = Signer.sign(payload, platform_private)
|
|
73
|
+
return payload
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def test_envelope_is_signed_and_biz_content_is_compact_json():
|
|
77
|
+
merchant_private, merchant_public = generate_key_pair()
|
|
78
|
+
platform_private, platform_public = generate_key_pair()
|
|
79
|
+
payload = signed({"code": 200, "msg": "ok", "data": {"a": 1}}, platform_private)
|
|
80
|
+
|
|
81
|
+
config = Config("app-1", merchant_private, platform_public, True)
|
|
82
|
+
config.set_gateway_url("https://unit.test")
|
|
83
|
+
session = FakeSession(FakeResponse(payload))
|
|
84
|
+
|
|
85
|
+
client = Client(config, session=session)
|
|
86
|
+
result = client.post("/demo", {"foo": "bar", "nested": {"x": 1}})
|
|
87
|
+
parsed = client.parse_response(result)
|
|
88
|
+
|
|
89
|
+
assert parsed["data"] == {"a": 1}
|
|
90
|
+
assert len(session.calls) == 1
|
|
91
|
+
call = session.calls[0]
|
|
92
|
+
assert call["url"] == "https://unit.test/demo"
|
|
93
|
+
|
|
94
|
+
envelope = call["json"]
|
|
95
|
+
assert envelope["appId"] == "app-1"
|
|
96
|
+
assert envelope["signType"] == "RSA2"
|
|
97
|
+
assert envelope["version"] == "1.0"
|
|
98
|
+
assert envelope["charset"] == "UTF-8"
|
|
99
|
+
assert isinstance(envelope["timestamp"], int)
|
|
100
|
+
assert envelope["timestamp"] > 10**12
|
|
101
|
+
assert isinstance(envelope["bizContent"], str)
|
|
102
|
+
assert json.loads(envelope["bizContent"]) == {"foo": "bar", "nested": {"x": 1}}
|
|
103
|
+
assert "subSn" not in envelope
|
|
104
|
+
assert Signer.verify(envelope, merchant_public) is True
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def test_sub_sn_is_included_when_set():
|
|
108
|
+
client, session, merchant_public, platform_private = build_client(FakeResponse({"code": 200}))
|
|
109
|
+
session.response = FakeResponse(signed({"code": 200, "msg": "ok"}, platform_private))
|
|
110
|
+
|
|
111
|
+
client.set_sub_sn("sub-9")
|
|
112
|
+
client.post("/demo", {})
|
|
113
|
+
envelope = session.calls[0]["json"]
|
|
114
|
+
assert envelope["subSn"] == "sub-9"
|
|
115
|
+
assert Signer.verify(envelope, merchant_public) is True
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def test_business_error_is_raised_before_signature_verification():
|
|
119
|
+
payload = {"code": 500, "msg": "余额不足"}
|
|
120
|
+
response = FakeResponse(payload)
|
|
121
|
+
client, _, _, _ = build_client(response)
|
|
122
|
+
|
|
123
|
+
with pytest.raises(HttpException) as exc_info:
|
|
124
|
+
client.parse_response(payload)
|
|
125
|
+
|
|
126
|
+
assert "code=500" in str(exc_info.value)
|
|
127
|
+
assert "余额不足" in str(exc_info.value)
|
|
128
|
+
assert exc_info.value.code == 500
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def test_http_error_body_is_preserved():
|
|
132
|
+
client, _, _, _ = build_client(FakeResponse({"code": 401, "msg": "未授权"}, status_code=401))
|
|
133
|
+
|
|
134
|
+
with pytest.raises(HttpException) as exc_info:
|
|
135
|
+
client.post("/demo", {})
|
|
136
|
+
|
|
137
|
+
assert exc_info.value.status_code == 401
|
|
138
|
+
assert exc_info.value.code == 401
|
|
139
|
+
assert exc_info.value.response_body == {"code": 401, "msg": "未授权"}
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def test_non_json_response_raises_with_status_and_text():
|
|
143
|
+
client, _, _, _ = build_client(BadJsonResponse())
|
|
144
|
+
|
|
145
|
+
with pytest.raises(HttpException) as exc_info:
|
|
146
|
+
client.post("/demo", {})
|
|
147
|
+
|
|
148
|
+
assert exc_info.value.status_code == 500
|
|
149
|
+
assert exc_info.value.response_body == "<html>error</html>"
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def test_bad_signature_is_rejected():
|
|
153
|
+
client, _, _, _ = build_client(FakeResponse({"code": 200, "msg": "ok", "sign": "aW52YWxpZA=="}))
|
|
154
|
+
|
|
155
|
+
with pytest.raises(HttpException) as exc_info:
|
|
156
|
+
client.parse_response({"code": 200, "msg": "ok", "sign": "aW52YWxpZA=="})
|
|
157
|
+
|
|
158
|
+
assert str(exc_info.value) == "签名验证失败"
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def test_withdrawal_code_zero_is_success_and_data_string_is_decoded():
|
|
162
|
+
platform_private, platform_public = generate_key_pair()
|
|
163
|
+
payload = signed({"code": 0, "msg": "ok", "data": '{"status":"PROCESSING"}'}, platform_private)
|
|
164
|
+
client, _, _, _ = build_client(FakeResponse(payload), platform_keys=(platform_private, platform_public))
|
|
165
|
+
|
|
166
|
+
result = client.parse_response(payload)
|
|
167
|
+
|
|
168
|
+
assert client.is_success(payload) is True
|
|
169
|
+
assert result["data"] == {"status": "PROCESSING"}
|