Paypal SDK - Ajax/PHP Example

Has anyone got a good link to setting up Paypal using an ajax call, to php backend to handle payment and also db entries and return the result back to the page.

Without using composer!!
I have the paypal-php-sdk folder present.

I just seem to be going round in circles with this trying to google the information and paypal developer is pretty confusing and also doesn’t seem to explain this clearly.

Thanks

Can I ask you what’s wrong with the paypal-php-sdk library? I know it’s an archived library and PayPal recommends using the braintree library for payments, but IIRC the paypal-php-sdk library worked perfectly fine.

Because I do not understand the application process.

Every link or ‘developer’ examples shows a far too detailed and confusing application process, I currently use stripe which has a very clear process and developer support. Moving to paypal is seeming to be very problematic, every example is throwing 500 errors everywhere, whichever one I choose I hit a brick wall somewhere. Just after a clear tutorial to understand paypal without redirecting the user through complicated forms and using third parties, customised page coding to get the payment quick and simple for any user. php backend to process the payment and return a json response on status from paypal. (all db can be done at the same time)

try{
   \Stripe\Stripe::setApiKey('xxxxxxxxxxxxxx');
   $charge = \Stripe\Charge::create(array(
	"amount" => $amount,
	"currency" => "gbp",
	"source" => $tok, // obtained with Stripe.js
	"description" => $eml,
	"metadata" => $meta
  ));
} catch (\Stripe\Error\Card $e) {}
  $JSONarray = $charge->__toArray(true);	
  $txn = $charge['id'];
  $paid = $charge['amount'];
  if($charge['status'] == "succeeded"){
  }
}

Well there’s 2 things going on there.

  1. It could be because there’s bits missing in the code you copied from which produces 500 errors. Typically incorrect parameters, data, syntax, or all of the above.
  2. The endpoints no longer exist and you need to use v2.

I suspect it’s all of the above, but I don’t know which version you’re using and where you got it. The library itself has documentation on how to properly configure it. I do remember them attempting to upgrade the library to v2, but I think when they started doing that they thought it would be better for people to use braintree so they archived the PayPal library.

Have downloaded V2 but this line points nowhere?

/vendor/autoload.php

It should. If you downloaded it from Composer then Composer should have created a folder called vendor automatically. It’ll then store your libraries you download from Composer in that location. The /vendor/Autoload.php file is also automatically generated at the same time. The only thing you would need to do is include that file.

You said you downloaded v2, was it through Composer or was it through downloading the zipped file? If it’s a zipped file, none of that Composer stuff will be included. Double check the folders and let us know how you got those files.

It was the zipped file.
As stated before, I do not wish to use any composer element, or activate by any composer install

[11-Sep-2024 22:48:50 UTC] PHP Fatal error: Uncaught Error: Class “SandboxEnvironment” not found in D:\Web\arcadeoperator\account\ajax\testpaypal.php:8
Stack trace:
#0 {main}
thrown in D:\Web\arcadeoperator\account\ajax\testpaypal.php on line 8
[11-Sep-2024 22:49:27 UTC] PHP Fatal error: Uncaught Error: Class “PayPalCheckoutSdk\Core\PayPalHttpClient” not found in D:\Web\arcadeoperator\account\ajax\testpaypal.php:9
Stack trace:
#0 {main}
thrown in D:\Web\arcadeoperator\account\ajax\testpaypal.php on line 9
[11-Sep-2024 22:56:22 UTC] PHP Fatal error: Uncaught Error: Class “SandboxEnvironment” not found in D:\Web\arcadeoperator\account\ajax\testpaypal.php:8
Stack trace:
#0 {main}
thrown in D:\Web\arcadeoperator\account\ajax\testpaypal.php on line 8

So just going to be straightforward here. This is the reason why you’re seeing 500 internal server errors everywhere. It’s because you don’t have the starting point to initialize or “start up” the library. I don’t recall if they provide a bootstrap or autoloader file in their v2 library, but this is your main issue.

I don’t even need to dig too deep to know that the reason this is happening is because there’s no autoloader or bootstrap file that are calling these classes. The error message

Class ____ not found

Means just that. It’s because the class is not loaded into the base script aka where ever you are trying to call these scripts from.

My next question to you is, is there a requirement that disables you from using Composer?


On second thought, just took a look at the v1 and v2 libraries and they don’t provide an autoloader or boostrap file. You are required to use Composer for this even in their v1 version.

To be honest, I do not understand the composer.
I use my own server to test sites and also upload my sites to shared hosting that I cannot always do what I can on my own server.
My programming knowledge is pretty good, but I lose patience when it comes into something that I do not really understand and that something that potentially could take me hours to learn and understand.

I have got this far though with the vendor/autoload

Yeah. That’s perfectly fine. I encounter the same exact issue. When there’s something I don’t understand, I tend to read the same thing (or in this case same line) over and over and over until I can really understand what’s going on.

That’s great. Now all you need to do is include that autoload file and that’s it. Basically what Composer does is goes out to packagist.org and downloads any libraries out there that you supply it. That’s only if it exists on packagist.org. Don’t want to get too deep into the whole package hosting and what not. Essentially Composer is PHP’s package manager. If you’ve used Visual Studio with either C++ or C#, it’s equivalent to NuGet.

So when you supply Composer with the library or “package” you want to use, it downloads it and stores it in a folder called vendor. If you don’t have that folder, it’ll auto create it for you. Then it’ll auto create all the necessary autoload files. Essentially when you get that /vendor/autoload.php file, all you need to do is include it. No matter what class you want to use whether that’d be a mixture of libraries, you can instantly call those classes from your base script you are including the /vendor/autoload.php file in. The idea of “autoloading” is so that you don’t have to worry about hardcoding and including a file, then calling every single class you want from said file. You’re basically supposed to just only worry about the classes you need. That’s the intent of “autoloading”.

Still full of 500 errors.
Quitting this and going back to Stripe

[12-Sep-2024 21:15:52 UTC] PHP 1. {main}() D:\Web\arcadeoperator\account\ajax\testpaypal.php:0
[12-Sep-2024 21:15:53 UTC] PHP Warning: Undefined property: stdClass::$refresh_token in D:\Web\arcadeoperator\account\ajax\testpaypal.php on line 30
[12-Sep-2024 21:15:53 UTC] PHP Stack trace:
[12-Sep-2024 21:15:53 UTC] PHP 1. {main}() D:\Web\arcadeoperator\account\ajax\testpaypal.php:0
[12-Sep-2024 21:15:53 UTC] PHP Warning: Undefined property: stdClass::$access_token in D:\Web\arcadeoperator\account\ajax\testpaypal.php on line 42
[12-Sep-2024 21:15:53 UTC] PHP Stack trace:
[12-Sep-2024 21:15:53 UTC] PHP 1. {main}() D:\Web\arcadeoperator\account\ajax\testpaypal.php:0
[12-Sep-2024 21:23:12 UTC] PHP Warning: require(…/vendor/autoload.php): Failed to open stream: No such file or directory in D:\Web\arcadeoperator\account\paypal\src\bootstrap.php on line 6
[12-Sep-2024 21:23:12 UTC] PHP Stack trace:
[12-Sep-2024 21:23:12 UTC] PHP 1. {main}() D:\Web\arcadeoperator\account\paypal\src\request.php:0
[12-Sep-2024 21:23:12 UTC] PHP 2. require() D:\Web\arcadeoperator\account\paypal\src\request.php:9
[12-Sep-2024 21:23:12 UTC] PHP Fatal error: Uncaught Error: Failed opening required ‘…/vendor/autoload.php’ (include_path=‘.;C:\php\pear’) in D:\Web\arcadeoperator\account\paypal\src\bootstrap.php:6

Umm where are these variables coming from? Please post your code here so we can get a better understanding on what’s going on.

Just from the errors, it looks like you’re trying to reference properties on an object that doesn’t have those properties.

You will have the same issues with Stripe if you aren’t understanding what the code is doing.

I have a perfectly working environment with stripe on a different site.

One single ‘lib’ folder in root. Perfectly simple.
No other calls to anywhere else on the site, no other folders, vendor folders, just include the root lib on the pages I want.

That’s what I don’t get with this Paypal setup.
I’m lost to where what files are supposed to be where, nothing is explained anywhere. Composer is calling files from god knows where.

So I have a couple of things here. First, before we jump to any conclusion, let’s take a look at your code so we can figure out why it’s erroring out.

Second,

This is what “autoloading” does. Please read my post #15. Composer is just the package manager that downloads the libraries. What really happens is during “autoloading”. When the package is “autoloaded”, all the classes are registered along with their namespace to avoid conflicting with multiple classes of the same name.

All you need to worry about is actually calling the class and constructing the object. That’s all. I suspect you are copying from multiple sources which will cause issues because not everyone will be using the same code and not everyone will be providing complete or “working” code. Please provide what you currently have here so I can take a look at it. A while back, I was able to get v2 working on my side. I can compare what you have with what I had to see if you’re doing it right.

Checkout-PHP-SDK-develop.zip (268.7 KB)