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

使用Stripe PHP API从客户已有支付方式扣款的问题

从客户已有支付方式发起Stripe扣款的解决方案

你不需要从客户已有卡片生成新Token,直接利用客户关联的支付方式即可发起扣款,以下是两种可行方法:

方法1:适配你当前使用的Charge API

启用customer字段并可选指定支付方式ID,即可从客户已绑定的卡片扣款:

$chargeData = [
    "amount" => $amountTaxed,
    "currency" => $this->settings->currency_code,
    "customer" => $this->user->stripe_id, // 启用该字段,关联客户账户
    // 若客户有多个支付方式,可指定特定卡片的payment_method ID(替换为实际ID)
    // "payment_method" => "pm_xxxxxxxxxxxxxx",
    "description" => __('general.add_funds') . ' (Auto-recharge) @' . $this->user->username,
    'metadata' => [
        'user' => $this->user->stripe_id,
        'amount' => $amountTaxed,
        'taxes' => $this->settings->tax_on_wallet ? $this->user->taxesPayable() : null,
        'type' => 'deposit'
    ],
    "confirm" => true // 立即执行扣款
];
$charge = \Stripe\Charge::create($chargeData);

获取客户的支付方式ID

如果需要指定特定卡片,可先查询客户名下的所有支付方式:

// 获取客户绑定的所有卡片支付方式
$paymentMethods = \Stripe\PaymentMethod::all([
    'customer' => $this->user->stripe_id,
    'type' => 'card',
]);
// 取第一个支付方式的ID,可根据业务逻辑选择其他
$targetPaymentMethodId = $paymentMethods->data[0]->id;

方法2:使用Stripe推荐的PaymentIntent API

Charge属于旧版API,Stripe现在推荐用PaymentIntent处理扣款,支持更多支付场景:

$paymentIntent = \Stripe\PaymentIntent::create([
    'amount' => $amountTaxed,
    'currency' => $this->settings->currency_code,
    'customer' => $this->user->stripe_id,
    // 可选:指定特定支付方式ID
    // 'payment_method' => 'pm_xxxxxxxxxxxxxx',
    'description' => __('general.add_funds') . ' (Auto-recharge) @' . $this->user->username,
    'metadata' => [
        'user' => $this->user->stripe_id,
        'amount' => $amountTaxed,
        'taxes' => $this->settings->tax_on_wallet ? $this->user->taxesPayable() : null,
        'type' => 'deposit'
    ],
    'confirm' => true, // 立即确认扣款
    'automatic_payment_methods' => [
        'enabled' => true // 自动使用客户的默认支付方式
    ]
]);

核心说明

  • 无需生成新Token:客户已绑定的支付方式直接关联在其Stripe账户下,通过customer字段即可调用。
  • 默认支付方式:若不指定payment_method,Stripe会自动使用客户设置的默认支付方式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 03:02:31