Source for file PayPalPro.class.php

  1. <?php
  2. /**
  3.     * This class encapsulates all of the functions for interacting with PayPal Pro. Requires PHP 5.
  4.     * @author Richard Sharp
  5.     * @copyright 2011
  6.     * @version 1.0
  7.     * @package Payment
  8. */
  9.     class Gateway
  10.     {
  11.         /********************************* Public Variables **********************************************/
  12.         
  13.         
  14.         /**
  15.         * Gets or sets the customer's address.
  16.         *    @var string 
  17.         */
  18.         public $Address="";
  19.         
  20.         /**
  21.         * Sets the ammount to charge.
  22.         *    @var decimal 
  23.         */
  24.         public $Amount=0;
  25.         
  26.         /**
  27.         * Sets the URL to return to if the customer clicks the cancel link in PayPal.
  28.         *    @var string 
  29.         */
  30.         public $CancelURL="";
  31.         
  32.         /**
  33.         * Sets the credit card number.
  34.         *    @var string 
  35.         */
  36.         public $CardNumber="";
  37.         
  38.         /**
  39.         * Gets or sets the customer's city.
  40.         *    @var string 
  41.         */
  42.         public $City="";
  43.         
  44.         /**
  45.         * Gets or sets the customer's country.
  46.         *    @var string 
  47.         */
  48.         public $Country="US";
  49.         
  50.         /**
  51.         * Sets the currency code. For a list of valid currency codes, see: https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_api_nvp_currency_codes
  52.         *    @var string 
  53.         */        
  54.         public $CurrencyCode="USD";
  55.         
  56.         /**
  57.         * Gets or sets the custom order information.
  58.         *    @var string 
  59.         */
  60.         public $Custom="";
  61.         
  62.         /**
  63.         * Sets the order description.
  64.         *    @var string 
  65.         */
  66.         public $Description="";
  67.         
  68.         /**
  69.         * Gets or sets the customer's email address.
  70.         *    @var string 
  71.         */
  72.         public $Email="";
  73.         
  74.         /**
  75.         * Sets the credit card expiration month.
  76.         *    @var int 
  77.         */
  78.         public $ExpirationMonth="";
  79.         
  80.         /**
  81.         * Sets the credit card expiration year.
  82.         *    @var int 
  83.         */
  84.         public $ExpirationYear="";
  85.         
  86.         /**
  87.         * Gets or sets the customer's first name.
  88.         *    @var string 
  89.         */
  90.         public $FirstName="";
  91.         
  92.         /**
  93.         * Sets the client's PayPal API Username.
  94.         *    @var string 
  95.         */
  96.         public $GatewayMerchantID="";
  97.         
  98.         /**
  99.         * The client's PayPal API Password
  100.         *    @var string 
  101.         */
  102.         public $GatewayPassword="";
  103.         
  104.         /**
  105.         * The client's PayPal API Signature
  106.         *    @var string 
  107.         */
  108.         public $GatewaySignature="";
  109.         
  110.         /**
  111.         * Gets or sets the invoice number.
  112.         *    @var string 
  113.         */
  114.         public $InvoiceNumber="";
  115.         
  116.         /**
  117.         * Sets the URL for Instant Payment Notifications.
  118.         *    @var string 
  119.         */
  120.         public $IPN="";
  121.         
  122.         /**
  123.         * Gets or sets the customer's last name.
  124.         *    @var string 
  125.         */
  126.         public $LastName="";
  127.         
  128.         /**
  129.         * Gets any special notes entered by the client in their PayPal account, returned from the GetExpressCheckoutDetails function.
  130.         *    @var string 
  131.         */
  132.         public $Note="";
  133.         
  134.         /**
  135.         * Gets or sets the customer's PayPal Payer ID.
  136.         *    @var string 
  137.         */
  138.         public $PayerID="";
  139.         
  140.         /**
  141.         * Gets or sets the customer's phone number.
  142.         *    @var string 
  143.         */
  144.         public $Phone="";
  145.         
  146.         /**
  147.         * Sets the URL to return to from PayPal once the customer completes the transaction.
  148.         *    @var string 
  149.         */
  150.         public $ReturnURL="";
  151.         
  152.         /**
  153.         * Sets the credit card security code (CVV2).
  154.         *    @var string 
  155.         */
  156.         public $SecurityCode="";
  157.         
  158.         /**
  159.         * Sets the shipping amount of the order.
  160.         *    @var decimal 
  161.         */
  162.         public $Shipping=0;
  163.         
  164.         /**
  165.         * Sets whether or not to show the shipping fields in PayPal.
  166.         *    @var bool 
  167.         */
  168.         public $ShowShipping=true;
  169.         
  170.         /**
  171.         * Gets or sets the customer's state.
  172.         *    @var string 
  173.         */
  174.         public $State="";
  175.         
  176.         /**
  177.         * Sets the sub-total of the order.
  178.         *    @var decimal 
  179.         */
  180.         public $SubTotal=0;
  181.         
  182.         /**
  183.         * Sets the sales tax amount.
  184.         *    @var decimal 
  185.         */
  186.         public $Tax=0;
  187.         
  188.         /**
  189.         * Sets whether or not to use test mode. If true, the transaction will be sent to the PayPal sandbox.
  190.         *    @var bool 
  191.         */
  192.         public $Test=false;
  193.         
  194.         /**
  195.         * Gets or sets the Toekn used by PayPal for express checkout.
  196.         *    @var string 
  197.         */
  198.         public $Token="";
  199.         
  200.         /**
  201.         * Gets or sets the transaction ID.
  202.         *    @var string 
  203.         */
  204.         public $TransactionID="";
  205.         
  206.         /**
  207.         * Gets or sets the customer's zip/postal code.
  208.         *    @var string 
  209.         */
  210.         public $Zip="";
  211.         
  212.         
  213.         //////////////////// public return variables ////////////////////////////
  214.                         
  215.         /**
  216.         * Gets the returned approval code.
  217.         *    @var string 
  218.         */
  219.         public $ApprovalCode="";
  220.         
  221.         /**
  222.         * Gets the last error that ocurred.
  223.         *    @var string 
  224.         */
  225.         public $LastError="";
  226.         
  227.         /**
  228.         * Gets the last request that was sent to PayPal in raw format.
  229.         *    @var string 
  230.         */
  231.         public $LastRequest="";
  232.         
  233.         /**
  234.         * Gets the last response returned from PayPal in raw format.
  235.         *    @var string 
  236.         */
  237.         public $LastResponse="";
  238.         
  239.         /**
  240.         * Gets the reason for the last response.
  241.         *    @var string 
  242.         */
  243.         public $ResponseReason="";
  244.         
  245.         /********************************* Private Variables **********************************************/
  246.         /**
  247.         * @access private
  248.         */
  249.         private $sTestAPIEndpoint "https://api-3t.sandbox.paypal.com/nvp";
  250.         /**
  251.         * @access private
  252.         */
  253.         private $sTestPayPalURL "https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&token=";
  254.         /**
  255.         * @access private
  256.         */
  257.         private $sProductionAPIEndpoint "https://api-3t.paypal.com/nvp";
  258.         /**
  259.         * @access private
  260.         */
  261.         private $sProductionPayPalURL "https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=";
  262.         
  263.          
  264.         
  265.         /********************************* Public Methods **********************************************/
  266.         
  267.         /**
  268.          * Constructor for the class.
  269.          * @param string The client's API Username with PayPal.
  270.          * @param string The client's API Password with PayPal.
  271.          * @param string The client's API Signature with PayPal.
  272.          * @return void 
  273.          */ 
  274.         function __construct($GatewayMerchantID,$GatewayPassword,$GatewaySignature
  275.         {
  276.             $this->GatewayMerchantID=$GatewayMerchantID;
  277.             $this->GatewayPassword=$GatewayPassword;
  278.             $this->GatewaySignature=$GatewaySignature;
  279.         }
  280.         
  281.         /**
  282.         * Processes a direct credit card payment. Returns true on successful payment, otherwise false.
  283.         * @return bool 
  284.         */ 
  285.         public function ProcessTransaction()
  286.         {
  287.             try
  288.             {
  289.                 $aErrors=array();
  290.                 //clear the request and response fields
  291.                 $this->ClearLog();
  292.                 
  293.                 //validate the credit card expiration date
  294.                 $iExpirationTime=mktime(0,0,0,$this->ExpirationMonth,1,$this->ExpirationYear);
  295.                 if($iExpirationTime==-1
  296.                 {
  297.                     throw new Exception("The expiration date is invalid.");
  298.                 }
  299.                 elseif($iExpirationTime<mktime(0,0,0,date("m")-1,1,date("Y"))) 
  300.                 {
  301.                     throw new Exception("The credit card expiration date has already passed.");
  302.                 }
  303.                 
  304.                 //validate the credit card number and determine the card type
  305.                 if(preg_match("/^4[0-9]{12}(?:[0-9]{3})?$/",$this->CardNumber)) $sCardType="Visa"//All Visa card numbers start with a 4. New cards have 16 digits. Old cards have 13. 
  306.                 elseif(preg_match("/^5[1-5][0-9]{14}$/",$this->CardNumber)) $sCardType="MasterCard"// All MasterCard numbers start with the numbers 51 through 55. All have 16 digits. 
  307.                 elseif(preg_match("/^3[47][0-9]{13}$/",$this->CardNumber)) $sCardType="Amex"// American Express card numbers start with 34 or 37 and have 15 digits. 
  308.                 elseif(preg_match("/^6(?:011|5[0-9]{2})[0-9]{12}$/",$this->CardNumber)) $sCardType="Discover"//Discover card numbers begin with 6011 or 65. All have 16 digits.
  309.                 else throw new Exception("The credit card number is not valid.");
  310.                 
  311.                 //validate other fields
  312.                 if(!$this->Amount$aErrors[]="The amount to charge is required.";
  313.                 elseif($this->Amount<.5$aErrors[]="The amount to charge is required.";
  314.                 if(!$this->GatewayMerchantID$aErrors[]="The GatewayMerchantID is required.";
  315.                 if(!$this->GatewayPassword$aErrors[]="The GatewayPassword is required.";
  316.                 if(!$this->CardNumber$aErrors[]="The credit card number is required.";
  317.                 if(!$this->SecurityCode$aErrors[]="The security code is required.";
  318.                 if(!$this->FirstName$aErrors[]="The first name is required.";
  319.                 if(!$this->LastName$aErrors[]="The last name is required.";
  320.                 if(!$this->Address$aErrors[]="The address is required.";
  321.                 if(!$this->City$aErrors[]="The city is required.";
  322.                 if(!$this->State$aErrors[]="The state is required.";
  323.                 if(!$this->Zip$aErrors[]="The zip code is required.";
  324.                 
  325.                 if(count($aErrors)>0throw new Exception(implode(", ",$aErrors));
  326.                 //sanitize the credit card number
  327.                 $this->CardNumber=preg_replace("/[^0-9]/","",$this->CardNumber);
  328.                 
  329.                 $aFields=array();
  330.                 //transaction method
  331.                 $aFields["METHOD"]="DoDirectPayment";
  332.                 
  333.                 //information specific to this transaction
  334.                 $aFields["AMT"]=$this->Amount;
  335.                 $aFields["CURRENCYCODE"]=$this->CurrencyCode;
  336.                 $aFields["PAYMENTACTION"]="Sale";
  337.                 $aFields["CREDITCARDTYPE"]=$sCardType;
  338.                 $aFields["ACCT"]=$this->CardNumber;
  339.                 $aFields["EXPDATE"]=date("mY",$iExpirationTime);
  340.                 $aFields["CVV2"]=$this->SecurityCode;
  341.                 $aFields["FIRSTNAME"]=$this->FirstName;
  342.                 $aFields["LASTNAME"]=$this->LastName;
  343.                 $aFields["STREET"]=$this->Address;
  344.                 $aFields["CITY"]=$this->City;
  345.                 $aFields["STATE"]=$this->State;
  346.                 $aFields["COUNTRYCODE"]=$this->Country;
  347.                 $aFields["ZIP"]=preg_replace("/[^0-9A-Za-z]/","",$this->Zip);
  348.                 $aFields["IPADDRESS"]=$_SERVER['REMOTE_ADDR'];
  349.                 $aFields["SHIPPINGAMT"]=$this->Shipping;
  350.                 $aFields["TAXAMT"]=$this->Tax;
  351.                 if($this->Description$aFields["DESC"]=$this->Description;
  352.                 if($this->Custom$aFields["CUSTOM"]=$this->Custom;
  353.                 $aFields["INVNUM"]=$this->InvoiceNumber;
  354.                 if($this->IPN$aFields["NOTIFYURL"]=$this->IPN;
  355.                 $aFields["ITEMAMT"]=$this->SubTotal;
  356.                 
  357.                 //submit the request to PayPal
  358.                 $this->SubmitRequest($aFields);
  359.                 
  360.                 //break the response into an associative array
  361.                 $aResponseParts=explode("&",$this->LastResponse);
  362.                 $aReturn=array();
  363.                 if(count($aResponseParts)>0)
  364.                 {
  365.                     foreach($aResponseParts as $sResponsePart)
  366.                     {
  367.                         list($sKey,$sValue)=explode("=",$sResponsePart,2);
  368.                         $aReturn[urldecode($sKey)]=urldecode($sValue);
  369.                     }
  370.                 }
  371.                 else throw new Exception("An invalid response was returned.");
  372.                 $sResult strtoupper($aReturn["ACK"]);
  373.                 if($sResult=="SUCCESS" || $sResult=="SUCCESSWITHWARNING")
  374.                 {
  375.                     $this->TransactionID=$aReturn["TRANSACTIONID"];
  376.                     return true;
  377.                 }
  378.                 else
  379.                 {
  380.                     $this->ResponseReason=$aReturn["L_LONGMESSAGE0"];
  381.                     return false;
  382.                 }
  383.                 
  384.             catch(Exception $e{
  385.                 $this->LastError=$e->getMessage();
  386.                 $this->ResponseReason=$this->LastError;
  387.                 return false;
  388.             }            
  389.             return false;
  390.             
  391.  
  392.         }
  393.         
  394.         /**
  395.         * Completes a previously begun Express Checkout transaction. Returns true on success, false on failure.
  396.         * @return bool 
  397.         */ 
  398.         public function CompleteExpressCheckout()
  399.         {
  400.             try {
  401.                 //clear the request and response fields
  402.                 $this->ClearLog();
  403.                 
  404.                 $aFields["METHOD"]="DoExpressCheckoutPayment";
  405.                 $aFields["TOKEN"]=$this->Token;
  406.                 $aFields["PAYERID"]=$this->PayerID;
  407.                 $aFields["PAYMENTREQUEST_0_PAYMENTACTION"]="Sale";
  408.                 $aFields["PAYMENTREQUEST_0_AMT"]=$this->Amount;
  409.                 $aFields["PAYMENTREQUEST_0_CURRENCYCODE"]=$this->CurrencyCode;
  410.                 $aFields["IPADDRESS"]=$_SERVER['REMOTE_ADDR'];
  411.                 if(isset($this->SubTotal)) $aFields["PAYMENTREQUEST_0_ITEMAMT"]=$this->SubTotal;
  412.                 if(isset($this->Shipping)) $aFields["PAYMENTREQUEST_0_SHIPPINGAMT"]=$this->Shipping;
  413.                 if(isset($this->Tax)) $aFields["PAYMENTREQUEST_0_TAXAMT"]=$this->Tax;
  414.                 $aFields["PAYMENTREQUEST_0_DESC"]=$this->Description;
  415.                 if(isset($this->InvoiceNumber)) $aFields["PAYMENTREQUEST_0_INVNUM"]=$this->InvoiceNumber;
  416.                 if(isset($this->IPN)) $aFields["PAYMENTREQUEST_0_NOTIFYURL"]=$this->IPN;
  417.                 if(isset($this->Custom)) $aFields["PAYMENTREQUEST_0_CUSTOM"]=$this->Custom;
  418.                 
  419.                 //submit the request to PayPal
  420.                 $this->SubmitRequest($aFields);
  421.                 
  422.                 //break the response into an associative array
  423.                 $aResponseParts=explode("&",$this->LastResponse);
  424.                 $aReturn=array();
  425.                 if(count($aResponseParts)>0)
  426.                 {
  427.                     foreach($aResponseParts as $sResponsePart)
  428.                     {
  429.                         list($sKey,$sValue)=explode("=",$sResponsePart,2);
  430.                         $aReturn[urldecode($sKey)]=urldecode($sValue);
  431.                     }
  432.                 }
  433.                 else throw new Exception("An invalid response was returned.");
  434.                 $sResult strtoupper($aReturn["ACK"]);
  435.                 if($sResult=="SUCCESS" || $sResult=="SUCCESSWITHWARNING")
  436.                 {
  437.                     if($aResult["PAYMENTINFO_0_PAYMENTSTATUS"]=="Completed")
  438.                     {
  439.                         $this->TransactionID=$aResult["PAYMENTINFO_0_TRANSACTIONID"];
  440.                         return true;
  441.                     }
  442.                     else
  443.                     {
  444.                         if($aReturn["PAYMENTINFO_0_PENDINGREASON"]=="address"$sReason="The payment is pending because your customer did not include a confirmed shipping address and your Payment Receiving Preferences is set such that you want to manually accept or deny each of these payments. To change your preference, go to the Preferences section of your Profile. ";
  445.                         elseif($aReturn["PAYMENTINFO_0_PENDINGREASON"]=="echeck"$sReason="The payment is pending because it was made by an eCheck that has not yet cleared. ";
  446.                         elseif($aReturn["PAYMENTINFO_0_PENDINGREASON"]=="intl"$sReason="The payment is pending because you hold a non-U.S. account and do not have a withdrawal mechanism. You must manually accept or deny this payment from your Account Overview.         ";
  447.                         elseif($aReturn["PAYMENTINFO_0_PENDINGREASON"]=="multi-currency"$sReason="You do not have a balance in the currency sent, and you do not have your Payment Receiving Preferences set to automatically convert and accept this payment. You must manually accept or deny this payment. ";
  448.                         elseif($aReturn["PAYMENTINFO_0_PENDINGREASON"]=="verify"$sReason="The payment is pending because you are not yet verified. You must verify your account before you can accept this payment. ";
  449.                         else $sReason="The payment is pending for an unknown reason. For more information, contact PayPal customer service.";
  450.                         $sMessage='The order was submitted, but payment has not yet been completed. You will be notified once payment has been completed. '.$sReason;
  451.                         throw new Exception($sMessage);
  452.                     }
  453.                 }
  454.                 else
  455.                 {
  456.                     $this->ResponseReason=$aReturn["L_LONGMESSAGE0"];
  457.                     return false;
  458.                 }
  459.             catch(Exception $e{
  460.                 $this->LastError=$e->getMessage();
  461.                 $this->ResponseReason=$this->LastError;
  462.                 return false;
  463.             }                
  464.         }
  465.         
  466.         /**
  467.         * Retrieves information from PayPal regarding the current express checkout user's account. Returns true on success, false on failure.
  468.         * @param string The Token received from PayPal. The Token is originally generated by the SetExpressCheckout method, but is sent back in a GET variable from PayPal upon return. Returns true on success, otherwise false.
  469.         * @return bool 
  470.         */ 
  471.         public function GetExpressCheckoutDetails($Token)
  472.         {
  473.             try {
  474.                 //clear the request and response fields
  475.                 $this->ClearLog();
  476.                 
  477.                 $aFields["METHOD"]="GetExpressCheckoutDetails";
  478.                 $aFields["TOKEN"]=$Token;
  479.                 
  480.                 //submit the request to PayPal
  481.                 $this->SubmitRequest($aFields);
  482.                 
  483.                 //break the response into an associative array
  484.                 $aResponseParts=explode("&",$this->LastResponse);
  485.                 $aReturn=array();
  486.                 if(count($aResponseParts)>0)
  487.                 {
  488.                     foreach($aResponseParts as $sResponsePart)
  489.                     {
  490.                         list($sKey,$sValue)=explode("=",$sResponsePart,2);
  491.                         $aReturn[urldecode($sKey)]=urldecode($sValue);
  492.                     }
  493.                 }
  494.                 else throw new Exception("An invalid response was returned.");
  495.                 $sResult strtoupper($aReturn["ACK"]);
  496.                 if($sResult=="SUCCESS" || $sResult=="SUCCESSWITHWARNING")
  497.                 {
  498.                     $this->Email=$aReturn["EMAIL"];
  499.                     $this->FirstName=$aReturn["FIRSTNAME"];
  500.                     $this->LastName=$aReturn["LASTNAME"];
  501.                     $this->Country=$aReturn["COUNTRYCODE"];
  502.                     $this->Address=$aReturn["SHIPTOSTREET"];
  503.                     $this->City=$aReturn["SHIPTOCITY"];
  504.                     $this->State=$aReturn["SHIPTOSTATE"];
  505.                     $this->Zip=$aReturn["SHIPTOZIP"];
  506.                     $this->PayerID=$aReturn["PAYERID"];
  507.                     $this->Custom=$aReturn["CUSTOM"];
  508.                     $this->Note=$aReturn["PAYMENTREQUEST_0_NOTETEXT"];
  509.                     return true;
  510.                 }
  511.                 else
  512.                 {
  513.                     $this->ResponseReason=$aReturn["L_LONGMESSAGE0"];
  514.                     return false;
  515.                 }
  516.             catch(Exception $e{
  517.                 $this->LastError=$e->getMessage();
  518.                 $this->ResponseReason=$this->LastError;
  519.                 return false;
  520.             }                
  521.         }
  522.         
  523.         
  524.         /**
  525.         * Notifies PayPal to begin an Express checkout, and generates the token. Returns true on successful payment, otherwise false.
  526.         * @return bool 
  527.         */ 
  528.         public function SetExpressCheckout()
  529.         {
  530.             try
  531.             {
  532.                 $aErrors=array();
  533.                 //clear the request and response fields
  534.                 $this->ClearLog();
  535.                 
  536.                 
  537.                 if(count($aErrors)>0throw new Exception(implode(", ",$aErrors));
  538.                 
  539.                 
  540.                 $aFields=array();
  541.                 $aFields["METHOD"]="SetExpressCheckout";
  542.                 
  543.                 //information specific to this transaction
  544.                 $aFields["PAYMENTREQUEST_0_AMT"]=$this->Amount;
  545.                 $aFields["PAYMENTREQUEST_0_CURRENCYCODE"]=$this->CurrencyCode;
  546.                 $aFields["PAYMENTREQUEST_0_PAYMENTACTION"]="Sale";
  547.                 $aFields["RETURNURL"]=$this->ReturnURL;
  548.                 $aFields["CANCELURL"]=$this->CancelURL;
  549.                 $aFields["NOSHIPPING"]=($this->ShowShipping?0:1);
  550.                 if(isset($this->LastName)) $aFields["PAYMENTREQUEST_0_SHIPTONAME"]=trim($this->FirstName." ".$this->LastName);
  551.                 if(isset($this->Address)) $aFields["PAYMENTREQUEST_0_SHIPTOSTREET"]=$this->Address;
  552.                 if(isset($this->City)) $aFields["PAYMENTREQUEST_0_SHIPTOCITY"]=$this->City;
  553.                 if(isset($this->State)) $aFields["PAYMENTREQUEST_0_SHIPTOSTATE"]=$this->State;
  554.                 if(isset($this->Zip)) $aFields["PAYMENTREQUEST_0_SHIPTOZIP"]=$this->Zip;
  555.                 if(isset($this->Country)) $aFields["PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE"]=$this->Country;
  556.                 if(isset($this->Email)) $aFields["EMAIL"]=$this->Email;
  557.                 if(isset($this->SubTotal)) $aFields["PAYMENTREQUEST_0_ITEMAMT"]=$this->SubTotal;
  558.                 if(isset($this->Shipping)) $aFields["PAYMENTREQUEST_0_SHIPPINGAMT"]=$this->Shipping;
  559.                 if(isset($this->Tax)) $aFields["PAYMENTREQUEST_0_TAXAMT"]=$this->Tax;
  560.                 if(isset($this->Description)) $aFields["PAYMENTREQUEST_0_DESC"]=$this->Description;
  561.                 if(isset($this->Custom)) $aFields["PAYMENTREQUEST_0_CUSTOM"]=$this->Custom;
  562.                 if(isset($this->InvoiceNumber)) $aFields["PAYMENTREQUEST_0_INVNUM"]=$this->InvoiceNumber;
  563.                 if(isset($this->IPN)) $aFields["PAYMENTREQUEST_0_NOTIFYURL"]=$this->IPN;
  564.                 
  565.                 //submit the request to PayPal
  566.                 $this->SubmitRequest($aFields);
  567.                 
  568.                 //break the response into an associative array
  569.                 $aResponseParts=explode("&",$this->LastResponse);
  570.                 $aReturn=array();
  571.                 if(count($aResponseParts)>0)
  572.                 {
  573.                     foreach($aResponseParts as $sResponsePart)
  574.                     {
  575.                         list($sKey,$sValue)=explode("=",$sResponsePart,2);
  576.                         $aReturn[urldecode($sKey)]=urldecode($sValue);
  577.                     }
  578.                 }
  579.                 else throw new Exception("An invalid response was returned.");
  580.                 $sResult strtoupper($aReturn["ACK"]);
  581.                 if($sResult=="SUCCESS" || $sResult=="SUCCESSWITHWARNING")
  582.                 {
  583.                     $this->Token=$aReturn["TOKEN"];
  584.                     return true;
  585.                 }
  586.                 else
  587.                 {
  588.                     $this->ResponseReason=$aReturn["L_LONGMESSAGE0"];
  589.                     return false;
  590.                 }
  591.                 
  592.             catch(Exception $e{
  593.                 $this->LastError=$e->getMessage();
  594.                 $this->ResponseReason=$this->LastError;
  595.                 return false;
  596.             }            
  597.             return false;
  598.             
  599.  
  600.         }
  601.         
  602.         
  603.         /********************************* Private Methods **********************************************/
  604.         /*
  605.         @access private
  606.         */
  607.         
  608.         private function ClearLog()
  609.         {
  610.             $this->LastRequest="";
  611.             $this->LastResponse="";
  612.             $this->LastError="";
  613.         }
  614.         
  615.         /*
  616.         @access private
  617.         */
  618.         private function SubmitRequest($aFields)
  619.         {
  620.             
  621.             //authentication information
  622.             $aFields["VERSION"]="65";
  623.             $aFields["PWD"]=$this->GatewayPassword;
  624.             $aFields["USER"]=$this->GatewayMerchantID;
  625.             $aFields["SIGNATURE"]=$this->GatewaySignature;
  626.             
  627.             //encode the values to ensure they are passed properly
  628.             foreach($aFields as $key => $value
  629.             if(($key=="CANCELURL"|| ($key=="RETURNURL")) $aFields[$key]=urlencode($key)."=".$value;
  630.             else $aFields[$key]=urlencode($key)."=".urlencode($value);
  631.             
  632.             //initialize CURL and set the options
  633.             $oCURL curl_init();
  634.             $sURL=(true==$this->Test?$this->sTestAPIEndpoint:$this->sProductionAPIEndpoint);
  635.             curl_setopt($oCURLCURLOPT_URL,$sURL);
  636.             curl_setopt($oCURLCURLOPT_VERBOSE1);
  637.             curl_setopt($oCURLCURLOPT_RETURNTRANSFER,1);
  638.             curl_setopt($oCURLCURLOPT_POST1);
  639.     
  640.             //turning off the server and peer verification
  641.             curl_setopt($oCURLCURLOPT_SSL_VERIFYPEERFALSE);
  642.             curl_setopt($oCURLCURLOPT_SSL_VERIFYHOSTFALSE);
  643.             
  644.             //setting the nvpreq as POST FIELD to curl
  645.             $this->LastRequest=implode("&",$aFields);
  646.             curl_setopt($oCURLCURLOPT_POSTFIELDS$this->LastRequest);
  647.     
  648.             //getting response from server
  649.             $sResponse curl_exec($oCURL);
  650.             if (curl_errno($oCURL)) 
  651.             {
  652.                 throw new Exception("An error ocurred while communicating with PayPal.");
  653.             
  654.             else 
  655.             {
  656.                 //Close the CURL handle.
  657.                 curl_close($oCurl);
  658.             }
  659.             $this->LastResponse=$sResponse;
  660.         }
  661.     }
  662. ?>

Documentation generated on Wed, 26 Jan 2011 15:24:44 -0700 by phpDocumentor 1.4.3