File: /home/storage/5/78/dd/wicomm2/public_html_backup/clientes/amakha/index.php
<?php
require 'vendor/autoload.php';
use GuzzleHttp\Client;
$client = new Client(['verify' => false]);
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$json = file_get_contents('php://input');
$dataSend = json_decode($json, true);
if (isset($dataSend['action'])) {
switch ($dataSend['action']) {
case 'post':
processCart($client, $dataSend);
break;
case 'put':
updateLeadInRDStation($client, $dataSend);
break;
default:
respondError('Action not found');
}
} else {
respondError('Action parameter missing');
}
}
// Processa dados do carrinho
function processCart($client, $lead) {
try {
$cartData = fetchVtexCart($client, $lead['cartId']);
sendToRDStation($client, $cartData);
} catch (Exception $e) {
respondError($e->getMessage());
}
}
// Obtém informações do carrinho na VTEX
function fetchVtexCart($client, $cartId) {
$url = "https://amakha.myvtex.com/api/checkout/pub/orderForm/$cartId";
$headers = [
"Accept" => "application/json",
"Content-Type" => "application/json",
"X-VTEX-API-AppKey" => "vtexappkey-amakha-UPXQDT",
"X-VTEX-API-AppToken" => "XTVOHWWRKTJHISGQWTSNEYFRJDURMKWYRRYNNKNFFXMQIUZHPFXREWLTLBICVHLTFGAFCBONPXEEDLDBDZCFTLPHJMPQDFJNFSSUSIBMZZDCCPHKMLNTAPIYFWNIJOHT"
];
$response = $client->request('GET', $url, ['headers' => $headers]);
if ($response->getStatusCode() !== 200) {
throw new Exception("Failed to fetch VTEX cart. HTTP Code: " . $response->getStatusCode());
}
return json_decode($response->getBody(), true);
}
// Envia dados para RD Station
function sendToRDStation($client, $cartData) {
$url = "https://api.rd.services/platform/events";
$headers = [
"Authorization" => "Bearer 674604ac9b396a001a450b04",
"Content-Type" => "application/json"
];
$payload = [
"event_type" => "CART_ABANDONED",
"event_family" => "CDP",
"payload" => [
"email" => $cartData['clientProfileData']['email'],
"name" => $cartData['clientProfileData']['firstName'] . ' ' . $cartData['clientProfileData']['lastName'],
"cf_cart_total" => $cartData['value'] / 100,
"cf_cart_items" => array_map(function ($item) {
return [
"sku" => $item['id'],
"name" => $item['name'],
"price" => $item['price'] / 100,
"quantity" => $item['quantity']
];
}, $cartData['items'])
]
];
$response = $client->request('POST', $url, [
'headers' => $headers,
'json' => $payload
]);
if ($response->getStatusCode() !== 201) {
throw new Exception("Failed to send data to RD Station. HTTP Code: " . $response->getStatusCode());
}
echo json_encode(['message' => 'Data sent successfully']);
}
// Atualiza um lead na RD Station
function updateLeadInRDStation($client, $lead) {
try {
$url = "https://crm.rdstation.com/api/v1/deals/{$lead['id']}";
$headers = [
"Content-Type" => "application/json",
"Authorization" => "Bearer 674604ac9b396a001a450b04"
];
$data = [
"win" => true,
"deal_stage_id" => "6470c007f7e553001cf2fd12"
];
$response = $client->request('PUT', $url, [
'headers' => $headers,
'json' => $data
]);
if ($response->getStatusCode() === 200) {
echo json_encode(['message' => 'Lead updated successfully']);
} else {
throw new Exception("Failed to update lead in RD Station");
}
} catch (Exception $e) {
respondError($e->getMessage());
}
}
// Responde erro no formato JSON
function respondError($message) {
echo json_encode(['error' => $message]);
exit;
}
?>