<?php
require_once('session.php');
$usersID = $_GET['extplayer'];

  $getPlayer = mysqli_query($conn,"SELECT * FROM `tb_ppplayer` WHERE userID = '$usersID' AND provider = 'PragmaticPlay'") or die(mysqli_error());
  $gp = mysqli_fetch_array($getPlayer);
  $providernya = $gp['provider'];
  $balance = $gp['balance'];
  $usersID = $gp['userID'];
  $externalPlayerId = $gp['$externalPlayerId'];

  $kode_unik = substr(str_shuffle(1234567890),0,3);
  $kd_transaksi = date('Ymds').$kode_unik;
  $requestID = substr(str_shuffle('abcdefghijklmnopqrstuvwxyz123456789'),0,8);
  $requestIDD = $requestID.date('s');
  $created_date = date('Y-m-d H:i:s');

  $sql_provider = mysqli_query($conn,"SELECT * FROM `tb_tripayapi` WHERE provider = 'PragmaticPlay'") or die(mysqli_error());
  $sp = mysqli_fetch_array($sql_provider);
  $urlRequest = $sp['urlRequest'];
  $gameUrl = $sp['urlResponse'];
  $secureLogin = $sp['api_key']; //apiKey
  $secretKey = $sp['secret_key']; //brandID

  $catatan = $providernya.' Transfer Back';

  $params = 'externalPlayerId='.$externalPlayerId.'&secureLogin='.$secureLogin.$secretKey;
  $hashNeed = md5($params);
  $curl = curl_init();
                                                    
  curl_setopt_array($curl, array(
    CURLOPT_URL => $urlRequest.'/balance/current',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => "secureLogin=".$secureLogin."&externalPlayerId=".$externalPlayerId."&hash=".$hashNeed,
    CURLOPT_HTTPHEADER => array(
      "Content-Type: application/x-www-form-urlencoded",
      "Cache-Control: no-cache"
    ),
  ));
                                                                
  $response = curl_exec($curl);
  //echo $response;
  curl_close($curl);
  $hasil = json_decode($response, true);
  $newSaldo = $hasil['balance'];
  if($newSaldo > 0){
    $updatePlayerSaldo = mysqli_query($conn,"UPDATE `tb_ppplayer` SET `balance` = '$newSaldo' WHERE userID = '$usersID' AND provider = 'PragmaticPlay'") or die(mysqli_error());
    sleep(2);
    $params1 = 'amount=-'.$newSaldo.'&externalPlayerId='.$externalPlayerId.'&externalTransactionId='.$kd_transaksi.'&secureLogin='.$secureLogin.$secretKey;
    $hashNeed1 = md5($params1);
    $curl1 = curl_init();
                                 
    curl_setopt_array($curl1, array(
      CURLOPT_URL => $urlRequest.'/balance/transfer',
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "POST",
      CURLOPT_POSTFIELDS => "secureLogin=".$secureLogin."&externalPlayerId=".$externalPlayerId."&externalTransactionId=".$kd_transaksi."&amount=-".$newSaldo."&hash=".$hashNeed1,
      CURLOPT_HTTPHEADER => array(
        "Content-Type: application/x-www-form-urlencoded",
        "Cache-Control: no-cache"
      ),
    ));
                                       
    $response1 = curl_exec($curl1);
    //echo $response1;
    curl_close($curl1);
    $hasil1 = json_decode($response1, true);
    if($hasil1['description'] == 'OK'){
      $getBalancePlayer = mysqli_query($conn,"SELECT * FROM `tb_ppplayer` WHERE userID = '$usersID' AND provider = 'PragmaticPlay'") or die(mysqli_error());
      $gbp = mysqli_fetch_array($getBalancePlayer);
      $balances = $gbp['balance'];
      $insert_transaksi = mysqli_query($conn,"INSERT INTO `tb_transaksi` (`kd_transaksi`, `date`, `transaksi`, `total`, `saldo`, `note`, `gameid`, `providerID`, `jenis`, `metode`, `pay_from`, `userID`, `status`) VALUES ('$kd_transaksi','$created_date','Transfer','$balances',0, '$catatan', '', '1','6','0','1','$usersID',1)") or die(mysqli_error($conn));
      $updateBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = '$balances' WHERE userID = '$usersID'") or die(mysqli_error($conn));
      $updatePlayer = mysqli_query($conn,"UPDATE `tb_ppplayer` SET `status` = 0, `balance` = balance - '$balances' WHERE userID = '$usersID' AND provider = 'PragmaticPlay'") or die(mysqli_error($conn));
          
      $params2 = 'externalPlayerId='.$externalPlayerId.'&secureLogin='.$secureLogin.$secretKey;
      $hashNeed2 = md5($params2);
      $curl2 = curl_init();
                                      
      curl_setopt_array($curl2, array(
        CURLOPT_URL => $urlRequest.'/game/session/terminate',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => "secureLogin=".$secureLogin."&externalPlayerId=".$externalPlayerId."&hash=".$hashNeed2,
        CURLOPT_HTTPHEADER => array(
          "Content-Type: application/x-www-form-urlencoded",
          "Cache-Control: no-cache"
        ),
      ));
                                              
      $response2 = curl_exec($curl2);
      //echo $response1;
      curl_close($curl2);
  
    }
  }

header('location:../room/');
exit();
?>