/**
  * Setup the smarty variables for the confirmation form. Also if the form
  * is submitted do the desired actions (close token, order etc.)
  *
  * @return array (if not redirected)
  */
 public function paymentConfirmation($token)
 {
     $this->_checkBeforeSend();
     $tokenRow = $this->_canUseToken((int) $token);
     if (empty($token) || !$tokenRow) {
         $this->_redirectToCardForm();
     }
     // check to see if some details on cart have changed in the meanwhile
     if ($amountOfCartHasChanged = $this->_tokenDetailsHaveChanged($tokenRow)) {
         $updateAmountOnCart = $this->_updateDetailsOfToken($token);
         if ($updateAmountOnCart) {
             Tools::redirect(Context::getContext()->link->getModuleLink($this->name, 'confirm', array('token' => $token, 'msg' => 410), true));
         } else {
             $this->_redirectToCardForm();
         }
     }
     $cart = Context::getContext()->cart;
     $customer = new Customer($cart->id_customer);
     $currency = new Currency($cart->id_currency);
     $params = array('amountInteger' => (int) Tools::ps_round($cart->getOrderTotal() * 100), 'amount' => (double) $cart->getOrderTotal(), 'form_action' => Context::getContext()->link->getModuleLink($this->name, 'confirm', array('token' => $token), true), 'cart' => $cart, 'customer' => $customer, 'tokenRow' => $tokenRow);
     if (1 || Tools::isSubmit('submitConfirm')) {
         //Set the API key
         try {
             Everypay\Everypay::setApiKey($this->sk);
         } catch (Exception $e) {
             $params['message'] = $e->getMessage();
             $params['status'] = self::ERRORNEOUS;
             $this->_closeToken($params);
             Tools::redirect(Context::getContext()->link->getPageLink('order', true, NULL, "step=3"));
         }
         $evPayParams = array('token' => $tokenRow['crd_token'], 'currency' => strtoupper($currency->iso_code));
         if (!empty($tokenRow['save_customer']) && $this->configuration['EVERYPAY_CUSTOMER_MODE'] && !empty($tokenRow['crd_token'])) {
             $evPayParams = array_merge($evPayParams, array('create_customer' => 1));
             $evCusUpdateParams = array('full_name' => $customer->firstname . ' ' . $customer->lastname, 'description' => Context::getContext()->shop->name . ' - ' . $this->l('Customer') . '#' . $customer->id, 'email' => $customer->email);
         } elseif (!empty($tokenRow['save_customer']) && !$this->configuration['EVERYPAY_CUSTOMER_MODE'] && !empty($tokenRow['crd_token'])) {
             $params['message'] = $this->l('The save card option got disabled during a payment proccess');
             $params['status'] = self::ERRORNEOUS;
             $this->_closeToken($params);
             $this->_redirectToCardForm();
         } elseif (!is_null($tokenRow['id_customer_token']) && !empty($tokenRow['cus_token']) && $this->configuration['EVERYPAY_CUSTOMER_MODE']) {
             $evPayParams['token'] = $tokenRow['cus_token'];
         } elseif (!is_null($tokenRow['id_customer_token']) && !empty($tokenRow['cus_token']) && !$this->configuration['EVERYPAY_CUSTOMER_MODE']) {
             $params['message'] = $this->l('The save card option got disabled during a payment proccess');
             $params['status'] = self::ERRORNEOUS;
             $this->_closeToken($params);
             $this->_redirectToCardForm();
         }
         try {
             $evPayParams = array_merge($evPayParams, array('payee_email' => $customer->email, 'amount' => $params['amountInteger'], 'description' => Context::getContext()->shop->name . ' - ' . $this->l('Cart') . ' #' . $cart->id . ' - ' . Tools::displayPrice($cart->getOrderTotal()), 'capture' => 0));
             $evPayment = Everypay\Payment::create($evPayParams);
             //now update the customer if necessary
             if (isset($evCusUpdateParams) && isset($evPayment->customer->token)) {
                 try {
                     $evCustomer = Everypay\Customer::update($evPayment->customer->token, $evCusUpdateParams);
                     $evPayParams['token'] = $evCustomer->token;
                 } catch (Exception $e) {
                     //we didnt manage to update the customer details
                     // no big deal
                 }
             }
             //error with the payment
             if (isset($evPayment->error)) {
                 $params['message'] = $evPayment->error->message;
                 $params['status'] = self::ERRORNEOUS;
                 $this->_closeToken($params);
                 $this->_redirectToCardForm(415);
                 exit;
             }
         } catch (Exception $e) {
             $params['message'] = $e->getMessage();
             $params['status'] = self::ERRORNEOUS;
             $this->_closeToken($params);
             $this->_redirectToCardForm(415);
             exit;
         }
         $mailVars = array();
         $validateOrder = $this->validateOrder($cart->id, self::ORDER_STATUS_CAPTURE_PENDING, $params['amount'], $this->displayName, NULL, $mailVars, $params['cart']->id_currency, false, $params['customer']->secure_key);
         if ($validateOrder) {
             $params['id_order'] = $this->currentOrder;
             $params['status'] = self::SUCCESS;
             $params['pmt_token'] = $evPayment->token;
             $params['message'] = 'Success on ' . date('d/m/Y H:i:s');
             if (!is_null($tokenRow['save_customer']) && $this->configuration['EVERYPAY_CUSTOMER_MODE']) {
                 $id_customer_token = $this->_insertCardCustomer($evPayment->customer);
                 if ($id_customer_token) {
                     $params['id_customer_token'] = $id_customer_token;
                 }
             }
             $_closeToken = $this->_closeToken($params);
             $order = new Order((int) $this->currentOrder);
             //capture or die
             try {
                 $evCapture = Everypay\Payment::capture($evPayment->token);
                 $order->setCurrentState(Configuration::get('PS_OS_PAYMENT'));
                 $redirect = array('controller=order-confirmation', 'id_cart=' . (int) $params['cart']->id, 'id_module=' . (int) $this->id, 'id_order=' . (int) $this->currentOrder, 'key=' . $order->secure_key);
                 Tools::redirect('index.php?' . implode('&', $redirect));
             } catch (Exception $e) {
                 $this->_redirectToCardForm();
             }
         } else {
             try {
                 $evRefund = Everypay\Payment::refund($evPayment->token);
             } catch (Exception $e) {
                 //do nothing
             }
             $this->_redirectToCardForm();
         }
     }
     $this->context->smarty->assign($params);
     return $params;
 }
 function plgVmConfirmedOrder(VirtueMartCart $cart, $order)
 {
     if (!($this->_currentMethod = $this->getVmPluginMethod($order['details']['BT']->virtuemart_paymentmethod_id))) {
         return null;
         // Another method was selected, do nothing
     }
     if (!$this->selectedThisElement($this->_currentMethod->payment_element)) {
         return FALSE;
     }
     $session = JFactory::getSession();
     $return_context = $session->getId();
     // Prepare data that should be stored in the database
     $dbValues['order_number'] = $order['details']['BT']->order_number;
     $dbValues['virtuemart_order_id'] = $order['details']['BT']->virtuemart_order_id;
     $dbValues['payment_method_id'] = $order['details']['BT']->virtuemart_paymentmethod_id;
     $dbValues['return_context'] = $return_context;
     $dbValues['payment_name'] = parent::renderPluginName($this->_currentMethod);
     $dbValues['cost_per_transaction'] = $this->_currentMethod->cost_per_transaction;
     $dbValues['cost_percent_total'] = $this->_currentMethod->cost_percent_total;
     $dbValues['payment_order_total'] = $totalInPaymentCurrency['value'];
     $dbValues['payment_currency'] = $payment_currency_id;
     $this->debugLog("before store", "plgVmConfirmedOrder", 'debug');
     if ($this->_currentMethod->sandbox) {
         Everypay\Everypay::$isTest = true;
     }
     Everypay\Everypay::setApiKey($this->getSecretKey());
     $token = $this->getToken();
     $response = Everypay\Payment::create(array('token' => $token, 'description' => 'Order #' . $order['details']['BT']->order_number));
     if (isset($response->error)) {
         $new_status = $this->_currentMethod->payment_declined_status;
         $this->_handlePaymentCancel($order['details']['BT']->virtuemart_order_id, $html);
         return;
         // will not process the order
     }
     $dbValues['everypay_response_token'] = $response->token;
     $dbValues['everypay_response_description'] = $response->description;
     $dbValues['everypay_response_status'] = $response->status;
     $dbValues['everypay_response_last_four'] = $response->last_four;
     $dbValues['everypay_response_holder_name'] = $response->holder_name;
     $dbValues['payment_order_total'] = number_format($response->amount / 100, 2);
     $this->storePSPluginInternalData($dbValues);
     $cart->emptyCart();
     $session = JFactory::getSession();
     $session->clear('everypay_token', 'vm');
 }