Badge 2

Open Source Projects Category

Using PHP to Encrypt Credit Card Data for Storage in a Database

Friday, December 17th, 2010

One of the issues with working in the software-as-a-service industry is that credit card numbers often have to be stored locally in a database. Keeping it on file with your payment gateway alone has a few limitations. The business folks may request storage for various reasons such as:

  • Recurring subscriptions (often with variable amounts).
  • Customers wanting to keep their card on file for future purchases.
  • Transactional based software providers (such as cloud providers).
  • Customer service wanting to verify the card on file with a customer.
  • Managers wanting to authorize certain charges for employees.

When storing information as confidential as credit card data, even though it’s behind multiple passwords and buried in a database, it should be encrypted. PHP’s mcrypt library is ideal for this, using a cipher like MCRYPT_RIJNDAEL_256 (AES 128-bit) to encode 32 bytes of data (ideal for a card number and expiration). If you’re anything like me, developing a home grown solution means:

  • Researching the various encryption ciphers.
  • Looking into legal requirements.
  • Re-reading PHP’s mcrypt function reference.
  • Write a class or extend one with methods to provide encryption and decryption, generating IVs and such.
  • Working with your project’s ORM or database libraries to implement the new class or methods for encryption.
  • Build a testing strategy to ensure everything works and continues to work with future development.

I’d prefer not to do this with every implementation, so I decided to write a class called CreditCardFreezer. CreditCardFreezer is a PHP class which automates the storage and retrieval of encrypted credit card information to and from a database.

Although there are plenty of classes and packages (such as those in the Zend framework) for validating credit card numbers or interfacing with payment gateways, I was hard pressed to find anything that deals with encryption and storage of the secure data. With CreditCardFreezer, it’s as simple as:

$obj = new CreditCardFreezer;
$obj->number = '1234-1234-1234-1234';
$encrypted = $obj->setPassKey('super secret')
                 ->get('number', true); // 'ViSxj3...'

// Store $encrypted into your database

$obj = new CreditCardFreezer;
$number = $obj->set('number', $encrypted, true)
              ->get('number');
echo $number; // 1234123412341234

CreditCardFreezer implements both a fluent interface for method chaining as well as an object-based one (shown above) which should be familiar for those who use an ORM like Doctrine. A complete documentation on the usage and syntax can be found here.

CreditCardFreezer is open source and PHPUnit tested. It’s released under the BSD license, so you’re free to use it as you like. More information can be found on my Github project page.

Some of the features it currently includes are:

  • AES 128-bit encryption through PHP’s MCRYPT_RIJNDAEL_256 cipher.
  • CFB mode for random IV generation upon every encryption to prevent patterns in the output.
  • Numerous methods for storing and accessing various attributes used with credit card transactions, including secure attributes like card numbers and unsecured attributes like names and addresses.
  • PDO class which can be used to implement ORM-like functionality to directly store and retrieve data from a database connection (and even create the schema).
  • PHPUnit tested to ensure it works now and after future development.

    More features to come such as direct Authorize.net and Paypal integration. Your feedback and any ideas or contributions are always welcome, just leave a comment or email me at me [at] andrewkandels.com.

  • Facebook
  • Twitter

Posted in Open Source Projects, PHP | Comments Off

PHP Library to Draw Election Results on a Map

Saturday, October 30th, 2010

I am releasing PHPStateMapper 1.0.5, which includes a new class called PHPStateMapper_Election for reporting election results (just in time, I know).

Preview

Loading data in is easy, either by chaining calls in PHP or by importing a CSV file. The results are outputted into a PNG file (as shown above). You can choose the colors and size of the file.

Obviously, the default U.S. state map is more suited for a presidential election, so another new feature is the ability to load custom maps. A map need only be a PNG file with specific colors for the regions within it. More information is available on the project page. World and continent maps will be available in the next release.

Finally, I’ve recreated the existing maps so they render a little cleaner in different sizes. The output is now a little sharper.

Let me know what you think or if you have any ideas for future development. Finally, you can download the library on its GitHub project page.

  • Facebook
  • Twitter

Posted in Open Source Projects, PHP | Comments Off

Shade in Areas of a Map and Export to a PNG with the PHPStateMapper Library

Friday, October 29th, 2010

PHPStateMapper is an open source PHP library for drawing a map with areas shaded by varying degrees of intensity based on data given as a simple list (e.g.: MN: 5, WI: 12, MI: 23). It exports a PNG image in a configurable size and color.

I’ve wanted this for awhile. I had a table with states and the number of users from that state in a CSV file (a report I exported). I wanted to show this on a map. I didn’t want a huge hassle with a big charting package and I didn’t need all the bells and whistles — just make the states shade darker to represent usage trends on a report:

Preview

And so I wrote PHPStateMapper. The first version of my PHPStateMapper script is now available for download at the project page here. It’s features include:

  • Easy to deploy – Only an include and a few lines of code.
  • Custom size – Anywhere between 100 and 2,000 pixels wide for web or printing.
  • Custom color – Just give it a hex color when you instantiate the object.
  • Extendible – Just about any map can be added.
  • Clean code – Object oriented, PHP 5+ well documented source code.
  • Easy to seed – Use the CSV importer object or just pass the data into the object.

In the near future, I plan on adding a few more features, such as:

  1. More reporting styles – like inflating circles or red/blue states.
  2. More maps (currently only U.S.).
  3. More loaders (currently only CSV/PHP object calls).
  4. Making it a PEAR library for easy installation.
  5. Latitude/longitude importer for raw coordinate loading.

The project is released under the BSD license. Use it however and wherever you like. Enjoy :)

  • Facebook
  • Twitter

Posted in HTML, Open Source Projects, PHP | Comments Off