<?php

header("Access-Control-Allow-Origin: *");
$output = array();

require("../shared/functions.php");

// Var initialize
$post = new stdClass();
$post->action           = filter_input(INPUT_POST, 'action', FILTER_SANITIZE_STRING);
$post->account          = filter_input(INPUT_POST, 'account', FILTER_SANITIZE_STRING);
$post->account_id       = filter_input(INPUT_POST, 'account_id', FILTER_SANITIZE_STRING);
$post->is_sandbox       = filter_input(INPUT_POST, 'is_sandbox', FILTER_SANITIZE_STRING);
$post->ip_checked       = filter_input(INPUT_POST, 'ip_checked', FILTER_SANITIZE_STRING);
$post->is_cloud         = filter_input(INPUT_POST, 'is_cloud', FILTER_SANITIZE_STRING);

// Debug ?
//print_r($post);

// Functionnal escape that is repeated in the code
function enrolled($payment_product) {
    closejs(array(
        'error' => "No enrolled Card $payment_product found."
    ));
}

if(isset($post->account) && isset($post->account_id) && isset($post->is_sandbox)) {
    $db = connecti('jabella');

    if ($db) {
        if($post->action == 'CheckMerchant') {
            
            $query = "SELECT is_cloud FROM tpp_cloud_merchants WHERE account_id = '$post->account_id' LIMIT 1;";
            $sql = $db->query($query);
            if(isset($sql->num_rows) && $sql->num_rows > 0) {
                $row = $sql->fetch_object();

                $output['is_cloud'] = $row->is_cloud;
                
                // Update the ip_checked current status
                $query = "UPDATE tpp_cloud_merchants SET ip_restriction = '$post->ip_checked' WHERE account_id = '$post->account_id' LIMIT 1;";
                $sql = $db->query($query);
                
                closejs($output);
            }
            else {
                // If merchant not found, create with blank status
                $query_ins = "INSERT INTO tpp_cloud_merchants SET "
                        . "is_sandbox = '$post->is_sandbox', "
                        . "account = '".$db->real_escape_string($post->account). "', "
                        . "account_id = '$post->account_id'";
                $sql = $db->query($query_ins);
                
                $output['is_cloud'] = 0;
                closejs($output);
            }
        }
        elseif($post->action == 'UpdateMerchant') {
            
            $query = "UPDATE tpp_cloud_merchants SET is_cloud = '$post->is_cloud', date = CURTIME() WHERE account_id = '$post->account_id' LIMIT 1;";
            $sql = $db->query($query);
            
            if(mysql_error()) {
                $output['error'] = mysql_error();
            }
            else {
                $output['is_cloud'] = $post->is_cloud;
            }
            closejs($output);
        }
        elseif($post->action == 'CloudNotification') {
            
            $query = "SELECT account_id FROM tpp_cloud_merchants WHERE is_cloud = '0' AND ip_restriction = '0' AND is_sandbox = '$post->is_sandbox' AND (account = '$post->account' OR account_id = '$post->account_id') LIMIT 1;";
            $sql = $db->query($query);
            if(isset($sql->num_rows) && $sql->num_rows > 0) {
                $row = $sql->fetch_object();
                output($row->account_id);
            }
        }
        
        $db->close();
    }
}

?>