Source for file paypal_base.php

Documentation is available at paypal_base.php

  1. <?php
  2.  
  3. /**
  4.  * Paypal Base Files
  5.  *
  6.  * This file contains very important classes and interfaces that are going to be used in this program
  7.  * 
  8.  * - interface OperationsTemplate
  9.  * - abstract class PaypalAPI
  10.  * - static class PaypalBase
  11.  * - final class PaypalRegistrar
  12.  * - final class WebsitePaymentsPro
  13.  * 
  14.  * phpPaypalPro currently supports 4 major operations available for the Website Payments Pro SOAP API namely:
  15.  * 
  16.  * (a) DoDirectPayment
  17.  * (b) SetExpressCheckout
  18.  * (c) GetExpressCheckoutDetails
  19.  * (d) DoExpressCheckoutPayment
  20.  * 
  21.  * Please be on the lookout as more operations are scheduled to be added in the immediate future.
  22.  * 
  23.  * @author Israel Ekpo <perfectvista@users.sourceforge.net>
  24.  * @copyright Copyright 2007, Israel Ekpo
  25.  * @license http://phppaypalpro.sourceforge.net/LICENSE.txt BSD License
  26.  * @version 0.1.0
  27.  * @package PaypalBase
  28.  * @filesource
  29.  */
  30.  
  31. /**
  32.  * Contains Webservice Data Types
  33.  * 
  34.  * All the data types used by this program is prepared
  35.  * by a class called PaypalTypes in this file.
  36.  */
  37. require_once('paypal_types.php');
  38.  
  39. /**
  40.  * Contains Configuration Data
  41.  * 
  42.  * Things like the API version, connection timeouts etc are
  43.  * registered in this module.
  44.  */
  45. require_once('config.inc.php');
  46.  
  47. /**
  48.  * Paypal Base Class
  49.  *
  50.  * This class contains important variables that are going to be
  51.  * used throughout the entire system.
  52.  * 
  53.  * @author Israel Ekpo <perfectvista@users.sourceforge.net>
  54.  * @copyright Copyright 2007, Israel Ekpo
  55.  * @license http://phppaypalpro.sourceforge.net/LICENSE.txt BSD License
  56.  * @package PaypalBase
  57.  */
  58. class PaypalBase
  59. {
  60.     /**
  61.      * Soap Client
  62.      *
  63.      * @access private
  64.      * @var soapClient 
  65.      */
  66.     private static $soapClient;
  67.     
  68.     /**
  69.      * Soap Header
  70.      *
  71.      * @access private
  72.      * @var soapHeader 
  73.      */
  74.     private static $soapHeader;
  75.     
  76.     /**
  77.      * Was the Last Operation a Success
  78.      *
  79.      * @access private
  80.      * @var bool 
  81.      */
  82.     private static $lastOperationSuccessful;
  83.     
  84.     /**
  85.      * API Reponse
  86.      *
  87.      * This is the last reponse recieved from the paypal web service
  88.      * 
  89.      * @access private
  90.      * @var mixed 
  91.      */
  92.     private static $apiResponse;
  93.     
  94.     /**
  95.      * API Exception
  96.      *
  97.      * @access private
  98.      * @var soapFault 
  99.      */
  100.     private static $apiSoapException;
  101.  
  102.     /**
  103.      * Sets the Value of the soapClient Attribute
  104.      *
  105.      * @static
  106.      * @access public
  107.      * @param soapClient $soapClient 
  108.      */
  109.     public static function setSoapClient($soapClient)
  110.     {
  111.         self::$soapClient $soapClient;
  112.     }
  113.     
  114.     /**
  115.      * Sets the Value of the SoapHeader
  116.      *
  117.      * @static
  118.      * @access public
  119.      * @param SoapHeader $soapHeader 
  120.      */
  121.     public static function setSoapHeader($soapHeader)
  122.     {
  123.         self::$soapHeader $soapHeader;
  124.     }
  125.     
  126.     /**
  127.      * Sets API response from Webservice
  128.      *
  129.      * @static
  130.      * @access public
  131.      * @param mixed $apiResponse 
  132.      */
  133.     public static function setApiResponse($apiResponse)
  134.     {
  135.         self::$apiResponse $apiResponse;
  136.     }
  137.     
  138.     /**
  139.      * Registers the Exception that was thrown
  140.      *
  141.      * @static
  142.      * @access public
  143.      * @param soapFault $apiSoapException 
  144.      */
  145.     public static function setAPISoapFault($apiSoapException)
  146.     {
  147.         self::$apiSoapException $apiSoapException;
  148.     }
  149.     
  150.     /**
  151.      * Sets the Last Operation Status
  152.      * 
  153.      * This tells us whether or not the last operation was a success.
  154.      * 
  155.      * @static
  156.      * @access public
  157.      * @param bool $status 
  158.      */
  159.     public static function setOperationStatus($status)
  160.     {
  161.         self::$lastOperationSuccessful $status;
  162.     }
  163.     
  164.     /**
  165.      * Returns the Soap Client
  166.      *
  167.      * @static
  168.      * @access public
  169.      * @return SoapClient 
  170.      */
  171.     public static function getSoapClient()
  172.     {
  173.         return self::$soapClient;
  174.     }
  175.     
  176.     /**
  177.      * Returns the Soap Header
  178.      *
  179.      * @static
  180.      * @access public
  181.      * @return SoapHeader 
  182.      */
  183.     public static function getSoapHeader()
  184.     {
  185.         return self::$soapHeader;
  186.     }
  187.     
  188.     /**
  189.      * Returns the last API Response
  190.      *
  191.      * This method returns the last response from the paypal webservice.
  192.      * 
  193.      * @static
  194.      * @access public
  195.      * @return mixed 
  196.      */
  197.     public static function getAPIResponse()
  198.     {
  199.         return self::$apiResponse;
  200.     }
  201.     
  202.     /**
  203.      * Returns the Exception object that was thrown
  204.      *
  205.      * @static
  206.      * @access public
  207.      * @return soapFault 
  208.      */
  209.     public static function getAPIException()
  210.     {
  211.         return self::$apiSoapException;
  212.     }
  213.     
  214.     /**
  215.      * Returns the Last Operation Status
  216.      *
  217.      * It returns true if the last operation was a success and false
  218.      * if it was not.
  219.      * 
  220.      * @static
  221.      * @access public
  222.      * @return bool 
  223.      */
  224.     public static function getOperationStatus()
  225.     {
  226.         return self::$lastOperationSuccessful;
  227.     }
  228. }
  229.  
  230. /**
  231.  * Template for all Operations Objects
  232.  *
  233.  * This is the template for all the classes that are used for each operation.
  234.  * Each and every one of them must have these two methods. The other methods will
  235.  * be inherited from the abstract PaypalAPI class.
  236.  * 
  237.  * @author Israel Ekpo <perfectvista@users.sourceforge.net>
  238.  * @copyright Copyright 2007, Israel Ekpo
  239.  * @license http://phppaypalpro.sourceforge.net/LICENSE.txt BSD License
  240.  * @package PaypalBase
  241.  */
  242. interface OperationsTemplate
  243. {   
  244.     /**
  245.      * Calls the Paypal Web Service
  246.      *
  247.      * This method prepares the final message for the operation,
  248.      * puts it in the right format and then invokes the __soapCall method
  249.      * from the static soapClient. The name of the operation, the message, and the
  250.      * input headers are passed to the __soapCall() method
  251.      */
  252.     public function execute();
  253.     
  254.     /**
  255.      * Tells us if the Operation was Successful
  256.      *
  257.      * If the operation was successful, this method will return true,
  258.      * otherwise it will return false
  259.      * 
  260.      * @return bool
  261.      */
  262.     public function success()
  263.  
  264.  
  265. /**
  266.  * Abstract Class for Paypal API Core Methods
  267.  *
  268.  * This is an abstract class that contains 6 important methods that
  269.  * are used by all the Operations classes.
  270.  * 
  271.  * @abstract
  272.  * @author Israel Ekpo <perfectvista@users.sourceforge.net>
  273.  * @copyright Copyright 2007, Israel Ekpo
  274.  * @license http://phppaypalpro.sourceforge.net/LICENSE.txt BSD License
  275.  * @package PaypalBase
  276.  */
  277. abstract class PaypalAPI
  278. {
  279.     /**
  280.      * Operation Status Registration
  281.      *
  282.      * This methods sets the lastOperationStatus attribute in the PaypalBase class.
  283.      * If the last operation was a success, this attribute is set to true, otherwise
  284.      * it will be set to false.
  285.      * 
  286.      * @access private
  287.      * @param bool $status 
  288.      */
  289.     protected function registerLastOperationStatus($status)
  290.     {
  291.         PayPalBase::setLastOperationStatus($status);
  292.     }
  293.     
  294.     /**
  295.      * API Response Registration
  296.      *
  297.      * This method registers the response recieved from the Paypal Webservice.
  298.      * As long as there were no errors it will be set. If it returns only one value
  299.      * then it may be an integer, float or a string. However it is returning
  300.      * multiple values then it may be an associative array or an object.
  301.      * 
  302.      * @access private
  303.      * @param mixed $APIResponse 
  304.      */
  305.     protected function registerAPIResponse($APIResponse)
  306.     {
  307.         PayPalBase::setApiResponse($APIResponse);
  308.     }
  309.     
  310.     /**
  311.      * API Exception Registration
  312.      * 
  313.      * This method registers any exception that occurs as a result of a soapFault
  314.      * being thrown. If a soapFault object was throw while the Operations object
  315.      * was calling the execute method, then this method will be invoked instead
  316.      * of the registerAPIResponse() method.
  317.      * 
  318.      * @access private
  319.      * @param SoapFault $APIException 
  320.      */
  321.     protected function registerAPIException($APIException)
  322.     {
  323.         PayPalBase::setAPISoapFault($APIException);
  324.     }
  325.     
  326.     /**
  327.      * Was the Operation Successful
  328.      * 
  329.      * This method tells us whether or not the last operation was a success
  330.      * 
  331.      * @access public
  332.      * @return bool 
  333.      */
  334.     public function success()
  335.     {
  336.         return PaypalBase::getOperationStatus();
  337.     }
  338.     
  339.     /**
  340.      * Returns the API Response
  341.      *
  342.      * The response from the Paypal Webservice can be accessed from this method
  343.      * 
  344.      * @access public
  345.      * @return mixed 
  346.      */
  347.     public function getAPIResponse()
  348.     {
  349.         return PaypalBase::getAPIResponse();
  350.     }
  351.     
  352.     /**
  353.      * Returns any SoapFault thrown
  354.      *
  355.      * Any exception that is thrown can be accessed from this method
  356.      * 
  357.      * @access public
  358.      * @return SoapFault 
  359.      */
  360.     public function getAPIException()
  361.     {
  362.         return PaypalBase::getAPIException();
  363.     }
  364. }
  365.  
  366. /**
  367.  * Paypal Registrar
  368.  *
  369.  * The purpose of this class is to create SoapClients and SoapHeaders just before each operation
  370.  * is carried out.
  371.  * 
  372.  * @author Israel Ekpo <perfectvista@users.sourceforge.net>
  373.  * @copyright Copyright 2007, Israel Ekpo
  374.  * @license http://phppaypalpro.sourceforge.net/LICENSE.txt BSD License
  375.  * @package PaypalBase
  376.  */
  377. final class PayPalRegistrar
  378. {   
  379.     /**
  380.      * Creates the Static SoapClient
  381.      *
  382.      * The soap_version element is used to indicate whether you are using SOAP 1.1 or SOAP 1.2
  383.      * The exceptions option is a boolean value defining whether soap errors throw exceptions of type SoapFault.
  384.      * 
  385.      * Setting the boolean trace option enables use of the methods  SoapClient->__getLastRequest,  SoapClient->__getLastRequestHeaders,
  386.      * SoapClient->__getLastResponse and  SoapClient->__getLastResponseHeaders
  387.      * 
  388.      * The trace option is necessary if you will need to use the SoapClient->__getLastRequest() or SoapClient->__getLastReponse() methods
  389.      * later for debugging purposes. The connection_timeout option defines a timeout in seconds for the connection to the SOAP service.
  390.      * I have set it to 10 minutes.
  391.      * 
  392.      * If for any reason you believe you software is going to take longer than usually, please feel free to
  393.      * adjust the maximum_execution_time value either during runtime or in the php.ini configuration file.
  394.      * 
  395.      * @access public
  396.      */
  397.     public static function registerSoapClient()
  398.     {
  399.         $clientOptions array("soap_version" => SOAP_1_1"exceptions" => true"trace" => true"connection_timeout " => API_CONNECTION_TIMEOUT);
  400.         
  401.         PayPalBase::setSoapClient(new SoapClient(API_WSDL$clientOptions));
  402.     }
  403.     
  404.     /**
  405.      * Creates the static SoapHeader
  406.      * 
  407.      * This is the Custom Security Header passed to paypal.
  408.      * 
  409.      * The username here is not the same as you account username and so is the password.
  410.      * The subject is the payment on whose behalf you have been authorized to make the operation call.
  411.      *
  412.      * @param string $Username 
  413.      * @param string $Password 
  414.      * @param string $Signature 
  415.      * @param string $Subject 
  416.      */
  417.     public static function registerSoapHeader($Username$Password$Signature$Subject '')
  418.     {
  419.         $Credentials     new SoapVar(PayPalTypes::UserIdPasswordType($Username$Password$Signature$Subject)SOAP_ENC_OBJECT);
  420.         $headerNameSpace 'urn:ebay:api:PayPalAPI';
  421.         $headerName      'RequesterCredentials';
  422.         $headerData      array("Credentials" =>  $Credentials);
  423.         $mustUnderstand  true;
  424.         
  425.         PayPalBase::setSoapHeader(new SoapHeader($headerNameSpace$headerName$headerData$mustUnderstand));
  426.     }
  427. }
  428.  
  429. /**
  430.  * Website Payments Pro Operations Factory
  431.  *
  432.  * The purpose of this class is to provide a way of selecting any operation object
  433.  * from just a single source. It also intializes the soapClient and soapHeaders that
  434.  * are going to be used for any of the operations.
  435.  * 
  436.  * @author Israel Ekpo <perfectvista@users.sourceforge.net>
  437.  * @copyright Copyright 2007, Israel Ekpo
  438.  * @license http://phppaypalpro.sourceforge.net/LICENSE.txt BSD License
  439.  * @package PaypalBase
  440.  */
  441. final class WebsitePaymentsPro
  442. {   
  443.     /**
  444.      * Prepares the System for an Operation call
  445.      *
  446.      * This is when the SoapClient and SoapHeader static objects are actually created.
  447.      * 
  448.      * @param string $Username The API user name
  449.      * @param string $Password The password for the API call. Different from account password
  450.      * @param string $Signature The signature for the 3-token authentication
  451.      * @param string $Subject The person on whose behalf the operation is made
  452.      * @uses PaypalRegistrar::registerSoapClient()
  453.      * @uses PaypalRegistrar::registerSoapHeader()
  454.      * @access public
  455.      */
  456.     public function prepare($Username$Password$Signature$Subject='')
  457.     {
  458.  
  459.        PayPalRegistrar::registerSoapHeader($Username$Password$Signature$Subject);      
  460.     }   
  461.     
  462.     /**
  463.      * WebSite Payments Pro Operations Factory
  464.      *
  465.      * The name of the operation is passed to this method so that
  466.      * it will return the right object to do the job. It is not case sensitive,
  467.      * so you do not have to worry about the case of the letters when passing the
  468.      * operation name to the method.
  469.      *  
  470.      * @access public
  471.      * @param string $operation This is case insensitive
  472.      * @return mixed The Operation you wish to call
  473.      */
  474.     public function selectOperation($operation)
  475.     {
  476.         switch (strtolower(trim($operation)))
  477.         {
  478.             case 'dodirectpayment':
  479.             {
  480.                 require_once('dodirectpayment.php');
  481.                 return new DoDirectPayment();
  482.             }
  483.                 
  484.             case 'setexpresscheckout':
  485.             {
  486.                 require_once('setexpresscheckout.php');
  487.                 return new SetExpressCheckout();
  488.             }
  489.             
  490.             case 'getexpresscheckoutdetails':
  491.             {
  492.                 require_once('getexpresscheckoutdetails.php');
  493.                 return new GetExpressCheckoutDetails();
  494.             }
  495.             
  496.             case 'doexpresscheckoutpayment':
  497.             {
  498.                 require_once('doexpresscheckoutpayment.php');
  499.                 return new DoExpressCheckoutPayment();
  500.             }
  501.             
  502.             case 'transactionsearch':
  503.             {
  504.                 require_once('transactionsearch.php');
  505.                 return new TransactionSearch();
  506.             }
  507.             
  508.             case 'gettransactiondetails':
  509.             {
  510.                 require_once('gettransactiondetails.php');
  511.                 return new GetTransactionDetails();
  512.             }
  513.         }
  514.     }
  515. }
  516. ?>

Documentation generated on Sat, 03 Feb 2007 20:59:03 -0800 by phpDocumentor 1.3.1