Files
crm.twinpol.com/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/importTEMUInvoices.php
2025-10-29 08:12:07 +00:00

130 lines
4.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// ====== KONFIG ======
$APP_KEY = '02cbeff8afd35247294810033b036cfe';
$APP_SECRET = '8ecfa21b55cddf66c66043cbb80756efc4ba6596';
$ACCESS_TOKEN = 'epld1qlfxtx9u9hfojczkqj7ez73jcqp3qiwy1aupqa5qcinxemkcja21dp';
$BASE_URL = 'https://openapi-b-eu.temu.com/openapi/router'; // EU router
$DATA_TYPE = 'JSON';
function noquote($v)
{
$j = json_encode($v, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
return (is_string($v) && $j[0] === '"' && substr($j, -1) === '"') ? substr($j, 1, -1) : $j;
}
function sign_md5($p, $secret)
{
ksort($p, SORT_STRING);
$s = $secret;
foreach ($p as $k => $v) {
if ($v !== '' && $v !== null) $s .= $k . noquote($v);
}
$s .= $secret;
return strtoupper(md5($s));
}
// --- LISTA ZAMÓWIEŃ (region 162, status 4) ---
$biz = [
"type" => "bg.order.list.v2.get",
"regionId" => 162,
"pageNumber" => 1,
"pageSize" => 10,
"parentOrderStatus" => 4
];
$common = [
'app_key' => $APP_KEY,
'access_token' => $ACCESS_TOKEN,
'data_type' => 'JSON',
'timestamp' => (int)floor(microtime(true)),
];
$req = $biz + $common;
$req['sign'] = sign_md5($req, $APP_SECRET);
$payload = json_encode($req, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $BASE_URL,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_POSTFIELDS => $payload
]);
$resp = curl_exec($ch);
$http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$data = json_decode($resp, true);
if (is_array($data)
&& isset($data['result'])
&& isset($data['result']['pageItems'])
&& is_array($data['result']['pageItems'])
&& count($data['result']['pageItems']) > 0) {
foreach ($data['result']['pageItems'] as $row) {
if (!isset($row['parentOrderMap']) || !isset($row['parentOrderMap']['parentOrderSn'])) {
continue;
}
$parentOrderSn = $row['parentOrderMap']['parentOrderSn'];
// === SEPARATOR ZAMÓWIENIA ===
echo '<hr style="margin:24px 0;border:0;border-top:2px solid #999;">';
echo '<h3 style="margin:6px 0;font-family:monospace;">ORDER: ' . htmlspecialchars($parentOrderSn, ENT_QUOTES, 'UTF-8') . '</h3>';
// 1) NAJPIERW OBIEKT Z LISTY
echo '<div style="border:1px solid #ddd;padding:8px;margin:8px 0;background:#fafafa;">';
echo '<div style="font-weight:bold;margin-bottom:6px;">=== ORDER FROM LIST (pageItems row) ===</div>';
debug_full($row);
echo '</div>';
// 2) POTEM DETAIL TEGO SAMEGO ZAMÓWIENIA
$detailBiz = [
"type" => "bg.order.detail.v2.get",
"parentOrderSn" => $parentOrderSn,
"regionId" => 162,
];
$detailReq = $detailBiz + [
'app_key' => $APP_KEY,
'access_token' => $ACCESS_TOKEN,
'data_type' => 'JSON',
'timestamp' => (int)floor(microtime(true)),
];
$detailReq['sign'] = sign_md5($detailReq, $APP_SECRET);
$detailPayload = json_encode($detailReq, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$ch2 = curl_init();
curl_setopt_array($ch2, [
CURLOPT_URL => $BASE_URL,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_POSTFIELDS => $detailPayload
]);
$detailResp = curl_exec($ch2);
$detailHttp = curl_getinfo($ch2, CURLINFO_HTTP_CODE);
curl_close($ch2);
$detailJson = json_decode($detailResp, true);
echo '<div style="border:1px solid #ddd;padding:8px;margin:8px 0;background:#f5faff;">';
echo '<div style="font-weight:bold;margin-bottom:6px;">=== ORDER DETAIL (HTTP ' . (int)$detailHttp . ') ===</div>';
debug_full($detailJson);
echo '</div>';
}
} else {
echo "<pre>(brak pageItems w liście nic do dociągnięcia)\n";
debug_full($data);
echo "</pre>";
}
function debug_full($data)
{
echo '<pre style="white-space:pre; font-size:12px; margin:0;">';
echo json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
echo '</pre>';
}