Everyone’s been really excited about PayPal REST API since its launch, so I decided to try and explore PayPal’s new RESTful web service. And after playing a bit with it, I was able to create my own simple online cell phone store successfully. Here’s how!
Before we continue, make sure you have signed up for PayPal Developer account for testing mode, and for production mode you will need real PayPal account. Obtain your Client Id and Client Secret from developer portal (for testing) OR from your real PayPal account (for production mode). PayPal REST SDK requires PHP version 5.3.0 +, so make sure your PHP is latest.
PayPal PHP SDK is included in downloadable file below, but you can get your own copy from github. If you are using composer, just point to downloaded file and give “composer update” command to have your SDK updated to latest version. Well, you can forget whole composer thing right now if it doesn’t ring a bell!
Back to tutorial, I have created four files for my Online Cell Phone store, but we only need to focus on first two files to get things done. Other two files are helper script and cancellation page.
- Index.php – (Products page) contains list of products for sale.
- Order_process.php – Processes PayPal payment.
- Functions.inc.php – Helper script, contains functions needed for processing.
- Payment_cancel.html – PayPal redirects user to this page in-case of cancellation.
In this page I have created list of items I want to sell, each item has HTML form, which contains its name, quantity, code and price ready to be posted to Order_process.php. I have tried using some CSS to make it look good, but hopefully you will do it better.
Next we look into order processes page. Here you might get bit confused, but the steps are fairly simple, let me make bit easier for you by pointing out the steps below.
- Step 1: Get product POST Values from products page.
- Step 2: Create a payment by constructing a payment object.
- Step 3: Get approval url from the response, and redirect the user to PayPal for approval page.
- Step 4: After Payment confirmation by user, execute payment on behalf of user using Payment ID andPayer ID.
- Step 5: Redirect user to return URL and display success message set earlier in PHP session.
That’s it! you should now have a functional online store of your own using PayPal REST API. There is now just one more step to follow. Before you make it live find sdk_config.ini config file, open it and change Service mode to live and don’t forget to enter real PayPal client ID and Secret in order_process.php.
PayPal REST API has lots of new features and supports a number of countries and currencies, please check whether your country is in the list. For countries not yet supported by the REST API, you can take a look at my other tutorial about PayPal.