GDY
GDY

Reputation: 2951

Class 'PayPal\Api\Itemlist' not found

I get this PHP error on the production site but not localhost:

Fatal error: Uncaught Error: Class 'PayPal\Api\Itemlist' not found in … Stack trace: …

Here is the function in which the error occurs:

function paypal_submit( $orderID, $cart ) {
    // Get settings
    global $settings;

    // Init SDK
    $paypal = paypal_init();

    // Create payer
    $payer = new \PayPal\Api\Payer();
    $payer->setPaymentMethod('paypal');

    // Iterate through items
    $items = array();
    $total = 0;

    foreach( $cart as $imageID => $imagePrice ):

        $total = $total + $imagePrice;

        $item = new \PayPal\Api\Item();
        $item->setName( 'Bild #' . $imageID )
             ->setCurrency( 'EUR' )
             ->setQuantity( 1 )
             ->setPrice( $imagePrice );

        $items[] = $item;

    endforeach;

    $itemList = new \PayPal\Api\Itemlist();
    $itemList->setItems( $items );

    $amount = new \PayPal\Api\Amount();
    $amount->setCurrency( 'EUR' )
           ->setTotal( $total );

    $transaction = new \PayPal\Api\Transaction();
    $transaction->setAmount( $amount )
                ->setItemList( $itemList )
                ->setDescription( 'Bestellung #' . $orderID )
                ->setInvoiceNumber( 'RE' . $orderID )
                ->setCustom( $orderID );


    $redirectURLs = new \PayPal\Api\RedirectUrls();
    $redirectURLs->setReturnUrl( add_query_arg( 'success', 1, permalink( 'checkout-paypal' ) ) )
                 ->setCancelUrl( add_query_arg( 'success', 0, permalink( 'checkout-paypal' ) ) );



    $payment = new \PayPal\Api\Payment();
    $payment->setIntent( 'sale' )
            ->setPayer( $payer )
            ->setRedirectUrls( $redirectURLs )
            ->setTransactions( [ $transaction ] );

    try {

        $payment->create( $paypal );

    } catch( PayPal\Exception\PayPalConnectionException $ex ) {

        echo $ex->getCode(); // Prints the Error Code
        echo $ex->getData(); // Prints the detailed error message 

        die( $ex );

    } catch( Exception $ex ) {
        die( $ex );
    }

    $approvalUrl = $payment->getApprovalLink();
    header( 'Location: ' . $approvalUrl ); exit;
}

Here is the init function (just for the sake of completeness):

function paypal_init() {
    // Get settings
    global $setting;

    // Load PayPal SDK
    require_once( ABSPATH . 'system/classes/paypal/autoload.php' );

    // Register app
    $paypal = new \PayPal\Rest\ApiContext(
        new \PayPal\Auth\OAuthTokenCredential(
            $setting['paypal_clientid'],
            $setting['paypal_clientsecret']
        )
    );

    // Return app
    return $paypal;
}

All other classes do work.

Strangely the exact same integration works on localhost fine but not the production environment … PHP version is the same. Only difference as far as I can see is that the production site runs over https.

Any ideas what the problem can be? I guess the autoloader maybe doesn't include this class for any reason?

Upvotes: 0

Views: 397

Answers (1)

IncredibleHat
IncredibleHat

Reputation: 4104

The autoloader looks for a file named by the class name you are using. Since you are using Itemlist, it looks for a file of Itemlist.php.

Changing it to \PayPal\Api\ItemList will allow the autoloader to find the right file (on case sensitive systems). Because the actual file in the SDK is ItemList.php.

Upvotes: 1

Related Questions