<?php
define('MLMIPN_LOG_FILE_PATH', dirname(__FILE__) . '/log/mlm/');

/**
 * @filename        InstantPaymentNotifier.php
 * @description     This class is for sending instant payment notification to a given URL
 * @version         1.0
 * @package
 * @author          Mohammad Sajjad Hossain - info@sajjadhossain.com
 *
 * @link            http://sajjadhossain.com
 * @created on      September 07, 2011
 * @Dependencies
 * @license
 ***/
class MlmPaymentNotifier
{
    private $testMode = 1;
    private $apiKey   = 'mlm1bqrsc2e';
    private $webId        = 'MLMBrilliance';
    private $packageId    = 'P6357';
    private $siteUrl;

    private $requestVariables = array();

    private $error;

    private $items = array();

    private $success = false;

    private $debug = false;

    /**
    *@desc Constructor
    */
    public function MlmPaymentNotifier()
    {
         $this->setCustomVariable('STestDrive', $this->testMode);
         $this->setCustomVariable('apikey', $this->apiKey);
         $this->setCustomVariable('WebID', $this->webId);
         $this->setCustomVariable('PackageID', $this->packageId);
    }

    /**
    *@desc URL to which the notification will be posted.
    * @param string $url
    */
    public function setSiteUrl($url)
    {
        $this->siteUrl = $url;
    }

    public function setCustomVariable($varName, $value)
    {
        $this->requestVariables[$varName] = $value;
    }

    public function enableDebugging()
    {
        $this->debug = true;
    }

    /**
    *@desc Returns the error message
    * @return string
    */
    public function getError()
    {
        return $this->error;
    }

    public function notify($productName = null, $productQuantity = null, $productPrice = null)
    {
        if(empty($this->siteUrl))
        {
            $this->logMessage('Site URL is missing.');
            $this->error = 'Site URL has not been set';
            return;
        }
        $this->logMessage('Preparing request...');

        foreach($this->requestVariables as $key => $value)
        {
            $nameValuePairs[] = $key .'=' . (empty($value) ? '' : urlencode($value));
        }

        $requestString = implode('&', $nameValuePairs);

        $this->logMessage($requestString);

        $this->logMessage('Initializing cURL...');

        $ch = curl_init();

        $this->logMessage('Sending Request to : ' . $this->siteUrl);
        curl_setopt($ch, CURLOPT_URL, $this->siteUrl);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $requestString);
        curl_setopt($ch,CURLOPT_USERAGENT, 'PWCIPN');
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_VERBOSE, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

        $this->logMessage('Getting response...');
        $response = trim(curl_exec($ch));

        $this->logMessage(urldecode($response));

        if (curl_errno($ch))
        {
            $this->error = 'cURL Error # ' . curl_errno($ch) . ' : ' . curl_error($ch);

            $this->logMessage($error);
        }
        else
        {
            curl_close($ch);
        }

        $this->logMessage('Done...');
    }

    private function logMessage($message)
    {
        if($this->debug)
        {
            $fileName = MLMIPN_LOG_FILE_PATH . 'log_' . date('Y-m-d') . '.php';

            $text = "$message\n\r-----------------------------------------------------------------------------\n\r";

            if(!file_exists($fileName))
            {
                $text = "<?php die('Access Denied'); ?>\n\n" . $text;
            }

            $this->debugText .= $text;

            $f = fopen($fileName, 'ab');
            flock($f, LOCK_EX);
            fwrite($f, $text);
            flock($f, LOCK_UN);
            fclose($f);
        }
    }
}

//End of file Plexum.php