Stripe API版本过时及PayPal沙箱认证失败问题求助
Stripe API版本过时问题修复
错误提示明确旧版API中line_items直接定义amount/name/currency的写法已废弃,需改用price_data结构重构价格信息。替换原代码的line_items部分即可解决:
else if ($request->gateway == "stripe") { try { $stripe = new StripeClient($this->settings->stripe_secret_key); $data = $stripe->checkout->sessions->create([ 'success_url' => route('fund.verify', ['trans' => $ref]), 'cancel_url' => route('fund.verify', ['trans' => $ref]), 'payment_method_types' => ['card'], 'line_items' => [ [ 'quantity' => 1, 'price_data' => [ 'currency' => $link->getCurrency->real->currency, 'unit_amount' => number_format($amount + $charge, 2, '.', '') * 100, 'product_data' => [ 'name' => $ref, ], ], ], ], 'mode' => 'payment', ]); $sav->update([ 'charge_id' => $data->id, ]); } catch (\Stripe\Exception\CardException $e) { return back()->with('alert', $e->getMessage()); } catch (\Stripe\Exception\InvalidRequestException $e) { return back()->with('alert', $e->getMessage()); }
核心修改点:
- 将原
line_items下的name移至price_data.product_data中 - 原
amount改为price_data.unit_amount,保留乘以100的计价逻辑(Stripe以最小货币单位计费) - 原
currency移至price_data层级下
PayPal沙箱模式客户端认证失败解决
原代码固定调用生产环境API地址,且未区分沙箱/生产密钥,导致认证失败。修改方案如下:
else if ($request->gateway == "paypal") { // 根据沙箱模式切换API地址 $apiUrl = $this->settings->paypal_sandbox_mode ? "https://api-m.sandbox.paypal.com/v2/checkout/orders" : "https://api-m.paypal.com/v2/checkout/orders"; // 确保使用对应环境的Client ID和Secret Key $authToken = base64_encode($this->settings->paypal_client_id . ':' . $this->settings->paypal_secret_key); $param = [ 'intent' => "CAPTURE", "purchase_units" => [ [ 'amount' => [ "currency_code" => $link->getCurrency->real->currency, "value" => number_format($amount + $charge, 2, '.', '') ], ] ], "application_context" => [ 'return_url' => route('fund.verify', ['trans' => $ref]), 'cancel_url' => route('fund.failed', ['trans' => $ref]) ] ]; $curl = new Curl(); $curl->setHeader('Authorization', 'Basic ' . $authToken); $curl->setHeader('Content-Type', 'application/json'); $curl->post($apiUrl, $param); $curl->close(); $data = $curl->response; }
关键操作:
- 登录PayPal开发者平台,创建沙箱应用,获取沙箱专属的Client ID和Secret Key(不可与生产环境混用)
- 在脚本设置中添加沙箱模式开关(如
paypal_sandbox_mode字段),开启时自动切换到沙箱API地址并使用沙箱密钥
内容的提问来源于stack exchange,提问作者David Sundberg
相关产品推荐
相关产品推荐

