Integrate PayPal with REST API using PHP

PayPal is the most convenient way to receive payments. PayPal has more than 350 million users. If you want to sale any product on your website, PayPal is the best way to get paid. Its easy and safe as Credit card/Bank account  information is not required to be submitted on Seller’s website. We are going to integrate PayPal in website using new REST API by PayPal.


Integrate PayPal REST API into Website

Recently I posted Simple PayPal Button integration which used Classic API by PayPal. But the new REST API is more secure with lots of new features.

Detailed information about PayPal REST API is available here. Let’s start with tutorial now. Please download complete files. We will be working with create.php, execute.php & example.php. Proper understanding of these files is necessary.


Bootstrap.php: Changing the client ID & Secret is optional in testing mode. But obtain your PayPal Client ID & Secret for REST API if you want to start selling in real world.

 Product Page (example.php):

example.php is product listing page where buyers will see your products. I have included sample products. It doesn’t matter your page is displaying single product or list of products. You can get product details from database too.

Each product item has it’s form, which contains hidden input fields itemname, itemprice, shipping, tax, currencycode & paypaldesc.Product quantity select-box allows user to select quantity. When user decides to buy the product and clicks buy button, then product data is posted to create.php.

Note: It is advised that you store product price, shipping, tax, currency code in database. And fetch pricing information in create.php from database. Please check sample code of fetching product price from database in create.php code.




Data received from example.php is processed, total payable amount is calculated using product price, shipping and tax information. We can define URLs where buyer will be redirected after payment cancellation/success. After that payment is created using ‘create’ method passing it a valid apiContext. The return object contains the state and the PayPal URL to which the buyer must be redirected to (for payment). Check out below code now.

Note: For payment on PayPal Sandbox website, please create a Personal sandbox account through or



User is redirected back to our website after authentication from PayPal. Now you can complete the payment that has been approved by buyer, by logging into PayPal website. If you are using database to store Payment ID from create.php, you can authenticate Payment ID stored in $_SESSIONS with Payment ID stored in database. After authentication payment is executed.

Now you can do the following:

  • Show payment confirmation page.
  • Allow buyer to download software/product.
  • Store Transaction information from $result array into your database.
  • Much more can be done as needed.

 Known issues:

1: PayPal SDK works fine on Localhost, but testing on live Web Server may occur some “Fatal Errors”. Please check if there is any PHP caching activated on Web Server. PHP caching like APC, Eaccelerator, OPCache can create problems.In case of any issue try disabling PHP cache plugins and restarting Web Server.

2: After extensive testing of payment system on PayPal Sandbox Website. If you are not able to pay(PayPal does not authenticate your login credentials) and shows Dispatch Error, Please check log file for more information. Please try clearing all sessions and delete all cookies for or try using a different browser.

PayPal Sandbox Error Message

Changes for Live Selling:

In sdk_config.ini change mode=sandbox to mode=live before integrating it in live website. Also don’t forget to change Client ID and Secret key in bootstrap.php which I mentioned earlier.


Hope you like it. Nothing is perfect. Suggestions and improvements are welcomed in comments.

21 Replies to “Integrate PayPal with REST API using PHP”

  1. João says:

    You’re a life saver!
    I’ve never found a documentation so messed up as the Paypal API Docs!
    They should just put a link to your tutorial there! 😀

    1. Gregg says:

      I agree, I thought it was just me. Could never figure out the how to even get through the paypal api docs. I use Stripe api, fun and easy to use.

    2. Bob says:

      Indeed, I usually don’t reply but this documentation is way better.


  2. sharif says:

    Excellent job.but i can’t download source files. can you please send me via mail ?

  3. Hermes says:

    When I was buying in example.php, I had this error:
    Exception: Received HTTP code 407 from proxy after CONNECT


    I had configured http.Proxy variable in sdk_config.ini

  4. Lemay says:

    The best documentation about paypal API Rest I ever seen. Thank you so much.

  5. Ace says:

    This tutorial explains the basic things needed to know in implementing the paypal API Rest. Thanks for sharing.

  6. Biswajit Bardhan says:

    I am running this application in Xampp Apache in localhost. After submitting the product form just like the example.php shown above, I am getting this following Fatal error.

    Fatal error: Class ‘ApiContext’ not found in D:\Program_Files\xampp\htdocs\paypalTest\bootstrap.php on line 9

    P.S. – I am require-ing the autoload.php file in the beginning of my application’s index page.

  7. Roma says:

    Woooow!!! Nice!!! This tutorial really best!!! Thanks!!!!

  8. Adrian says:

    In paypal classic api, we can put a hidden form element with name, custom, item_number,
    in which you can store value of who user bought this product, product id, with the whole form submitted to paypal, and with a payment confirm successfully, this value will accompany with the redirect url, return to original site. Such that using this piece of information to update the back-end database record, let say, product sales count, user profile for points earning for buying.

    How to do that in paypal REST api ?

    1. Umar Hassan says:

      Hi Adrian. I will try to write a tutorial about it in few days

  9. Zachary says:

    Your demo is not working now. Please fix

    1. Umar Hassan says:

      Thanks for pointing out the issue Zachary. I fixed it

  10. sean says:

    what would happen if the user loses their internet connection AFTER paypal takes the payment but BEFORE they are successfully redirected back to the website?

    1. Umar Hassan says:

      Payment will be processed and record inserted in your database also. So you can send user an email also, which includes payment details and order delivery links

  11. Prashant Salunkhe says:

    We try to run your live demo, It reply back this

    Fatal error: Uncaught exception ‘ReflectionException’ with message ‘Method PayPal\Api\Sale::getTransactionFee() does not exist’ in /home/umar/domains/ Stack trace: #0 /home/umar/domains/ ReflectionMethod->__construct(‘PayPal\Api\Sale’, ‘getTransactionF…’) #1 /home/umar/domains/ PayPal\Common\PPReflectionUtil::propertyAnnotations(‘PayPal\Api\Sale’, ‘transaction_fee’) #2 /home/umar/domains/ PayPal\Common\PPReflectionUtil::getPropert in /home/umar/domains/ on line 60

    Please suggest the way to overcome this issue.


  12. ramanaraju says:

    Please send src files.

  13. Turaab says:

    Sir when I tried it on localhost….it gives the following error…
    error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure
    I’m using XAMPP
    Suggest me the solution…Thanks

  14. Turaab Ali says:

    Sir i tried this code on localhost but no working… the following error

    Exception: error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure

    please suggest me the solution….Thanks

  15. Jon says:

    Thanks for this fantastic demo! I am running into an odd problem I wonder if you’ve seen. In the sandbox, if I pick the “Bank account (eCheck)” payment method, the transaction completes successfully and I see the dump of the $result variable. But if I choose a different payment method, the payment is not executed. Specifically, if I pay via my sandbox’s Paypal Balance, the execute.php rpogram bombs when $result = $payment->execute($execution, $apiContext); is called. If I choose PayPal Credit, it bombs when $payment = Payment::get($paymentId, $apiContext); runs.

  16. Sunil says:

    Looks like demo page is broken again, shows “Exception: SSL connect error NULL”

Leave a Reply

Your email address will not be published. Required fields are marked *

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

scroll to top