<?php
/**
* @package      Easyshop Shop
* @copyright    Copyright (C) 2005 - 2011 TA-EDV
* @license      proprietary
* @author       John T. Daly <jd@ta-edv.de>
* 
* Easyway Shop is a web e-commerce system
*/

include_once './core/paymenthelper.class.php';

class admin_payment_method_actions {
	
	private $base_object;
	private $layout_object;
	
	function __construct($base_object, $layout_object) {
		$this->layout_object = $layout_object;
		$this->base_object = $base_object;
	}
		
	function run() {
		if (isset($_GET['action'])) {
			$action = $_GET['action'];
		} elseif (isset($_POST['action'])) {
			$action = $_POST['action'];
		} else {
			$action = false;
		}
		
		if ($action == "get_payment_data_by_id_and_country") {
			$this->get_payment_data_by_id_and_country();
		} elseif ($action == 'setup_payment_methods') {
			$this->setup_payment_methods();
		}elseif ($action == "ping") {
			$this->ping();
		}
	}
	
	private function get_payment_data_by_id_and_country() {
		if (isset($_GET['method_id'])) {
			$method_id = $_GET['method_id'];
		} else {
			$method_id = false;
		}
		
		if (isset($_GET['country_id'])) {
			$country_id = $_GET['country_id'];
		} else {
			$country_id = false;
		}
		
		if (isset($_GET['order_items_value'])) {
			$order_items_value = $_GET['order_items_value'];
		} else {
			$order_items_value = false;
		}
		
		if ($method_id && $country_id) {
			if ($order_items_value !== false) {
				$default_vat_value = $this->base_object->config->shopConfiguration['default_vat_value'];
				$result = PaymentHelper::getPaymentDataByIdAndCountry($method_id, $country_id, $default_vat_value, $order_items_value);
				
				if ($result) {
					$return = array('status' => 'success', 'data' => $result);
				} else {
					$return = array('status' => 'error', 'message' => 'no result');
				}
			} else {
				$return = array('status' => 'error', 'message' => 'no order items total');
			}
		} else {
			if (!$method_id) {
				$return = array('status' => 'error', 'message' => 'no method id');
			} elseif (!$country_id) {
				$return = array('status' => 'error', 'message' => 'no country id');
			}
		}
		
		echo json_encode($return);
		exit();
	}
	
	private function setup_payment_methods() {
		if (isset($_GET['countryId'])) {
			$countryId = $_GET['countryId'];
		} else {
			$countryId = false;
		}
		
		if ($countryId) {
				$result = PaymentHelper::getActivePaymentMethodsByCountry($countryId);
				
				if ($result) {
					$return = array('status' => 'success', 'data' => $result);
				} else {
					$return = array('status' => 'error', 'message' => 'no result');
				}
		} else {
			$return = array('status' => 'error', 'message' => 'missing argument');
		}
		
		echo json_encode($return);
		exit();
	}
	
	private function ping() {
		echo "<pre>";
		print_r($_GET);
		
		exit();
	}
}