<?php
class ControllerCheckoutSuccess extends Controller {
	public function index() {
		$this->load->language('checkout/success');
		$this->load->model('account/order');
		$this->load->model('checkout/order');

		$data['text_fpxstatus'] = $this->language->get('text_fpxstatus');
		$data['text_fpxTxnTime'] = $this->language->get('text_fpxTxnTime');
		$data['text_fpxTxnId'] = $this->language->get('text_fpxTxnId');
		$data['text_txnAmount'] = $this->language->get('text_txnAmount');
		$data['text_sellerOrderNo'] = $this->language->get('text_sellerOrderNo');
		$data['text_buyerBankBranch'] = $this->language->get('text_buyerBankBranch');

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

		// ipay88 return set back the login session
		if ( isset($this->request->get['ipay']) && $this->request->get['ipay'] == "1" ) {
			$this->session->data['customer_id'] = $this->customer->getId();
			//header('Set-Cookie: ' . $this->config->get('session_name') . '=' . $this->session->getId() . '; SameSite=None; Secure');
			// ; HttpOnly
			//echo $this->session->data['customer_id'];
			//echo 'yes';
			//$this->response->redirect($this->url->link('information/information', 'information_id=7', '', true));
		}

		if ( isset($this->request->get['pbb']) && $this->request->get['pbb'] == "1" ) {
			//header('Set-Cookie: ' . $this->config->get('session_name') . '=' . $this->session->getId() . '; SameSite=None; Secure');

				// check if is $_POST back from PayNet

				$temp_id = 0;
				if ( $_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['fpx_sellerOrderNo']) && !empty($_POST['fpx_sellerOrderNo']) ) {

					// temporarily
					//if( isset($this->request->get['code']) ) {
						$data['fpx_code'] = 1;

						$data['fpx_debitAuthCode'] = htmlspecialchars($_POST['fpx_debitAuthCode']);
						//$data['fpx_creditAuthCode'] = htmlspecialchars($_POST['fpx_creditAuthCode']);

						if( $data['fpx_debitAuthCode'] == "00" ) {
							$data['fpx_fpxstatus'] = '<span class="success-fpx">'.$this->language->get('text_fpx_success').'</span>';
						} elseif( $data['fpx_debitAuthCode'] == "2A" ) {
							$data['fpx_fpxstatus'] = '<span class="error-fpx">'.$this->language->get('text_fpx_unsuccess').'</span>'.$this->language->get('text_lower_limit');
						} elseif( $data['fpx_debitAuthCode'] == "48" ) {
							$data['fpx_fpxstatus'] = $this->language->get('text_fpx_unsuccess').$this->language->get('text_higher_limit');
						} else {
							$data['fpx_fpxstatus'] = '<span class="error-fpx">'.$this->language->get('text_fpx_unsuccess').'</span>';
						}

						$date = date_create(htmlspecialchars($_POST['fpx_fpxTxnTime']));
						if(empty($_POST['fpx_fpxTxnTime'])) {
							$data['fpx_fpxTxnTime'] = '';
						} else {
							$data['fpx_fpxTxnTime'] = date_format($date,"d-m-Y H:i:s");
						}

						$data['fpx_fpxTxnId'] = htmlspecialchars($_POST['fpx_fpxTxnId']);
						$data['fpx_txnAmount'] = htmlspecialchars($_POST['fpx_txnAmount']);
						$data['fpx_sellerOrderNo'] = htmlspecialchars($_POST['fpx_sellerOrderNo']);
						$data['fpx_buyerBankBranch'] = htmlspecialchars($_POST['fpx_buyerBankBranch']);
					//}
					// end temporarily

					$temp_id = intval(trim($_POST['fpx_sellerOrderNo']));

				} else {

					if (isset($this->session->data['order_id'])) {
						$temp_id = $this->session->data['order_id'];
					} else {
						$temp_id = 0;
					}

				}

				

				if ($this->customer->isLogged()) {
					$order_info = $this->model_account_order->getOrder($temp_id);
				} else {
					$order_info = $this->model_account_order->getOrderGuest($temp_id);
				}


				//$order_status = $this->model_account_order->getOrderStatus($temp_id);

				// $data = $temp_id . ' / ' . $this->session->data['order_id'] . ' / ' . (int)$order_info['order_status_id'] . ' / ' . (int)$this->config->get('payment_skrill_order_status_id');
				// $data .= print_r($order_info,true);
				// $filename = date('YmdHis').".txt";
				// if (!file_exists($filename)) {
				// 		$fh = fopen($filename, 'w') or die("Can't create file");
				// }
				// $ret = file_put_contents($filename, $data, FILE_APPEND | LOCK_EX);
				// if($ret === false) {
				// 		die('There was an error writing this file');
				// }
				// else {
				// 		//echo "$ret bytes written to file";
				// }


				if( isset($_POST['fpx_debitAuthCode'])) {

				} else {

					if( (int)$order_info['order_status_id'] == (int)$this->config->get('payment_skrill_order_status_id') ) {
						
						
					} else {

						if (isset($this->request->post['req_reference_number'])) {

							if ( $this->request->post['decision'] && $this->request->post['decision'] == "ACCEPT" ) {
								if( $this->request->post['decision'] !="" ) {
									//$verified = true;
								} else {
									$this->response->redirect($this->url->link('information/information', 'information_id=7', '', true));	
								}
							}

						} else {
							$this->response->redirect($this->url->link('information/information', 'information_id=7', '', true));	
						}
						
					}

				}


		}


		if( isset($_POST['fpx_debitAuthCode'])) {
			//if is paynet
			if($_POST['fpx_debitAuthCode'] == '00') {
				$this->cart->clear();

				unset($this->session->data['shipping_method']);
				unset($this->session->data['shipping_methods']);
				unset($this->session->data['payment_method']);
				unset($this->session->data['payment_methods']);
				unset($this->session->data['guest']);
				unset($this->session->data['comment']);
				unset($this->session->data['order_id']);
				unset($this->session->data['coupon']);
				unset($this->session->data['reward']);
				unset($this->session->data['voucher']);
				unset($this->session->data['vouchers']);
				unset($this->session->data['totals']);
			}
		} else {

			// normal payment gateway
			if (isset($this->session->data['order_id'])) {
				$this->cart->clear();

				unset($this->session->data['shipping_method']);
				unset($this->session->data['shipping_methods']);
				unset($this->session->data['payment_method']);
				unset($this->session->data['payment_methods']);
				unset($this->session->data['guest']);
				unset($this->session->data['comment']);
				unset($this->session->data['order_id']);
				unset($this->session->data['coupon']);
				unset($this->session->data['reward']);
				unset($this->session->data['voucher']);
				unset($this->session->data['vouchers']);
				unset($this->session->data['totals']);
			}


		}





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

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

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

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_basket'),
			'href' => $this->url->link('checkout/cart')
		);

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_checkout'),
			'href' => $this->url->link('checkout/checkout', '', true)
		);

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_success'),
			'href' => $this->url->link('checkout/success')
		);

		if ($this->customer->isLogged()) {
			$data['text_message'] = sprintf($this->language->get('text_customer'), $this->url->link('account/account', '', true), $this->url->link('account/order', '', true), $this->url->link('account/download', '', true), $this->url->link('information/contact'));
		} else {
			$data['text_message'] = sprintf($this->language->get('text_guest'), $this->url->link('information/contact'));
		}

		$data['continue'] = $this->url->link('common/home');

		$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('common/success', $data));
	}
}
