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

基于PHP实现买家跳转至Stripe完成信用卡支付的方案及代码示例问询

Stripe Marketplace 暂留资金场景下的PHP支付解决方案及示例代码

针对你提到的Marketplace平台场景(卖家通过Express账户入驻,需暂留资金数日,创建PaymentIntent引导买家支付),以下是完整的PHP实现方案:

核心逻辑说明

要实现资金暂留,有两种常见模式:

  1. 手动控制转账:买家支付后资金先留在平台账户,待指定天数后手动调用Stripe Transfer API将资金转给卖家
  2. 自动延迟转账:创建PaymentIntent时设置延迟天数,Stripe会自动在指定天数后将资金划转至卖家账户

两种模式都需要先创建PaymentIntent获取client_secret,用于引导买家完成支付流程。

前置准备

  1. 安装Stripe PHP SDK:
composer require stripe/stripe-php
  1. 准备平台的Stripe API密钥(Secret Key和Publishable Key),可在Stripe后台获取

模式一:手动控制资金暂留(推荐灵活场景)

后端创建PaymentIntent代码

<?php
require 'vendor/autoload.php';

// 初始化Stripe客户端(使用平台Secret Key)
\Stripe\Stripe::setApiKey('sk_test_你的平台Secret密钥');

// 从数据库读取目标卖家的Express账户ID
$seller_account_id = 'acct_xxxxxxxxx'; // 替换为实际存储的卖家account_id

try {
    $payment_intent = \Stripe\PaymentIntent::create([
        'amount' => 1000, // 支付金额,单位为分(示例:10美元)
        'currency' => 'usd',
        'payment_method_types' => ['card'],
        // 可选:平台收取的手续费(示例:1美元,即100分)
        'application_fee_amount' => 100,
        // 存储元数据,方便后续关联订单和卖家
        'metadata' => [
            'order_id' => 'ORD_20240501_001',
            'seller_stripe_account' => $seller_account_id
        ]
        // 注意:此处不设置transfer_data,资金将暂存于平台账户
    ]);

    // 返回client_secret给前端用于支付跳转
    echo json_encode([
        'client_secret' => $payment_intent->client_secret
    ]);
} catch (\Stripe\Exception\ApiErrorException $e) {
    http_response_code(500);
    echo json_encode(['error' => $e->getMessage()]);
}
?>

前端引导买家跳转支付

使用Stripe.js跳转至Stripe托管的支付页面:

<script src="https://js.stripe.com/v3/"></script>
<script>
// 初始化Stripe(使用平台Publishable Key)
const stripe = Stripe('pk_test_你的平台Publishable密钥');

// 请求后端获取client_secret并跳转支付
fetch('/path/to/your/create-payment-intent.php')
  .then(res => res.json())
  .then(data => {
    stripe.redirectToCheckout({
      clientSecret: data.client_secret,
      successUrl: 'https://你的域名.com/payment-success', // 支付成功跳转页
      cancelUrl: 'https://你的域名.com/payment-cancel' // 支付取消跳转页
    }).then(result => {
      // 处理跳转失败的情况
      if (result.error) alert(result.error.message);
    });
  });
</script>

后续手动转账给卖家

当需要划转资金时(比如暂留7天后),通过监听payment_intent.succeeded webhook事件触发转账,示例代码:

<?php
require 'vendor/autoload.php';

\Stripe\Stripe::setApiKey('sk_test_你的平台Secret密钥');

// 从webhook事件或数据库获取PaymentIntent ID和卖家账户ID
$payment_intent_id = 'pi_xxxxxxxxx';
$seller_account_id = 'acct_xxxxxxxxx';

try {
    // 获取PaymentIntent关联的Charge记录
    $payment_intent = \Stripe\PaymentIntent::retrieve($payment_intent_id);
    $charge_id = $payment_intent->charges->data[0]->id;

    // 创建转账
    $transfer = \Stripe\Transfer::create([
        'amount' => 900, // 扣除手续费后的金额(示例:9美元)
        'currency' => 'usd',
        'destination' => $seller_account_id,
        'source_transaction' => $charge_id, // 指定从该笔Charge转账
        'transfer_group' => $payment_intent_id // 关联原支付,方便对账
    ]);

    echo json_encode(['transfer_success' => true, 'transfer_id' => $transfer->id]);
} catch (\Stripe\Exception\ApiErrorException $e) {
    http_response_code(500);
    echo json_encode(['error' => $e->getMessage()]);
}
?>

模式二:自动延迟转账(适合固定暂留周期场景)

创建PaymentIntent时直接设置延迟天数,Stripe会自动完成后续划转:

<?php
require 'vendor/autoload.php';

\Stripe\Stripe::setApiKey('sk_test_你的平台Secret密钥');

$seller_account_id = 'acct_xxxxxxxxx';

try {
    $payment_intent = \Stripe\PaymentIntent::create([
        'amount' => 1000,
        'currency' => 'usd',
        'payment_method_types' => ['card'],
        'application_fee_amount' => 100,
        'transfer_data' => [
            'destination' => $seller_account_id,
            'delay_days' => 7 // 暂留7天后自动转账给卖家
        ],
        'metadata' => [
            'order_id' => 'ORD_20240501_001'
        ]
    ]);

    echo json_encode([
        'client_secret' => $payment_intent->client_secret
    ]);
} catch (\Stripe\Exception\ApiErrorException $e) {
    http_response_code(500);
    echo json_encode(['error' => $e->getMessage()]);
}
?>

前端跳转支付代码与模式一完全一致。


关键注意事项

  • 所有API调用必须使用平台的Stripe密钥,禁止使用卖家的Express账户密钥
  • 金额单位为分,需注意业务金额与分单位的转换
  • 务必配置Stripe Webhook监听支付状态事件(如payment_intent.succeeded),用于后续业务逻辑处理(如更新订单状态、触发转账)

内容的提问来源于stack exchange,提问作者szatti1489

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 16:23:23