Integration with Joomla 1.5.X and lower

Task - when user signs up to Joomla, sign them up also to the affiliate system.

For this example we'll use Joomla 1.5.8, and we'll develop a simple Joomla plugin that will call PAP4 API upon user registration in Joomla.

To develop a new plugin in Joomla we need to create two files:

pap4.xml

<?xml version="1.0" encoding="utf-8"?>
<install version="1.5" type="plugin" group="user" method="upgrade">
  <name>User - PAP4 Register</name>
  <author>Maros Fric</author>
  <creationDate>December 2008</creationDate>
  <copyright>(C) 2008 Maros Fric</copyright>
  <license>http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL</license>
  <authorEmail>support@qualityunit.com</authorEmail>
  <authorUrl>www.qualityunit.com</authorUrl>
  <version>1.0</version>
  <description>Plugin that registers new user also to PAP4 using the API</description>
  <files>
    <filename plugin="pap4">pap4.php</filename>
    <filename plugin="pap4">PapApi.class.php</filename>
  </files>
  <params/>
</install>

This file contains a definition of the plugin.

 

The second file pap4.php is the file where the API call happens:

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );

jimport('joomla.plugin.plugin');

class plgUserPap4 extends JPlugin {

    function plgUserPap4(& $subject, $config) {
        parent::__construct($subject, $config);
    }

    function onAfterStoreUser($user, $isnew, $succes, $msg) {
        global $mainframe;

        if ($isnew) {
            // here we start executing our API code
            require("PapApi.class.php");
            try {
                // login to API
                $session = new Pap_Api_Session("http://demo.qualityunit.com/pax4/scripts/server.php");
                if(!$session->login("merchant@example.com", "demo")) {
                    // cannot login to API
                    // do something with the error
                    return;
                }

                //identify the referrer (affiliate)
                $clickTracker = new Pap_Api_ClickTracker($session);
                try {
                    $clickTracker->track();
                    $clickTracker->saveCookies();

                    $affiliate = $clickTracker->getAffiliate();
                    $referralid = '';

	            //if cookies were found - so an affiliate was recongized as a referrer
	            if ($affiliate !== null) { 
		           $referralid = @$affiliate->getParam('refid'); //get the referral id of the referring affiliate
	            }
                 }
                 catch (Exception $e) {
		    //echo("No parent affiliate recognized: ".$e->getMessage());
                 }

                // create new affiliate
                $affiliate = new Pap_Api_Affiliate($session);
                $affiliate->setUsername($user['email']);
                $affiliate->setPassword($user['password_clear']);
                $affiliate->setFirstname('Joomla');
                $affiliate->setLastname($user['fullname']);
                if ($referralid != '') $affiliate->setParentUserId($referralid); //setting the parent affiliate

                if(!$affiliate->add()) {
                    // cannot add record
                    // do something with the error
                    return;
                } else {
                    // affiliate was successfully added
                }
            } catch(Exception $e) {
                // do something with the error
                return;
            }
        }
    }
}

This is a very simple plugin, it can be improved in many ways. For example, it does not notify us about an error when the API call fails. So if you cannot login to API, or adding user fails, you won't know about it.

To make it work in production, you should implement some email notifications or logging when API call fails or returns error.

 

Installing the plugin

You can download the full source of this example.

Unzip files into a directory and then install the plugin using Joomla> Extensions> Install/Uninstall, then activate it in Joomla> Extensions> Plugin manager