How to Integrate PayPal CodeIgniter Library

Learn to integrate PayPal’s Classic APIs into any CodeIgniter application using this PayPal CodeIgniter class library.

Setup the PayPal CodeIgniter Library
  • First, Download the PHP PayPal CodeIgniter class library for PayPal and upload the contents of the /application directory to the /application directory in your CodeIgniter install.
  • Second, open /application/config/payal-sample.php and adjust the values with your own Sandbox and Production API credentials. Then save-as paypal.php.
    • In addition, detailed comments are inside the config file to help you fill it out correctly.
How to Make PayPal API Calls
  • Open /application/controllers/paypal/templates/Controller_name.php depending on which PayPal API you’re going to be using.
    • Example: If we want to make a call to the RefundTransaction API we open Payments_pro.php and then seek out the Refund_transaction() function.
    • You can build directly into the template functions if you want to, however, I recommend you setup your own controller and copy the functions there. This keeps the templates clean for future reference.
      • Just remember to load the PayPal config file and library in your constructor the same way the samples and templates do.
  • Each template controller file includes functions with PHP arrays for every parameter available to that particular API. Simply fill in the array parameters with your own dynamic (or static) data. This data may come from…
    • Session Variables
    • PHP Variables
    • Database Recordsets
    • Static Values
    • Etc.
  • When you run the function you will get a $PayPalResult array that consists of all the response parameters from PayPal, original request parameters sent to PayPal, and raw request/response info for troubleshooting.
    • You may refer to the PayPal API Reference Guide for details about what response parameters you can expect to get back from any successful API request.
      • Example: When working with RefundTransaction, I can see that PayPal will return a REFUNDTRANSACTIONID, FEEREFUNDAMT, etc.
      • As such, I know that those values are available in $PayPalResult[‘REFUNDTRANSACTIONID’] and $PayPalResult[‘FEEREFUNDAMT’] respectively.
  • If errors occur they will be available in $PayPalResult[‘ERRORS’].
Samples
  • After installing the PayPal library into CodeIgniter, the demo can be loaded at {base_url}/paypal/samples/demo/.
    • This allows you to run the fully functional samples that are included with the library.
    • The samples that the demo loads can be found at /application/controllers/paypal/samples/.
  • The Payments Pro sample controller has two methods ready for demo purposes after you update your config file.
    • Do_direct_payment()
    • Get_balance()
  • The Adaptive Payments controller has one sample ready for demo.
    • Convert_currency()
  • The PayFlow controller has one sample ready for demo.
    • Process_transaction()

NOTE: Check out our fully functional CodeIgniter PayPal Integration demo kits!

 

Browse our CodeIgniter PayPal Integration Demo Kits

General PayPal CodeIgniter Tips
  • The result of the API call will come back in $PayPalResult[‘ACK’].
    • Remember when checking the value that it could be Success, Failure, SuccessWithWarning, or FailureWithWarning. Simply checking for Success isn’t enough.
    • You may use $PayPal->APICallSuccessful($Ack) to do this for you.
Tips for Testing in the PayPal Sandbox
  • Firstly, create an account at http://developer.paypal.com
  • Secondly, create at least one sandbox Seller account and one sandbox Buyer account from within your developer account.
    • Use these accounts to go through the entire process of buying items within your application.
      • You can review what each party involved will see in their PayPal account when transactions are complete.
    • Learn how to create PayPal sandbox accounts.
Tips for Going Live

Download the FREE PayPal Express Checkout Demo Kit

Our CodeIgniter PayPal Library comes with basic API call samples for free.  However, this kit provides a fully functional shopping experience with the Express Checkout calls integrated directly into it.