You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Stripe PHP API从商户账户向联盟客户支付佣金?

使用Stripe PHP API向联盟客户支付佣金的实操方案

我之前帮不少开发者搞定过类似的联盟佣金支付场景,用Stripe PHP API其实没那么复杂,下面给你一步步拆解实操方案:

一、前期准备工作

先把基础配置搞定,不然后面没法推进:

  • 确保你的Stripe账户已经开通转账权限(如果是向外部银行/银行卡转账,需要完成账户身份验证)
  • 用Composer安装Stripe PHP SDK,执行命令:composer require stripe/stripe-php
  • 准备好你的Stripe API密钥:测试环境用sk_test_xxx,生产环境用sk_live_xxx(千万别把生产密钥泄露了)

二、核心实现步骤

1. 创建/获取联盟客户的Stripe Customer对象

你需要先把联盟用户在Stripe系统里注册成Customer,这样方便统一管理他们的付款方式和转账记录:

require 'vendor/autoload.php';

// 初始化Stripe
\Stripe\Stripe::setApiKey('sk_test_your_secret_key');

// 如果用户还没在Stripe系统里,创建新客户
$customer = \Stripe\Customer::create([
  'email' => 'affiliate_user@example.com',
  'name' => '张三(联盟推广)',
]);

// 要是已经有客户ID了,直接调取已有记录
// $customer = \Stripe\Customer::retrieve('cus_xxxxxx');

2. 绑定客户的付款方式(银行卡/银行账户)

要给用户打钱,得先把他们的收款方式关联到Customer对象上,这里推荐用Stripe Elements让用户在前端输入信息(符合PCI合规,不用你碰敏感数据):

// 假设前端通过Stripe Elements生成了payment_method_id(格式是pm_xxx)
$paymentMethod = \Stripe\PaymentMethod::retrieve('pm_xxxxxx');
// 把付款方式绑定到客户
$paymentMethod->attach(['customer' => $customer->id]);

// 可选:设置为该客户的默认收款方式
\Stripe\Customer::update(
  $customer->id,
  ['invoice_settings' => ['default_payment_method' => 'pm_xxxxxx']]
);

如果你已经有用户的银行账户信息(必须符合PCI要求),也可以直接创建外部账户:

// btok_xxx是通过Stripe.js生成的银行账户token
$externalAccount = \Stripe\Customer::createSource(
  $customer->id,
  ['source' => 'btok_xxxxxx']
);

3. 发起佣金转账

当你要给联盟用户付佣金时,调用Transfer API即可,金额单位是分(比如500就是5美元):

$transfer = \Stripe\Transfer::create([
  'amount' => 1000, // 这里是10美元佣金
  'currency' => 'usd',
  'destination' => 'pm_xxxxxx', // 填刚才绑定的payment_method或银行账户ID(ba_xxx)
  'description' => '联盟佣金:产品A推广奖励', // 备注清楚,方便对账
]);

// 记得把$transfer->id存到你的数据库里,后续对账、查状态都用得到

4. 监听转账状态(可选但推荐)

转账可能会出现pending、paid、failed等状态,建议通过Stripe Webhook实时监听,自动更新你的系统记录:

// Webhook处理脚本示例
$payload = @file_get_contents('php://input');
$event = null;

try {
  $event = \Stripe\Event::constructFrom(
    json_decode($payload, true)
  );
} catch(\UnexpectedValueException $e) {
  // 无效的请求 payload,直接返回400
  http_response_code(400);
  exit();
}

// 处理转账成功事件
if ($event->type == 'transfer.paid') {
  $transfer = $event->data->object;
  // 这里写你的业务逻辑:比如更新数据库里的佣金状态为「已支付」,给用户发通知
}

// 处理转账失败事件
if ($event->type == 'transfer.failed') {
  $transfer = $event->data->object;
  // 这里处理失败逻辑:比如记录失败原因,给运营发提醒,或者通知用户重新绑定收款方式
}

// 告诉Stripe你已经收到事件
http_response_code(200);

三、关键注意事项

  • 合规第一:联盟佣金支付需要有明确的合作协议,保留好所有支付记录,符合当地金融法规
  • 费用说明:Stripe转账会收取手续费,不同地区、不同收款方式费率不一样,提前查好定价
  • 测试环境模拟:用Stripe提供的测试卡号(比如4242 4242 4242 4242)和测试银行账户来跑通流程,别直接用生产环境
  • 最低转账额:不同币种有最低转账要求,比如美元最低0.5美元,别低于这个数,不然会失败

内容的提问来源于stack exchange,提问作者joomla.dev250

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:51:08