Paypal SDK - Ajax/PHP Example

Please post only the base script that you’re having issues with. I will not download a zipped file off the internet for security reasons.

<?php namespace Sample; require __DIR__ . '/vendor/autoload.php'; // Creating an environment $clientId = "----------"; $clientSecret = "--------"; $pp_client_id = "----------------------"; $pp_secret = "--------------------"; $code = $_GET['code']; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://api.paypal.com/v1/oauth2/token'); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERPWD, $pp_client_id.':'.$pp_secret); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array( 'grant_type'=>'authorization_code', 'code'=> $code ))); $res_authcode = curl_exec($ch); if (empty($res_authcode)) { // print error } else { $json_authcode = json_decode($res_authcode); $refresh_token = $json_authcode->refresh_token; curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array( 'grant_type'=>'refresh_token', 'refresh_token'=> $refresh_token ))); $res_token = curl_exec($ch); if(empty($res_token)) { // print error } else { $json_token = json_decode($res_token); $access_token = $json_authcode->access_token; curl_setopt($ch, CURLOPT_URL, 'https://api.paypal.com/v1/oauth2/userinfo/?schema=paypalv1.1'); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array( 'access_token'=> $access_token ))); $res_userinfo = curl_exec($ch); if(empty($res_userinfo)) { // print error } else { $json_userinfo = json_decode($res_userinfo); print_r($json_userinfo); } } } curl_close($ch); ?>

I do appreciate your patience on this.
I have wasted hours on this now, but would be nice to understand what is going on

Yeah see, I was suspecting something like this is happening. You’re not using the actual classes you’re supposed to be using. You’re doing cURL requests which are basically web requests to Paypal’s endpoints however that’s not the proper way to do it if you’re going to be using the library.

I would expect something similar to this. Now take this with a grain of salt since not everyone’s setup will be the same nor have the same variables/constants/configurations.

<?php
require_once '/vendor/autoload.php';

use \PayPal\Core\PayPalHttpClient;
use \PayPal\Core\SandboxEnvironment;
use \PayPal\Core\ProductionEnvironment;

if(PAYPAL_MODE == 'sandbox') {
	$environment = new SandboxEnvironment(PAYPAL_CLIENT_ID, PAYPAL_SECRET, PAYPAL_MODE);
} elseif(PAYPAL_MODE == 'live') {
	$environment = new ProductionEnvironment(PAYPAL_CLIENT_ID, PAYPAL_SECRET, PAYPAL_MODE);
} else {
	throw new Exception('Unknown Paypal mode');
}

// Instantiate PayPal's entrypoint
$client = new PayPalHttpClient($environment);

... do some more Paypal stuff

To better understand what the code I just posted does

  • We basically include or “require” the autoload file
  • Then we declare use statements so that we can call those classes
    • Typically all you would need to do is call the class however like I mentioned earlier, libraries and packages can have namespaces in them to avoid conflicting with other libraries in case they have the same class names
  • Next we do a check on the constant called PAYPAL_MODE to determine what environment we’re in
    • Typically you would create the constant or variable a head of time and then assign it with a value
  • If the passed in PAYPAL_MODE doesn’t equal to “sandbox” or “live”, we throw an exception error
  • Next we instantiate the environment class depending on what environment we want to run in
    • We also pass in the client ID, client secret, and then the environment mode
  • Next we instantiate PayPal’s entrypoint class which is called PayPalHttpClient with our $environment variable
    • The $environment variable should have the appropriate information for it to create a connection with the PayPal API
1 Like

Right, okay.

I changed the src to vendor one above root in relation to where my page is, which is where the autoload is.
require_once ‘…/…/…/vendor/autoload.php’;

One error now using your script

[12-Sep-2024 22:57:03 UTC] PHP Fatal error: Uncaught Error: Undefined constant “PAYPAL_MODE” in D:\Web\arcadeoperator\account\ajax\testpaypal.php:12

This line:
if(PAYPAL_MODE == ‘sandbox’) {

Yup, this is explained in my latest comment.

Just an FYI, this isn’t the “complete” setup. This is just the starting point to get you started. The PayPal documentation that comes with the Paypal library when you downloaded it using Composer has the complete setup.

This is the bit where I get thrown off course.
I understand what you are saying now.

So where would this library be in relation to the root?
If root is “…/…/”
Vendor is “…/…/…/”

PayPal\Core\PayPalHttpClient

Well, it would be located at

D:\Web\arcadeoperator\account\ajax\vendor\autoload.php

That’s where you downloaded it to via Composer from the screenshot right?

This class

PayPal\Core\PayPalHttpClient

Would be located at

D:\Web\arcadeoperator\account\ajax\vendor\paypal\rest-api-sdk-php\lib\PayPal\Core\PayPalHttpClient.php

If that’s what you were looking for. You typically wouldn’t include these files directly in your script. You would just include the autoload.php file from Composer. Composer will take care of the rest so you don’t have 50 lines of include or “require” in your base script.

No.
I compared my online and offline folders to match.

There was already a vendor folder on my hosting, so I downloaded it to D:\Web
This cured all errors in locating ‘vendor’.

D:\Web\vendor

D:\Web\arcadeoperator

No other folders or files get created anywhere using your script

It’s wherever you put your vendor folder is all that matters. Just remember to update your script to reflect that change. Let me know if you need more help. Other than that, you’re now heading in the right direction. I don’t want to give you everything on a silver platter so I’ll let you do some more digging until you get more errors.

What I tend to do is I output the objects to see what they have so in this case, I would typically do

print_r($client);

Just to see what that object has.

The payloads and such coming from the SDK library have the same payloads on this API page so take a look and compare/use this page as a reference. Not everything is on that API page so you’ll need to do some more digging.

Also if you read the README.md file from this Github repo, it’s exactly what you’ll be looking for.

Thread continues here Paypal SDK - Ajax to PHP Setup