<?php
header('Content-Type: application/json');

function generateSignature($operatorcode, $password, $providercode, $type, $username, $secret_key) {
    $raw_string = $operatorcode . $password . $providercode . $type . $username . $secret_key;
    return strtoupper(md5($raw_string));
}

function generateSignatureTo($operatorcode, $secret_key) {
    $raw_string = $operatorcode . $secret_key;
    return strtoupper(md5($raw_string));
}
function generateSignatureHistory($operatorcode, $secret_key) {
    $raw_string = $operatorcode . $secret_key;
    return strtoupper(md5($raw_string));
}
function generateSignatureGame($operatorcode, $providercode, $secret_key) {
    $raw_string = $operatorcode . $providercode . $secret_key;
    return strtoupper(md5($raw_string));
}

function generateSignatureBalance($operatorcode, $password, $providercode, $username, $secret_key) {
    $raw_string = $operatorcode . $password . $providercode . $username . $secret_key;
    return strtoupper(md5($raw_string));
}
function generateSignatureInGame($operatorcode, $password, $providercode, $username, $secret_key) {
    $raw_string = $operatorcode . $password . $providercode . $username . $secret_key;
    return strtoupper(md5($raw_string));
}
function generateSignatureProduct($operatorcode, $providercode, $username, $secret_key) {
    $raw_string = $operatorcode . $providercode . $username . $secret_key;
    return strtoupper(md5($raw_string));
}
function generateSignatureGetGame($operatorcode, $providercode, $secret_key) {
    $raw_string = $operatorcode . $providercode . $username . $secret_key;
    return strtoupper(md5($raw_string));
}
function generateReferenceId($length = 19) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $referenceId = '';

    for ($i = 0; $i < $length; $i++) {
        $referenceId .= $characters[random_int(0, $charactersLength - 1)];
    }

    return $referenceId;
}

function withdraw($amount, $operatorcode, $password, $providercode, $typeTrx, $username, $secret_key, $referenceid) {
    $raw_string = $amount . $operatorcode . $password . $providercode . $referenceid . $typeTrx . $username . $secret_key;
    return strtoupper(md5($raw_string));
}

// Variabel Input
$operatorcode = "oaci";
$username = strtolower("z7k51VUc");
$password = "ogF17NuO";
$typeTrx = "1";
$amount = "10000";
$providercode = "GB";
$type = "SL";
$secret_key = "f3c865e04f2f3ebf08269f1d278389fb";
$gameid = "1";
$ticket = '46360187669';

$referenceid = generateReferenceId();

// Generate Signatures
$signature = generateSignature($operatorcode, $password, $providercode, $type, $username, $secret_key);
$signatureTo = generateSignatureTo($operatorcode, $secret_key);
$signatureGame = generateSignatureGame($operatorcode, $providercode, $secret_key);
$signatureGet = generateSignatureBalance($operatorcode, $password, $providercode, $username, $secret_key);
$signatureInGame = generateSignatureInGame($operatorcode, $password, $providercode, $username, $secret_key);
$signaturewithdraw = withdraw($amount, $operatorcode, $password, $providercode, $typeTrx, $username, $secret_key, $referenceid);
$HistoryA = generateSignatureHistory($operatorcode, $secret_key);
$HistoryB = generateSignatureHistory($operatorcode, $secret_key);
$HistoryG = generateSignatureHistory($operatorcode, $secret_key);

// Output JSON
$output = [
    "launchGame" => "http://gsmd.336699bet.com/launchGames.aspx?" . http_build_query([
        "operatorcode" => $operatorcode,
        "providercode" => $providercode,
        "username" => $username,
        "password" => $password,
        "type" => $type,
        "gameid" => $gameid,
        "lang" => "id-ID",
        "html5" => "1",
        "signature" => $signature
    ]),
    "getBalance" => "http://gsmd.336699bet.com/getBalance.aspx?" . http_build_query([
        "operatorcode" => $operatorcode,
        "providercode" => $providercode,
        "username" => $username,
        "password" => $password,
        "signature" => $signatureGet
    ]),
    "makeTransfer" => "http://gsmd.336699bet.com/makeTransfer.aspx?" . http_build_query([
        "operatorcode" => $operatorcode,
        "providercode" => $providercode,
        "username" => $username,
        "password" => $password,
        "referenceid" => $referenceid,
        "type" => $typeTrx,
        "amount" => $amount,
        "signature" => $signaturewithdraw
    ]),
    "getGameList" => "http://gsmd.336699bet.com/getGameList.ashx?" . http_build_query([
        "operatorcode" => $operatorcode,
        "providercode" => $providercode,
        "lang" => "id-ID",
        "html" => "0",
        "reformatjson" => "yes",
        "signature" => $signatureGame
    ]),
    "getHistory" => "http://fetch.336699bet.com/fetchbykey.aspx?" . http_build_query([
        "operatorcode" => $operatorcode,
        "versionkey" => "0",
        "signature" => $signatureTo
    ]),
    "PlayerInGame" => "http://gsmd.336699bet.com/isPlayerIngame.ashx?" . http_build_query([
        "operatorcode" => $operatorcode,
        "providercode" => $providercode,
        "username" => $username,
        "password" => $password,
        "signature" => $signatureInGame
    ]),
    "History_Archieve" => "https://fetch.336699bet.com/fetchArchieve.aspx?" . http_build_query([
        "operatorcode" => $operatorcode,
        "versionkey" => 0,
        "signature" => $HistoryA
    ]),
    "History_Betting" => "https://fetch.336699bet.com/markbyjson.aspx?" . http_build_query([
        "operatorcode" => $operatorcode,
        "ticket" => $ticket,
        "signature" => $HistoryB
    ]),
    "History_BettingGet" => "https://fetch.336699bet.com/markArchieve.ashx?" . http_build_query([
        "operatorcode" => $operatorcode,
        "ticket" => $ticket,
        "signature" => $HistoryG
    ]),
    "CheckProduct" => "http://gsmd.336699bet.com/checkMemberProductUsername.ashx?" . http_build_query([
        "operatorcode" => $operatorcode,
        "providercode" => $providercode,
        "username" => "$username",
        "signature" => $signatureTo
    ])
];

echo json_encode($output, JSON_PRETTY_PRINT);
?>