top
logo


Home Joomla Development Joomla Custom PayPal IPN

Joomla Custom PayPal IPN

PDF Print E-mail
(7 votes, average 4.43 out of 5)
Written by Alex Balyuk   
Monday, 30 August 2010 04:57

PayPal_mark_180x113All businesses are different and require specific software to run the business property. PayPal is a great service that millions use to quickly, safely and securely process online payments and send/receive money. Joomla being the most popular framework has many developers reinventing the wheel  and re-developng the IPN (instant payment notification) over and over and over again. I have seen some Joomla IPN that is horrible, not secure, bad logic, but was used by businesses. I while back, I developed a solid IPN for Joomla. 

It works as a second entry point for the front-end, but only for IPN Processing. There is not output at all, if use accesses the page a blank page will be displayed.

 

This make is development job easier and save you a lot of time. You do not have to handle talking to PayPal nor figuring out how to load joomla settings to connect to the same database. Everything is done for you. You only need to change IPN Configuration program your application logic.

 

Step 1

Download Joomla IPN source code.

Source code written in PHP 5. However, it can easily be converted to PHP 4, just cahnge "public static" to "var" in the IPNConfig class.

 

Step 2

Change IPN Configuration by editing IPNConfig.php

/**
 * Description of IPNConfig
 *
 * @author Alex
 */
class IPNConfig {
    
    public static $PAYPAL_EMAIL = "accoutn@gmail.com";
    public static $SECURE_MERCHANT_ACCOUNT_ID = "123456789"; 
        
    public static $ADDRESS = "ssl://www.sandbox.paypal.com";
    public static $PORT = 443;
    public static $TIMEOUT  = 30;    
}

 

Step 3

Code application logic in ipn.php.


/*
 * @version     $Id$
 * @author      Oleksandr Balyuk <obalyuk@boolcast.com>
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL v2
 * Creation Date:   26 June, 2009
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * ------------------------------------------------------------------------
 *
 * MUST BE LOCATED INSIDE OF SOME COMPONENT DERECTORY
 * ON THE SITE SIDE (NOT ADMINISTRATOR/COMPOENTS)
 * FOR EXAMPLE: /components/com_ipn/
 *
 * IF YOU CANT TO PLACE IT INTO DIFFERENT DIRECTORY MAKE SURE
 * $path = split(DS."component", $path);
 * IS EVALUATED TO ROOT OF SITE
 *
 */

//  Prevent Direct User Access (Need Transaction Type)
if (!isset($_POST["txn_type"]))
    die;

// Set flag that this is a parent file
define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);

$path = dirname(__FILE__);
$path = split(DS . "component", $path);

define('JPATH_BASE', $path[0]);
define('JPATH_COMPONENT', JPATH_BASE . DS . 'components' . DS . 'com_ipn');

require_once ( JPATH_BASE . DS . 'includes' . DS . 'defines.php' );
require_once ( JPATH_BASE . DS . 'includes' . DS . 'framework.php' );
require_once (JPATH_COMPONENT . DS . 'IPNConfig.php');
$mainframe = & JFactory::getApplication('site');
$mainframe->initialise();


//  Log the Transaction into the IPN Transaction Log [PTIONAL]
// @todo add loggin logic
//  Prevent User Access (Continue only if Transaction ID is set)
if (!isset($_POST["txn_id"]))
    die;


// Read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
    $value = urlencode(stripslashes($value));
    $req .= "&$key=$value";
}


// Post back to PayPal to validate
$server = IPNConfig::$ADDRESS;
$port = IPNConfig::$PORT;
$timeout = IPNConfig::$TIMEOUT;

$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";

$fp = fsockopen($server, $port, $errno, $errstr, $timeout);


// Process validation from PayPal
if (!$fp) {
    // HTTP ERROR
} else {

    // NO HTTP ERROR
    fputs($fp, $header . $req);

    while (!feof($fp)) {
        $res = fgets($fp, 1024);
        if (strcmp($res, IPNConfig::$VERIFIED) == 0) {

            //  Validate IPN
            if (IPNMAnager::getInstance()->isValid($_POST)) {
                //  Process Payment
                // @todo add processing logic

                break;  //  Exit the loop
            }
        } else if (strcmp($res, IPNConfig::$INVALID) == 0) {
            //  Log the INVALID IPN Transaction for manual invenstigation
            // @todo add loggin invalid logic
            break;  //  Exit the loop
        }
    }
    fclose($fp);
}
?>

 

Step 4

On PayPal website under Profile->IPN enable IPN and set the url to the ipn.php file (http://yoursite.com/components/com_custom_component/ipn.php).

 

 

You can download archived code below

Joola IPN Version:1.0
 GNU/GPL v2GNU/GPL v2    This e-mail address is being protected from spambots. You need JavaScript enabled to view it    Joomla 1.5  2.57 KB

Last Updated on Tuesday, 07 September 2010 14:25
 

Add comment


Security code
Refresh



Copyright © 2012 (bool) cast.com. All Rights Reserved.
Joomla!Joomla! is Free Software released under the GNU/GPL License.GNU/GPL License.

bottom
top
Site Map | Contact Us

bottom