Location: PHPKode > scripts > SnPayPalApi > snpaypalapi/lib/SnPayPalApi.class.php
<?php
/**
 * Implementation of some PayPal API Methods
 *
 * @author Michael J. Burgess
 * @package SnPayPal
 * @copyright Michael J. Burgess, 2008
 * @version 1.0
 * @licence MIT
 */

/**
 * Interface to the PayPal API
 *
 */
class SnPayPalApi extends SnPayPalApiInterface
{
    /**
     * Get all the details of a transaction
     *
     * @param string $transactionId
     * @return SnPayPalApi
     */
    public function getTransactionDetails($transactionId)
    {
        return $this->callAPI('GetTransactionDetails', new SnPayPalApiParameter('TransactionID', $transactionId, 'ebl:TransactionId'));
    }
    
    /**
     * Get all the transactions of a given type from a given date
     * The type is blank on 'TransactionClass' which should be ebl:PaymentTransactionClassCodeType
     * as anything else seems to cause errors (data type mismatch)
     * 
     * @param string $startDate (as date('c');)
     * @param string $type (see SnPayPalConfig.class.php, SnPayPalApiConst::TRANS_CLASS_*)
     * @return SnPayPalApi
     */
    public function transactionSearchByType($startDate, $type)
    {
        return $this->callAPI('TransactionSearch', array(new SnPayPalApiParameter('StartDate', $startDate,'xs:dateTime'),
                                                         new SnPayPalApiParameter('TransactionClass', $type,  '')));
    }
    
    /**
     * Get all the transactions occuring between $startDate and $endDate
     *
     * @param string $startDate ISO Format i.e. date('c');
     * @param string $endDate       "
     * @return SnPayPalApi
     */
    public function transactionSearchByDate($startDate, $endDate)
    {
        return $this->callAPI('TransactionSearch', array(new SnPayPalApiParameter('StartDate', $startDate,'xs:dateTime'),
                                                         new SnPayPalApiParameter('EndDate',   $endDate,  'xs:dateTime')));
    }
    
    
    /**
     * Makes two calls to paypal, one to search for a TransactionID,  the other to get detailed information about that id
     *
     * @param string $startDate date in ISO format  (i.e. date('c'))
     * @param string $endDate date in ISO format  (i.e. date('c'))
     * @return SnPayPalApi
     */
    public function fullTransactionSearchByDate($startDate, $endDate)
    {
        $transactionResponse = $this->transactionSearchByDate($startDate, $endDate)->getResponse('TransactionSearchResponse');
        return $this->getTransactionDetails($transactionResponse->PaymentTransactions->TransactionID);
    }
    
    /**
     * Pay a list of accounts a given amount, 
     * supply also the subject (purpose) of the payment and a message to payees
     *
     * @param array $emailList
     * @param string $subject
     * @param string $message
     * @param string $amount
     * @param string $currency e.g. USD, GBP, EUR
     * @return SnPayPalApi
     */
    public function massPay($emailList, $subject, $message, $amount, $currency = 'USD')
    {
        $parameters = array(new SnPayPalApiParameter('ebl:string', 'EmailSubject', $amount));

        foreach ($emailList as $email)
        {
            $subParameters = array( new SnPayPalApiParameter('ReceiverEmail', $email, 'ebl:string'),
                                    new SnPayPalApiParameter('Amount',        $amount, 'ebl:string', "currencyID=\"$currency\""),
                                    new SnPayPalApiParameter('Note',          $message, 'ebl:string'));
                                    
            $parameters[] = new SnPayPalApiParameter('MassPayItem', $subParameters, 'ebl:MassPayItemType');
        }

        return $this->callAPI('MassPay', $parameters);
    }
    
    /**
     * Refund a transaction by TransactionId
     *
     * @param string $transactionId
     * @return SnPayPalApi
     */
    public function refundTransaction($transactionId)
    {
        return $this->callAPI('RefundTransaction',  new SnPayPalApiParameter('TransactionID', $transactionId, 'ebl:TransactionId'));
    }
    
    /**
     * Return account balance response
     *
     * @return SnPayPalApi
     */
    public function getBalance()
    {
        return $this->callAPI('GetBalance');
    }
    
}
Return current item: SnPayPalApi