Omnipay Stripe支付验证错误排查求助(附示例代码)
问题解决方法
根据错误提示(The payment_method parameter should be passed at the root level.),问题出在支付方法参数的传递方式和命名不符合Stripe API要求,以下是具体修复方案:
方案1:使用setPaymentMethod()方法传递参数
修改代码,将支付方法ID通过单独的方法设置,而非直接放在authorize的参数数组中:
$paymentMethod = $_POST['paymentMethodId']; $response = $gateway->authorize([ 'amount' => '10.00', 'currency' => 'USD', 'description' => 'This is a test purchase transaction.', 'returnUrl' => $completePaymentUrl, 'confirm' => true, ])->setPaymentMethod($paymentMethod)->send();
方案2:调整参数键名为下划线格式
如果你的Omnipay-Stripe版本支持直接在参数数组中传递,将驼峰命名的paymentMethod改为下划线格式的payment_method:
$paymentMethod = $_POST['paymentMethodId']; $response = $gateway->authorize([ 'amount' => '10.00', 'currency' => 'USD', 'description' => 'This is a test purchase transaction.', 'payment_method' => $paymentMethod, 'returnUrl' => $completePaymentUrl, 'confirm' => true, ])->send();
额外检查项
- 确保你使用的
omnipay/stripe库是最新稳定版,版本差异可能导致参数结构不同 - 验证
$_POST['paymentMethodId']获取到的是合法的Stripe支付方法ID(格式通常为pm_xxxxxx)
内容的提问来源于stack exchange,提问作者Dasun Tharanga
相关产品推荐
相关产品推荐

