CodeIgniter Payments – A New Library for CodeIgniter Reactor


Since writing this blog post, I’ve released a new library which replaces this one, using the CodeIgniter Sparks system. You can find it here:

I am currently supporting 6 payment gateways, and should have several more added very soon. Thanks for reading!

NOTE: You can skip reading the post and get right to the github repo if you like by clicking here.

I’ve been working on a simple payments library for a CodeIgniter project.  I want to preface everything else here by saying that I’ve only been actually developing for a year (meaning all efforts before this I would call frivolous), and I’ve only been serious (meaning I write code more than I do anything else) for about three months.  So if you see a billion problems in this – please – take it easy on me, help me learn, and make a constructive suggestion.  If you decide you want to use this code, drop me an email and give me an ego boost = D.


The project I am working on that needed a payments system in the first place only requires that PayPal Payments pro is supported (which does direct pay and recurring billing), to handle recurring payments.

I could just use the standard SDK, but I have some other goals in mind:

  1. I want to be able to reuse this code on future projects
  2. I want to be able to support multiple payment systems with the same function calls and code base.  I want to configure my library when I reuse it on another project, I don’t want to have to rewrite any code.
  3. I want to use a single interface for generating payments – regardless of what payments system is being used.  This isn’t just useful for jumping from PayPal on this project to on the next…it’s useful because making the payment system a variable opens up the possibility to easily support providing multiple checkout mechanisms to my users.

As of writing, only PayPal is supported, and only a handful of PayPal’s API methods are included in the library, but the project has to start somewhere and I’m sure (with your help) it will grow over time.

So let’s jump in!

The Code

I decided that it was probably a good idea to start using some type of open source repository.  Frameworks (like CodeIgniter) all share this, and as such many people end up contributing to the codebase.   I hope that could happen for the things I write as well = ).  You can grab the source code here.

The code is structured as such (I’m assuming if you’re reading this you have some working level of knowledge about CodeIgniter):

  • Application
    • config
      • payments
        • paypal.php
        • More to be added
      • payments.php
    • libraries
      • payments
        • paypal.php
        • More to be added
      • payments.php

So, as you can see, there is a main config for payments, and then a subfolder with a file for each module.  Each gets a separate config.

The idea here is that any call from your application should be made to payments.php.  You set the payment type (ie recurring, onetime, etc.) and module (ie paypal,, etc.) to a session during your checkout process, and then you retrieve this value when you actually make the payments call (from around line 32):

$payment_function = 'make_'.$this->check_payment_type().'_payment';
$payment = $this->$payment_function($this->check_payment_module(), $billing_data, $payment_function);

Notice that the payment function is being passed as a variable based on the payment type.  That brings up another point, that for each library (paypal,, etc.) there should be some functions which retain identical naming conventions.  In this case, any time payments.php calls a payments library to make a recurring payment, it will do it by make_recurring_payment.  This is the code from the libraries/payments/paypal.php:

public function make_recurring_payment($billing_data)
$billing_keys = array(

$function_settings = array(
‘DESC’ => $this->ci->config->item(‘paypal_api_service_description’),
‘METHOD’ => ‘CreateRecurringPaymentsProfile’

return $this->handle_query(array_merge($this->settings, $function_settings), array_combine($billing_keys, $billing_data), $this->endpoint);

Here, I’m assuming (this may change later) that the billing data required by each library will be fairly similar, but the params a given API needs may differ.  So billing data should be passed in as a keyless array, and then it gets merged with the proper keys as a parameter of handle_query (this function name will also probably be the same across multiple payment modules).

Handle query then calls the function that builds the request and parses a response from paypal.  All of this stuff is probably going to differ depending on the payment module, but the interface for making a recurring payment will not.  What I’m hoping for is that ANY call made to the payments library, regardless of payment system needed, or the params which should be passed, will flow something like this:
Your user is in the checkout process, and they decide they want to create a recurring payment with PayPal.  They click a “PayPal” button or check a radio button, whatever, and then you set it to the session:


Maybe a screen or two later (maybe the same screen), a user enters their payment information, and you pass in an array of billing data

$payment = $this->payments->make_payment($billing);
$response = $payment->response();

Something like that = )

Anyways, I hope I’m getting somewhere in explaining where I hope this payments library could go.  Imagine an application that allows users to (for example) define how other users pay them.  In such an instance, a modular payments system, with a universal interface, would be completely necessary.

Thoughts?  Comments?  Think I should trash everything and start over?  I’d love to know.

This entry was posted in Career, CodeIgniter, Programming. Bookmark the permalink. Both comments and trackbacks are currently closed.


  1. Posted April 23, 2011 at 12:56 pm | Permalink

    Thanks for sharing your code for us…:D . I am looking forward for other payment options. Anyway, do you have any plan to implement credit card support(VISA/Master)?

  2. admin
    Posted April 24, 2011 at 5:53 am | Permalink

    Hi Enam,

    Visa / Mastercard is already supported if you have a Website Payments Pro account with PayPal. I do plan on adding support for and some other providers in the very near future. If you watch the GitHub repo, you can wait on new releases….and feel free to ask me any questions about the library. Thanks!

  3. Kay
    Posted June 9, 2011 at 5:39 pm | Permalink

    I love the fact that you are sharing our code, I have having problem integrating paypal with codeIgniter. I tried some other libraries and not working proper.
    Do you have an example of how to use paypal express on a shopping cart, Full sample?


  4. Posted June 13, 2011 at 8:11 pm | Permalink

    Hello and thanks for the article.
    Let me tell you how I got here. I searched Google with “CodeIgniter PayPal Website Payments Pro” and found this and one more article. The other article was Angell Eye’s PayPal Payments Pro CodeIgniter library and I liked pretty much how he explained how to use it. You should see it. But! I like this article even more, because it gives hope to see in the nearest future a more general library not only for PayPal, but for other systems as well. Will you notify commenters somehow when you release first version?

  5. Posted July 1, 2011 at 4:21 am | Permalink

    Yup, that’ll do it. You have my apprecatiion.

  6. Posted August 17, 2011 at 1:08 pm | Permalink

    Great library.. Thanks for sharing your code..
    Actually until now,i always code from scratch base on SDK every time my project need paypal, LR , or other payment gateway support and i think it’s stupid action. :(
    It’s better to make it module like your jobs.. :D

  7. Calvin Froedge
    Posted August 17, 2011 at 10:43 pm | Permalink

    Hey, Thanks Tommy! As a follow up, this code has actually been replaced by a newer initiative, which now supports 6 gateways (and will support more soon). PayPal support has also been improved.

    Enjoy! Thanks for reading = )

  8. Calvin Froedge
    Posted August 17, 2011 at 11:07 pm | Permalink

    Hi Kay,

    Please check out the new library – CodeIgniter Payments:

    It supports 6 gateways. Support for all PayPal APIs is planned.

    - Calvin

  9. Posted September 19, 2011 at 9:00 pm | Permalink

    Very nice, one more thing though, it would be nice if you could incorporate First Data API as well.

    Thanks, for the code.

  10. Posted August 30, 2012 at 5:03 pm | Permalink

    I thought we were going to have to use Expression Engine for out client site but your code looks like it will work. This is great! Thanks so much.