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

时间:2018-09-27 12:36:26

标签: php laravel-5 paypal paypal-rest-sdk

我已经实现了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 个答案:

答案 0 :(得分:0)

请您尝试一下吗?

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
    )
);