PayPal Express checkout in PHP MySQL – Part 2

PayPal JavaScript Express Checkout in PHP and MySql
Written by Yogesh Koli

In last tutorial I have given details on following points:

  1. Database Setup and Database Table Script
  2. PayPal Sandbox user setup
  3. PayPal Sandbox REST Application setup

If you have not check first part of this tutorial click to visit: PayPal JavaScript Express checkout Integration in PHP MySQL – Part 1

In this tutorial we are going to see the actual scripting part of the PayPal Express checkout integration:

Tutorial Features:

  • Login screen for the demo (we will have a predefined user ready to login and test the demo)
  • Product Listing with Add to Shopping Cart Button
  • Add Product to Shopping Cart
  • Remove Product From Shopping Cart
  • Shopping Cart page with good design
  • PayPal Express Checkout Button (this button will help us to get paid)
  • My Orders Page with order listing details such as Order Total and Ordered items.
  • Logout the user from the demo

You can checkout the live demo and Tutorial Overview Video:

Live Demo

Tutorial Overview Video

Step 4: Setup PHP Application:

Let’s to create brand new PHP application into your PHP development environment and use following screen to design folder structure for the application:

PayPal Express checkout in PHP MySQL Folder Structure

Folder Structure

Step 5: Database Connection Settings:

If you are done with the project structure, go a head and open app.php file from lib/config/app.php location, and add following script

Don’t forget to update database connection settings variables from the above file, the above file is going to help application to connect MySQL database using MySQLi.

Step 6: PayPal Application Configuration Settings:

Open lib/config/paypal.php file and add following script and update global variables to match with your PayPal application settings and don’t forget to update APP_URL to your application URL, it may be from your development environment or production environment but make sure it need to match with your own application url.

Quick note If you want to make this application live in feature make sure to change the above setting to live, meaning update application mode from sandbox to live and update client id, client secret and PayPal base url according to your live applications details.

Step 7: Application Library Class:

We have lib/PayPalDemo.php, file added into the project folder structure I am going to call it application library class the reason behind this is, this file is going to handle all backend operation such as user login, get user details, register new user, add product to shopping cart, remove product from shopping cart, checkout the PayPal Payment status, execute the payment and so on.

So in this step we are just going to make ready for upcoming function, go ahead and open PayPalDemo.php file and add following script:

We have just created an empty class we will add functions later on as we needed.

Step 8: Login Page Design, Handling Login Request:

We will design login page on index.php page, open index.php file and following login page design.

Login Page Design

Login Page Design

Handle Login action:

Open PayPalDemo.php file from lib folder and add following functions:

Functions overview:

  • isEmail() – check given email is exists.
  • isValidPassword() – check if the given password is valid
  • findPasswordByEmail() – get the password for given email address
  • getUserIDByEmail() – find out the user id for given email address

Let’s user this function on index.php page to handle login request, add following script at the top of the index.php page.


Our login page is ready to use, let’s move on to the next step to design our products page.

Step 9: Product Page:

Products page needs to have products listing along with the add to shopping cart button and little bit information about user.

Add Required functions for the product page into the PayPalDemo Class as showing below:

getAllProducts() – used to get all the products from the system:

getUserDetails() – used to get user details for the given user id.

Open products.php page and add this php script at the top of the page:

here we are handling session as well as Products request, lets move on to the design part.


Products Page Design Overview

Products Page Design Overview

Step 10: Design and Development of Shopping Cart:

This is core feature of the application, which is required for every e-commerce  application, it helps user to select and buy a particular product.

First thing let’s add required functions into PayPalDemo class:

get_product_details() – use to get the product details for given product id.

add_new_product() – use to add given product into shopping cart.

remove_product() – remove particular item from the shopping with the given index

_get_sum() – Calculate the total price of the selected items from the shopping cart.

Our required functions are ready to use, go ahead and open shopping-cart.php page and add following script:

The above script will handle add new product request from product page as well as remove product request from the same page and it will show shopping cart.

Shopping Cart Page Overview

Shopping Cart Page Overview

Step 11: Handling PayPal Express Checkout Action:

This is almost an important step, in this step we are going to write a script for PayPal Checkout button and script to execute PayPal payment.

If you notice in shopping-cart.php page we have included pay-with-paypal.php page, this is the page where we are going to have our PayPal checkout generated, let’s open pay-with-paypal.php page and add following script:


in above script we are doing few different things by using PayPal JavaScript Library, first thing is we are using PayPal object to render the required button and the secondly we are providing required settings, such as application environment or the buyer, PayPal application credentials, order total and currency and finally after authorising the merchant  we are executing the payment and redirecting back to the execute-payment.php page along with the few different parameters.

So let’s update our execute-payment.php page according to the following script:

We are going to required two functions here, let’s add them next into the PayPalDemo.php file:

getUserDetails() – use to get user details for the given user id.

paypal_check_payment() – use to checkout the payment status:

Again the above script will required few internal function to add success order details, order items and make shopping cart empty for approved orders:

Step 12: Design My Orders Page:

Add required functions into PayPalDemo class:

getOrders() – get all the order for given user.

getOrderItems() – get all the items for the given order

after adding the above function add following script into my-orders.php page to display all order for the given active user.

We will have following details design after everything goes successfully:

My Orders Page Overview

My Orders Page Overview

Step 13: Logout Page:

Finally here we goes to the last step for this tutorial that is logout user from the system:

We are done!

I know the tutorial is bit long you may have questions or get some errors let me know in the comments box below, I have tried my best to get you understandings of entire process for PayPal integrations into your application.

The advantage of using this JavaScript PayPal checkout is it’s secure and your don’t have to add code for managing or validating Payment information, its PayPal that does that job, and your customer/buyer will be aware about it  and it makes simple way to Pay on websites.

You can checkout the live demo of this tutorial for your help and understanding click following button to visit live demo:

PayPal Express Checkout Live Demo

Let me know your thoughts and comments. Happy coding!

I will see you in the next tutorial.

About the author

Yogesh Koli

Yogesh Koli is a software engineer & a Blogger lives in India. He's driven by an addiction to learning and a love for adventure. he has 5+ years of experience working with the front-end, back-end, web application development, and system design.

3 Comment threads
2 Thread replies
Most reacted comment
Hottest comment thread
4 Comment authors
jackAlexisYogesh Kolidharmesh kumar Recent comment authors

This site uses Akismet to reduce spam. Learn how your comment data is processed.

newest oldest most voted
Notify of

With this bit of code, how best would you suggest to implement a coupon code? Thanks


I did not change anything of your curl part but it seems that I get an empty response from paypal. Any idea ?

dharmesh kumar
dharmesh kumar

Thank you for sharing your new information…