/**
  * Return the appropriate result type based off of the transaction.
  * @return Moneris_Result|Moneris_3DSecureResult
  */
 public static function factory(Moneris_Transaction $transaction)
 {
     $params = $transaction->params();
     if (in_array($params['type'], array('txn', 'acs'))) {
         return new Moneris_3DSecureResult($transaction);
     } else {
         return new Moneris_Result($transaction);
     }
 }
 /**
  * Do the curl call to process the API request.
  *
  * @param Moneris_Transaction $transaction
  * @return SimpleXMLElement
  */
 protected static function _call_api(Moneris_Transaction $transaction)
 {
     $gateway = $transaction->gateway();
     $config = self::config($gateway->environment());
     $params = $transaction->params();
     // frig... this MPI stuff is leaking gross code everywhere... needs to be refactored
     if (in_array($params['type'], array('txn', 'acs'))) {
         $config['url'] = '/mpi/servlet/MpiServlet';
     }
     $url = $config['protocol'] . '://' . $config['host'] . ':' . $config['port'] . $config['url'];
     $xml = str_replace(' </', '</', $transaction->to_xml());
     //var_dump($url, $xml);
     // this is pulled directly from mpgClasses.php
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_HEADER, 0);
     curl_setopt($ch, CURLOPT_POST, 1);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
     curl_setopt($ch, CURLOPT_TIMEOUT, $config['timeout']);
     curl_setopt($ch, CURLOPT_USERAGENT, $config['api_version']);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
     $response = curl_exec($ch);
     curl_close($ch);
     // if the response fails for any reason, just use some stock XML
     // also taken directly from mpgClasses:
     if (!$response) {
         return simplexml_load_string(self::$_error_response);
     }
     $xml = @simplexml_load_string($response);
     // they sometimes return HTML formatted Apache errors... NICE.
     if ($xml === false) {
         return simplexml_load_string(self::$_error_response);
     }
     // force fail AVS for testing
     //$xml->receipt->AvsResultCode = 'N';
     // force fail CVD for testing
     //$xml->receipt->CvdResultCode = '1N';
     //var_dump($xml);
     return $xml;
 }