File: /home/storage/5/78/dd/wicomm2/public_html/clientes/dpaschoal/hook/masterdata_saver.php
<?php
function saveToMasterData($order, $orderId, $apiKey, $apiToken, $accountName, $currentState)
{
if ($currentState != 'payment-approved') {
exit;
}
$orderSeller = $order['sellers'][0]['name'] ?? '';
// Blocked sellers list
$blockedSellers = [
'dpaschoal273itajai',
'dpaschoal282hubcampinas',
'dpaschoal286hubcachoeirinha',
'dpaschoal300hubguarulhos',
'dpaschoal303hubribeiraopreto',
'dpaschoal304hubduquedecaxias',
'dpaschoal308hububerlandia',
'dpaschoal309hubsaojosedospinhais',
'dpaschoal310hubcascavel',
'dpaschoal311serra',
'dpaschoal311hubserra',
'dpaschoal314hubcarazinho',
'dpaschoaltopservicesp',
'dpaschoal175campinas'
];
if (in_array($orderSeller, $blockedSellers)) {
logDebug("🚫 Skipping Master Data save — Seller is blocked: $orderSeller");
return;
}
$isPickup = false;
foreach ($order['shippingData']['logisticsInfo'] as $logisticsItem) {
if (($logisticsItem['deliveryChannel'] ?? '') === 'pickup-in-point') {
$isPickup = true;
break;
}
}
if (!$isPickup) {
logDebug("Order is not for pickup. Skipping Master Data save.");
return;
}
$pickupInfo = $order['shippingData']['logisticsInfo'][0]['pickupStoreInfo'] ?? [];
$pickupAddress = $pickupInfo['address'] ?? [];
$pickupCompanyName = $pickupInfo['friendlyName'] ?? '';
$pickupCompanyAddress = $pickupAddress ? formatAddress($pickupAddress) : '';
$customerEmail = $order['clientProfileData']['email'] ?? '';
$shippingEstimate = $order['shippingData']['logisticsInfo'][0]['shippingEstimate'] ?? '';
$customerEmailDecrypted = null;
if (!empty($order['marketingData']['utmipage'])) {
$utmiPageData = $order['marketingData']['utmipage'];
$jsonDecoded = json_decode($utmiPageData, true);
if (is_array($jsonDecoded) && isset($jsonDecoded[0]['userProperties'])) {
$encodedEmail = $jsonDecoded[0]['userProperties'];
$customerEmailDecrypted = base64_decode($encodedEmail);
} else {
$customerEmailDecrypted = base64_decode($utmiPageData);
}
}
$payload = [
"orderId" => $orderId,
"customerName" => $order['clientProfileData']['firstName'] . ' ' . $order['clientProfileData']['lastName'],
"customerEmail" => $customerEmail,
"customerEmailDecrypted" => $customerEmailDecrypted ?? $customerEmail,
"isReadyForPickup" => false,
"isTriggerFired" => false,
"pickupCompanyName" => $pickupCompanyName,
"pickupCompanyAddress" => $pickupCompanyAddress,
"shippingEstimate" => $shippingEstimate,
"orderSeller" => $orderSeller
];
$saveUrl = "https://$accountName.vtexcommercestable.com.br/api/dataentities/OH/documents";
$saveResponse = vtexPostRequest($saveUrl, $apiKey, $apiToken, $payload);
logDebug("Master Data Payload: " . json_encode($payload));
logDebug("Master Data Save Response: " . $saveResponse);
}