Paypal is the most popular and convenient way to get paid. If you are selling some products in your website, you should definitely use Paypal payment gateway, why? because it’s free and there are over 350 million Paypal users all over the world who would happily purchase your products.
In this tutorial let us find-out how we can use Paypal Express Checkout in websites to sell some products instantly.
To test your PayPal store in a local server, you need to signup for PayPal Developer account and use their sandbox. See creating PayPal Sandbox account. To sell your products in real world, you need to signup for real Paypal account and obtain sellers information needed for the configuration file.
As you can see we have four PHP files in this tutorial index.php, config.php, process.php and paypal.class.php, but if you observe mainly two files index.php and process.php, you should be pretty clear how everything works.
config.php PHP file is used to store seller’s Paypal API information. Use your sandbox or live PayPal API details to replace variables in config.php file.
index.php is initial page where your buyers get to see your products, it doesn’t matter how you are planning to present your products, it could be complex ajax driven page, single product or just list of few products, you just need to list your products similar to example below.
Each product item contains a form, and each form contains hidden input variables item name, item number, item quantity and price. A buyer is able to see product details and select quantity before buying the product. When buyer decides to buy, the selected product data gets posted to process.php.
Please Note: Do not include price in hidden field as shown in example below, as the value can be easily replaced using any developer tool. In practical world, you should check existence of product and fetch actual price from database using product ID in process page, so that people can not manipulate the price.
The downloadbale sample files also include a PHP class called paypal.class.php to interact with PayPal. paypal.class.php is nothing but collection of functions, PPHttpPost() PHP function is used to carry out API operations in progress.php file, such as sending HTTP POST Request or getting response from the server. You can add your own additional functions (if needed) in this class file and execute like this $paypal->functionname().
The most important segment of our PayPal Express Checkout system is the process.php file. Here the buyer’s product data is received and processed further for the payment.
We will execute mainly three Paypal methods here SetExpressCheckout, DoExpressCheckout & GetExpressCheckoutDetails. But before we proceed further, we need to collect product details such as product id, its name and price for process page.
As explained earlier, you should only fetch product price from database rather than hidden input fields for security reasons, once we have the price, we can calculate and include tax details, shipping cost and discounts, which we need to set as parameters for SetExpressCheckout and DoExpressCheckout.
There are other various parameters you can pass to SetExpressCheckout and DoExpressCheckout to control the outcome. I advice you to read official PayPal documentation to know more about these parameters.
When buyer sends product details by clicking buy button, process.php needs to obtain PayPal token with SetExpressCheckOut method using Seller’s PayPal API credentials.
Once we successfully receive PayPal token, we need to set some session variable (itemprice, totalamount, itemName, itemNo, itemQTY) for later use. And then we redirect buyer to PayPal order summary page, where buyer pays in secure PayPal environment, but remember PayPal doesn’t transfer money yet.
After payment, buyer is redirected back to process.php page with Paypal token and PayerID values. We again need to send these values back to PayPal using DoExpressCheckoutPayment method, where PayPal verifies these values, only then the money is transferred to Seller’s account.
Obtains information about a specific order using SetExpressCheckOut token, on successful payment you might want to save buyer details such as address, payment Information etc. in database, please see Mysqli basic usage.
Each comment line in PHP code below explains how process.php deals with data received from buyer and PayPal.
I hope this tutorial will help you sell your products easily using PayPal. If you are facing issue of Paypal holding fund for 21 days, please read their policy here, and if you sell huge amount of products everyday, you might want to automate things by setting up PayPal IPN listener script. Good luck!
You might also be interested in reading Creating Simple Shopping Cart with PHP. and Sending Multiple Items to PayPal using Shopping cart.