File: /home/storage/5/78/dd/wicomm2/public_html/blackfriday/lib/VTEX.php
<?php
require_once __DIR__.'/Utils.php';
class VTEXClient {
private string $account;
private string $env;
private string $appKey;
private string $appToken;
private string $entity;
public function __construct() {
foreach (['VTEX_ACCOUNT','VTEX_APPKEY','VTEX_APPTOKEN'] as $c) {
if (!defined($c) || constant($c) === '') {
throw new Exception("$c não configurado em config/config.php");
}
}
$this->account = VTEX_ACCOUNT;
$this->env = defined('VTEX_ENVIRONMENT') ? VTEX_ENVIRONMENT : 'vtexcommercestable';
$this->appKey = VTEX_APPKEY;
$this->appToken = VTEX_APPTOKEN;
$this->entity = defined('VTEX_DATA_ENTITY') ? VTEX_DATA_ENTITY : 'BF';
}
private function url(string $path): string {
return "https://{$this->account}.{$this->env}.com.br{$path}";
}
public function saveBF(array $payload): array {
$url = $this->url("/api/dataentities/{$this->entity}/documents");
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'X-VTEX-API-AppKey: '.$this->appKey,
'X-VTEX-API-AppToken: '.$this->appToken
],
CURLOPT_POSTFIELDS => json_encode($payload, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES),
CURLOPT_TIMEOUT => 30
]);
$res = curl_exec($ch);
if ($res === false) throw new Exception('VTEX request failed: '.curl_error($ch));
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$json = json_decode($res, true);
if ($code >= 300) throw new Exception("VTEX error $code: $res");
return $json ?: ['raw'=>$res];
}
}