Skip to main content
Change of provenance

The repository or configuration that created this release differ from a previous release. View details.

中文 | English

商户支付 OpenAPI — Python SDK

PyPI License: MIT

零第三方依赖的 Python 3 SDK:HTTP 用 urllib.request,签名用 hmac/hashlib,测试用标准库 unittest。无需 pip install 任何运行时依赖。

签名算法与服务端签名实现逐字节一致,单测对 ../test-vectors.json 全量复现 base 与 sign。

引入(无需安装依赖)

把 openapi_sdk/ 目录放进你的项目(或把本目录加入 sys.path / PYTHONPATH)即可:

import sys
sys.path.insert(0, "/path/to/python")

from openapi_sdk import Client, Config, Environment

可选地用标准打包安装(仍零依赖):

cd python
pip install .        # 或 python3 -m build;本身不拉任何第三方运行时依赖

快速开始

from openapi_sdk import Client, Config, Environment, ApiError, TransportError

config = Config(
    merchant_no="M00000001",
    api_key="ak_xxx",
    api_secret_pay="sk_pay_xxx",        # pay 类接口 + 代收/退款回调
    api_secret_payout="sk_payout_xxx",  # payout 类接口 + 代付回调
    environment=Environment.SANDBOX,    # 或 Environment.PRODUCTION(须显式传 base_url)
)
client = Client(config)

try:
    # 金额是最小单位整数:10000 = 1 元
    order = client.pay_create(
        out_order_no="ORD1", amount=10000, currency="PHP",
        pay_method="gcash", country="PH",
        notify_url="https://m.example.com/api/notify/pay",
    )
    print(order["pay_url"])
except ApiError as e:        # 业务失败 code != 0
    print(e.code, e.message, e.data)
except TransportError as e:  # HTTP / 网络 / 超时
    print(e, e.status_code)

双环境与自定义基址

环境 基址
Environment.PRODUCTION 无内置基址,必须显式传 base_url
Environment.SANDBOX http://127.0.0.1:3090/api/open/v1

正式环境地址请向服务商获取(形如 https://api.<service_domain>/api/open/v1),用 base_url= 显式传入。选 PRODUCTION 又不传 base_url 会抛 ValueError(提示 baseUrl is required):

config = Config(
    merchant_no="M00000001", api_key="ak_xxx",
    api_secret_pay="...", api_secret_payout="...",
    base_url="https://api.service.example.com/api/open/v1",  # 正式环境必传
)

全部 15 个端点

代收(密钥 api_secret_pay,自动选用):

方法 端点
pay_create(...) /merchant/pay/create
pay_query(order_no=, out_order_no=) /merchant/pay/query
pay_methods_query(country=) /merchant/pay-methods/query
groups_query(biz_type=, currency=, country=) /merchant/groups/query(兼容别名)
countries_query() /merchant/countries/query
balance_query(currency=) /merchant/balance/query
pay_test_complete(result=, ...) /merchant/pay/test/complete(仅测试密钥)

代付(密钥 api_secret_payout,自动选用):

方法 端点
payout_create(...) /merchant/payout/create
payout_query(payout_no=, out_payout_no=) /merchant/payout/query
payout_banks_query(pay_method=, country=, currency=) /merchant/payout/banks/query
payout_proof_query(payout_no=, out_payout_no=) /merchant/payout/proof/query
payout_receipt_query(..., inline=) /merchant/payout/receipt/query
payout_test_complete(result=, ...) /merchant/payout/test/complete(仅测试密钥)

工单(密钥 api_secret_pay,自动选用;对应代付单时也用此密钥):

方法 端点
issue_ticket_create(order_no=, ...) /merchant/issue-ticket/create
issue_ticket_query(ticket_no=, order_no=) /merchant/issue-ticket/query

约定:

  • 每请求自动注入 merchant_no/api_key/timestamp(Unix 秒)/唯一 nonce 与 sign。
  • 值为 None 的参数不放入请求体、也不参与签名。
  • payout_receipt_query 的 inline 以整数 1/0 发送(True→1 内联 base64 图片;False→0 返回带 token 的 URL)。
  • 金额是最小单位整数(10000 = 1 元),用 int 类型传入。
  • 成功返回 data(dict);code != 0(含 HTTP 503+300304、HTTP 500+300302 等非 2xx 业务信封)抛 ApiError(携带 code/message/data/status_code);HTTP/网络错误抛 TransportError(附带 request_sign)。
  • 需要原始信封时用 client.call_raw(path, body, secret)(不因 code != 0 抛异常)。

签名工具(可单独使用)

from openapi_sdk import build_sign_base, sign, verify_callback

base = build_sign_base(payload, secret)   # 逐字节可断言的签名 base
sig  = sign(payload, secret)              # HMAC-SHA256 -> hex 小写
ok   = verify_callback(callback, secret)  # 时序安全(hmac.compare_digest),字段无关

回调验签 + 处理

见 examples/callback_verify.py:解析原始 body → client.verify_pay_callback / client.verify_payout_callback / client.verify_issue_ticket_callback(或顶层 verify_callback,时序安全)→ 按 status 幂等处理(success/failed)→ 应答 HTTP 200 + 纯文本 success。代收/退款/工单回复回调用 api_secret_pay、代付回调用 api_secret_payout,示例各演示一次。验签失败不回成功;同一订单可能再次收到回调。处理务必幂等(同一订单可能多次回调)。

代付反查

平台审核时向商户自己的 HTTPS 地址 POST JSON。decide_payout_precheck(body, saved) 的返回值原样作为 HTTP 200 正文,只能是 approve 或 reject。saved["sign"] 用代付下单返回数据上的 .sign(若下单遇传输超时,可从 TransportError.request_sign 读取本次实际发出的 sign)。payout_no 必须出现,但不与下单请求比较。

from openapi_sdk import decide_payout_precheck

text = decide_payout_precheck(body, saved)

示例

cd python
python3 examples/pay_create.py
python3 examples/payout_create.py
python3 examples/callback_verify.py   # 自演示:造签名回调 -> 验签 -> 应答 -> 篡改反例

跑测试

cd python
python3 -m unittest discover -s tests

测试包含:

  • 读取 ../test-vectors.json,对每个向量断言 build_sign_base == base 且 sign == sign;
  • 回调验签正例 + 篡改一字节反例(含错误密钥、缺 sign);
  • 客户端请求构建(通用字段注入、None 过滤、密钥选择、inline 整数化、信封解析与异常分类)。

Metadata

Release files for bebebus-merchant-openapi-sdk 2.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Change of provenance

The repository or configuration that created these release files differ from that of a previous release.

We recommend inspecting the repository and configuration before proceeding. View details.

Source distribution (sdist)

Source distribution for bebebus-merchant-openapi-sdk 2.3.0
File Size Uploaded Provenance Actions
bebebus_merchant_openapi_sdk-2.3.0.tar.gz 25.1 kB With provenance Details for bebebus_merchant_openapi_sdk-2.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for bebebus-merchant-openapi-sdk 2.3.0
File Interpreter ABI Platform Actions
bebebus_merchant_openapi_sdk-2.3.0-py3-none-any.whl Python 3 none any Details for bebebus_merchant_openapi_sdk-2.3.0-py3-none-any.whl

Total release size: 44.6 kB

Release files / bebebus_merchant_openapi_sdk-2.3.0.tar.gz

Change of provenance

This file belongs to a release with changed provenance: the repository or configuration that created the release files differ from that of a previous release.

We recommend inspecting the repository and configuration before proceeding. View details.

Download URL bebebus_merchant_openapi_sdk-2.3.0.tar.gz
Size 25.1 kB
Tags Source
SHA-256 checksum
How to use checksums
979f93afc19901d1b2b84d321d572863f07c04b2094711de2d4971e23dd6784b
BLAKE2b-256 checksum
How to use checksums
a93d90fea9411b8bc0287b9a8077757f3243180db9645580059ce4d37d675cb6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 21, 2026.

Transparency log

Release files / bebebus_merchant_openapi_sdk-2.3.0-py3-none-any.whl

Change of provenance

This file belongs to a release with changed provenance: the repository or configuration that created the release files differ from that of a previous release.

We recommend inspecting the repository and configuration before proceeding. View details.

Download URL bebebus_merchant_openapi_sdk-2.3.0-py3-none-any.whl
Size 19.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c2ca78ec18784909a9f1c24b089d47c91128493ea70fb7977ed12d20fcd88617
BLAKE2b-256 checksum
How to use checksums
f0beb92664630fc9f01fb5918e590b3f20824ed2ecbac498d6b8629d5cab55cf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 21, 2026.

Transparency log

Security for bebebus-merchant-openapi-sdk 2.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Change of provenance

The repository and configuration that created this release differ from a previous release.

While this could reflect a routine change by the project maintainers, it may also indicate that the release files have been compromised.

Select from 2 files

Attestation for bebebus_merchant_openapi_sdk-2.3.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 21, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page