Пример #1
0
 /**
  *  Send request to gateway
  *
  *  @param    Mage_Chronopay_Model_Gateway_Request
  *  @return	  mixed
  */
 protected function _postRequest(Mage_Chronopay_Model_Gateway_Request $request)
 {
     $result = AO::getModel('chronopay/gateway_result');
     $client = new Varien_Http_Client();
     $url = $this->getConfigData('cgi_url');
     $client->setUri($url ? $url : self::CGI_URL);
     $client->setConfig(array('maxredirects' => 0, 'timeout' => 30));
     $client->setParameterPost($request->getData());
     $client->setMethod(Zend_Http_Client::POST);
     if ($this->getConfigData('debug_flag')) {
         $debug = AO::getModel('chronopay/api_debug')->setRequestBody($client->getUri() . "\n" . print_r($request->getData(), 1))->save();
     }
     try {
         $response = $client->request();
         $body = $response->getRawBody();
         if (preg_match('/(T\\|(.+)\\|[\\r\\n]{0,}){0,1}(Y\\|(.+)?|\\|)|(N\\|(.+[\\r\\n]{0,}.+){0,})/', $body, $matches)) {
             $transactionId = $matches[2];
             $message = isset($matches[4]) ? trim($matches[4], '|') : '';
             if (isset($matches[5], $matches[6])) {
                 $result->setError($matches[6]);
                 AO::throwException($matches[6]);
             }
             if ($message == 'Completed') {
                 $result->setTransaction($request->getTransaction());
             }
             if (strlen($transactionId)) {
                 $result->setTransaction($transactionId);
             }
             if (!$result->getTransaction()) {
                 AO::throwException(AO::helper('chronopay')->__('Transaction ID is invalid'));
             }
         } else {
             AO::throwException(AO::helper('chronopay')->__('Invalid response format'));
         }
         if ($this->getConfigData('debug_flag')) {
             $debug->setResponseBody($body)->save();
         }
     } catch (Exception $e) {
         $result->setResponseCode(-1)->setResponseReasonCode($e->getCode())->setResponseReasonText($e->getMessage());
         $exceptionMsg = AO::helper('chronopay')->__('Gateway request error: %s', $e->getMessage());
         if ($this->getConfigData('debug_flag')) {
             $debug->setResponseBody($body)->save();
         }
         AO::throwException($exceptionMsg);
     }
     return $result;
 }