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_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;
}
?>