I'm having this problem where ther Paypal API is not loading on my server but it loads completely normal on my localhost. I really don't know what and where it went wrong. Everything is the same as I upload files from my localhost to the server, nothing change and it's exactly the same files.
When making a payment using Paypal REST API, I had this error on my server but not on my localhost.
Fatal error: Class 'PayPal\Api\item' not found in /var/www/test.my-domain.com/public_html/controllers/credits.php on line 24
My code as below:
require_once('./inc/lib/paypal/autoload.php');
$apiContext = new \PayPal\Rest\ApiContext(
new \PayPal\Auth\OAuthTokenCredential(
'hidden-clientid',// ClientID
'hidden-secret'// ClientSecret
)
);
if (isset($_POST['checkout'])) {
$payer = new \PayPal\Api\Payer();
$payer->setPaymentMethod('paypal');
$item = new \PayPal\Api\item();
$item->setName('Top Up')
->setDescription('My account.')
->setCurrency('USD')
->setQuantity(1)
->setTax(0)
->setPrice(10);
$itemList = new \PayPal\Api\itemList();
$itemList->setItems(array($item));
$details = new \PayPal\Api\details();
$details->setShipping("0")
->setTax("0")
->setSubtotal(10);
$amount = new \PayPal\Api\amount();
$amount->setCurrency("USD")
->setTotal(10)
->setDetails($details);
$transaction = new \PayPal\Api\transaction();
$transaction->setAmount($amount)
->setItemList($itemList)
->setDescription("Top up account")
->setInvoiceNumber(uniqid());
$redirectUrls = new \PayPal\Api\RedirectUrls();
$redirectUrls->setReturnUrl("http://".$_SERVER["HTTP_HOST"].$web_path."credits/done?status=success");
$redirectUrls->setCancelUrl("http://".$_SERVER["HTTP_HOST"].$web_path."credits/done?status=cancel");
$payment = new \PayPal\Api\Payment();
$payment->setIntent('sale');
$payment->setPayer($payer);
$payment->setRedirectUrls($redirectUrls);
$payment->setTransactions(array($transaction));
$response = $payment->create($apiContext);
$redirectUrl = $response->links[1]->href;
header( "Location: $redirectUrl" );
The weird part is that it goes through $payer->setPaymentMethod('paypal');
But it does not pass at $item = new \PayPal\Api\item();
All code are the same as my localhost and it works on my localhost. The Paypal API is also uploaded on my server on the exact location (/inc/lib/paypal/).
What does the autoload contain. Try and use the full path EX: /var/www/test.my-domain.com/public_html when using the autoload or
I've solved this problem by including the API files one by one.
I include these lines below:
require_once('./inc/lib/paypal/paypal/rest-api-sdk-php/lib/PayPal/Api/Item.php');
require_once('./inc/lib/paypal/paypal/rest-api-sdk-php/lib/PayPal/Api/ItemList.php');
require_once('./inc/lib/paypal/paypal/rest-api-sdk-php/lib/PayPal/Api/Details.php');
require_once('./inc/lib/paypal/paypal/rest-api-sdk-php/lib/PayPal/Api/Amount.php');
require_once('./inc/lib/paypal/paypal/rest-api-sdk-php/lib/PayPal/Api/Transaction.php');
require_once('./inc/lib/paypal/paypal/rest-api-sdk-php/lib/PayPal/Api/RedirectUrls.php');
require_once('./inc/lib/paypal/paypal/rest-api-sdk-php/lib/PayPal/Api/Payment.php');
require_once('./inc/lib/paypal/paypal/rest-api-sdk-php/lib/PayPal/Api/PaymentExecution.php');
and this fixed the problem. I think this is not really the best solution, but it works. I don't know why I still need to include these one by one where else the autoload.php should have included all these API files altogether.
I hope this helps for others who ran into the same problem like I do.