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:
setPaymentMethod("paypal"); $item = new Item(); $item->setName($descripcion) ->setCurrency('MXN') ->setQuantity(1) ->setPrice($precio); $itemList = new ItemList(); $itemList->setItems([$item]);
start.php
setConfig([ 'mode'=>'sandbox', 'http.ConnectionTimeOut'=>30, 'log.LogEnabled'=>false, 'log.FileName'=>'', 'log.LogLevel'=>'FINE', 'validation.level'=>'log' ]);
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(); }