让 AI Agent 自己收钱:一行代码集成支付宝/微信/USDC/ETH 支付。支持 LangChain Agent、x402 协议、AP2 协议。
Project description
langchain-paypack:让 AI Agent 自己收钱(支付宝/微信/USDC/ETH)
一行代码给你的 LangChain Agent 加上支付能力。支持 支付宝 ¥0.01 起、微信支付、USDC、ETH。
📦 安装
pip install paypack-langchain
要求 Python 3.10+。
🚀 3 分钟快速上手
第一步:获取 API Key
免费注册,获取你的 API Key:
curl -X POST https://rhcjw.com/pay/v1/register \
-H "Content-Type: application/json" \
-d '{"name": "你的项目名", "email": "你的邮箱"}'
返回示例:
{"api_key": "ppk_98daae1c3145406d9de46ec0fdbb82d6"}
⚠️ 保存好这个 Key,后面每次调用都需要它。
第二步:创建一笔支付
新建一个 Python 文件 test_pay.py:
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)
运行:
python test_pay.py
输出示例:
订单已创建
订单号: PP2026071400ABC123
金额: ¥0.01
状态: pending
支付链接: https://openapi.alipay.com/...
二维码: https://rhcjw.com/pay/q/PP2026071400ABC123
用支付宝扫码上面的二维码,¥0.01 就能验证整个流程。
第三步:查询支付结果
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 自动判断什么时候需要收费。
完整代码
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 还是链上签名。
用法示例:三种货币
人民币(支付宝/微信):
tool.run(amount=0.01, subject="AI 查询", currency="CNY")
USDC 链上支付:
tool.run(amount=0.01, subject="AI 查询", currency="USDC", to="0x收款钱包地址")
ETH 链上支付:
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 实测到账)。
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file langchain_paypack-0.7.4.tar.gz.
File metadata
- Download URL: langchain_paypack-0.7.4.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
81926f2eca5fa65e56e857f74995471190d84d06eba3baa9ca724e48830c5ae4
|
|
| MD5 |
59e6e37481ee3610b46a89a5a612c2c0
|
|
| BLAKE2b-256 |
0b2ef5fbbe36db49bac18d6fa2c8552f252cd0585c63503a4f3a1a86e370e4fe
|
File details
Details for the file langchain_paypack-0.7.4-py3-none-any.whl.
File metadata
- Download URL: langchain_paypack-0.7.4-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fc45353d9f5e7d19c40937caff35e68859922dcda92edf4740fbe06a1d9419ed
|
|
| MD5 |
18c3bb8593f00b77545fb817386427d2
|
|
| BLAKE2b-256 |
4ee7164fd397974a9fa385f552a2ae7f56a96785bb6988d89510a79d3019f19c
|