问题
I installed PayPal SDK with composer but my php file CHECKOUT.php the error is:
Fatal error: Class 'Paypal\Api\Payer' not found in C:\xampp\htdocs\pagos\checkout.php on line 23
checkout.php:
<?php
require ("start.php");
use Paypal\Api\Payer;
use Paypal\Api\Item;
$payer = new Payer();
$payer->setPaymentMethod("paypal");
$item = new Item();
$item->setName($descripcion)
->setCurrency('MXN')
->setQuantity(1)
->setPrice($precio);
$itemList = new ItemList();
$itemList->setItems([$item]);
start.php
<?php
// 1. Autoload the SDK Package. This will include all the files and classes to your autoloader
// Used for composer based installation
require __DIR__ . '/vendor/autoload.php';
// Use below for direct download installation
// require __DIR__ . '/PayPal-PHP-SDK/autoload.php';
$apiContext = new \PayPal\Rest\ApiContext(
new \PayPal\Auth\OAuthTokenCredential(
'ashdjkhaskdjhalksdfjhaglskjdfhlasdfasdfsdfgh', // ClientID
'ejkhsdkjhakjdhakjshdkjashdkjashdkjaskjdhaskjh' // ClientSecret
)
);
$apiContext->setConfig([
'mode'=>'sandbox',
'http.ConnectionTimeOut'=>30,
'log.LogEnabled'=>false,
'log.FileName'=>'',
'log.LogLevel'=>'FINE',
'validation.level'=>'log'
]);
回答1:
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 :)
回答2:
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();
}
来源:https://stackoverflow.com/questions/39400764/fatal-error-class-paypal-api-payer-not-found-en-sdk-paypal