<?php

/**
 * Class ControllerExtensionPaymentTrustPay
 *
 * The OpenCart extension for finby payment gateway API v2.
 * Instant Bank transfers.
 * Admin controller.
 * https://doc.finby.com/v02
 *
 * verzia PluginVersion sa pise v catalog/controller/.../trustpay_bt
 * 
 * @version    3.3.4
 * @copyright  2020 finby
 * @author     finby
 * @link       https://www.finby.com
 */
class ControllerExtensionPaymentTrustPayBT extends Controller {
    protected $trustpay_config = array(
        'version' => '3.3.4',
        'compatibility' => array('2.3.0.2'),
        'routes' => array(
            'success' => 'extension/payment/trustpay_bt/success',
            'cancel' => 'checkout/checkout',
            'error' => 'extension/payment/trustpay_bt/error',
            'notification' => 'extension/payment/trustpay_bt/callback',
            'debug' => 'extension/payment/trustpay_bt/debug'
        ),
        
        'api_merchant_portal' => array(
            'live' => 'https://amapi.finby.eu/mapi5/wire/paypopup',
        ),
    );
	
	private $error = array();

	public function index() {
		$this->load->language('extension/payment/trustpay_bt');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('setting/setting');

        $this->request->post['trustpay_bt_config'] = serialize($this->trustpay_config);

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			$this->model_setting_setting->editSetting('trustpay_bt', $this->request->post);

			$this->session->data['success'] = $this->language->get('text_success');

			$this->response->redirect($this->url->link('extension/extension', 'token=' . $this->session->data['token'] . '&type=payment', true));
		}

        if (isset($this->error['warning'])) {
            $data['error_warning'] = $this->error['warning'];
        } else {
            $data['error_warning'] = '';
        }

        // Heading
		$data['heading_title'] = $this->language->get('heading_title');

        $data['breadcrumbs'] = array();

        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
        );

        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_extension'),
            'href' => $this->url->link('extension/extension', 'token=' . $this->session->data['token'] . '&type=payment', true)
        );

        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('heading_title'),
            'href' => $this->url->link('extension/payment/trustpay_bt', 'token=' . $this->session->data['token'], true)
        );

        $data['action'] = $this->url->link('extension/payment/trustpay_bt', 'token=' . $this->session->data['token'], true);
        $data['cancel'] = $this->url->link('extension/extension', 'token=' . $this->session->data['token'] . '&type=payment', true);
        $data['button_save'] = $this->language->get('button_save');
        $data['button_cancel'] = $this->language->get('button_cancel');
        $data['text_edit'] = $this->language->get('text_edit');

        // Tabs
        $data['tab_general'] = $this->language->get('tab_general');
        $data['tab_order_statuses'] = $this->language->get('tab_order_statuses');
        $data['tab_return_urls'] = $this->language->get('tab_return_urls');
        $data['tab_about'] = $this->language->get('tab_about');

        // General
        $data['entry_aid'] = $this->language->get('entry_aid');
        $data['help_aid'] = $this->language->get('help_aid');

        if (isset($this->request->post['trustpay_bt_aid'])) {
            $data['trustpay_bt_aid'] = $this->request->post['trustpay_bt_aid'];
        } else {
            $data['trustpay_bt_aid'] = $this->config->get('trustpay_bt_aid');
        }

        if (isset($this->error['aid'])) {
            $data['error_aid'] = $this->error['aid'];
        } else {
            $data['error_aid'] = '';
        }

        $data['entry_key'] = $this->language->get('entry_key');
        $data['help_key'] = $this->language->get('help_key');

        if (isset($this->request->post['trustpay_bt_key'])) {
            $data['trustpay_bt_key'] = $this->request->post['trustpay_bt_key'];
        } else {
            $data['trustpay_bt_key'] = $this->config->get('trustpay_bt_key');
        }

        if (isset($this->error['key'])) {
            $data['error_key'] = $this->error['key'];
        } else {
            $data['error_key'] = '';
        }
        
        
        $data['help_merchant_ib'] = $this->language->get('help_merchant_ib');
        $data['entry_merchant_ib'] = $this->language->get('entry_merchant_ib');
        $data['text_merchant_portal'] = $this->language->get('text_merchant_portal');
        
        $data['text_yes'] = $this->language->get('text_yes');
        $data['text_no'] = $this->language->get('text_no');

        $data['entry_test'] = $this->language->get('entry_test');
        $data['help_test'] = $this->language->get('help_test');

        
        $data['entry_debug'] = $this->language->get('entry_debug');
        $data['help_debug'] = $this->language->get('help_debug');

        if (isset($this->request->post['trustpay_bt_debug'])) {
            $data['trustpay_bt_debug'] = $this->request->post['trustpay_bt_debug'];
        } else {
            $data['trustpay_bt_debug'] = $this->config->get('trustpay_bt_debug');
        }

        $data['entry_total'] = $this->language->get('entry_total');
        $data['help_total'] = $this->language->get('help_total');

        if (isset($this->request->post['trustpay_bt_total'])) {
            $data['trustpay_bt_total'] = $this->request->post['trustpay_bt_total'];
        } else {
            $data['trustpay_bt_total'] = $this->config->get('trustpay_bt_total');
        }

        $data['entry_geo_zone'] = $this->language->get('entry_geo_zone');
        $data['text_all_zones'] = $this->language->get('text_all_zones');

        $this->load->model('localisation/geo_zone');

        $data['geo_zones'] = $this->model_localisation_geo_zone->getGeoZones();

        if (isset($this->request->post['trustpay_bt_geo_zone_id'])) {
            $data['trustpay_bt_geo_zone_id'] = $this->request->post['trustpay_bt_geo_zone_id'];
        } else {
            $data['trustpay_bt_geo_zone_id'] = $this->config->get('trustpay_bt_geo_zone_id');
        }

        $data['entry_status'] = $this->language->get('entry_status');
        $data['text_disabled'] = $this->language->get('text_disabled');
        $data['text_enabled'] = $this->language->get('text_enabled');

        if (isset($this->request->post['trustpay_bt_status'])) {
            $data['trustpay_bt_status'] = $this->request->post['trustpay_bt_status'];
        } else {
            $data['trustpay_bt_status'] = $this->config->get('trustpay_bt_status');
        }

        $data['entry_sort_order'] = $this->language->get('entry_sort_order');

        if (isset($this->request->post['trustpay_bt_sort_order'])) {
            $data['trustpay_bt_sort_order'] = $this->request->post['trustpay_bt_sort_order'];
        } else {
            $data['trustpay_bt_sort_order'] = $this->config->get('trustpay_bt_sort_order');
        }

        // Order Statuses
        $this->load->model('localisation/order_status');

        $data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();

        $data['entry_success_status'] = $this->language->get('entry_success_status');
        $data['help_success_status'] = $this->language->get('help_success_status');

        if (isset($this->request->post['trustpay_bt_success_status_id'])) {
            $data['trustpay_bt_success_status_id'] = $this->request->post['trustpay_bt_success_status_id'];
        } else {
            $data['trustpay_bt_success_status_id'] = $this->config->get('trustpay_bt_success_status_id');
        }

        $data['entry_pending_status'] = $this->language->get('entry_pending_status');
        $data['help_pending_status'] = $this->language->get('help_pending_status');

        if (isset($this->request->post['trustpay_bt_pending_status_id'])) {
            $data['trustpay_bt_pending_status_id'] = $this->request->post['trustpay_bt_pending_status_id'];
        } else {
            $data['trustpay_bt_pending_status_id'] = $this->config->get('trustpay_bt_pending_status_id');
        }

        $data['entry_announced_status'] = $this->language->get('entry_announced_status');
        $data['help_announced_status'] = $this->language->get('help_announced_status');

        if (isset($this->request->post['trustpay_bt_announced_status_id'])) {
            $data['trustpay_bt_announced_status_id'] = $this->request->post['trustpay_bt_announced_status_id'];
        } else {
            $data['trustpay_bt_announced_status_id'] = $this->config->get('trustpay_bt_announced_status_id');
        }

        $data['entry_authorized_status'] = $this->language->get('entry_authorized_status');
        $data['help_authorized_status'] = $this->language->get('help_authorized_status');

        if (isset($this->request->post['trustpay_bt_authorized_status_id'])) {
            $data['trustpay_bt_authorized_status_id'] = $this->request->post['trustpay_bt_authorized_status_id'];
        } else {
            $data['trustpay_bt_authorized_status_id'] = $this->config->get('trustpay_bt_authorized_status_id');
        }

        $data['entry_processing_status'] = $this->language->get('entry_processing_status');
        $data['help_processing_status'] = $this->language->get('help_processing_status');

        if (isset($this->request->post['trustpay_bt_processing_status_id'])) {
            $data['trustpay_bt_processing_status_id'] = $this->request->post['trustpay_bt_processing_status_id'];
        } else {
            $data['trustpay_bt_processing_status_id'] = $this->config->get('trustpay_bt_processing_status_id');
        }

        $data['entry_failed_status'] = $this->language->get('entry_failed_status');
        $data['help_failed_status'] = $this->language->get('help_failed_status');

        if (isset($this->request->post['trustpay_bt_failed_status_id'])) {
            $data['trustpay_bt_failed_status_id'] = $this->request->post['trustpay_bt_failed_status_id'];
        } else {
            $data['trustpay_bt_failed_status_id'] = $this->config->get('trustpay_bt_failed_status_id');
        }

        $data['entry_amount_mismatch_status'] = $this->language->get('entry_amount_mismatch_status');
        $data['help_amount_mismatch_status'] = $this->language->get('help_amount_mismatch_status');

        if (isset($this->request->post['trustpay_bt_amount_mismatch_status_id'])) {
            $data['trustpay_bt_amount_mismatch_status_id'] = $this->request->post['trustpay_bt_amount_mismatch_status_id'];
        } else {
            $data['trustpay_bt_amount_mismatch_status_id'] = $this->config->get('trustpay_bt_amount_mismatch_status_id');
        }

        $data['text_status_note'] = $this->language->get('text_status_note');
        $data['text_test_note'] = $this->language->get('text_test_note');

        $data['entry_notify'] = $this->language->get('entry_notify');

        if (isset($this->request->post['trustpay_bt_notify'])) {
            $data['trustpay_bt_notify'] = $this->request->post['trustpay_bt_notify'];
        } else {
            $data['trustpay_bt_notify'] = $this->config->get('trustpay_bt_notify');
        }

        // Return URLs
        $data['text_return_urls'] = sprintf($this->language->get('text_return_urls'), $this->trustpay_config['api_merchant_portal']['live']);
        
        if ($this->ssl) {
            $url = HTTPS_CATALOG . 'index.php?route=';
        } else {
            $url = HTTP_CATALOG . 'index.php?route=';
        }

        $data['text_success_return'] = $this->language->get('text_success_return');
        $data['success_return'] = $url . $this->trustpay_config['routes']['success'];
        $data['text_error_return'] = $this->language->get('text_error_return');
        $data['error_return'] = $url . $this->trustpay_config['routes']['error'];
        $data['text_cancel_return'] = $this->language->get('text_cancel_return');
        $data['cancel_return'] = $url . $this->trustpay_config['routes']['cancel'];
        $data['text_notification_url'] = $this->language->get('text_notification_url');
        $data['notification_url'] = $url . $this->trustpay_config['routes']['notification'];

        // About
        $data['text_title'] = $this->language->get('text_title');
        $data['text_version'] = $this->language->get('text_version');
        $data['info_version'] = $this->trustpay_config['version'];
        if (!in_array(VERSION, $this->trustpay_config['compatibility'])) {
            $data['info_version'] .= $this->language->get('info_version');
        }
        $data['text_compatibility'] = $this->language->get('text_compatibility');
        $data['info_compatibility'] = sprintf($this->language->get('info_compatibility'), implode(",", $this->trustpay_config['compatibility']));
        $data['text_documentation'] = $this->language->get('text_documentation');
        $data['info_documentation'] = $this->language->get('info_documentation');
        $data['text_author'] = $this->language->get('text_author');
        $data['info_author'] = $this->language->get('info_author');
        $data['text_license'] = $this->language->get('text_license');
        $data['info_license'] = sprintf($this->language->get('info_license'), date("Y"));

		$data['header'] = $this->load->controller('common/header');
		$data['column_left'] = $this->load->controller('common/column_left');
		$data['footer'] = $this->load->controller('common/footer');

		$this->response->setOutput($this->load->view('extension/payment/trustpay_bt', $data));
	}

	private function validate() {
		if (!$this->user->hasPermission('modify', 'extension/payment/trustpay_bt')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		if (!$this->request->post['trustpay_bt_aid']) {
			$this->error['aid'] = $this->language->get('error_aid');
		}
		
		if (!$this->request->post['trustpay_bt_key']) {
			$this->error['key'] = $this->language->get('error_key');
		}

		return !$this->error;
	}
}
