public function getUrl()
 {
     $session = Mage::getSingleton('core/session');
     if (!$session->getWirecardCheckoutPageRedirectUrl()) {
         $params = $this->getFormFields();
         $client = new Zend_Http_Client();
         $response = $client->setUri(Phoenix_WirecardCheckoutPage_Helper_Configuration::getWcpFrontendUrl() . '/init-server.php')->setMethod(Zend_Http_Client::POST)->setParameterPost($params)->request();
         $responseKeyValuePairs = explode('&', $response->getBody());
         $response = array();
         foreach ($responseKeyValuePairs as $responseKeyValuePair) {
             $responseEntry = explode('=', $responseKeyValuePair);
             $response[urldecode($responseEntry[0])] = urldecode($responseEntry[1]);
         }
         if (!isset($response['redirectUrl'])) {
             if (isset($response['consumerMessage'])) {
                 $message = $response['consumerMessage'];
             } else {
                 $message = Mage::helper('wirecard_checkout_page')->__('An error during the payment process occurred.');
             }
             if (isset($response['message'])) {
                 Mage::log($this->_wirecardCheckoutPageInitUrl . ' request failed: ' . $response['message']);
             } else {
                 Mage::log($this->_wirecardCheckoutPageInitUrl . ': No valid Wirecard Checkout Page response.');
             }
             throw new Exception($message);
         } else {
             $session->setWirecardCheckoutPageRedirectUrl($response['redirectUrl']);
         }
     }
     return $session->getWirecardCheckoutPageRedirectUrl();
 }
 /**
  * Process the seamless Payment after Order is complete
  *
  * @param Varien_Event_Observer $observer
  *
  * @throws Exception
  * @return Phoenix_WirecardCheckoutPage_Model_Observer
  */
 public function salesOrderPaymentPlaceEnd(Varien_Event_Observer $observer)
 {
     /**
      * @var Phoenix_WirecardCheckoutPage_Model_Abstract
      */
     $payment = $observer->getPayment();
     $this->_order = $payment->getOrder();
     $storeId = $this->_order->getStoreId();
     $paymentInstance = $payment->getMethodInstance();
     if (Mage::getStoreConfigFlag('payment/' . $payment->getMethod() . '/useSeamless', $storeId)) {
         $storageId = $payment->getAdditionalData();
         $orderIdent = $this->_order->getQuoteId();
         $customerId = Mage::getStoreConfig('payment/' . $payment->getMethod() . '/customer_id', $storeId);
         $shopId = Mage::getStoreConfig('payment/' . $payment->getMethod() . '/shop_id', $storeId);
         $secretKey = Mage::getStoreConfig('payment/' . $payment->getMethod() . '/secret_key', $storeId);
         $serviceUrl = Mage::getUrl(Mage::getStoreConfig('payment/' . $payment->getMethod() . '/service_url', $storeId));
         $paymentType = $this->_getMappedPaymentCode($payment->getMethod());
         $returnurl = Mage::getUrl('wirecard_checkout_page/processing/checkresponse', array('_secure' => true, '_nosid' => true));
         $pluginVersion = WirecardCEE_Client_QPay_Request_Initiation::generatePluginVersion('Magento', Mage::getVersion(), $paymentInstance->getPluginName(), $paymentInstance->getPluginVersion());
         $initiation = new WirecardCEE_Client_QPay_Request_Initiation($customerId, $shopId, $secretKey, substr(Mage::app()->getLocale()->getLocaleCode(), 0, 2), $pluginVersion);
         $consumerData = new WirecardCEE_Client_QPay_Request_Initiation_ConsumerData();
         if (Mage::getStoreConfigFlag('payment/' . $payment->getMethod() . '/send_additional_data', $storeId)) {
             $consumerData->setEmail($this->_order->getCustomerEmail());
             $dob = $payment->getMethodInstance()->getCustomerDob();
             if ($dob) {
                 $consumerData->setBirthDate($dob);
             }
             $consumerData->addAddressInformation($this->_getBillingObject());
             if ($this->_order->hasShipments()) {
                 $consumerData->addAddressInformation($this->_getShippingObject());
             }
         }
         if ($payment->getMethod() == 'wirecard_checkout_page_invoice' || $payment->getMethod() == 'wirecard_checkout_page_installment') {
             $consumerData->setEmail($this->_order->getCustomerEmail());
             $dob = $payment->getMethodInstance()->getCustomerDob();
             if ($dob) {
                 $consumerData->setBirthDate($dob);
             } else {
                 throw new Exception('Invalid dob');
             }
             $consumerData->addAddressInformation($this->_getBillingObject('invoice'));
         }
         $consumerData->setIpAddress($this->_order->getRemoteIp());
         $consumerData->setUserAgent(Mage::app()->getRequest()->getServer('HTTP_USER_AGENT'));
         $initiation->setConfirmUrl(Mage::getUrl('wirecard_checkout_page/processing/seamlessConfirm', array('_secure' => true, '_nosid' => true)));
         $initiation->setWindowName('paymentIframe');
         $initiation->orderId = $this->_order->getIncrementId();
         $initiation->companyTradeRegistryNumber = $payment->getMethodInstance()->getCompanyTradeRegistrationNumber();
         if ($orderIdent && $storageId) {
             $initiation->setStorageReference($orderIdent, $storageId);
         }
         if (Mage::getStoreConfigFlag('payment/' . $payment->getMethod() . '/auto_deposit', $storeId)) {
             $initiation->setAutoDeposit(true);
         }
         $initiation->setOrderReference($this->_order->getIncrementId());
         $financialInstitution = $payment->getMethodInstance()->getFinancialInstitution();
         if ($financialInstitution) {
             $initiation->setFinancialInstitution($financialInstitution);
         }
         Phoenix_WirecardCheckoutPage_Helper_Configuration::configureWcsLibrary();
         $response = $initiation->initiate(round($this->_order->getBaseGrandTotal(), 2), $this->_order->getBaseCurrencyCode(), $paymentType, $this->_order->getIncrementId(), $returnurl, $returnurl, $returnurl, $returnurl, $serviceUrl, $consumerData);
         if (isset($response) && $response->getStatus() == WirecardCEE_Client_QPay_Response_Initiation::STATE_SUCCESS) {
             $payment->setAdditionalData(serialize($payment->getAdditionalData()))->save();
             Mage::getSingleton('core/session')->unsetData('wirecard_checkout_page_payment_info');
             Mage::getSingleton('core/session')->setWirecardCheckoutPageRedirectUrl(urldecode($response->getRedirectUrl()));
         } elseif (isset($response)) {
             $errorMessage = '';
             foreach ($response->getErrors() as $error) {
                 $errorMessage .= ' ' . $error->getMessage();
             }
             throw new Exception(trim($errorMessage));
         } else {
             $payment->setAdditionalData(serialize($payment->getAdditionalData()))->save();
             Mage::getSingleton('core/session')->unsetData('wirecard_checkout_page_payment_info');
         }
     }
     return $this;
 }
 /**
  * @param $customerId
  * @param $storeId
  * @param $shopId
  * @param $methodCode
  * @return WirecardCEE_Client_DataStorage_Response_Read|bool
  */
 public function readWirecardCEE($customerId, $storeId, $shopId, $methodCode)
 {
     Phoenix_WirecardCheckoutPage_Helper_Configuration::configureWcsLibrary();
     $secretKey = Mage::getStoreConfig('payment/' . $methodCode . '/secret_key', $storeId);
     $session = Mage::getSingleton('checkout/session');
     $order = Mage::getModel('sales/order');
     $order->loadByIncrementId($session->getLastRealOrderId());
     $payment = $order->getPayment();
     $storageId = $payment->getAdditionalData();
     $dataStorageRead = new WirecardCEE_Client_DataStorage_Request_Read($customerId, $shopId, $secretKey);
     try {
         $response = $dataStorageRead->read($storageId);
         if ($response->getStatus() != WirecardCEE_Client_DataStorage_Response_Read::STATE_FAILURE) {
             return $response;
         } else {
             Mage::getSingleton('checkout/session')->setData('wirecard_checkout_page_data_storage_enabled', false);
             $dsErrors = $response->getErrors();
             foreach ($dsErrors as $error) {
                 Mage::log($error->getMessage(), true, 'wirecard_checkout_page_exception.log');
             }
             return false;
         }
     } catch (WirecardCEE_Exception $e) {
         //communication with dataStorage failed. we choose a none dataStorage fallback
         Mage::getSingleton('checkout/session')->setData('wirecard_checkout_page_data_storage_enabled', false);
         Mage::logException($e);
         return false;
     }
 }