<?php
define('ROOT', dirname(__DIR__));
require_once('system/func.php');
auth(); // Закроем от гостей
$title = 'Покупка алмазов';
require_once('system/header.php');
include_once("OdnoklassnikiSDK.php");
?>

    <style>
        img.icon_l {
            border-radius: 4px 4px 4px 4px;
            float: left;
            margin: 2px 7px 4px 0;
        }
    </style>

<?php
$arrayPrise = [
    1 => [
        'img' => '/images/diamond_img/one.png',
        'count' => 1100,
        'price' => 100
    ],
    2 => [
        'img' => '/images/diamond_img/two.png',
        'count' => 3300,
        'price' => 300
    ],
    3 => [
        'img' => '/images/diamond_img/three.png',
        'count' => 5500,
        'price' => 500
    ],
    4 => [
        'img' => '/images/diamond_img/four.png',
        'count' => 11000,
        'price' => 1000
    ],
    5 => [
        'img' => '/images/diamond_img/five.png',
        'count' => 16500,
        'price' => 1500
    ],
];


if (filter_has_var(INPUT_GET, 'count') && array_key_exists($_GET['count'], $arrayPrise) && $user['uid_ok'] == false) {

    $count = $arrayPrise[$_GET['count']]['count'];
    $curl = curl_init();
    #curl_setopt($curl, CURLOPT_URL, 'https://api.xsolla.com/merchant/merchants/23775/token');
    curl_setopt($curl, CURLOPT_URL, 'https://api.xsolla.com/merchant/merchants/23775/token');
    $h = array("Content-Type: application/json");
    curl_setopt($curl, CURLOPT_HTTPHEADER, $h);
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($curl, CURLOPT_USERPWD, '23775:KhlODRtQ93mvkAzm');
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    $json = [
        "user" => [
            "id" => [
                "value" => $user['id'],
                "hidden" => true,

            ],
        ],
        "settings" => [
            "project_id" => 16248,
            'ui' => [
                'version' => 'mobile',
                'size' => 'medium',
                'theme' => 'dark'
            ]
        ],
        "purchase" => array(
            "virtual_currency" => array(
                "quantity" => $count,
                "currency" => "RUB"
            )
        )
    ];
    $json = json_encode($json);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
    $token = json_decode(curl_exec($curl))->token;
    curl_close($curl);
    header("Location: https://secure.xsolla.com/paystation2/?access_token=" . $token);
    exit();
}


$getSale = mysql_fetch_assoc(mysql_query("SELECT * FROM `avenax_admin` WHERE `id` = '1'"));

$current = '₽';
$current_user = false;
if ($user['uid_ok'] == true) {
    $public_key = urlencode('CBAKFJDMEBABABABA');
    $session_key = urlencode('64f82ae4dbd30ba6bf0a0b848446cc61');
    $current_user = OdnoklassnikiSDK::makeRequest("users.getCurrentUser", array("fields" => "name, gender, email",));

    $current = ' ОК';


    $current_user = OdnoklassnikiSDK::makeRequest("users.getCurrentUser", array("fields" => "name, gender, email",));

    echo '<pre>';
    print_r($current_user);
    echo '</pre>';


}

foreach ($arrayPrise as $price => $item) {
    echo '<div class="text_p1"><img src="' . $item['img'] . '" width="82" class="icon_l"> Алмазы <img src="/images/icons/almaz.png" width="16"> +<span style="color: lime">' . $item['count'] . '</span>';

    if (!empty($getSale) && $getSale['sale_xsolla_time'] > time()) {
        echo '<div><span style="color: orange; font-size: small;">Акция! Бонус +' . intval($getSale['sale_xsolla']) . '% <img src="/images/icons/almaz.png" width="16"></span></div>';
        echo '<div><span style="color: #bd3e0e; font-size: small;">Акция закончится через: ' . tl($getSale['sale_xsolla_time'] - time()) . '</span>';
        echo '<div style="clear: both"></div></div></div>';
    }

echo div('header');
    if ($user['uid_ok'] == false) {
        echo '<div style="text-align: center"><a href="?count=' . $price . '" class="btn_grey" target="_blank">Купить за ' . $item['price'] . $current . '</a>';


    } else {

        #$sig = md5(application_key=<APPLICATION_PUBLIC_KEY>code=1name=20 coinsprice=10session_key=<SESSION_KEY><SESSION_SECRET_KEY>)
        $sig = md5('application_key=' . $public_key . '&code=' . $price . '&name=' . $item['count'] . ' алмазов&price=' . $item['price'] . '&session_key=' . $session_key);
        # https://m.ok.ru/api/show_payment?application_key=' . $public_key . '&session_key=' . $session_key . '&sig={signature}&{params}
        $url = 'https://m.ok.ru/api/show_payment?application_key=' . $public_key . '&session_key=' . $session_key . '&sig=' . $sig . '&name=' . $item['count'] . ' алмазо&вprice=' . $item['price'] . '&code=' . $price . '';
        echo '<div style="text-align: center"><a href="' . $url . '" class="btn_grey" target="_blank">Купить за ' . $item['price'] . $current . '</a></div>';
echo "</div>";
    }

    echo '<div style="clear: both"></div>';
    echo '</div></div>';


}


require_once('system/footer.php');