Fatal error: Class 'Paypal\\Api\\Payer' not found en sdk Paypal

≯℡__Kan透↙ 提交于 2019-11-29 15:00:44

Instead of:
use Paypal\Api\Payer;
use Paypal\Api\Item;

Change it to:
use PayPal\Api\Payer;
use PayPal\Api\Item;

Just capitalize the second "P" on the word "PayPal". Hope it works :)

I run into this problem too.

My solution was to copy inner lib/PayPal folder

from composer installation or from direct downloaded files package to some directory in src, for example to src/Components.

When add needed PayPal folders to composer :

"psr-4" : {

    "PayPal\\" : "src/Components/PayPal/",
    "PayPal\\Api\\" : "src/Components/PayPal/Api/", 
    "PayPal\\Rest\\" : "src/Components/PayPal/Rest/", 
    "PayPal\\Auth\\" : "src/Components/PayPal/Auth/",
    "PayPal\\Exception\\" : "src/Components/PayPal/Exception/"
}

run from command line composer dump-autoload.

When you can use the classes everywhere in your project.

use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Api\Payer;
use PayPal\Api\Amount;
use PayPal\Api\Transaction;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Payment;
use PayPal\Exception\PayPalConnectionException;



if ( isset( $_POST['ppalBtn'] ) ) {

    $apiContext = new ApiContext(
            new OAuthTokenCredential(
    'ClientID',    
    'ClientSecret'      
            )
    );



    $payer = new Payer();
    $payer->setPaymentMethod('paypal');

    $amount = new Amount();
    $amount->setTotal('1.00');
    $amount->setCurrency('USD');

    $transaction = new Transaction();
    $transaction->setAmount($amount);

    $redirectUrls = new RedirectUrls();
    $redirectUrls->setReturnUrl("https://domain/redirect.php")
        ->setCancelUrl("https://dcancel.php");

    $payment = new Payment();
    $payment->setIntent('sale')
        ->setPayer($payer)
        ->setTransactions(array($transaction))
        ->setRedirectUrls($redirectUrls);

    try {
        $payment->create($apiContext);
        echo $payment;

        echo "\n\nRedirect user to approval_url: " . $payment->getApprovalLink() . "\n";
    }
    catch (\PayPal\Exception\PayPalConnectionException $ex) {
        // This will print the detailed information on the exception.
        //REALLY HELPFUL FOR DEBUGGING
        echo $ex->getData();
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!