Metadata-Version: 2.4
Name: langchain-paypack
Version: 0.7.4
Summary: 让 AI Agent 自己收钱：一行代码集成支付宝/微信/USDC/ETH 支付。支持 LangChain Agent、x402 协议、AP2 协议。
Project-URL: Homepage, https://github.com/rhcjw/paypack
Author: 荣华
License: Apache-2.0
Keywords: agent-pay,agent收款,ai支付,alipay,ap2,base,dify,langchain,usdc,wechat,x402
Requires-Python: >=3.10
Requires-Dist: httpx>=0.27.0
Requires-Dist: langchain-core>=0.3.0
Requires-Dist: paypack>=0.7.0
Requires-Dist: pydantic>=2.0.0
Requires-Dist: web3<8.0.0,>=6.0.0
Description-Content-Type: text/markdown

# langchain-paypack：让 AI Agent 自己收钱（支付宝/微信/USDC/ETH）

> 一行代码给你的 LangChain Agent 加上支付能力。支持 **支付宝 ¥0.01 起**、**微信支付**、**USDC**、**ETH**。

---

## 📦 安装

```bash
pip install paypack-langchain
```

要求 Python 3.10+。

---

## 🚀 3 分钟快速上手

### 第一步：获取 API Key

免费注册，获取你的 API Key：

```bash
curl -X POST https://rhcjw.com/pay/v1/register \
  -H "Content-Type: application/json" \
  -d '{"name": "你的项目名", "email": "你的邮箱"}'
```

返回示例：
```json
{"api_key": "ppk_98daae1c3145406d9de46ec0fdbb82d6"}
```

> ⚠️ 保存好这个 Key，后面每次调用都需要它。

### 第二步：创建一笔支付

新建一个 Python 文件 `test_pay.py`：

```python
from paypack_langchain import PayPackTool

# 用你的 API Key 初始化
tool = PayPackTool(
    api_key="ppk_你的API_KEY",      # 上一步获取的 Key
    base_url="https://rhcjw.com/pay"  # PayPack Cloud 服务地址
)

# 创建一笔 ¥0.01 的支付
result = tool.run(amount=0.01, subject="AI 数据查询")
print(result)
```

运行：

```bash
python test_pay.py
```

输出示例：
```
订单已创建
订单号: PP2026071400ABC123
金额: ¥0.01
状态: pending
支付链接: https://openapi.alipay.com/...
二维码: https://rhcjw.com/pay/q/PP2026071400ABC123
```

用支付宝扫码上面的二维码，¥0.01 就能验证整个流程。

### 第三步：查询支付结果

```python
from paypack_langchain import PayPackQueryTool

query = PayPackQueryTool(
    api_key="ppk_你的API_KEY",
    base_url="https://rhcjw.com/pay",
)

result = query.run(order_id="PP2026071400ABC123")
print(result)
# 输出: {"status": "success", "amount": 0.01, ...}
```

---

## 🧠 在 LangChain Agent 中使用

这是最常见的使用场景——让 Agent 自动判断什么时候需要收费。

### 完整代码

```python
from langchain.agents import create_react_agent, AgentExecutor
from langchain_openai import ChatOpenAI
from langchain_core.tools import Tool
from paypack_langchain import PayPackTool

# 1. 初始化支付工具
pay_tool = PayPackTool(
    api_key="ppk_你的API_KEY",
    base_url="https://rhcjw.com/pay"
)

# 2. 包装成 LangChain Tool
tools = [
    Tool(
        name="pay_for_data",
        func=lambda q: pay_tool.run(amount=0.01, subject=q),
        description="当用户需要付费数据时调用此工具，返回支付二维码链接"
    )
]

# 3. 创建 Agent
llm = ChatOpenAI(model="gpt-4")
agent = create_react_agent(llm, tools, prompt="你是一个数据查询助手。当用户查询需要付费的数据时，先用 pay_for_data 生成支付，用户扫码支付后再返回结果。")
executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

# 4. 运行
result = executor.invoke({"input": "帮我查最新的市场数据"})
print(result)
```

运行效果：
```
> Entering new AgentExecutor chain...
用户需要付费数据，我先调用支付工具生成收款码。
调用 pay_for_data...
订单已创建，金额 ¥0.01，请扫码支付后继续。
> Finished chain.
```

---

## 💰 支持的币种和网络

| 币种 | 路由方式 | 支持网络 | 状态 |
|------|---------|---------|------|
| **CNY (人民币)** | PayPack Cloud API | 支付宝 / 微信支付 | ✅ 生产可用 |
| **USDC** | 链上签名 | Base / Ethereum / Polygon / Arbitrum | ✅ 可用 |
| **ETH** | 链上签名 | Base / Ethereum / Polygon / Arbitrum | ✅ 可用 |

自动路由——你只需指定 `currency` 参数，SDK 自动走 Cloud API 还是链上签名。

### 用法示例：三种货币

**人民币（支付宝/微信）：**
```python
tool.run(amount=0.01, subject="AI 查询", currency="CNY")
```

**USDC 链上支付：**
```python
tool.run(amount=0.01, subject="AI 查询", currency="USDC", to="0x收款钱包地址")
```

**ETH 链上支付：**
```python
tool.run(amount=0.01, subject="AI 查询", currency="ETH", to="0x收款钱包地址")
```

---

## 📖 API 参考

### PayPackTool（创建支付）

| 参数 | 类型 | 必填 | 说明 |
|------|------|------|------|
| `api_key` | string | CNY 必填 | PayPack Cloud API Key (`ppk_...`) |
| `base_url` | string | CNY 必填 | Cloud 服务地址: `https://rhcjw.com/pay` |
| `private_key` | string | USDC/ETH 必填 | 钱包私钥 (`0x...`)，或设置环境变量 `PRIVATE_KEY` |
| `network` | string | USDC/ETH 可选 | 区块链网络，默认 `base-sepolia` |
| `broadcast` | bool | 否 | `True`=广播上链，`False`=仅签名预览 |
| `amount` | float | **是** | 支付金额，如 `0.01`（CNY 即 ¥0.01，USDC 即 0.01 USDC） |
| `subject` | string | **是** | 商品描述，显示在支付页面 |
| `currency` | string | 否 | 币种，默认 `CNY`，可选 `USDC`/`ETH` |
| `to` | string | USDC/ETH 必填 | 收款钱包地址 (`0x...`) |
| `callback_url` | string | 否 | 支付结果回调地址（Cloud 模式） |

### PayPackQueryTool（查询支付）

| 参数 | 类型 | 必填 | 说明 |
|------|------|------|------|
| `api_key` | string | 是 | 你的 API Key |
| `base_url` | string | 是 | Cloud 服务地址 |
| `order_id` | string | 是 | 支付返回的订单号 |

---

## 🔗 相关链接

- **在线体验**: https://rhcjw.com/pay — 浏览器直接测试支付流程
- **PyPI 页面**: https://pypi.org/project/paypack-langchain/
- **GitHub**: https://github.com/rhcjw/paypack
- **问题反馈**: https://github.com/rhcjw/paypack/issues

---

## ❓ 常见问题

**Q: 需要商户资质吗？**
A: 支付宝/微信通道需要商户号（我们提供），数字货币通道不需要任何资质。

**Q: 手续费多少？**
A: 支付宝/微信按官方费率，数字货币仅需链上 Gas 费。

**Q: 能用在生产环境吗？**
A: 可以。支付宝/微信已生产验证（¥0.10 / ¥0.05 实测到账）。
