Location: PHPKode > scripts > SnPayPalApi > snpaypalapi/lib/SnCliPayPalMethods.class.php
<?php
 /**
 * 
 * 
 * @author Michael J. Burgess
 * @package SnPayPal
 * @subpackage SnCliPayPal
 * @copyright Michael J. Burgess, 2008
 * @version 1.0
 * @licence MIT
 */
 
 /**
 * Commandline interface to some PayPal API Methods
 *
 */
class SnCliPayPalMethods extends SnPayPalCliMethod
{
    /**
     * Return the commands made available by this class
     *
     * @return array
     */
    public function getCliCommands()
    {
        $commads = array();
        
        $commads['info'] = 'info'; 
        $commads['pa']   = 'printArgs'; 
        $commads['cb']   = 'checkBalance'; 
        $commads['fw']   = 'getWithdrawlTransactions';
        $commads['lt']   = 'getLatestTransactions';
        $commads['gt']   = 'getTransactions';
        
        return $commads;
    }
    
    /**
     * Get the balance of the account specified in SnPayPalConfig
     *
     * @param SnCliPayPal $cli
     * @param SnPayPalApi $paypal
     * @param stdClass $eventArgs
     * @return boolean
     */
    public function checkBalance($cli, $paypal, $eventArgs)
    {
        $this->disableReturnRequest();        
        $cli->write('Account: ' . SnPayPalConfig::USERNAME);
        $cli->write('Requesting Balance...', true);
             
        if ($response = $paypal->getBalance()->getResponse('GetBalanceResponse')) 
        {
            $cli->write('Balance: ' . $response->Balance . ' ' . $response->Balance['currencyID']);
            $cli->write('Read on: ' . date('F dS Y - H:i', strtotime($response->BalanceTimeStamp)));
            return true;
        }
        
        else 
        {
            $cli->write('Could not retrieve data!');
            return false;
        }
    }
    
    /**
     * Get information relating to SnCliPayPal and this class
     *
     * @example php cli info
     * @param SnCliPayPal $cli
     * @param SnPayPalApi $paypal
     * @param stdClass $eventArgs
     * @return boolean
     */
    public function info($cli, $paypal, $eventArgs)
    {
        $cli->write('Purpose:   PayPal API Command Line Interface');
        $cli->write('Written:   Jan 2008');
        $cli->write('Contact:   hide@address.com');
        $cli->write('Use:       php cli function fnc_arg=value');
        $cli->write('Copyright: Michael John Burgess, 2008');
        $cli->write('Licence:   MIT');
        $cli->write('Getting Available Commands', true);
        
        foreach ($this->getCliCommands() as $command => $function)
        {
            $cli->write("$command  \t$function()");
        }
        
        return true;
        
    }
    
    /**
     * Print the arguments supplied
     *
     * @example php cli pa arg=1 23=b eg3=m1x7ur3
     * @param SnCliPayPal $cli
     * @param SnPayPalApi $paypal
     * @param stdClass $eventArgs
     * @return boolean
     */
    public function printArgs($cli, $paypal, $eventArgs)
    {                
        foreach ($eventArgs as $variable => $value)
        {
            $cli->write("[$variable] = > \t$value\n");
        }
        
        return true;
    }
    
    /**
     * Return one day of received transactions
     *
     * @example php cli lt
     * @param SnCliPayPal $cli
     * @param SnPayPalApi $paypal
     * @param stdClass $eventArgs
     * @return boolean
     */
    public function getLatestTransactions($cli, $paypal, $eventArgs)
    {
        $eventArgs->type   = 'r';
        $eventArgs->nodays = 1;
        return $this->getTransactions($cli, $paypal, $eventArgs);
    }
    
    /**
     * Get one week of transactions
     *
     * @example php cli fw
     * @param SnCliPayPal $cli
     * @param SnPayPalApi $paypal
     * @param stdClass $eventArgs
     * @return boolean
     */
    public function getWithdrawlTransactions($cli, $paypal, $eventArgs)
    {
        $eventArgs->type   = 'w';
        $eventArgs->nodays = 7;
        return $this->getTransactions($cli, $paypal, $eventArgs);
    }   
    
    /**
     * Return a list of transactions, options from the cmdline:
     * nodays=number_of_days_to_search_from_today
     * type=r||w - funds received or withdrawls
     * 
     * @example php cli gt nodays=7 type=r
     * @param SnCliPayPal $cli
     * @param SnPayPalApi $paypal
     * @param stdClass $eventArgs
     * @return boolean
     */
    public function getTransactions($cli, $paypal, $eventArgs)
    {
        $cmdArg = array('w' => SnPayPalApiConst::TRANS_CLASS_WITHDRAWN,
                        'r' => SnPayPalApiConst::TRANS_CLASS_RECEIVED);
                         
        $cmdArg = isset($eventArgs->type)   ? $cmdArg[$eventArgs->type] : $cmdArg['w'];
        $days   = isset($eventArgs->nodays) ? $eventArgs->nodays : 2;
        
        $date   = date('c', (time() - (3600*24*$days)));
        
        $cli->write('Account: '.SnPayPalConfig::USERNAME);
        $cli->write('Requesting Transactions', true);
        
        if ($response = $paypal->transactionSearchByType($date, $cmdArg)) 
        {
            $transactions = $response->getResponse('TransactionSearchResponse');
            
            if(empty($transactions->PaymentTransactions)) 
            {
                $cli->write('There have been no transactions '. (($days > 1)  ? 'in the last '.$days.' days!' : 'today!'));
                $this->disableReturnRequest();
                return true;
            }
            
            /**
             * to combat some weird SimpleXML behaviour
             * acting on ->PaymentTransactions seems to destroy the arrays it contains
             */
            if(empty($transactions->PaymentTransactions[1]))
            {                
                $array = array(clone $transactions->PaymentTransactions);
                $transactions = new stdClass();
                $transactions->PaymentTransactions = $array;
                
            }
            
            foreach ($transactions->PaymentTransactions as $transaction)
            {                              
                $cli->write('Transaction: ' . $transaction->TransactionID);
                if($cmdArg == SnPayPalApiConst::TRANS_CLASS_RECEIVED) $cli->write('Payer: '. $transaction->Payer);
                $cli->write('Amount: '  . $transaction->GrossAmount);
                $cli->write('Status: '  . $transaction->Status);
                $cli->write('Date: '    . date('d-m-Y H:i', strtotime($transaction->Timestamp)));
                $cli->write('', true);
                
            }
            
            return true;
        }
        
        else 
        {
            $cli->write('Could not retrieve data!');
            return false;
        }
    }    
}

?>
Return current item: SnPayPalApi