Skip to main content

alipay

Build Status Coverage Status PyPI GitHub code size in bytes

支付宝第三方 Python SDK

2.0.1:Antom 国际退款

新增 client.payment.refund(...) 与 client.payment.inquiry_refund(...)。 支持全额/部分退款请求;实际退款范围、次数、期限及可用币种以商户合同和支付方式为准。 这是 SDK 接口支持,不自动为 Odoo 等调用方启用退款按钮或完成账务处理。

from alipay.api import Antom

# 密钥由应用从安全配置加载,不要写入代码仓库。
client = Antom(client_id, merchant_private_key, public_key=antom_public_key,
               location="as", sandbox=True, timeout=30)

# 由应用持久化此 ID;同一退款超时后不得生成新 ID 重试。
result = client.payment.refund(
    refundRequestId="refund-order-1001-1",
    paymentId="original-antom-payment-id",
    refundAmount={"currency": "USD", "value": "100"},  # 1.00 USD
    refundReason="部分退款",
    refundNotifyUrl="https://merchant.example/refund/notify",
)
status = client.payment.inquiry_refund(refundRequestId="refund-order-1001-1")
  • 金额必须是最小货币单位的正整数字符串;JPY 等零小数币种不能一律乘 100。
  • refund 的 resultStatus=S 表示退款成功;F 为失败,U 为未知/处理中。 inquiry_refund 的 resultStatus=S 仅表示查询成功,最终结果要读取 refundStatus。
  • 网络超时、验签失败不能当作退款失败后重新建单;先使用原 refundRequestId 查询。 对 REFUND_IN_PROCESS 应等待通知或查询,不重发退款。SDK 不自动重试或轮询。
  • 两个退款接口必须提供 Antom 公钥,在读取响应 JSON 前验证响应签名。 签名、参数错误抛出 ValueError;网络/HTTP 异常由 requests 原样抛出。
  • 应用负责退款权限、支付归属、累计可退额度、并发和持久化幂等控制。

通知验签使用原始请求正文,不要先解析 JSON 再序列化:

valid = client.comm.verify_signature(
    "POST", "/refund/notify", request_headers, raw_body,
    timestamp_header="request-time",
)

验签通过后仍需校验订单、退款请求号、金额和币种,幂等更新业务状态。

升级注意

2.0.0 忽略了 sandbox 参数,始终请求沙箱。2.0.1 已修正:测试必须显式传 sandbox=True; 默认 sandbox=False 会访问生产 /ams/api/。升级前必须审计所有 Antom 调用配置。 生产/沙箱密钥、订单不能混用。欧洲在线支付域名改为 open-de-global.alipay.com。 请求时间改为毫秒,增加超时及禁止自动 HTTP 重定向,隔离不同客户端的绑定凭据。 已有支付/查询接口在提供 public_key 时也会验证响应;未提供公钥的旧调用保留原兼容行为, 建议补配公钥。国内 AliPay 接口未改动。

验证范围:离线测试生成临时 RSA 密钥并 Mock HTTP;没有发起真实支付或退款,也未做商户沙箱联调。 安全测试命令:python -m unittest tests.test_refund_offline -v。 历史测试可能调用外部接口,不要直接运行整个测试目录。

官方依据:退款、 退款查询、 签名与验签、 环境与报文。

About

由于官方sdk有很大的嫌疑是从java拷贝过来的,因此,重新起了这个项目

Main functionalities

  • 支付功能
  • 口碑功能

由于沙箱环境缺少口碑权限支持,仅定义了接口功能,未进行完整的单元测试

更多功能正在开发中...

Requirements

python >= 3.6

Install

pip install alipay_sdk

How to use

首先要到支付宝开放平台注册一个开发者账号,并创建一个应用并获取应用ID(AppID)。

通知需要配置应用密钥、商户密钥和支付宝公钥,详情参考官方文档

假设我们的AppId是12345,商户密钥文件是1.txt,支付宝公钥是2.txt 那么我们的可以这么使用:

with open("1.txt", "r") as f:
    private_key = RSA.importKey(f.read())
with open("2.txt", "r") as f:
    publick_key = RSA.importKey(f.read())
alipay = Alipay("12345",private_key,private_key,
    sign_type="rsa2", ali_public_key=publick_key)

0.0.3 版本起,支持直接传入密钥文本

注意:支付宝公钥等可以使用官方提供的工具生成,但是对于非java平台的语言,需要补齐密钥格式中的格式头:

-----BEGIN RSA PRIVATE KEY-----
..........
-----END RSA PRIVATE KEY-----

接下来就可以使用sdk来调用接口了,以统一下单接口为例:

res = alipay.pay.trade_create(self.alipay.pay.trade_create(
        "5489763229687797", 1.01, "测试统一下单", buyer_id="208810217951438X"))

Metadata

Release files for alipay-sdk 2.0.1

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

Source distribution (sdist)

Source distribution for alipay-sdk 2.0.1
File Size Uploaded
alipay_sdk-2.0.1.tar.gz 26.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for alipay-sdk 2.0.1
File Interpreter ABI Platform
alipay_sdk-2.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 52.1 kB

Release files / alipay_sdk-2.0.1.tar.gz

Download URL alipay_sdk-2.0.1.tar.gz
Size 26.1 kB
Tags Source
SHA-256 checksum
How to use checksums
d3ff07b2002f3f79cdd3f9f006d9b79d9ec52b2d238d5cc10ab8646dd56f4172
BLAKE2b-256 checksum
How to use checksums
179e91b09cfa2337cab87028df32b65563a955f96952d82a05f2cbd9ccf29b3f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / alipay_sdk-2.0.1-py3-none-any.whl

Download URL alipay_sdk-2.0.1-py3-none-any.whl
Size 26.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c8e27e6a6d3c40f6e59d03881aec60f0a556a311dc4934a00d1bad44b53f3186
BLAKE2b-256 checksum
How to use checksums
2b6ae719da38d2a4715c1029ecf721962d48f30d40d9d1a661a8573728184c70
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

2.0.1 This release

2 release files

2.0.0

1 release file

0.0.1

1 release file

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