Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

PayPal's REST APIs are a very easy way to add e-Commerce functionality to your web project. There is a PHP SDK available for it, and there is a very good API Samples site that covers just about anything you would want to do with the SDK. However something that I thought was missing from this documentation was how to create your PayPal App and Sandbox accounts as well as the autoload.php file that's references but never actually included in the package.

This post is in response to this missing bit of documentation. I won't attempt to cover anything more than the official documentation covers since it does a really good job in that respect already.

To get started you need to head over to the Sandbox Accounts page and create two accounts that you will use during development and testing. These are for the Sandbox PayPal site. When creating these accounts you can assign the amount of money they will contain, whether they are verified, etc. You need at least the business account, but it's best to create a business and a personal account straight away. Remember these are just for the sandbox and will not affect the live PayPal servers.
ppacc1.png




Once the accounts are created, you can go over to the My Apps page and start creating an App by clicking the Create App button.
ppapp1.png


Fill in the App name and select the Sandbox developer account for the app, then click Create App.
ppapp2.png


The next screen will display your App's credentials, for now you can scroll past those all the way to the bottom and select the features that your app will use. It's best to select a minimal set of features rather than have everything available. Not all apps will have all features unlocked, this depends on your country. The Return URL is not necessary because you specify this when making a call to PayPal anyway. Click Save when done.
ppapp3.png


You should take note of the Client ID and Secret from your App's information page, this is used in your bootstrap.php file later.
ppapp4.png


Now you are almost ready to use the SDK. I opted for the 'direct download' approach without using Composer. This meant that I needed to use an auto-loader. The API examples keep referencing the autoload.php script in the PayPal directory from the SDK, but this is nowhere to be found, so I've created the following as a quick fix.
 PayPal/autoload.php
<?php
function PayPal_PHP_SDK_autoload($className)
{
$classPath = explode('\\', $className);
if ($classPath[0] != 'PayPal') {
return;
}
$classPath = array_slice($classPath, 1, 2);
$filePath = dirname(__FILE__) . '/' . implode('/', $classPath) . '.php';
if (file_exists($filePath)) {
require_once($filePath);
}
}
spl_autoload_register('PayPal_PHP_SDK_autoload');
?>


To make use of that in your code, simply use the following snippet:
 Your Code
require __DIR__ . '/PayPal/autoload.php';


Now you are ready for the rest of the SDK. Good luck!

-i

A quick disclaimer...

Although I put in a great effort into researching all the topics I cover, mistakes can happen. Use of any information from my blog posts should be at own risk and I do not hold any liability towards any information misuse or damages caused by following any of my posts.

All content and opinions expressed on this Blog are my own and do not represent the opinions of my employer (Oracle). Use of any information contained in this blog post/article is subject to this disclaimer.
Hi! You can search my blog here ⤵
NOTE: (2022) This Blog is no longer maintained and I will not be answering any emails or comments.

I am now focusing on Atari Gamer.