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

Laravel执行composer update后Netshell/PayPal包数组转字符串错误

解决Composer Update后Netshell/PayPal包的Array to string conversion错误

我之前碰到过一模一样的问题,本质是composer update后,paypal/rest-api-sdk-php被更新到了和Netshell/PayPal不兼容的版本——毕竟Netshell这个封装包已经很久没维护了,对官方SDK的新版本适配有问题。下面是几个可行的解决方案:

方案一:锁定官方SDK的兼容版本

  1. 打开项目根目录的composer.json,找到require部分,添加或修改官方SDK的版本约束:
    "paypal/rest-api-sdk-php": "~1.14.0"
    
    这个版本是经过验证和Netshell/PayPal兼容的稳定版本。
  2. 执行以下命令重新安装依赖,确保版本锁定:
    composer update paypal/rest-api-sdk-php --with-dependencies
    
  3. 清除缓存并重新测试支付流程:
    php artisan cache:clear
    php artisan config:clear
    

方案二:检查API Context的初始化

错误出现在OauthHandler里,也可能是你的_apiContext实例初始化有问题,更新后的官方SDK对参数格式要求更严格。确保你的API Context是这样创建的:

use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;

$apiContext = new ApiContext(
    new OAuthTokenCredential(
        config('services.paypal.client_id'),
        config('services.paypal.secret')
    )
);

// 不要传入数组,确保是正确的ApiContext实例
$response = $payment->create($apiContext);

避免把配置数组直接传给create方法,必须是封装好的ApiContext对象。

方案三:替换为官方SDK(长期解决方案)

因为Netshell/PayPal已经多年没有更新,后续还可能出现更多兼容问题,建议直接迁移到PayPal官方SDK。替换后的核心支付代码大概是这样:

use PayPal\Api\Payment;
use PayPal\Api\Payer;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Transaction;
use PayPal\Api\Amount;

// 初始化Payer
$payer = new Payer();
$payer->setPaymentMethod('paypal');

// 设置跳转URL
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl(route('paypal.success'))
             ->setCancelUrl(route('paypal.cancel'));

// 设置金额和交易
$amount = new Amount();
$amount->setCurrency('USD')
       ->setTotal('10.00');

$transaction = new Transaction();
$transaction->setAmount($amount)
            ->setDescription('Test Payment');

// 创建Payment对象
$payment = new Payment();
$payment->setIntent('sale')
        ->setPayer($payer)
        ->setRedirectUrls($redirectUrls)
        ->setTransactions([$transaction]);

// 执行创建请求
try {
    $payment->create($apiContext);
    // 获取跳转URL
    foreach($payment->getLinks() as $link) {
        if($link->getRel() == 'approval_url') {
            $redirectUrl = $link->getHref();
            break;
        }
    }
    return redirect()->away($redirectUrl);
} catch (\PayPal\Exception\PayPalConnectionException $ex) {
    Log::error($ex->getData());
    abort(500, 'Payment error');
}

这种方式更稳定,也能直接跟进PayPal官方的更新。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:56:49