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

Stripe Charge创建收费报错,需替换为Payment Intent请求协助

问题解决:Stripe Charge转Payment Intent适配Payment Method

错误原因

你遇到的错误是因为Stripe已逐步淘汰旧版Charge API对新版Payment Method的支持。之前的成功是因为客户使用的是旧版的已保存支付详情(如Source/Card对象),而当前关联的是新版Payment Method对象,这类场景必须使用Payment Intents API处理。

代码修改方案

将原有的Charge创建逻辑替换为Payment Intent的创建与确认逻辑,示例代码如下:

// 构建Payment Intent参数
$paymentIntentParams = [
    'amount' => $totalCost * 100,
    'currency' => StripeHelper::CURRENCY,
    'description' => "recurring-" . $bookingid . "-C" . $weeks,
    'customer' => $source, // 保持原客户ID不变
    'transfer_group' => "$bookingid-$minderId",
    'confirm' => true, // 直接确认支付,适合后台自动扣费场景
    'automatic_payment_methods' => [
        'enabled' => true, // 自动使用客户关联的Payment Method
    ],
];

try {
    $paymentIntent = $this->stripe->paymentIntents->create($paymentIntentParams);
    // 检查支付状态,确保成功
    if ($paymentIntent->status === 'succeeded') {
        return $paymentIntent;
    } else {
        // 处理需要额外操作的情况(如3DS验证,后台扣费场景较少出现)
        Yii::error("Payment Intent not succeeded: " . $paymentIntent->status);
        return ['error' => 'Payment requires additional action', 'status' => $paymentIntent->status];
    }
} catch (\Stripe\Error\Api $ex) {
    Yii::error($ex->getMessage());
    return ['error' => $ex->getMessage(), 'code' => $ex->getCode()];
} catch (\Stripe\Error\InvalidRequest $ex) {
    Yii::error($ex->getMessage());
    return ['error' => $ex->getMessage(), 'code' => $ex->getCode()];
} catch (\Exception $ex) {
    Yii::error($ex->getMessage());
    return ['error' => $ex->getMessage(), 'code' => $ex->getCode()];
}

关键说明

  • confirm: true:后台自动扣费场景下,创建时直接确认支付,无需前端交互。
  • automatic_payment_methods.enabled: true:让Stripe自动选择客户已关联的Payment Method完成扣费,无需手动指定。
  • 状态检查:Payment Intent有多种状态,succeeded表示扣费成功,若出现requires_action则需要引导客户完成验证(但后台 recurring 扣费通常已通过预设配置自动处理)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 14:54:52