<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Payment extends CI_Controller {

	public function __construct() 
	{
		parent::__construct();
		
		$this->lang->load('account_general', get_language());
		$this->load->model('member_model');
    }
	
	public function paytrust($order_no = NULL)
	{
		if($this->session->userdata('user_type') && $this->session->userdata('user_type') == MEMBER) {
			$data['order'] = $this->member_model->get_pending_topup_details($order_no);
			
			if(isset($data['order']['member_transaction_id'])) {
				$payment_secret_key = PG_PAYTRUST_MYR_KEY;
				$payment_currency = 'MYR';
				switch($data['order']['payment_method']) {
					case 11: $payment_secret_key = PG_PAYTRUST_IDR_KEY; $payment_currency = 'IDR'; break;
					case 13: $payment_secret_key = PG_PAYTRUST_THB_KEY; $payment_currency = 'THB'; break;
					case 14: $payment_secret_key = PG_PAYTRUST_VND_KEY; $payment_currency = 'VND'; break;
					default: $payment_secret_key = PG_PAYTRUST_MYR_KEY; $payment_currency = 'MYR'; break;
				}
				
				$arr = array(
					'return_url' => base_url() . "callback/success/" . $data['order']['payment_method'] . "/",
					'failed_return_url' => base_url() . "callback/failed/" . $data['order']['payment_method'] . "/",
					'http_post_url' => base_url() . "callback/notify/" . $data['order']['payment_method'] . "/",
					'amount' => $data['order']['transaction_amount'],
					'currency' => $payment_currency,
					'item_id' => $data['order']['order_no'],
					'item_description' => "top up for account #" . $data['order']['member_transaction_id'],
					'name' => $data['order']['to_member_username'],
					'email' => "",
					'bank_code' => $data['order']['bank_code'], //"59f413927793b",
					'descriptor' => ""
				);
				
				$data_string = json_encode($arr);
				
				$url = PG_PAYTRUST_PAY_URL . '/v1/transaction/start';
				$ch = curl_init($url);
				curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                      
				curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);  
				curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
				curl_setopt($ch, CURLOPT_TIMEOUT, 60);
				curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);		
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                    	
				curl_setopt($ch, CURLOPT_HTTPHEADER, 
					array(
						'charset=UTF-8',
						'Content-Type: application/json',
						'DataType: JSON',
						'Authorization: Basic ' . base64_encode($payment_secret_key . ':')
					)
				);   	
				$response = curl_exec($ch);
				curl_close($ch);
				
				$json_arr = json_decode($response, true);
				
				redirect($json_arr['redirect_to'], 'refresh');
			}
			else {
				redirect('home/', 'refresh');
			}
		}
		else {
			redirect('', 'refresh');
		}
	}
	
	public function paylah($order_no = NULL)
	{
		if($this->session->userdata('user_type') && $this->session->userdata('user_type') == MEMBER) {
			$data['order'] = $this->member_model->get_pending_topup_details($order_no);
			
			if(isset($data['order']['member_transaction_id'])) {
				$this->load->view('payment_paylah', $data);
			}
			else {
				redirect('home/', 'refresh');
			}
		}
		else {
			redirect('', 'refresh');
		}
	}
	
	public function sfpay($order_no = NULL)
	{
		if($this->session->userdata('user_type') && $this->session->userdata('user_type') == MEMBER) {
			$data['order'] = $this->member_model->get_pending_topup_details($order_no);
			
			if(isset($data['order']['member_transaction_id'])) {
				$this->load->view('payment_sfpay', $data);
			}
			else {
				redirect('home/', 'refresh');
			}
		}
		else {
			redirect('', 'refresh');
		}
	}
	
	public function fpay($order_no = NULL)
	{
		if($this->session->userdata('user_type') && $this->session->userdata('user_type') == MEMBER) {
			$data['order'] = $this->member_model->get_pending_topup_details($order_no);
			
			if(isset($data['order']['member_transaction_id'])) {
				$username = PG_FPAY_USERNAME;
				$api_key = PG_FPAY_API_KEY;
				$send = array('username' => $username , 'api_key' => $api_key);
				$url = PG_FPAY_URL . '/merchant/auth';

				$curl = curl_init($url);
				curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
				curl_setopt($curl, CURLOPT_HEADER, false);
				curl_setopt($curl, CURLOPT_POST, true);
				curl_setopt($curl, CURLOPT_TIMEOUT, 400); 
				curl_setopt($curl, CURLOPT_POSTFIELDS, $send);
				curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data"));

				$response = curl_exec($curl);
				curl_close($curl);
				
				$json_arr = json_decode($response, true);
				
				if($json_arr['status'] == true) {
					$username = PG_FPAY_USERNAME; //Merchant Username
					$auth = $json_arr['auth']; //Get from auth api
					$amount = $data['order']['transaction_amount']; //Price amount
					$currency = "MYR"; //Currency code
					$orderid = $data['order']['order_no']; //Merchant order ID 
					$email = "user@example.com"; //Customer Email
					$phone_number = "601012456789"; //Customer Phone number
					$mode = ""; //optional params for apply prepaird card only . option : DIGI , MAXIS , CELCOM

					$send = array(
						  'username' => $username, 
						  'auth' => $auth, 
						  'amount' => $amount, 
						  'currency' => $currency, 
						  'orderid' => $orderid,
						  'email' => $email, 
						  'phone_number' => $phone_number,
						  'mode' => $mode
						);
						
					$url = PG_FPAY_URL . '/merchant/generate_orders';

					$curl = curl_init($url);
					curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
					curl_setopt($curl, CURLOPT_HEADER, false);
					curl_setopt($curl, CURLOPT_POST, true);
					curl_setopt($curl, CURLOPT_TIMEOUT, 400); 
					curl_setopt($curl, CURLOPT_POSTFIELDS, $send);
					curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data"));

					$response = curl_exec($curl);
					curl_close($curl);
					
					$json_arr = json_decode($response, true);
					
					if($json_arr['status'] == true) {
						redirect($json_arr['p_url'], 'refresh');
					}
					else {
						$this->member_model->update_pending_topup($data['order']['payment_method'], 2, $data['order']['order_no'], '');
					}
				}
				else {
					$this->member_model->update_pending_topup($data['order']['payment_method'], 2, $data['order']['order_no'], '');
				}
			}
			else {
				redirect('home/', 'refresh');
			}
		}
		else {
			redirect('', 'refresh');
		}
	}
	
	public function onepay($order_no = NULL)
	{
		if($this->session->userdata('user_type') && $this->session->userdata('user_type') == MEMBER) {
			$data['order'] = $this->member_model->get_pending_topup_details($order_no);
			
			if(isset($data['order']['member_transaction_id'])) {
				$arr = array(
					'appid' => PG_ONEPAY_APP_ID,
					'pay_type' => 'bank',
					'amount' => number_format($data['order']['transaction_amount'], 2, '.', ''),
					'callback_url' => base_url() . "callback/notify/" . $data['order']['payment_method'] . "/",
					'success_url' => base_url() . "callback/success/" . $data['order']['payment_method'] . "/",
					'error_url' => base_url() . "callback/failed/" . $data['order']['payment_method'] . "/",
					'out_uid' => $data['order']['to_member_username'],
					'out_trade_no' => $data['order']['order_no'],
					'version' => 'v1.1'
				);
				
				ksort($arr);
				$SignParams = "";
				foreach ($arr as $key => $val) {
					$SignParams = $SignParams . $key . "=" . $val . "&";
				}
				
				$arr['sign'] = strtoupper(md5($SignParams . "key=" . PG_ONEPAY_KEY));
				$data_string = http_build_query($arr);  
				
				$url = PG_ONEPAY_PAY_URL . '/index/unifiedorder?format=json';
				$ch = curl_init($url);
				curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                      
				curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);  
				curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
				curl_setopt($ch, CURLOPT_TIMEOUT, 60);
				curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);		
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                    	
				curl_setopt($ch, CURLOPT_HTTPHEADER, 
					array(
						'charset=UTF-8',
						'Content-Type: application/x-www-form-urlencoded'
					)
				);   	
				$response = curl_exec($ch);
				curl_close($ch);
				
				$json_arr = json_decode($response, true);
				
				redirect($json_arr['url'], 'refresh');
			}
			else {
				redirect('home/', 'refresh');
			}
		}
		else {
			redirect('', 'refresh');
		}
	}
	
	public function surepay($order_no = NULL)
	{
		if($this->session->userdata('user_type') && $this->session->userdata('user_type') == MEMBER) {
			$data['order'] = $this->member_model->get_pending_topup_details($order_no);
			
			if(isset($data['order']['member_transaction_id'])) {
				$this->load->view('payment_surepay', $data);
			}
			else {
				redirect('home/', 'refresh');
			}
		}
		else {
			redirect('', 'refresh');
		}
	}
}

/* End of file payment.php */
/* Location: ./application/controllers/payment.php */