<?php
class ControllerPaymentPaymentDefault extends Controller {
	protected function index() {
		$this->data['button_confirm']   = $this->language->get('button_confirm');
		$this->data['continue']         = $this->url->link('checkout/success');
		$this->data['captcha_tpl'] 		= $this->getChild('payment/captcha');
	}

	public function app(){
		$this->document->addScriptApp(array(
			'catalog/view/theme/appmobile/js/payment.redirect.js'
		));
		$this->data['scriptsApp'] = json_encode($this->document->getScriptsApp());
	}

	public function validate(){
        $this->load->dmodel('checkout/order');

		$error = array();

        $data_atual = new DateTime($this->db->dataAtual(), new DateTimeZone('America/Sao_Paulo'));
        $data_atual = $data_atual->format("Y-m-d");
        $count_customer_orders_day = $this->dmodel_checkout_order->getOrdersByCustommer($this->customer->getId(),$data_atual,$data_atual);
        $count_ip_orders_day = $this->dmodel_checkout_order->getOrdersByIP($this->request->server['REMOTE_ADDR'],$data_atual,$data_atual);

        if (!$this->debug && ((count($count_customer_orders_day) > 1) || (count($count_ip_orders_day) > 1))) {
            $this->language->load('payment/captcha');

            $google_captcha = $this->config->get('google_recaptcha_site_key') && $this->config->get('google_recaptcha_secret_key') && $this->config->get('wg24_google_recaptcha_status');
            $valid_captcha = $this->ftrgeralregistry->validateCaptcha($google_captcha ? $this->request->post['g-recaptcha-response'] : $this->request->post['captcha']);

            if (!$valid_captcha){
                $google_captcha ? $error['g-recaptcha'] = $this->language->get('t_e_google_captcha') : $error['captcha'] = $this->language->get('t_e_captcha');
            }
        }

        $lock = new limiterlock($this->registry);
        $customer_lock = $lock->checklimit( $this->customer);

        if($customer_lock){
            $error['t_e_limiter'] = $lock->getInformation();
        }

        // Recaptcha Google, apos validação em varios locais matar sessao para que na proxima faça a validação.
        if(isset($this->session->data['gcapcha'])){
            unset($this->session->data['gcapcha']);
        }

		if (count($error)>0) {
			return $error;
		} else {
			return false;
		}

	}

}
