Authorize.Net Integration with CakePHP

I did not expect it to be this easy to integrate. All I did was download the sdk for AIM method in authorize.net official site under developer section. You need to create a test account with the site and get the api login id and transaction id using which you can test the account.

--> Unzip the zip file copy the entire folder into your_project/app/controller/component/  folder.
--> Copy the lib folder, Authorize.Net.php file into the component folder, instead of having the entire thing into this folder "anet_php_sdk"
--> Now rename the AuthorizeNet.php to authorize_net.php and modify the class name to

class AuthorizeNetComponent extends Object

instead of  Exception class.

Now write a function for ex. validate_card() which accepts two parameters that is your login id and transaction id.

Here is how your class in authorize_net.php should look like

class AuthorizeNetComponent extends Object
{
    function validate_card($loginid=null,$trankey=null)
    {
        $transaction = new AuthorizeNetAIM($loginid, $trankey);
        $transaction->amount = '9.99';
        $transaction->card_num = '4007000000027';
        $transaction->exp_date = '10/16';
        //$transaction->cardholder_authentication_value = '778';
       
        return $transaction->authorizeAndCapture();
    }
}

In your controller, include another Component to your var $component list which is AuthorizeNet.

And now write another function which should look like this.

    function testauth()
    {
        $response = $this->AuthorizeNet->validate_card('YOUR_API_LOGIN_ID', 'YOUR_TRANSACTION_KEY'
);
       
        if ($response->approved) {
              echo "<h1>Success! The test credit card has been charged!</h1>";
              echo "Transaction ID: " . $response->transaction_id;
        } else {
              echo $response->error_message;
        }
    }

Thats it in your browser now call this action: http://localhost/your_controller/testauth

And you should see success message if you use the same card details, change a bit and see if it says success or failure.

You can either add parameters to the function validate_card to send in the card details etc., Or send as an array or have public variables in the AuthorizeNet class and use them to set the values from your form. You can use any method to integrate this payment method.

Empty password in Login form submission using Auth Component

I faced this couple of times. I go mad when I am working with Auth component and I wonder why the password is being sent as a blank one. I did not investigate into it completely but one thing that you need to do for sure is to remove the security salt value in cakephp core.php file. Then it works fine. I have to work with changing the hashing method too. I will modify this post after gaining complete knowledge about it.

Cakephp .htaccess problem (Internal Server Error)

I came across this Internal Server Error twice in my experience while working with cakephp. Once it was due to the database.php file, which was not modified after uploading. It was still referring to localhost db with login credentials. After I modified that it worked fine, was pretty easy.

But the second time, when I hit with this problem, I had to work for half a day to find a solution to this. This time it was because of the .htaccess file. This is what I had to do:
<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteRule    ^$ /app/webroot/    [L]
   RewriteRule    (.*) /app/webroot/$1 [L]
</IfModule>

I had to add a  '/' before the app folder. And similarly in the webroot folder I had to add a '/' in front of index.php file. This will work fine if you have only one app folder. But the next problem I had to face was, that I had another app folder which was renamed to admin for admin side scripts and it was sharing the same cake folder. This is how I had to fix this problem:

In the app folder I had one .htaccess file with the following piece of code:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule    ^$    /admin/webroot/    [L]
    RewriteRule    (.*) /admin/webroot/$1    [L]
 </IfModule>

And under webroot I had another .htaccess file with this following piece of code:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$    /admin/webroot/index.php?url=$1 [QSA,L]
</IfModule>

This was good enough for my site to be up and running. Later I was hit by another problem which was:

I had a login form, and after login, it has to redirect to a page. During the process it was giving a 200 OK error and showing the source code of the file instead of executing it. As far as I understood, the redirect in cakephp was trying to find in the cache (as cake is famous for its strong caching), and failing to find the file. So, I removed the $this->redirect tag and instead wrote header() function of PHP and it worked. Voila, I am writing all this for my sake and for others who may hit with similar kind of problems.



How to work with MPForm in WebsiteBaker CMS

WebsiteBaker is the most easy CMS in the world that will allow you to create a website within minutes. More than half a million other websites found this to be true.
Is that true, well I was surprised when I got a project that was half done and thrown onto my face to extend the functionality and add some more. What I was asked to do is to have a form to upload files onto the server. I checked at the FORM module that was already existing and to my surprise I could not find the field called file. You know what I am talking about,  the field to generate “<input type=’file name=’file’>” was missing. I scratched my head and started searching for other modules which will help me do this. Half a day was given to me to understand the WebsiteBaker altogether and the project that was half done and start working on it. I thought that was unfair, but do I have a choice, well a big “NO”.
So I started searching failed to understand or find anything related to that. I posted in the forum and the next day I got a reply saying the solution is MPFORM module, which is an extended version of FORM module. You should have seen my face at that time, a sign of relief, a glow of 1000 W bulb. I posted a ton thanks to the person who saved me and reverted back to my project.
I downloaded and installed MPFORM. You can download it from here:
http://www.websitebakers.com/pages/modules/interaction/mpform.php?searchresult=1&sstring=MPFORM
And here you will find the help that you need to work with MPForm
http://wbdemo.heysoft.de/pages/en/mpform.php?lang=EN
Little Tutorial how to work with MPFORM:
1. Create a page by selecting the MPForm as the module you want to implement in it.
2. You see General Options, Advanced Settings, CSS Settings, Add Field and Help buttons
3. General Options has all the layout settings for the form. Tip: You may want to remove the email address under the To Addresses as it tries to send an email to the site owner after uploading the file. If you are working under localhost it fails as the email will not be sent. And I personally think it is not necessary to have it there.
4. In the Advanced Settings you will have field formatting information.
Tip: a. Change the path in “Upload file folder”  under “Settings for File Upload” section to suit to your requirements, where on your server you want to upload the files.
b. In case you want to attach the uploaded file to email then make “Attach uploaded file to email” enabled
c. You can set the “Max upload file size Kb” to your desired size. Files more than that size fail to get uploaded.
d. Disable the “Captcha Verification” under “SPAM Protection“  if that is not required to have on the page.
5. You may change the look and feel of the form by using the “CSS Settings” section
6.  “Add Field” button enables you to add a field to the form.
This will only upload the file and will not do anything after that. What if you want to upload the file and write the information into the db table? Yes you have a provision for that:
You will find your mpform related scripts here :
C:\wamp\www\myproject\modules\mpform
In that folder you will have a script called private.php and in that you have a function called “private_function_on_success” you can write your db code in that.
In case you want to execute any code before rendering the form you write that functionality in a function called “private_function_before_new_form“. For ex. I want to populate the select box for category from the categories table. I retrieve all the values into an array and set that into session variable. Which will be used later on while painting the form.
There is one more function called “private_function_before_email” which will be called before sending an email.
The script that generates the form and shows it in our page is “C:/wamp/www/sbdmember/modules/mpform/paintform.php“.
I had to tweak this script little bit to populate the select box with the categories which are retrieved from categories table.
This is the function I modified:
// Function for generating an options for a select field
if (!function_exists(‘make_option’)) {
function make_option(&$n,$keys, $k, $params) {
global $code;
$values = $params[0];
$isnew = $params[1];
$def = strpos($n, IS_DEFAULT);
($def > 0) ? $h = substr($n, 0, $def) : $h = $n;
// start option group if it exists
if (substr($n, 0, 2) == ‘[=') {
$n = '<optgroup label="'.substr($n,2,strlen($n)).'">';
} elseif ($n == ']‘) {
$n = ‘</optgroup>’;
} else {
if (in_array($h, $values) or ($isnew and $def > 0)) {
$n = ‘<option selected=”selected” value=”‘.$h.’”>’.$h.’</option>’;
} else {
$n = ‘<option value=”‘.$keys.’”>’.$h.’</option>’;
}
}
}
}
and changed a little bit under the condition where it checks it is a select box.
if(!$value)
{
$options=$_SESSION['category'];
}
else
{
$options = explode(‘,’, $value);
}
//                array_walk($options, ‘make_option’, array((isset($_SESSION['field'.$field_id])?$_SESSION['field'.$field_id]:array()), $isnew));
array_walk_recursive($options, ‘make_option’, array((isset($_SESSION['field'.$field_id])?$_SESSION['field'.$field_id]:array()), $isnew));
I changed array_walk to array_walk_recursive to make it work for associative array.
Tip:One more thing you need to remember is whenever you add a field in the page using MPForm, the details about the field is stored in a db table called “mod_mpform_fields“. Identify the field names from this table and use them where required.
Tip: You may use the “section_id” field to identify the page you are working with. You also have page_id in case you  need that while writing into your db tables or working with them.
Tip: “evalform.php” is where the filename for uploaded file is set and actual upload is done. File name is generated using “date+random number+filename+extension”. In case you want to change the filenames you can search for that in this script file and change it there.
I will write how to work with databases in my next post.
That is all about MPForm. Hope that helped few atleast who are working with MPForm.

About this blog

This blog is all about PHP and related frameworks. It reveals my little experiences, the challenges I faced in my career, my explorations and my innovations. I hope it benefits me and others too who might face these challenges in their career. Have a nice time reading the blog. Enjoy.

About Me

Hi, my name is Hima. I am an experienced programmer in PHP and related frameworks. I live in Hyderabad, India.
Copyright © 2010 - My PHP Tips & Tutorials. Powered by Blogger.. Powered by Blogger.