跳转到主内容

SDK

TypeScript、 Python 和 官方客户端 PHP相同。 API 三者行为相同,行为相同 测试。

那个... SDK 已存在,因为原始 API 有一些容易犯错的行为 做的时候很贵——一个 201 Created 包含拒绝,a hosted 没有默认的旗帜, 非时间戳 ISO 8601,一个被接受和忽略的呼号参数。 每一个文件都写在代码上 在那里你将遇到它。


安装​

软件包不在npm、PyPI或Packagist上。每个软件包都从自己的公共寄存器中安装到 - 由于 npm 和 composer 只能从寄存器的根部读到一个表单——或 从这个网站的存档服务, 对于一个无法访问 GitHub 的建筑。

类型脚本 / 节点js 18+​

npm install github:4pay-online/sdk-typescript

或从档案:

curl -O https://docs.4pay.online/sdk/4pay-sdk-typescript.tar.gz
npm install ./4pay-sdk-typescript.tar.gz

dist/ 被承诺,所以没有安装编译任何内容。

蟒蛇 3.9+​

pip install "git+https://github.com/4pay-online/sdk-python.git"
# or: pip install https://docs.4pay.online/sdk/4pay-sdk-python.tar.gz

SDK 没有运行时依赖。授权委托签名验证(AP2)需要额外安装一个依赖:

pip install "4pay-sdk[crypto] @ git+https://github.com/4pay-online/sdk-python.git"

PHP 8.1+​

作曲家通过一个 vcs 条目 :

{
"repositories": [
{ "type": "vcs", "url": "https://github.com/4pay-online/sdk-php.git" }
],
"require": { "4pay/sdk": "dev-master" }
}
composer install

或解开归档并运行 composer install 里面,需要你帮忙 ext-json, ext-curl 和 ext-bcmath.

来源: github.com/4pay-online — 类型脚本、 Python 和 PHP, MIT. ** 所有三个档案:** docs.4pay.online/sdk/ - 包括来源、实例和测试。


第一次付款​

import { FourPay } from "@4pay/sdk";

const pay = new FourPay({
apiKey: process.env.FOURPAY_API_KEY,
organizationId: process.env.FOURPAY_ORGANIZATION_ID, // issued together with the key
baseUrl: "https://sandbox.4pay.online",
});

const { transaction, paymentUrl } = await pay.createHostedPayment({
type: "payment",
amount: "150.00", // or "15000" in minor units — both work
currency: "USD",
env: "test",
txid: "order-8891", // your order id, and the idempotency key
returnUrl: "https://shop.example/thanks",
failUrl: "https://shop.example/sorry",
});

redirect(paymentUrl); // valid for ~15 minutes
from fourpay import FourPay

client = FourPay(
api_key=os.environ["FOURPAY_API_KEY"],
organization_id=os.environ["FOURPAY_ORGANIZATION_ID"],
base_url="https://sandbox.4pay.online",
)

transaction, payment_url = client.create_hosted_payment(
amount="150.00", currency="USD", env="test", txid="order-8891",
returnUrl="https://shop.example/thanks", failUrl="https://shop.example/sorry",
)
$pay = new FourPay\Client(
apiKey: getenv('FOURPAY_API_KEY'),
organizationId: getenv('FOURPAY_ORGANIZATION_ID'),
baseUrl: 'https://sandbox.4pay.online',
);

[$transaction, $paymentUrl] = $pay->createHostedPayment([
'amount' => '150.00', 'currency' => 'USD', 'env' => 'test', 'txid' => 'order-8891',
'returnUrl' => 'https://shop.example/thanks', 'failUrl' => 'https://shop.example/sorry',
]);

对你有什么好处?​

原始行为 API或 SDK 已经
hosted 没有违约;没有违约,将拒绝付款。 unexpected cardcreateHostedPayment 摆好
201 Created 携带拒绝(rejected, failed)涨薪 TransactionRejectedError,与 isRoutingFailure 告诉"没有终点站" 从"卡拒绝了"
失踪的 x-organization-id 拒绝使用 401 支付结束点和 400 其他问题两个信头总是发送; 一个空的 400 or 401 表面为认证错误
时间戳像 2026-09-03 11:18:30.101839 Etc/UTCparseTimestamp 返回正确的 UTC 所有三种语文的日期
呼号是 cursor; start_cursor 被默默忽略 。iterateTransactions 跟踪真实的光标, 如果它停止移动则停止
page, offset, from, to, currency 拒绝使用 422不在类型定义中, 所以您的编辑器先拒绝它们
重试创建时没有 txid 两次收费写作只随身携带一等钥匙进行重试
货币作为小单位的小数字符串toMinorUnits / fromMinorUnits,整个整数算术——永远不浮
Webhook 签名结束 <timestamp>.<raw body>verifyWebhook ——恒定时,重播窗口,生字节

Webhook 通知​

import { verifyWebhook, EventDeduplicator } from "@4pay/sdk";

const seen = new EventDeduplicator();

app.post("/webhooks/payment", express.raw({ type: "application/json" }), (req, res) => {
let event;
try {
event = verifyWebhook(req.body, req.headers, process.env.WEBHOOK_SECRET);
} catch {
return res.sendStatus(401);
}

res.sendStatus(200); // acknowledge first, work afterwards
if (!seen.accept(event.event_id)) return; // deliveries repeat

if (event.data.status === "charged") fulfil(event.data.txid);
});

原始体重要: 签名覆盖字节, 和解析时的复序体 意思是说... express.raw() 此路径; 在弗拉斯克 request.get_data(); in PHP file_get_contents('php://input').


钱​

toMinorUnits("10.00", "USD")   // "1000"
toMinorUnits("10.001", "USD") // throws — USD has two decimals, and rounding money is not ours to do
toMinorUnits(10.5, "USD") // throws — pass "10.50", not a float
fromMinorUnits("1000", "JPY") // "1000" — JPY has no minor unit
formatAmount("2500", "EUR") // "25.00 EUR"

一个未知的货币上升而不是假设两个小数:一个猜测的比额过高 JPY a 百倍,而 ETH 的偏差会达到十六个数量级。


沙盒固定装置​

import { sandbox } from "@4pay/sdk";

sandbox.CARD_CHARGE // settles
sandbox.CARD_REJECT // refused by the provider
sandbox.CARD_REFUND // settles, then refunds
sandbox.CARD_FAIL // fails

模拟器匹配整个TUPL——数字, CVV持有者 过期 —— 所以这些常数可以拯救你 调自 a rejected 由小写纸牌持有人名所导致。见 沙盒测试( T).


代理商业​

那个... SDK 还有这四个协议的商家一方 AI 使用代理人支付 -- MCP, AP2, ACP 和 A2A已经 其自己的页面.


实例​

档案船可以使用的例子:

示例所显示的
first-payment/所有三种语言的生命周期结束
webhook-server/三种语言的经核实的网页浏览端点
agent-tools/An AI 代理通过函数调用接收付款
acp-merchant/An ACP 从您的端点取出
a2a-agent/支付代理人可以聘请其他代理人
ap2-checkout/在提出指控前核查一项任务授权

支助​

  • 来源、问题和更改日志:档案 README.md
  • 平台问题: 支助@4pay.online - 包括交易 id