public function __invoke($layout, $post, $amount = null)
 {
     $token = $this->filterString($post['everypayToken']);
     $this->setUpClient();
     $params = array('token' => $token);
     try {
         $response = Customer::create($params);
     } catch (\Exception $e) {
         $response = new \stdClass();
         $response->error = new \stdClass();
         $response->error->message = $e->getMessage();
     }
     if (isset($response->error)) {
         return $layout->render('customer/error', array('errorMessage' => $response->error->message));
     }
     return $layout->render('customer/success', array('token' => $response->token));
 }
 /**
  * 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()
 {
     global $cart;
     global $smarty;
     global $link;
     $this->_checkBeforeSend();
     $token = (int) Tools::getValue('token');
     $tokenRow = $this->_canUseToken($token);
     if (empty($token) || empty($tokenRow) || !$tokenRow) {
         $this->_redirectToCardForm();
     }
     // check to see if some details on cart have changed in the meanwhile
     if ($this->_tokenDetailsHaveChanged($tokenRow)) {
         $updateAmountOnCart = $this->_updateDetailsOfToken($token);
         if ($updateAmountOnCart) {
             Tools::redirect(_MODULE_DIR_ . $this->name . '/controllers/front/confirm.php?msg=410&token=' . $token);
         } else {
             $this->_redirectToCardForm();
         }
     }
     $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(), 'EVERYPAY_FORM_ACTION' => _MODULE_DIR_ . $this->name . '/controllers/front/confirm.php?token=' . $token, 'cart' => $cart, 'customer' => $customer, 'tokenRow' => $tokenRow);
     //bypass the submit state
     if (1 || Tools::isSubmit('submitConfirm')) {
         //Set the API key
         try {
             Everypay::setApiKey($this->configuration['EVERYPAY_SECRET_KEY']);
         } catch (Exception $e) {
             $params['message'] = $e->getMessage();
             $params['status'] = self::ERRORNEOUS;
             $this->_closeToken($params);
             Tools::redirect(__PS_BASE_URI__ . 'order.php?step=3');
         }
         $evPayParams = array('token' => $tokenRow['crd_token'], 'currency' => strtoupper($currency->iso_code));
         $shopname = Configuration::get('PS_SHOP_NAME');
         if (!empty($tokenRow['save_customer']) && $this->configuration['EVERYPAY_CUSTOMER_MODE'] && !empty($tokenRow['crd_token'])) {
             $evCusParams = array_merge($evPayParams, array('full_name' => $customer->firstname . ' ' . $customer->lastname, 'description' => $shopname . ' - ' . $this->l('Customer') . '#' . $customer->id, 'email' => $customer->email));
             try {
                 $evCustomer = EverypayCustomer::create($evCusParams);
                 $evPayParams['token'] = $evCustomer->token;
             } catch (Exception $e) {
                 $params['message'] = $e->getMessage();
                 $params['status'] = self::ERRORNEOUS;
                 $this->_closeToken($params);
                 $this->_redirectToCardForm();
             }
         } 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' => $shopname . ' - ' . $this->l('Cart') . '#' . $cart->id . ' - ' . Tools::displayPrice($cart->getOrderTotal())));
             //var_dump($evPayParams);
             $evPayment = EverypayPayment::create($evPayParams);
             //error with the payment
             if (isset($evPayment->error)) {
                 $params['message'] = $evPayment->error->message;
                 $params['status'] = self::ERRORNEOUS;
                 $this->_closeToken($params);
                 $this->_redirectToCardForm(415);
             }
         } catch (Exception $e) {
             $params['message'] = $e->getMessage();
             $params['status'] = self::ERRORNEOUS;
             $this->_closeToken($params);
             $this->_redirectToCardForm(415);
         }
         $mailVars = array();
         $validateOrder = $this->validateOrder($params['cart']->id, Configuration::get('PS_OS_PAYMENT'), $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($evCustomer);
                 if ($id_customer_token) {
                     $params['id_customer_token'] = $id_customer_token;
                 }
             }
             $_closeToken = $this->_closeToken($params);
             $redirect = array('id_cart=' . (int) $params['cart']->id, 'id_module=' . (int) $this->id, 'id_order=' . (int) $this->currentOrder, 'key=' . $params['customer']->secure_key);
             Tools::redirect(__PS_BASE_URI__ . 'order-confirmation.php?' . implode('&', $redirect));
         } else {
             $this->_redirectToCardForm();
         }
     }
     $smarty->assign($params);
     return $this->_fetchTemplate('views/templates/front/confirm.tpl');
 }