WIP: TEMU
This commit is contained in:
@@ -3,7 +3,6 @@
|
||||
//error_reporting(LC_ALL);
|
||||
//ini_set('display_errors', 1);
|
||||
// ?XDEBUG_SESSION_START=PHPSTORM
|
||||
//importInvoices();
|
||||
function apilo_importInvoices()
|
||||
{
|
||||
$apilo_config = apilo_loadConfiguration();
|
||||
@@ -14,7 +13,8 @@ function apilo_importInvoices()
|
||||
$offset = intval($db->fetchByAssoc($dbRes)['last_id']);
|
||||
|
||||
$invoices = apilo_loadInvoices($apilo_config['token'], $offset);
|
||||
if (isset($invoices->error)) {
|
||||
brecho($apilo_config);
|
||||
if (isset($invoices->errors)) {
|
||||
if (apilo_refreshToken($apilo_config['refreshToken'], $apilo_config['clientId'], $apilo_config['clientSecret']) == true) {
|
||||
//$apilo_config = apilo_loadConfiguration();
|
||||
//$invoices = apilo_loadInvoices($apilo_config['token'], $offset);
|
||||
@@ -23,6 +23,8 @@ function apilo_importInvoices()
|
||||
}
|
||||
}
|
||||
|
||||
brecho($invoices);
|
||||
|
||||
$GLOBALS['log']->bimit('----- Importing invoices from Apilo, documents count', count($invoices->documents));
|
||||
|
||||
$platforms = apilo_loadPlatformsList($apilo_config['token']);
|
||||
|
||||
@@ -6,77 +6,125 @@ $ACCESS_TOKEN = 'epld1qlfxtx9u9hfojczkqj7ez73jcqp3qiwy1aupqa5qcinxemkcja21dp';
|
||||
$BASE_URL = 'https://openapi-b-eu.temu.com/openapi/router'; // EU router
|
||||
$DATA_TYPE = 'JSON';
|
||||
|
||||
|
||||
// ==== HELPERY ====
|
||||
function json_stringify_no_outer_quotes($val)
|
||||
function noquote($v)
|
||||
{
|
||||
$json = json_encode($val, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
if (is_string($val) && strlen($json) >= 2 && $json[0] === '"' && substr($json, -1) === '"') return substr($json, 1, -1);
|
||||
return $json;
|
||||
$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 temu_sign_md5(array $params, $secret)
|
||||
function sign_md5($p, $secret)
|
||||
{
|
||||
ksort($params, SORT_STRING);
|
||||
$buf = $secret;
|
||||
foreach ($params as $k => $v) {
|
||||
if ($v !== '' && $v !== null) {
|
||||
$buf .= $k . json_stringify_no_outer_quotes($v);
|
||||
}
|
||||
ksort($p, SORT_STRING);
|
||||
$s = $secret;
|
||||
foreach ($p as $k => $v) {
|
||||
if ($v !== '' && $v !== null) $s .= $k . noquote($v);
|
||||
}
|
||||
$buf .= $secret;
|
||||
return strtoupper(md5($buf));
|
||||
$s .= $secret;
|
||||
return strtoupper(md5($s));
|
||||
}
|
||||
|
||||
function http_post_json_diag($url, array $body)
|
||||
{
|
||||
$ch = curl_init();
|
||||
$payload = json_encode($body, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_URL => $url, // <— jawnie ustawiamy URL (by uniknąć „malformed”)
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_POST => true,
|
||||
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
|
||||
CURLOPT_POSTFIELDS => $payload, // JSON body jak w Postmanie
|
||||
CURLOPT_CONNECTTIMEOUT => 30,
|
||||
CURLOPT_TIMEOUT => 60,
|
||||
CURLOPT_SSL_VERIFYPEER => true,
|
||||
CURLOPT_SSL_VERIFYHOST => 2,
|
||||
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
|
||||
]);
|
||||
$resp = curl_exec($ch);
|
||||
if ($resp === false) {
|
||||
$errno = curl_errno($ch);
|
||||
$err = curl_error($ch);
|
||||
curl_close($ch);
|
||||
die("cURL error: #$errno $err\nURL: $url\nBODY: $payload\n");
|
||||
}
|
||||
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
return [$code, $resp];
|
||||
}
|
||||
|
||||
// ==== REQUEST: najpierw lista v2 (tak jak wcześniej) ====
|
||||
|
||||
// --- LISTA ZAMÓWIEŃ (region 162, status 4) ---
|
||||
$biz = [
|
||||
'type' => 'bg.order.list.v2.get',
|
||||
'parentOrderStatus' => 0,
|
||||
'regionId' => 211,
|
||||
'pageNumber' => 1, // jeśli router zgłosi błąd paramów, zamień na 'pageNo'
|
||||
'pageSize' => 1,
|
||||
"type" => "bg.order.list.v2.get",
|
||||
"regionId" => 162,
|
||||
"pageNumber" => 1,
|
||||
"pageSize" => 10,
|
||||
"parentOrderStatus" => 4
|
||||
];
|
||||
|
||||
// common wg Twojego pre-request
|
||||
$common = [
|
||||
'app_key' => $APP_KEY,
|
||||
'access_token' => $ACCESS_TOKEN,
|
||||
'data_type' => $DATA_TYPE,
|
||||
'data_type' => 'JSON',
|
||||
'timestamp' => (int)floor(microtime(true)),
|
||||
];
|
||||
|
||||
$req = $biz + $common;
|
||||
$req['sign'] = temu_sign_md5($req, $APP_SECRET);
|
||||
$req['sign'] = sign_md5($req, $APP_SECRET);
|
||||
|
||||
list($http, $raw) = http_post_json_diag($BASE_URL, $req);
|
||||
$payload = json_encode($req, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
|
||||
echo "HTTP $http\n=== REQUEST BODY ===\n", json_encode($req, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
|
||||
echo "=== RAW RESPONSE ===\n$raw\n";
|
||||
$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>';
|
||||
}
|
||||
Reference in New Issue
Block a user