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.
Create a class ( name ? anything )
make sure mcrypt enable in your server
var $key =’H%&gdfoRE^#$@#dfesdfd;
// * Do not change this key after we are in production unless you know what
If you change this key, no one can process * payments!
This function will encrypt the string that is passed to it
- function enCrypt($data = null) {
- if ($data != null) {
- $td = mcrypt_module_open(‘cast-256’, ”, ‘ecb’, ”);
- $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
- mcrypt_generic_init($td, $this->key, $iv);
- $encrypted_data = mcrypt_generic($td, $data);
- $encoded = base64_encode($encrypted_data);
- if (!mcrypt_generic_deinit($td) || !mcrypt_module_close($td)) {
- $encoded = false;
- }
- } else {
- $encoded = false;
- }
- return $encoded;
- }
* This function will de-crypt the string that is passed to it
- function deCrypt($data = null) {
- if ($data != null) {
- // The reverse of encrypt. See that function for details
- $data = (string) base64_decode(trim($data));
- $td = mcrypt_module_open(‘cast-256’, ”, ‘ecb’, ”);
- $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
- mcrypt_generic_init($td, $this->key, $iv);
- $data = (string) trim(mdecrypt_generic($td, $data));
- // Make sure the encryption modules get un-loaded
- if (!mcrypt_generic_deinit($td) || !mcrypt_module_close($td)) {
- $data = false;
- }
- } else {
- $data = false;
- }
- return $data;
- }
* This method will mask a credit card number (eg . shos last 4 char XXXXXXXXXXXX287)
- function maskCardNumber($cardNumber) {
- $cardArray = str_split($cardNumber);
- $length = count($cardArray);
- $maskedCardNumber = “”;
- // Mask all numbers except the last 4
- for ($i = 0; $i < $length -4; $i++) {
- $cardArray[$i] = ‘X’;
- }
- // Turn back into a string
- for ($i = 0; $i < $length; $i++) {
- $maskedCardNumber = $maskedCardNumber . $cardArray[$i];
- }
- return $maskedCardNumber;
- }