Metadata-Version: 2.4
Name: alipay-sdk
Version: 2.0.1
Summary: AliPay Python SDK
Home-page: https://github.com/block-cat/alipay_sdk
Author: blackcat
Author-email: kfx2007@163.com
License: GNU
Keywords: alipay sdk
Classifier: License :: OSI Approved :: GNU Affero General Public License v3
Classifier: Programming Language :: Python
Classifier: Operating System :: OS Independent
Classifier: Topic :: Scientific/Engineering :: Astronomy
Classifier: Topic :: Scientific/Engineering :: GIS
Classifier: Topic :: Scientific/Engineering :: Mathematics
Classifier: Intended Audience :: Science/Research
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Information Technology
Description-Content-Type: text/markdown
Requires-Dist: autils>=0.2.8
Requires-Dist: requests
Requires-Dist: pycryptodome
Dynamic: author
Dynamic: author-email
Dynamic: classifier
Dynamic: description
Dynamic: description-content-type
Dynamic: home-page
Dynamic: keywords
Dynamic: license
Dynamic: requires-dist
Dynamic: summary

![alipay](alipay.png)

[![Build Status](https://travis-ci.org/block-cat/alipay_sdk.svg?branch=master)](https://travis-ci.org/block-cat/alipay_sdk)
[![Coverage Status](https://coveralls.io/repos/github/block-cat/alipay_sdk/badge.svg?branch=master)](https://coveralls.io/github/block-cat/alipay_sdk?branch=master)
![PyPI](https://img.shields.io/pypi/v/alipay_sdk)
![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/block-cat/alipay_sdk)


支付宝第三方 Python SDK

## 2.0.1：Antom 国际退款

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

```python
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 再序列化：

```python
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`。
历史测试可能调用外部接口，不要直接运行整个测试目录。

官方依据：[退款](https://docs.antom.com/ac/ams/refund_online)、
[退款查询](https://docs.antom.com/ac/ams/ir_online)、
[签名与验签](https://docs.antom.com/ac/ams/digital_signature)、
[环境与报文](https://docs.antom.com/ac/ams/api_fund)。

# About

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

### Main functionalities

* 支付功能
* 口碑功能

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

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

### Requirements

python >= 3.6

### Install

```
pip install alipay_sdk
```

### How to use

首先要到[支付宝开放平台](https://openhome.alipay.com/)注册一个开发者账号，并创建一个应用并获取应用ID(AppID)。

通知需要配置应用密钥、商户密钥和支付宝公钥，详情参考[官方文档](https://docs.open.alipay.com/291/105971/)

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

```python
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平台的语言，需要补齐密钥格式中的格式头：

```txt
-----BEGIN RSA PRIVATE KEY-----
..........
-----END RSA PRIVATE KEY-----
```

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

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