PayPal 异常 PayPalInvalidCredentialException:找不到默认用户的凭据

Posted

技术标签:

【中文标题】PayPal 异常 PayPalInvalidCredentialException:找不到默认用户的凭据【英文标题】:PayPal Exception PayPalInvalidCredentialException: Credential not found for default user 【发布时间】:2019-03-03 09:09:18 【问题描述】:

我已经实现了 PayPal(php、Laravel),付款时出现了一些问题。我在现场遇到以下异常。

PayPal\Exception\PayPalInvalidCredentialException: Credential not found for default user. Please make sure your configuration/APIContext has credential information in /var/www/project/project-files/vendor/paypal/rest-api-sdk-php/lib/PayPal/Core/PayPalCredentialManager.php

我已经检查了this 和其他类似问题,但没有帮助,我仍然遗漏了一些东西。一点帮助或指导会很有帮助。

场景是:一旦用户请求付款,管理员批准,付款将被处理。

为此创建了一个 Laravel 作业,以下作业的代码是:

namespace Test\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use PayPal;
use Test\Models\Transaction;

class ProcessPayout implements ShouldQueue


use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

/**
 * @var Transaction
 */
protected $transaction;

/**
 * Create a new job instance.
 *
 * @param Transaction $transaction
 */
public function __construct(Transaction $transaction) 
    $this->transaction = $transaction;


/**
 * Execute the job.
 *
 * @return void
 */
public function handle() 
    try 
        \Log::info($this->transaction); // I am getting all the data in this to be processed from Transaction Model

        $response = PayPal::makePayoutEnvironment()
            ->setPayoutEmailSubject($this->transaction->sender_batch_id)
            ->setPayoutItem($this->transaction->receiver, $this->transaction->amount, $this->transaction->sender_item_id)
            ->makePayout();

        if(empty($response))
            throw new \Exception();
        

    catch(\Exception $e)
        \Log::info($e); 
    

PayPal.php如下:

namespace Test\PayPal;

use PayPal\Api\Currency;
use PayPal\Api\Payout;
use PayPal\Api\PayoutItem;
use PayPal\Api\PayoutSenderBatchHeader;
use PayPal\Rest\ApiContext;
use ResultPrinter;

class PayPal


/**
 * @var ApiContext
 */
protected $context;

/**
 * @var Payout
 */
protected $payout;

/**
 * @var PayoutSenderBatchHeader
 */
protected $senderHeader;

/**
 * PayPal constructor.
 */
public function __construct()

    $this->context = app('PayPalContext');


/**
 * Create payout environment.
 */
public function makePayoutEnvironment()

    $this->payout = new Payout();
    $this->senderHeader = new PayoutSenderBatchHeader();
    return $this;


/**
 * Set payout sender email subject.
 *
 * @param      $senderId
 * @param null $subject
 *
 * @return $this
 */
public function setPayoutEmailSubject($senderId, $subject = null)

    $this->senderHeader->setSenderBatchId($senderId)
        ->setEmailSubject($subject ?: config('mail.subject_prefix') . 'Payout accepted.');
    $this->payout->setSenderBatchHeader($this->senderHeader);

    return $this;


/**
 * Set the payout item with receiver and amount of.
 *
 * @param $receiverEmail
 * @param $amount
 *
 * @param $itemId
 *
 * @return $this
 */
public function setPayoutItem($receiverEmail, $amount, $itemId)

    $item = (new PayoutItem())->setRecipientType('Email')
        ->setNote($this->senderHeader->getEmailSubject())
        ->setReceiver($receiverEmail)
        ->setSenderItemId($itemId)
        ->setAmount(new Currency(sprintf('
                    "value":"%s",
                    "currency":"%s"
                ', $amount, config('currency.fallback'))));

    $this->payout->addItem($item);

    return $this;


/**
 * Make payout for recipient.
 *
 * @return bool|\PayPal\Api\PayoutBatch
 */
public function makePayout()

    try 
        $output = $this->payout->create(['sync_mode' => false], $this->context);
     catch (\Exception $ex) 
        \Log::info($ex);
        return false;
    

    return $output;

PayPalServiceProvider 的代码如下:

namespace Test\Providers;
use Illuminate\Support\ServiceProvider;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Rest\ApiContext;
use Test\PayPal\PayPal;
class PayPalServiceProvider extends ServiceProvider

    public function boot() 

    
    public function register()
    
        $this->app->singleton('PayPalContext', function ($app) 

        $config = array('mode' => 'live');

        $apiContext = new ApiContext(
            new OAuthTokenCredential(
                config('services.paypal.client_id'),// ClientID
                config('services.paypal.client_secret')// ClientSecret
            )
        );

        $apiContext->setConfig($config);

        return $apiContext;
    );

    $this->app->singleton('PayPal', function ($app) 
        return new PayPal();
    );


/**
 * Get the services provided by the provider.
 *
 * @return array
 */
    public function provides()
        return [ 'PayPal' ];
    

经过一些调试,PaypalServiceProvider 在 PayPal.php 的构造函数中没有返回任何内容,不知道为什么。因此,当我在 Paypal.php 的 $this->context 中设置凭据和配置时,不会出现凭据错误,但现在我可以看到一个新错误。

访问https://api.paypal.com/v1/payments/payouts时得到Http响应码403

name : AUTHORIZATION_ERROR, debug_id : 6dd18f794dd9, message : Authorization erroroccurred

我关注了this 及其帐户验证问题,我检查了我的帐户是否已通过验证。请有其他解决此问题的建议。

【问题讨论】:

【参考方案1】:

你能试试这个吗?

PayPals PHP REST-API-SDK 附带了一些很棒的示例。看一下第84行/vendor/paypal/rest-api-sdk-php/sample/中的bootstrap.php。获取api上下文后,发生了一些配置。

$apiContext = new ApiContext(
    new OAuthTokenCredential(
        $clientId,
        $clientSecret
    )
);

// Comment this line out and uncomment the PP_CONFIG_PATH
// 'define' block if you want to use static file
// based configuration

$apiContext->setConfig(
    array(
        'mode' => 'sandbox',
        'log.LogEnabled' => true,
        'log.FileName' => '../PayPal.log',
        'log.LogLevel' => 'DEBUG', // PLEASE USE `INFO` LEVEL FOR LOGGING IN LIVE ENVIRONMENTS
        'cache.enabled' => true,
      implementing \PayPal\Log\PayPalLogFactory
    )
);

【讨论】:

以上是关于PayPal 异常 PayPalInvalidCredentialException:找不到默认用户的凭据的主要内容,如果未能解决你的问题,请参考以下文章

PayPal .NET SDK - new Payment() - 引发 NullReference 异常

Paypal 空指针异常 nvp.get("ACK") Java

Paypal SDK 支付 - 无法捕获异常并接收 http 400

Java Paypal 集成 SOAP JAX-WS - SSL 握手异常

PayPal IPN意外更改

访问paypal支付时http响应码500