<?php

// Fungsi untuk memanggil API
function callAPI($request) {
    $url = parse_url($request);
    $path = $url['path'];
    $params = $url['query'];
    $endpoint = 'https://order-sg.codashop.com/initPayment.action';
    $id = $_GET['id'];
    $zone = $_GET['zone'];

    try {
        if (!$id) {
            return '{"success":false,"message":"Bad Request"}';
        }
        if (strpos($path, '/hsr') !== false) {
            if (strpos($id, '6') === 0) {
                $sn = 'America';
                $sv = 'prod_official_usa';
            } else if (strpos($id, '7') === 0) {
                $sn = 'Europe';
                $sv = 'prod_official_eur';
            } else if (strpos($id, '8') === 0) {
                $sn = 'Asia';
                $sv = 'prod_official_asia';
            } else if (strpos($id, '9') === 0) {
                $sn = 'SAR (Taiwan, Hong Kong, Macao)';
                $sv = 'prod_official_cht';
            } else {
                return '{"success":false,"message":"Bad request"}';
            }
            $body = http_build_query(array(
                'voucherPricePoint.id' => '855316',
                'voucherPricePoint.price' => '16000.0',
                'voucherPricePoint.variablePrice' => '0',
                'user.userId' => $id,
                'user.zoneId' => $sv,
                'voucherTypeName' => 'HONKAI_STAR_RAIL',
                'shopLang' => 'id_ID'
            ));
            $options = array(
                'http' => array(
                    'header' => "Content-Type: application/x-www-form-urlencoded\r\n",
                    'method' => 'POST',
                    'content' => $body
                )
            );
            $context = stream_context_create($options);
            $response = file_get_contents($endpoint, false, $context);
            $data = json_decode($response, true);
            return '{"success":true,"game":"Honkai: Star Rail","server":"' . $sn . '","id":"' . $id . '","name":"' . $data['confirmationFields']['username'] . '"}';
        }
        if (strpos($path, '/gi') !== false) {
            // Mengonversi bagian lain dari kode...
        }
        // Menambahkan kondisi untuk bagian lain dari kode...
    } catch (Exception $error) {
        return '{"success":false,"message":"Cannot find nickname from your request."}';
    }
}

// Fungsi untuk melayani hasil
function serveResult($request) {
    $dc = $_GET['decode'];
    $code = 200;
    $result = callAPI($request);
    if (strpos($result, '"undefined"') !== false) {
        $result = '{"success":false,"message":"Cannot find nickname from your request."}';
    }
    if (json_decode($result, true)['success'] == false) {
        $code = 400;
    }
    $result = str_replace("\u002B", "%20", $result);
    if (!$dc || $dc == true) {
        $result = urldecode($result);
    }
    $response = json_encode($result);
    http_response_code($code);
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Method: GET');
    header('Cache-Control: max-age=600');
    header('Content-Type: application/json; charset=utf-8');
    return $response;
}

// Fungsi untuk memeriksa cache
function checkCache($request) {
    if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
        http_response_code(405);
        return 'Method Not Allowed';
    }
    $now = time();
    $cache = caches.default;
    $response = $cache->get($request);
    if (!$response) {
        $response = serveResult($request);
        $cache->put($request, $response);
    }
    header_remove('Cache-Control');
    header('X-Response-Time: ' . (time() - $now));
    return $response;
}

// Pemanggilan fungsi untuk memeriksa cache
checkCache($_SERVER['REQUEST_URI']);

?>
