3

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'
]);
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
Alexcode89
  • 31
  • 1
  • 4
  • Adding `require __DIR__ . '/vendor/autoload.php';` to the top of `checkout.php` will probably solve the issue. Please read http://stackoverflow.com/q/36577020/934739 and understand how `include()` and `require()` works, and also how [Composer Autoloading](https://getcomposer.org/doc/01-basic-usage.md#autoloading) works. – Gerard Roche Sep 08 '16 at 22:38
  • Didn't work :( i dont understand why? The vendor/autoload.php works fine, the error is only in checkout.php this php file dont read the namespaces. – Alexcode89 Sep 08 '16 at 23:00
  • `Paypal\Api\Payer` doesn't exist. Maybe the autoloader for it isn't defined or broken. You are using https://github.com/paypal/PayPal-PHP-SDK? Show your `composer.json` file, have you tried regenerating the composer autoloading? The [SDK](https://github.com/paypal/PayPal-PHP-SDK/blob/master/composer.json) seems to use PSR-0. Check that the PSR-0 for the package is defined by the generated composer autoloaders. Hint: `vendor/composer/*`. – Gerard Roche Sep 08 '16 at 23:11
  • I am using PayPal sdk from composer. My json is: {"requiere":{"paypal-sdk-php":"*"}} sorry for the "code" iam writting from the celphone – Alexcode89 Sep 09 '16 at 01:00

2 Answers2

6

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

1

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();
    }
olga
  • 959
  • 1
  • 15
  • 42