Development

Changeset 12620

You must first sign up to be able to contribute.

Changeset 12620

Show
Ignore:
Timestamp:
11/03/08 22:57:49 (5 years ago)
Author:
davec
Message:

Updated exception and error handling in CIM API library

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfAuthorizeNetCIMPlugin/lib/sfAuthorizeNetCIM.class.php

    r12590 r12620  
    99*/ 
    1010 
    11 class CIMAPIException extends Exception  { 
     11//FIXME add package.xml dependancys for SimpleXML and CURL 
     12//FIXME make debugging hook existing symfony logging/debugging infrastrucure 
     13 
     14class CIMAPIException extends sfException  { 
     15  public function __construct($msg) { 
     16    parent::__construct($msg, 0); 
     17  } 
     18
     19 
     20 
     21class CIMDirectResponseException extends CIMAPIException { 
    1222  public function __construct($msg) { 
    1323    parent::__construct($msg); 
     
    2737} 
    2838 
    29 class DeclinedCardException extends CIMAPIException { 
     39class CIMDeclinedCardException extends CIMAPIException { 
    3040  public function __construct($msg) { 
    3141    parent::__construct($msg); 
     
    4454 
    4555  public function addShippingAddressList($sp) { 
    46     //FIXME validate 
     56    if( ! isset($pp) || ! $pp instanceof AuthorizeNetCIMCustomerShippingAddress ) { 
     57                        throw(new CIMAPIException("Must pass in a AuthorizeNetCIMCustomerShippingAddress object")); 
     58    } 
    4759    $this->shippingAddressList[] = $sp; 
    4860  } 
    4961 
    5062  public function addCustomerPaymentProfile($pp) { 
    51     //FIXME validate adding only payment profile objects 
     63    if( ! isset($pp) || ! $pp instanceof AuthorizeNetCIMCustomerPaymentProfile ) { 
     64                        throw(new CIMAPIException("Must pass in a AuthorizeNetCIMCustomerPaymentProfile object")); 
     65    } 
    5266    $this->paymentProfilesList[] = $pp; 
    5367  } 
     
    5569        public function __set($key,$val) { 
    5670                if( ! isset($this->$key) ) { 
    57                         throw(new Exception("ERROR: Non-existant setter: '$key'")); 
     71                        throw(new CIMAPIException("ERROR: Non-existant setter: '$key'")); 
    5872                } 
    5973                if( isset($val) ) { 
     
    6478        public function __get($key) { 
    6579                if( ! isset($this->$key) ) { 
    66                         throw(new Exception("ERROR: Non-existant getter: '$key'")); 
     80                        throw(new CIMAPIException("ERROR: Non-existant getter: '$key'")); 
    6781                } 
    6882                return $this->$key; 
     
    100114        public function __set($key,$val) { 
    101115                if( ! isset($this->$key) ) { 
    102                         throw(new Exception("ERROR: Non-existant setter: '$key'")); 
     116                        throw(new CIMAPIException("ERROR: Non-existant setter: '$key'")); 
    103117                } 
    104118 
     
    110124        public function __get($key) { 
    111125                if( ! isset($this->$key) ) { 
    112                         throw(new Exception("ERROR: Non-existant getter: '$key'")); 
     126                        throw(new CIMAPIException("ERROR: Non-existant getter: '$key'")); 
    113127                } 
    114128                return $this->$key; 
     
    133147        public function __set($key,$val) { 
    134148                if( ! isset($this->$key) ) { 
    135                         throw(new Exception("ERROR: Non-existant setter: '$key'")); 
     149                        throw(new CIMAPIException("ERROR: Non-existant setter: '$key'")); 
    136150                } 
    137151                if( isset($val) ) { 
     
    142156        public function __get($key) { 
    143157                if( ! isset($this->$key) ) { 
    144                         throw(new Exception("ERROR: Non-existant getter: '$key'")); 
     158                        throw(new CIMAPIException("ERROR: Non-existant getter: '$key'")); 
    145159                } 
    146160                return $this->$key; 
     
    183197        public function __set($key,$val) { 
    184198                if( ! isset($this->$key) ) { 
    185                         throw(new Exception("ERROR: Non-existant setter: '$key'")); 
     199                        throw(new CIMAPIException("ERROR: Non-existant setter: '$key'")); 
    186200                } 
    187201                if( isset($val) ) { 
     
    192206        public function __get($key) { 
    193207                if( ! isset($this->$key) ) { 
    194                         throw(new Exception("ERROR: Non-existant getter: '$key'")); 
     208                        throw(new CIMAPIException("ERROR: Non-existant getter: '$key'")); 
    195209                } 
    196210                return $this->$key; 
     
    243257    if( ! isset($customerProfileObject) || ! $customerProfileObject) { 
    244258      throw(new CIMAPIException("ERROR: Must pass in a customerProfileObject")); 
     259    } 
     260 
     261    $paymentProfilesList = $customerProfileObject->paymentProfilesList; 
     262    if( count($paymentProfilesList) == 0 ) { 
     263      throw(new CIMAPIException("The customer profile must have at least one payment profile added to it for authorize.net to accept.")); 
    245264    } 
    246265 
     
    252271    $content .= "    " . $this->makeXML('email', $customerProfileObject->email); 
    253272    $content .= "        <paymentProfiles>\n"; 
    254     $paymentProfilesList = $customerProfileObject->paymentProfilesList; 
    255273    foreach( $paymentProfilesList as $pp ) { 
    256274      $content .= $this->getXMLForPaymentProfile($pp); 
    257  
    258275    } 
    259276    $content .= "        </paymentProfiles>\n"; 
     
    292309    //Send XML and get parsed response back 
    293310    $parsed = $this->sendXMLAndParseResponse($content); //may throw an exception if somthing went wrong 
     311 
     312    $customerPaymentProfileObject->customerPaymentProfileId = $parsed->customerPaymentProfileId; 
    294313  } 
    295314 
     
    396415 
    397416    //Send XML and get parsed response back 
    398     $parsed = $this->sendXMLAndParseResponse($content, false); //may throw an exception if somthing went wrong 
     417    $parsed = $this->sendXMLAndParseResponse($content, false ); //may throw an exception if somthing went wrong 
     418     
    399419    if ("Ok" != $parsed->messages->resultCode) { 
    400420      if( ! isset($parsed->directResponse) ) { 
     
    410430      } 
    411431    } 
    412  
    413  
    414     //FIXME parse the CSV data 
     432     
     433 
    415434    $customerTransactionObject->directResponse = (string) $parsed->directResponse; 
    416435    $directResponseFields = explode(',', $customerTransactionObject->directResponse); 
     
    427446      $customerTransactionObject->responseReasonText = $directResponseFields[3]; 
    428447 
    429       throw(new DeclinedCardException($errorMessage)); 
     448      throw(new CIMDeclinedCardException($errorMessage)); 
    430449 
    431450    } else if( $directResponseFields[0] == 3 ) { //error 
     
    686705 
    687706 
    688   private function sendXMLAndParseResponse($content, $throwErrorCodeExceptions = true) { 
     707  private function sendXMLAndParseResponse($content, $checkResultCode = true) { 
    689708    $this->lastRequestXML = $content; 
    690709    $this->lastResponseXML = ''; 
     
    696715                $this->debugPrint("response = " . $this->lastResponseXML . "\n"); 
    697716 
    698                 $parsed = $this->parseAPIResponse($this->lastResponseXML, $throwErrorCodeExceptions);//throws exception if somthing went wrong 
     717                $parsed = $this->parseAPIResponse( $this->lastResponseXML, $checkResultCode );//throws exception if somthing went wrong 
    699718    $this->lastParsedResponseObject = $parsed; 
    700719 
     
    717736 
    718737 
    719   private function parseAPIResponse($response, $throwErrorCodeExceptions = true ) { 
     738  private function parseAPIResponse($response, $checkResultCode = true ) { 
    720739    $matches = array(); 
    721740    preg_match('/^.*?(<\?xml.*)$/m', $response, $matches); 
     
    725744    #$parsedresponse = simplexml_load_string($response); 
    726745 
    727     if ("Ok" != $parsedresponse->messages->resultCode) { 
    728       $err = "The operation failed with the following errors:\n"; 
     746    if ($checkResultCode && "Ok" != $parsedresponse->messages->resultCode) { 
     747      $err = "$checkResultCode: The operation failed with the following errors:\n"; 
    729748      foreach ($parsedresponse->messages->message as $msg) { 
    730749        $err .= "[" . $msg->code . "] " . $msg->text . "\n"; 
     
    732751      $err .= "\n"; 
    733752 
    734       if( $throwErrorCodeExceptions ) { 
     753 
     754      if( isset($parsedresponse->validationDirectResponse) ) { 
     755        $directResponseFields = explode(',', (string)$parsedresponse->validationDirectResponse); 
     756        throw(new CIMDirectResponseException($directResponseFields[3])); 
     757 
     758      } else if( isset($parsedresponse->directResponse) ) { 
     759        $directResponseFields = explode(',', (string)$parsedresponse->directResponse); 
     760        throw(new CIMDirectResponseException($directResponseFields[3])); 
     761 
     762      } else { 
     763 
    735764        throw(new CIMErrorResultCodeException($err)); 
    736765      }