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

基于Stripe网关实现APP REST API向用户银行账户打款的技术问询

实现Stripe向用户银行账户打款的REST API(PHP)

首先,确保你已经通过Composer安装了Stripe PHP SDK,并且完成了用户银行账户信息的收集(比如用Stripe Elements收集银行账户,或用户已关联Stripe Connect账户)。下面是完整的代码实现和关键说明:

完整代码示例

<?php
require_once('vendor/autoload.php');

// 设置Stripe API密钥(生产环境务必替换为sk_live_开头的正式密钥)
\Stripe\Stripe::setApiKey("sk_test_tD8Pxvmv2sOhucPuhqvPDvSP");

$error = '';
$success = '';

// 处理POST请求(前端需传递打款金额、目标账户ID等参数)
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    try {
        // 1. 验证请求参数合法性
        $amount = isset($_POST['amount']) ? (int)$_POST['amount'] : 0;
        $currency = isset($_POST['currency']) ? $_POST['currency'] : 'usd';
        $destination = isset($_POST['destination']) ? $_POST['destination'] : '';

        if ($amount <= 0 || empty($destination)) {
            throw new Exception('无效的打款金额或目标账户ID');
        }

        // 2. 创建打款请求
        // 两种场景可选:
        // 场景A:向平台自身关联的银行账户打款(无需destination参数)
        // $payout = \Stripe\Payout::create([
        //     'amount' => $amount,
        //     'currency' => $currency,
        // ]);

        // 场景B:向用户的外部银行账户/借记卡打款(需传入Stripe返回的账户ID,格式如ba_xxx)
        $payout = \Stripe\Payout::create([
            'amount' => $amount,
            'currency' => $currency,
            'destination' => $destination,
            'description' => '用户账户打款',
        ]);

        // 3. 成功逻辑:记录打款信息到数据库,返回响应给移动端
        $success = "打款已发起,ID: {$payout->id},当前状态: {$payout->status}";
        
        header('Content-Type: application/json');
        echo json_encode([
            'status' => 'success',
            'message' => $success,
            'payout_id' => $payout->id
        ]);
        exit;

    } catch (\Stripe\Exception\ApiErrorException $e) {
        // 处理Stripe API层面的错误(比如余额不足、账户异常)
        $error = "Stripe API错误: {$e->getMessage()}";
        header('Content-Type: application/json');
        echo json_encode([
            'status' => 'error',
            'message' => $error
        ]);
        exit;
    } catch (Exception $e) {
        // 处理参数验证等业务逻辑错误
        $error = "请求错误: {$e->getMessage()}";
        header('Content-Type: application/json');
        echo json_encode([
            'status' => 'error',
            'message' => $error
        ]);
        exit;
    }
}

// GET请求返回API使用说明
header('Content-Type: application/json');
echo json_encode([
    'message' => '请通过POST请求发起打款,必填参数:amount(单位:最小货币单位,如1美元传100)、destination(Stripe银行账户ID),可选参数:currency(默认usd)'
]);
?>

关键注意事项

  • 金额单位规则:Stripe API要求金额以最小货币单位传入,比如1美元对应100(分),1欧元对应100(欧分),千万别直接传整数金额。
  • 目标账户ID获取:destination参数必须是Stripe返回的银行账户ID(格式如ba_xxx),你需要先通过Stripe Elements或API收集用户银行账户信息并创建外部账户,才能拿到这个ID。
  • 生产环境配置:正式上线时一定要切换为live模式的API密钥,同时确保你的Stripe账户已开启打款权限。
  • 状态监听建议:强烈配置Stripe Webhook监听payout.paid、payout.failed等事件,这样能实时更新系统内的打款状态,还能及时通知用户打款结果。

Webhook状态监听示例(可选)

<?php
// 接收Stripe Webhook事件
$payload = @file_get_contents('php://input');
$sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
$endpoint_secret = 'whsec_xxx'; // 你的Webhook密钥

try {
    $event = \Stripe\Webhook::constructEvent(
        $payload, $sig_header, $endpoint_secret
    );

    // 根据事件类型处理
    if ($event->type === 'payout.paid') {
        $payout = $event->data->object;
        // 业务逻辑:更新数据库中该打款记录为「已成功」
    } elseif ($event->type === 'payout.failed') {
        $payout = $event->data->object;
        // 业务逻辑:记录失败原因,给用户推送通知
    }

    http_response_code(200);
} catch (\UnexpectedValueException $e) {
    // 无效的请求体
    http_response_code(400);
    exit();
} catch (\Stripe\Exception\SignatureVerificationException $e) {
    // 无效的签名
    http_response_code(400);
    exit();
}
?>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 04:25:46