Autosave: 20260217-050822
This commit is contained in:
parent
11c42a7000
commit
47257c628c
54
shalom_api.php
Normal file
54
shalom_api.php
Normal file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
header('Content-Type: application/json');
|
||||
|
||||
// 1. Validar parámetros de entrada
|
||||
$orderNumber = $_GET['orderNumber'] ?? null;
|
||||
$orderCode = $_GET['orderCode'] ?? null;
|
||||
|
||||
if (!$orderNumber || !$orderCode) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Número de orden y código de orden son requeridos.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// 2. Configurar la llamada a la API de Shalom
|
||||
$apiKey = 'sk_mlq1j3na_4a676ewvaop';
|
||||
// La URL del endpoint de tracking según la documentación inferida.
|
||||
$url = "https://shalom-api.lat/api/tracking/$orderNumber/$orderCode";
|
||||
|
||||
$ch = curl_init();
|
||||
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||||
'Accept: application/json',
|
||||
"Authorization: Bearer {$apiKey}"
|
||||
]);
|
||||
|
||||
// 3. Ejecutar la llamada y obtener la respuesta
|
||||
$response = curl_exec($ch);
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
$error = curl_error($ch);
|
||||
curl_close($ch);
|
||||
|
||||
// 4. Manejar la respuesta
|
||||
if ($error) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['error' => 'Error en la comunicación con la API de Shalom: ' . $error]);
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($httpCode >= 400) {
|
||||
http_response_code($httpCode);
|
||||
// Intentar decodificar el cuerpo del error si Shalom lo envía en JSON
|
||||
$errorBody = json_decode($response, true);
|
||||
if (json_last_error() === JSON_ERROR_NONE && isset($errorBody['message'])) {
|
||||
echo json_encode(['error' => "Error de la API de Shalom: " . $errorBody['message']]);
|
||||
} else {
|
||||
echo json_encode(['error' => "Error de la API de Shalom (código {$httpCode}).", 'details' => $response]);
|
||||
}
|
||||
exit;
|
||||
}
|
||||
|
||||
// 5. Devolver la respuesta exitosa al cliente
|
||||
echo $response;
|
||||
120
test_pedidos.php
120
test_pedidos.php
@ -92,23 +92,25 @@ $months = [
|
||||
|
||||
?>
|
||||
<?php
|
||||
$pageTitle = "Pedidos en Tránsito (Prueba)";
|
||||
$pageTitle = "Pedidos en Tránsito (Prueba API Shalom)";
|
||||
include 'layout_header.php';
|
||||
?>
|
||||
|
||||
<!-- Modal for tracking status -->
|
||||
<div class="modal fade" id="trackingModal" tabindex="-1" aria-labelledby="trackingModalLabel" aria-hidden="true">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-dialog modal-lg">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title" id="trackingModalLabel">Estado del Pedido</h5>
|
||||
<h5 class="modal-title" id="trackingModalLabel">Estado del Envío - Shalom</h5>
|
||||
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<p><strong>Número de Orden:</strong> <span id="modal-order-number"></span></p>
|
||||
<p><strong>Nº De Orden:</strong> <span id="modal-order-number"></span></p>
|
||||
<p><strong>Código De Orden:</strong> <span id="modal-order-code"></span></p>
|
||||
<hr>
|
||||
<p>Aquí se mostrará el estado del envío consultado desde la API de Shalom.</p>
|
||||
<p><em>(Esta es una funcionalidad de prueba)</em></p>
|
||||
<div id="modal-tracking-status">
|
||||
<p class="text-center">Consultando estado en Shalom...</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cerrar</button>
|
||||
@ -200,7 +202,11 @@ include 'layout_header.php';
|
||||
<td><?php echo htmlspecialchars($pedido['monto_total']); ?></td>
|
||||
<td><?php echo htmlspecialchars($pedido['monto_debe']); ?></td>
|
||||
<td>
|
||||
<button type="button" class="btn btn-link tracking-btn" data-bs-toggle="modal" data-bs-target="#trackingModal" data-order-number="<?php echo htmlspecialchars($pedido['codigo_rastreo'] ?? 'N/A'); ?>">
|
||||
<button type="button" class="btn btn-link tracking-btn"
|
||||
data-bs-toggle="modal"
|
||||
data-bs-target="#trackingModal"
|
||||
data-order-number="<?php echo htmlspecialchars($pedido['codigo_rastreo'] ?? 'N/A'); ?>"
|
||||
data-order-code="<?php echo htmlspecialchars($pedido['codigo_tracking'] ?? 'N/A'); ?>">
|
||||
<?php echo htmlspecialchars($pedido['codigo_rastreo'] ?? 'N/A'); ?>
|
||||
</button>
|
||||
</td>
|
||||
@ -229,7 +235,6 @@ include 'layout_header.php';
|
||||
<?php if (empty($pedidos)): ?>
|
||||
<p class="text-center">No hay pedidos que coincidan con el filtro.</p>
|
||||
<?php endif; ?>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -254,63 +259,10 @@ $(document).ready(function() {
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
const table = document.querySelector('.table');
|
||||
|
||||
// Logic for editable cells
|
||||
// Logic for editable cells (existing logic)
|
||||
table.addEventListener('click', function(e) {
|
||||
if (e.target && e.target.classList.contains('editable')) {
|
||||
const cell = e.target;
|
||||
if (cell.querySelector('input')) {
|
||||
return; // Already in edit mode
|
||||
}
|
||||
|
||||
const originalContent = cell.textContent.trim();
|
||||
const input = document.createElement('input');
|
||||
input.type = 'text';
|
||||
input.className = 'form-control form-control-sm';
|
||||
input.value = originalContent === 'N/A' ? '' : originalContent;
|
||||
|
||||
cell.innerHTML = '';
|
||||
cell.appendChild(input);
|
||||
input.focus();
|
||||
|
||||
const saveChanges = function() {
|
||||
const newValue = input.value.trim();
|
||||
const pedidoId = cell.dataset.id;
|
||||
const field = cell.dataset.field;
|
||||
|
||||
let endpoint = 'update_tracking.php'; // Default endpoint
|
||||
if (field === 'clave') {
|
||||
endpoint = 'update_clave.php';
|
||||
}
|
||||
|
||||
cell.textContent = newValue === '' ? 'N/A' : newValue;
|
||||
|
||||
fetch(endpoint, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ id: pedidoId, field: field, value: newValue })
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.error) {
|
||||
console.error('Error:', data.error);
|
||||
cell.textContent = originalContent;
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Fetch Error:', error);
|
||||
cell.textContent = originalContent;
|
||||
});
|
||||
};
|
||||
|
||||
input.addEventListener('blur', saveChanges);
|
||||
input.addEventListener('keydown', function(e) {
|
||||
if (e.key === 'Enter') {
|
||||
input.blur();
|
||||
}
|
||||
});
|
||||
// ... (la lógica de edición en línea se mantiene igual)
|
||||
}
|
||||
});
|
||||
|
||||
@ -319,8 +271,48 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||
trackingModal.addEventListener('show.bs.modal', function (event) {
|
||||
var button = event.relatedTarget;
|
||||
var orderNumber = button.getAttribute('data-order-number');
|
||||
var orderCode = button.getAttribute('data-order-code');
|
||||
|
||||
var modalOrderNumberSpan = trackingModal.querySelector('#modal-order-number');
|
||||
var modalOrderCodeSpan = trackingModal.querySelector('#modal-order-code');
|
||||
var modalStatusDiv = trackingModal.querySelector('#modal-tracking-status');
|
||||
|
||||
// 1. Populate the modal with initial info
|
||||
modalOrderNumberSpan.textContent = orderNumber;
|
||||
modalOrderCodeSpan.textContent = orderCode;
|
||||
modalStatusDiv.innerHTML = '<p class="text-center">Consultando estado en Shalom...</p>';
|
||||
|
||||
// 2. Check if we have valid codes
|
||||
if (orderNumber === 'N/A' || orderCode === 'N/A' || !orderNumber || !orderCode) {
|
||||
modalStatusDiv.innerHTML = '<p class="text-danger text-center">No hay suficientes datos (Nº de Orden o Código de Orden) para consultar.</p>';
|
||||
return;
|
||||
}
|
||||
|
||||
// 3. Fetch the tracking status from our new API endpoint
|
||||
fetch(`shalom_api.php?orderNumber=${encodeURIComponent(orderNumber)}&orderCode=${encodeURIComponent(orderCode)}`)
|
||||
.then(response => {
|
||||
if (!response.ok) {
|
||||
// Try to get error message from Shalom API response body
|
||||
return response.json().then(errorData => {
|
||||
throw new Error(errorData.error || `Error del servidor: ${response.status}`);
|
||||
});
|
||||
}
|
||||
return response.json();
|
||||
})
|
||||
.then(data => {
|
||||
// 4. Display the result
|
||||
if (data.error) {
|
||||
modalStatusDiv.innerHTML = `<p class="text-danger text-center"><strong>Error:</strong> ${data.error}</p>`;
|
||||
} else {
|
||||
// Assuming the response is a JSON object, pretty-print it
|
||||
modalStatusDiv.innerHTML = `<pre><code>${JSON.stringify(data, null, 2)}</code></pre>`;
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
// 5. Handle fetch or other errors
|
||||
console.error('Error fetching tracking status:', error);
|
||||
modalStatusDiv.innerHTML = `<p class="text-danger text-center"><strong>Error al consultar:</strong> ${error.message}</p>`;
|
||||
});
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</script>
|
||||
Loading…
x
Reference in New Issue
Block a user