function generate_begateway_form($order_id)
   {
       //creates a self-submitting form to pass the user through to the beGateway server
       global $woocommerce;
       $order = new WC_order($order_id);
       if ('yes' == $this->debug) {
           $this->log->add('begateway', 'Generating payment form for order ' . $order->get_order_number());
       }
       // Order number & Cart Contents for description field - may change
       $item_loop = 0;
       //grab the langauge
       $lang = explode('-', get_bloginfo('language'));
       $lang = $lang[0];
       if (in_array($lang, \beGateway\Language::getSupportedLanguages())) {
           $language = $lang;
       } else {
           $language = 'en';
       }
       $token = new \beGateway\GetPaymentToken();
       if ($this->transaction_type == 'authorization') {
           $token->setAuthorizationTransactionType();
       }
       $token->money->setCurrency(get_woocommerce_currency());
       $token->money->setAmount($order->order_total);
       $token->setDescription(__('Order', 'woocommerce') . ' # ' . $order->get_order_number());
       $token->setTrackingId(ltrim($order->get_order_number(), '#'));
       $token->customer->setFirstName($order->billing_first_name);
       $token->customer->setLastName($order->billing_last_name);
       $token->customer->setCountry($order->billing_country);
       $token->customer->setCity($order->billing_city);
       $token->customer->setPhone($order->billing_phone);
       $token->customer->setZip($order->billing_postcode);
       $token->customer->setAddress($order->billing_address_1 . $order->billing_address_2);
       $token->customer->setEmail($order->billing_email);
       if (in_array($order->billing_country, array('US', 'CA'))) {
           $token->customer->setState($order->billing_state);
       }
       $token->setSuccessUrl(esc_url_raw($this->get_return_url($order)));
       $token->setDeclineUrl(esc_url_raw($order->get_cancel_order_url_raw()));
       $token->setFailUrl(esc_url_raw($order->get_cancel_order_url_raw()));
       $token->setCancelUrl(esc_url_raw($order->get_cancel_order_url_raw()));
       $token->setNotificationUrl($this->notify_url);
       $token->setLanguage($language);
       $token->setAddressHidden();
       if ('yes' == $this->debug) {
           $this->log->add('begateway', 'Requesting token for order ' . $order->get_order_number());
       }
       $response = $token->submit();
       if (!$response->isSuccess()) {
           if ('yes' == $this->debug) {
               $this->log->add('begateway', 'Unable to get payment token on order: ' . $order_id . 'Reason: ' . $response->getMessage());
           }
           $woocommerce->add_error(__('Unable to contact the payment server at this time. Please try later.'));
           $woocommerce->add_error($response->getMessage());
           exit;
       }
       //now look to the result array for the token
       if ($response->getToken()) {
           $payment_url = $response->getRedirectUrlScriptName();
           update_post_meta(ltrim($order->get_order_number(), '#'), '_Token', $token);
           if ('yes' == $this->debug) {
               $this->log->add('begateway', 'Token received, forwarding customer to: ' . $payment_url);
           }
       } else {
           $woocommerce->add_error(__('Payment error: ') . $response->getMessage());
           if ('yes' == $this->debug) {
               $this->log->add('begateway', 'Payment error order ' . $order_id . '  ' . $error_to_show);
           }
           exit('Sorry - there was an error contacting the bank server, please try later');
       }
       wc_enqueue_js('
   jQuery("body").block({
     message: "' . __('Thank you for your order. We are now redirecting you to make the payment.', 'woocommerce-begateway') . '",
       overlayCSS: {
         background: "#fff",
         opacity: 0.6
       },
       css: {
         padding:        20,
         textAlign:      "center",
         color:          "#555",
         border:         "3px solid #aaa",
         backgroundColor:"#fff",
         cursor:         "wait",
         lineHeight:		"32px"
       }
   });
   jQuery("#submit_begateway_payment_form").click();
 ');
       return '<form action="' . $payment_url . '" method="post" id="begateway_payment_form">
   <input type="hidden" name="token" value="' . $response->getToken() . '">
   <input type="submit" class="button-alt" id="submit_begateway_payment_form" value="' . __('Make payment', 'woocommerce-begateway') . '" /> <a class="button cancel" href="' . $order->get_cancel_order_url() . '">' . __('Cancel order &amp; restore cart', 'woothemes') . '</a>
   </form>';
   }
 public function hookPayment($params)
 {
     global $smarty, $cookie;
     if (!$this->active) {
         return;
     }
     $err_msg = '';
     $customer = new Customer((int) $params['cart']->id_customer);
     $address = new Address(intval($params['cart']->id_address_invoice));
     $country = Country::getIsoById((int) $address->id_country);
     $lang_iso_code = strtolower(Language::getIsoById((int) $cookie->id_lang));
     $sp_lang = \beGateway\Language::getSupportedLanguages();
     if (!in_array($lang_iso_code, $sp_lang)) {
         $lang_iso_code = 'en';
     }
     $shop_ptype = trim(Configuration::get('BEGATEWAY_SHOP_PAYTYPE'));
     $currency = new Currency((int) $params['cart']->id_currency);
     $currency_code = trim($currency->iso_code);
     $amount = $params['cart']->getOrderTotal(true, 3);
     $return_base_url = (Configuration::get('PS_SSL_ENABLED') ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . __PS_BASE_URI__ . 'modules/' . $this->name . '/validation.php?';
     $callbackurl = $return_base_url . 'action=callback';
     $callbackurl = str_replace('carts.local', 'webhook.begateway.com:8443', $callbackurl);
     $cancelurl = (Configuration::get('PS_SSL_ENABLED') ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . __PS_BASE_URI__;
     $successurl = $return_base_url . 'action=success&id_cart=' . (int) $params['cart']->id . '&id_module=' . (int) $this->id . '&key=' . $customer->secure_key;
     $failurl = $return_base_url . 'action=fail';
     $state_val = NULL;
     if (in_array($country, array('US', 'CA'))) {
         $state = new State((int) $address->id_state);
         if (Validate::isLoadedObject($state)) {
             $state_val = $state->iso_code;
         } else {
             $state_val = 'NA';
         }
     }
     $phone = $address->phone ? $address->phone : $address->phone_mobile;
     $transaction = new \beGateway\GetPaymentToken();
     if ($shop_ptype == 'authorization') {
         $transaction->setAuthorizationTransactionType();
     } else {
         $transaction->setPaymentTransactionType();
     }
     $transaction->money->setCurrency($currency_code);
     $transaction->money->setAmount($amount);
     $transaction->setDescription($this->l('Order No. ') . $params['cart']->id);
     $transaction->setTrackingId($params['cart']->id);
     $transaction->setLanguage($lang_iso_code);
     $transaction->setNotificationUrl($callbackurl);
     $transaction->setSuccessUrl($successurl);
     $transaction->setDeclineUrl($failurl);
     $transaction->setFailUrl($failurl);
     $transaction->setCancelUrl($cancelurl);
     $transaction->customer->setFirstName($address->firstname);
     $transaction->customer->setLastName($address->lastname);
     $transaction->customer->setCountry($country);
     $transaction->customer->setAddress($address->address1 . ' ' . $address->address2);
     $transaction->customer->setCity($address->city);
     $transaction->customer->setZip($address->postcode);
     $transaction->customer->setEmail($cookie->email);
     $transaction->customer->setPhone($phone);
     $transaction->customer->setState($state_val);
     $transaction->setAddressHidden();
     $paymentUrl = '';
     try {
         $response = $transaction->submit();
         if ($response->isError()) {
             $err_msg .= $response->getMessage();
         }
         if ($response->isSuccess()) {
             $paymentUrl = $response->getRedirectUrl();
         }
     } catch (Exception $e) {
         $err_msg .= $e->getMessage();
     }
     $smarty->assign(array('paymentUrl' => $paymentUrl, 'err_msg' => $err_msg, 'this_path' => $this->_path));
     $template = 'beGateway.tpl';
     if (_PS_VERSION_ < 1.6) {
         $template = 'beGateway_1_5.tpl';
     }
     return $this->display(__FILE__, $template);
 }