HEX
Server: Apache
System: Linux vpshost11508.publiccloud.com.br 5.15.179-grsec-vpshost-10.lc.el8.x86_64 #1 SMP Mon Apr 7 12:04:45 -03 2025 x86_64
User: wicomm2 (10002)
PHP: 8.3.0
Disabled: apache_child_terminate,dl,escapeshellarg,escapeshellcmd,exec,link,mail,openlog,passthru,pcntl_alarm,pcntl_exec,pcntl_fork,pcntl_get_last_error,pcntl_getpriority,pcntl_setpriority,pcntl_signal,pcntl_signal_dispatch,pcntl_sigprocmask,pcntl_sigtimedwait,pcntl_sigwaitinfo,pcntl_strerror,pcntl_wait,pcntl_waitpid,pcntl_wexitstatus,pcntl_wifexited,pcntl_wifsignaled,pcntl_wifstopped,pcntl_wstopsig,pcntl_wtermsig,php_check_syntax,php_strip_whitespace,popen,proc_close,proc_open,shell_exec,symlink,system
Upload Files
File: /home/storage/5/78/dd/wicomm2/public_html/clientes/eucatex/showcase/search.php
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    http_response_code(204);
    exit;
}
header('Content-Type: application/json');
ini_set('display_errors', 1);
error_reporting(E_ALL);

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

$cep = $_GET['cep'] ?? null;
$ean = $_GET['ean'] ?? null;

if (!$cep || !$ean) {
    http_response_code(400);
    echo json_encode(['error' => 'Missing parameters (cep or ean)']);
    exit;
}

// log simples de debug
file_put_contents(__DIR__ . '/debug_log.txt', date('Y-m-d H:i:s') . " CEP: {$cep} | EAN: {$ean}\n", FILE_APPEND);

$MD_APPKEY = getenv('VTEX_APP_KEY') ?: 'vtexappkey-lojaeucatex-CZXEWO';
$MD_APPTOKEN = getenv('VTEX_APP_TOKEN') ?: 'TOBKCMNYXCXZWZCNXTZAAHHUSVWPOGNJGDWHAIWNOTGKGUWHRFHRYRLFJSEOFPDPNEWZWDIEWXNWNLWSXVPODRWJDDQSPLQGGCYQLKSAVHBVEINUONIAEPNOSUCTYVOX';

// Parâmetros
$cep = $_GET['cep'] ?? null;
$ean = $_GET['ean'] ?? null;

if (!$cep || !$ean) {
    echo json_encode(['error' => 'Missing parameters (cep, ean)']);
    exit;
}

// Função: Buscar produto por EAN na VTEX
function vtexSearchByEan($account, $env, $ean)
{
    $url = "https://{$account}.{$env}/api/catalog_system/pub/products/search?fq=alternateIds_Ean:{$ean}";
    $ch = curl_init($url);
    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_TIMEOUT => 15,
    ]);
    $resp = curl_exec($ch);
    $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    if ($status !== 200 || !$resp) return null;

    $data = json_decode($resp, true);
    return $data[0] ?? null;
}

// Função: Fazer simulation para cada seller
function vtexSimulation($account, $appKey, $appToken, $skuId, $sellerId, $cep, $env, $sc)
{
    $url = "https://{$account}.{$env}/api/checkout/pub/orderForms/simulation?sc={$sc}";
    $payload = [
        'items' => [[
            'id' => (string)$skuId,
            'quantity' => 1,
            'seller' => (string)$sellerId,
        ]],
        'country' => 'BRA',
        'postalCode' => $cep,
    ];

    $headers = [
        "Content-Type: application/json",
        "X-VTEX-API-AppKey: {$appKey}",
        "X-VTEX-API-AppToken: {$appToken}",
    ];

    $ch = curl_init($url);
    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST => true,
        CURLOPT_HTTPHEADER => $headers,
        CURLOPT_POSTFIELDS => json_encode($payload),
        CURLOPT_TIMEOUT => 15,
    ]);

    $resp = curl_exec($ch);
    $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    if ($status !== 200 || !$resp) return null;

    return json_decode($resp, true);
}

// Configurações
$VTEX_ACCOUNT = 'lojaeucatex';
$VTEX_ENV = 'vtexcommercestable.com.br';
$VTEX_APPKEY = 'vtexappkey-lojaeucatex-CZXEWO';
$VTEX_APPTOKEN = 'TOBKCMNYXCXZWZCNXTZAAHHUSVWPOGNJGDWHAIWNOTGKGUWHRFHRYRLFJSEOFPDPNEWZWDIEWXNWNLWSXVPODRWJDDQSPLQGGCYQLKSAVHBVEINUONIAEPNOSUCTYVOX';
$VTEX_SC = '1';

// Buscar produto
$product = vtexSearchByEan($VTEX_ACCOUNT, $VTEX_ENV, $ean);
if (!$product) {
    echo json_encode(['error' => 'Product not found']);
    exit;
}

$result = [
    'ean' => $ean,
    'sku_found' => false,
    'skuId' => null,
    'sellerId' => null,
    'checked_sellers' => [],
];

foreach ($product['items'] ?? [] as $item) {
    $skuId = $item['itemId'] ?? ($item['sku'] ?? null);
    foreach ($item['sellers'] ?? [] as $seller) {
        $sellerId = $seller['sellerId'] ?? null;
        if (!$skuId || !$sellerId) continue;

        $sim = vtexSimulation($VTEX_ACCOUNT, $VTEX_APPKEY, $VTEX_APPTOKEN, $skuId, $sellerId, $cep, $VTEX_ENV, $VTEX_SC);
        if ($sim && !empty($sim['valid_slas'])) {
            // Captura skuId do simulation se não veio do produto
            if (!$skuId && !empty($sim['items'][0]['id'])) {
                $skuId = $sim['items'][0]['id'];
            }
            $result['sku_found'] = true;
            $result['skuId'] = $skuId;
            $result['sellerId'] = $sellerId;
            echo json_encode($result);
            exit;
        }

        $result['checked_sellers'][] = $sellerId;
    }
}

echo json_encode($result);