public function confirm()
 {
     ini_set('html_errors', 0);
     if (!$this->session->data['payment_method']['code'] == 'divido') {
         return false;
     }
     $this->load->model('payment/divido');
     $this->load->language('payment/divido');
     $api_key = $this->config->get('divido_api_key');
     $deposit = $this->request->post['deposit'];
     $finance = $this->request->post['finance'];
     $country = $this->session->data['payment_address']['iso_code_2'];
     $language = strtoupper($this->language->get('code'));
     if ($this->is22) {
         $currency = strtoupper($this->config->get('config_currency'));
     } else {
         $currency = strtoupper($this->currency->getCode());
     }
     $order_id = $this->session->data['order_id'];
     if ($this->customer->isLogged()) {
         $this->load->model('account/customer');
         $customer_info = $this->model_account_customer->getCustomer($this->customer->getId());
         $firstname = $customer_info['firstname'];
         $lastname = $customer_info['lastname'];
         $email = $customer_info['email'];
         $telephone = $customer_info['telephone'];
     } elseif (isset($this->session->data['guest'])) {
         $firstname = $this->session->data['guest']['firstname'];
         $lastname = $this->session->data['guest']['lastname'];
         $email = $this->session->data['guest']['email'];
         $telephone = $this->session->data['guest']['telephone'];
     }
     $postcode = $this->session->data['payment_address']['postcode'];
     $products = array();
     foreach ($this->cart->getProducts() as $product) {
         $products[] = array('type' => 'product', 'text' => $product['name'], 'quantity' => $product['quantity'], 'value' => $product['price']);
     }
     if ($this->is22) {
         list($total, $totals) = $this->model_payment_divido->getOrderTotals22();
     } else {
         list($total, $totals) = $this->model_payment_divido->getOrderTotals();
     }
     $sub_total = $total;
     $cart_total = $this->cart->getSubTotal();
     $shiphandle = $sub_total - $cart_total;
     $products[] = array('type' => 'product', 'text' => 'Shipping & Handling', 'quantity' => 1, 'value' => $shiphandle);
     $deposit_amount = round($deposit / 100 * $total, 2, PHP_ROUND_HALF_UP);
     $response_url = $this->url->link('payment/divido/update', '', true);
     $redirect_url = $this->url->link('checkout/success', '', true);
     $checkout_url = $this->url->link('checkout/checkout', '', true);
     $salt = uniqid('', true);
     $hash = $this->model_payment_divido->hashOrderId($order_id, $salt);
     $this->model_payment_divido->saveLookup($order_id, $salt);
     $request_data = array('merchant' => $api_key, 'deposit' => $deposit_amount, 'finance' => $finance, 'country' => $country, 'language' => $language, 'currency' => $currency, 'metadata' => array('order_id' => $order_id, 'order_hash' => $hash), 'customer' => array('title' => '', 'first_name' => $firstname, 'middle_name' => '', 'last_name' => $lastname, 'country' => $country, 'postcode' => $postcode, 'email' => $email, 'mobile_number' => '', 'phone_number' => $telephone), 'products' => $products, 'response_url' => $response_url, 'redirect_url' => $redirect_url, 'checkout_url' => $checkout_url);
     $response = Divido_CreditRequest::create($request_data);
     if ($response->status == 'ok') {
         $data = array('status' => 'ok', 'url' => $response->url);
     } else {
         $data = array('status' => 'error', 'message' => $this->language->get($response->error));
     }
     $this->response->setOutput(json_encode($data));
 }
 /**
  * Start Standard Checkout and dispatching customer to divido
  */
 public function startAction()
 {
     $resource = Mage::getSingleton('core/resource');
     $readConnection = $resource->getConnection('core_read');
     $table = $resource->getTableName('core_config_data');
     $query = "select value from {$table} where path = 'payment/pay/api_key'";
     $api_encode = $readConnection->fetchOne($query);
     $apiKey = Mage::helper('core')->decrypt($api_encode);
     Divido::setMerchant($apiKey);
     $quote_cart = Mage::getModel('checkout/cart')->getQuote();
     $checkout_session = Mage::getSingleton('checkout/session');
     $quote_id = $checkout_session->getQuoteId();
     $quote_session = $checkout_session->getQuote();
     $quote_session_data = $quote_session->getData();
     $deposit_percentage = $this->getRequest()->getParam('divido_deposit') / 100;
     $finance = $this->getRequest()->getParam('divido_finance');
     $language = strtoupper(substr(Mage::getStoreConfig('general/locale/code', Mage::app()->getStore()->getId()), 0, 2));
     $currency = Mage::app()->getStore()->getCurrentCurrencyCode();
     $shipAddr = $quote_session->getShippingAddress();
     $shipping = $shipAddr->getData();
     $postcode = $shipping['postcode'];
     $telephone = $shipping['telephone'];
     $firstname = $shipping['firstname'];
     $lastname = $shipping['lastname'];
     $country = $shipping['country_id'];
     $email = $quote_session_data['customer_email'];
     $middlename = $quote_session_data['customer_middlename'];
     $item_quote = Mage::getModel('checkout/cart')->getQuote();
     $items_in_cart = $item_quote->getAllItems();
     $products = array();
     foreach ($items_in_cart as $item) {
         $item_qty = $item->getQty();
         $item_value = $item->getPrice();
         $products[] = array("type" => "product", "text" => $item->getName(), "quantity" => $item_qty, "value" => $item_value);
     }
     $totals = Mage::getSingleton('checkout/session')->getQuote()->getTotals();
     $grand_total = $totals['grand_total']->getValue();
     foreach ($totals as $total) {
         if (in_array($total->getCode(), array('subtotal', 'grand_total'))) {
             continue;
         }
         $products[] = array('type' => 'product', 'text' => $total->getTitle(), 'quantity' => 1, 'value' => $total->getValue());
     }
     $deposit = round($deposit_percentage * $grand_total, 2);
     $salt = uniqid('', true);
     $quote_hash = Mage::helper('pay')->hashQuote($salt, $quote_id);
     $customer = array('title' => '', 'first_name' => $firstname, 'middle_name' => $middlename, 'last_name' => $lastname, 'country' => $country, 'postcode' => $postcode, 'email' => $email, 'mobile_number' => '', 'phone_number' => $telephone);
     $metadata = array('quote_id' => $quote_id, 'quote_hash' => $quote_hash);
     $request_data = array('merchant' => $apiKey, 'deposit' => $deposit, 'finance' => $finance, 'country' => $country, 'language' => $language, 'currency' => $currency, 'metadata' => $metadata, 'customer' => $customer, 'products' => $products, 'response_url' => Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB) . 'divido_callback.php', 'checkout_url' => Mage::helper('checkout/url')->getCheckoutUrl(), 'redirect_url' => Mage::getUrl('pay/payment/return', array('quote_id' => $quote_id)));
     if (Mage::getStoreConfig('payment/pay/debug')) {
         Mage::log('Request: ' . json_encode($request_data), Zend_Log::DEBUG, 'divido.log', true);
     }
     $response = Divido_CreditRequest::create($request_data);
     if (Mage::getStoreConfig('payment/pay/debug')) {
         Mage::log('Response: ' . $response->__toJSON(), Zend_Log::DEBUG, 'divido.log', true);
     }
     if ($response->status == 'ok') {
         $lookup = Mage::getModel('callback/lookup');
         $lookup->setQuoteId($quote_id);
         $lookup->setSalt($salt);
         $lookup->setCreditRequestId($response->id);
         $lookup->setDepositAmount($deposit);
         $existing_lookup = Mage::getModel('callback/lookup')->load($quote_id, 'quote_id');
         if ($existing_lookup->getId()) {
             $lookup->setId($existing_lookup->getId());
         }
         if (Mage::getStoreConfig('payment/pay/debug')) {
             Mage::log('Lookup: ' . json_encode($lookup->getData()), Zend_Log::DEBUG, 'divido.log', true);
         }
         $lookup->save();
         $this->getResponse()->setRedirect($response->url);
         return;
     } else {
         if ($response->status === 'error') {
             Mage::getSingleton('checkout/session')->addError($response->error);
             $this->_redirect('checkout/cart');
         }
     }
 }