<?php
class ControllerExtensionPaymentParspal extends Controller {
	public function index() {
		$this->load->language('extension/payment/parspal');
		
		$data['text_connect'] = $this->language->get('text_connect');
		$data['text_loading'] = $this->language->get('text_loading');
 		$data['text_wait'] = $this->language->get('text_wait');
		
		$data['button_confirm'] = $this->language->get('button_confirm');

		return $this->load->view('extension/payment/parspal', $data);
	}

	public function confirm() {
		$this->load->model('checkout/order');
		$order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);

		$apikey = $this->config->get('payment_parspal_apikey');

		$amount = $this->currency->format($order_info['total'], $order_info['currency_code'], $order_info['currency_value'], false);
		$amount = $this->currency->convert($amount, $order_info['currency_code'], "RLS");

		$json = array();

		/*$encrypted_order_id = $this->encryption->encrypt($this->config->get('config_encryption'), $this->session->data['order_id']);*/
		$encrypted_order_id = $this->session->data['order_id'];
		$callbackUrl = $this->url->link('extension/payment/parspal/callback', '', true);

        require_once(dirname(__FILE__) . '/parspal_api_helper.class.php');
		$papi = new ParsPal_API_Helper($apikey);
		$rapi = $papi->paymentRequest(array(
				'amount'      => $amount,
				'order_id'    => $order_info['order_id'],
                'reserve_id'  => $encrypted_order_id,
				'description' => $order_info['comment'],
				'return_url'  => $callbackUrl,
                'currency'    => 'IRR',
				'payer'       => array(
				'name'        => $order_info['firstname'] .' '. $order_info['lastname'],
				'email'       => $order_info['email'],
				'mobile'      => $order_info['telephone'],
		)));

		if ($rapi) {
			$paymentUrl = $papi->paymentUrl;
			$json['success'] = $paymentUrl;
		} else {
			$error_message = $papi->error;
            $json['error'] = $error_message;
		}

		$this->response->addHeader('Content-Type: application/json');
		$this->response->setOutput(json_encode($json));
	}

	public function callback() {
		/*if ($this->session->data['payment_method']['code'] == 'parspal') {*/
			$this->load->language('extension/payment/parspal');

			$this->document->setTitle($this->language->get('text_title'));
			
			$data['heading_title'] = $this->language->get('text_title');
			$data['text_results'] = $this->language->get('text_results');

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

			$data['error_warning'] = "";
			$data['results'] = "";
			$data['continue'] = "";

			$apikey = $this->config->get('payment_parspal_apikey');
			
			$data['breadcrumbs'] = array();
			$data['breadcrumbs'][] = array(
				'text' => $this->language->get('text_home'), 
				'href' => $this->url->link('common/home', '', true)
			);
			$data['breadcrumbs'][] = array(
				'text' => $this->language->get('text_title'), 
				'href' => $this->url->link('checkout/checkout', '', true)
			);

			if (isset($this->request->post['status'])) {
       			$status = $this->request->post['status'];
       		} else {
       			$status = '';
       		}
			if (isset($this->request->post['receipt_number'])) {
       			$receipt_number = $this->request->post['receipt_number'];
       		} else {
       			$receipt_number = '';
       		}
			if (isset($this->request->post['order_id'])) {
       			$order_id = $this->request->post['order_id'];
       		} else {
       			$order_id = 0;
       		}
			if (isset($this->request->post['reserve_id'])) {
				/*$reserve_id = $this->encryption->decrypt($this->config->get('config_encryption'), $this->request->post['reserve_id']);*/
				$reserve_id = $this->request->post['reserve_id'];
       		} else {
       			$reserve_id = 0;
       		}

			$this->load->model('checkout/order');
   			$order_info = $this->model_checkout_order->getOrder($order_id);

            require_once(dirname(__FILE__) . '/parspal_api_helper.class.php');
    		$papi = new ParsPal_API_Helper($apikey);

            if ($papi->checkStatusCode($status)) {
    			if ($order_info && $reserve_id == $order_id) {
					$amount = $this->currency->format($order_info['total'], $order_info['currency_code'], $order_info['currency_value'], false);
        			$amount = $this->currency->convert($amount, $order_info['currency_code'], "RLS");

                    $rapi = $papi->paymentVerify($receipt_number, $amount,'IRR');

                    if ($rapi) {
                    	$comment = $this->language->get('text_results') . $receipt_number;
                		$this->model_checkout_order->addOrderHistory($order_id, $this->config->get('payment_parspal_order_status_id'), $comment, true);

                		$data['results'] = $receipt_number;
                		$data['button_continue'] = $this->language->get('button_complete');
                		$data['continue'] = $this->url->link('checkout/success');
                    } else {
                    	$data['error_warning'] = 'خطا در عملیات پردازش پرداخت:' . '<br>' . 'کد خطا:'.$papi->error;
                		$data['button_continue'] = $this->language->get('button_view_cart');
                		$data['continue'] = $this->url->link('checkout/cart');
                    }
                }
            } else {
                $data['error_warning'] = 'اطلاعات مبنی بر پرداخت موفق دریافت نشده است'.'<br>'.$papi->error;
				$data['button_continue'] = $this->language->get('button_view_cart');
				$data['continue'] = $this->url->link('checkout/cart');
            }

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

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