Paypal Plugin for CakePHP 2.0

  On 11 Jun, 2013   By Comments are Off

Integrate Paypal with your CakePHP application

Sometimes our cakephp projects require paypal integration in order to allow users make payments in your website. In this article, you will find a quick guide to the installation and setup of the Paypal IPN Plugin which provides paypal integration with your cakephp website.

1. The Paypal IPN Plugin for CakePHP version 2.0  is available in the following Git repository:, download it in the app/Plugins/paypal_ipn folder inside your cakePHP installation.

2.  Add the following to your bootstrap file: CakePlugin::load(‘PaypalIpn’, array(‘bootstrap’ => array(‘paypal_ipn_config’), ‘routes’ => true));

3. Open a terminal, go to your cake installation and run the following command:

$ cake schema create -path plugins/paypal_ipn/config/sql -name ipn

4. Add the following into your /app/config/routes.php file

/* Paypal IPN plugin */
Router::connect('/paypal_ipn/process', array('plugin' => 'paypal_ipn', 'controller' => 'instant_payment_notifications', 'action' => 'process'));
/* Optional Route, but nice for administration */
Router::connect('/paypal_ipn/:action/*', array('admin' => 'true', 'plugin' => 'paypal_ipn', 'controller' => 'instant_payment_notifications', 'action' => 'index'));
/* End Paypal IPN plugin */

5. Open your Paypal account and get your merchant credentials and update the /paypal_ipn/config/paypal_ipn_config.php file with that information.

6. Add PaypalIpn.Paypal to your helpers list in app_controller.php

var $helpers = array('Html','Form','PaypalIpn.Paypal');

7. if you want to process the information received by Paypal once the payment has been completed, you can add a callback function to the Paypal plugin controller or to you app controller. Like this:

function afterPaypalNotification($txnId){
    //Here is where you can implement code to apply the transaction to your app.
    //for example, you could now mark an order as paid, a subscription, or give the user premium access.
    //retrieve the transaction using the txnId passed and apply whatever logic your site needs.

    $transaction = ClassRegistry::init('PaypalIpn.InstantPaymentNotification')->findById($txnId);
    $this->log($transaction['InstantPaymentNotification']['id'], 'paypal');

    //Tip: be sure to check the payment_status is complete because failure 
    //     are also saved to your database for review.

    if($transaction['InstantPaymentNotification']['payment_status'] == 'Completed'){
        //Yay!  We have monies!
    else {
        //Oh no, better look at this transaction to determine what to do; like email a decline letter.

8. Add a button to your view by using the code below, where test determines wheter the button will use the sandbox credentials or not, amount is the amount of the transaction and item_name the product that will be sold. By setting test to false, the live paypal credentials will be used. You can also set the return url by adding the attribute ‘return’.

$paypal->button('Pay Now', array('test' => true, 'amount' => '12.00', 'item_name' => 'test item' ));

Part II: Getting your Paypal Sandbox credentials.

In order to test the Paypal integration with the sandbox, you need the merchant and the buyer credentials. In order to get them, you should log in to your paypal sandbox: and select Sandbox Test accounts. By default, you will be presented with you business account. On that page, you will be able to create a new buyer account. Once you have a business and a buyer account in your sandbox, you are ready to test your payments integration.


0 thoughts on "Paypal Plugin for CakePHP 2.0"