23-01-2026
This commit is contained in:
parent
a939cd51ef
commit
a8b395e20d
248 changed files with 29342 additions and 4805 deletions
|
|
@ -2,9 +2,11 @@
|
|||
|
||||
namespace App\Services;
|
||||
|
||||
use Acme\Dhl\Models\DhlShipment;
|
||||
use App\Models\ShoppingOrder;
|
||||
use App\Http\Controllers\SettingController;
|
||||
use App\Jobs\CreateShipmentJob;
|
||||
use App\Jobs\CancelShipmentJob;
|
||||
use App\Services\DhlDataHelper;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Exception;
|
||||
|
|
@ -144,4 +146,174 @@ class DhlShipmentService
|
|||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel a DHL shipment (sync or async based on config)
|
||||
*
|
||||
* @param DhlShipment $shipment
|
||||
* @param array $options
|
||||
* @return array
|
||||
*/
|
||||
public function cancelShipment(DhlShipment $shipment, array $options = []): array
|
||||
{
|
||||
// Get DHL configuration
|
||||
$settingController = new SettingController();
|
||||
$dhlConfig = $settingController->getDhlConfig();
|
||||
|
||||
// Check if queue should be used
|
||||
$useQueue = $dhlConfig['use_queue'] ?? false;
|
||||
|
||||
if ($useQueue) {
|
||||
return $this->cancelShipmentAsync($shipment, $options, $dhlConfig);
|
||||
} else {
|
||||
return $this->cancelShipmentSync($shipment, $options, $dhlConfig);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel shipment asynchronously using queue
|
||||
*
|
||||
* @param DhlShipment $shipment
|
||||
* @param array $options
|
||||
* @param array $dhlConfig
|
||||
* @return array
|
||||
*/
|
||||
private function cancelShipmentAsync(DhlShipment $shipment, array $options, array $dhlConfig): array
|
||||
{
|
||||
try {
|
||||
// Dispatch job
|
||||
CancelShipmentJob::dispatch($shipment, $options);
|
||||
|
||||
Log::info('[DHL Service] Shipment cancellation dispatched to queue', [
|
||||
'shipment_id' => $shipment->id,
|
||||
'dhl_shipment_no' => $shipment->dhl_shipment_no
|
||||
]);
|
||||
|
||||
return [
|
||||
'success' => true,
|
||||
'message' => 'Sendung wird storniert...',
|
||||
'queued' => true,
|
||||
'shipment_id' => $shipment->id
|
||||
];
|
||||
} catch (Exception $e) {
|
||||
Log::error('[DHL Service] Failed to dispatch shipment cancellation', [
|
||||
'error' => $e->getMessage(),
|
||||
'shipment_id' => $shipment->id,
|
||||
]);
|
||||
|
||||
return [
|
||||
'success' => false,
|
||||
'message' => 'Fehler beim Einreihen der Stornierung: ' . $e->getMessage(),
|
||||
'queued' => false
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel shipment synchronously
|
||||
*
|
||||
* @param DhlShipment $shipment
|
||||
* @param array $options
|
||||
* @param array $dhlConfig
|
||||
* @return array
|
||||
*/
|
||||
private function cancelShipmentSync(DhlShipment $shipment, array $options, array $dhlConfig): array
|
||||
{
|
||||
try {
|
||||
// Validate shipment has DHL number
|
||||
if (empty($shipment->dhl_shipment_no)) {
|
||||
return [
|
||||
'success' => false,
|
||||
'message' => 'Sendung hat keine DHL-Sendungsnummer und kann nicht storniert werden.',
|
||||
'queued' => false,
|
||||
'shipment_id' => $shipment->id
|
||||
];
|
||||
}
|
||||
|
||||
// Validate shipment can be cancelled
|
||||
if (! $shipment->canCancel()) {
|
||||
return [
|
||||
'success' => false,
|
||||
'message' => 'Sendung kann im aktuellen Status "' . $shipment->status . '" nicht storniert werden. Nur Status "created" oder "pending" sind stornierbar.',
|
||||
'queued' => false,
|
||||
'shipment_id' => $shipment->id
|
||||
];
|
||||
}
|
||||
|
||||
Log::info('[DHL Service] Cancelling shipment synchronously', [
|
||||
'shipment_id' => $shipment->id,
|
||||
'dhl_shipment_no' => $shipment->dhl_shipment_no,
|
||||
'status' => $shipment->status,
|
||||
'base_url' => $dhlConfig['base_url']
|
||||
]);
|
||||
|
||||
// Create DHL client
|
||||
$dhlClient = new \Acme\Dhl\Support\DhlClient(
|
||||
$dhlConfig['base_url'],
|
||||
$dhlConfig['api_key'],
|
||||
$dhlConfig['username'],
|
||||
$dhlConfig['password']
|
||||
);
|
||||
|
||||
$shippingService = new \Acme\Dhl\Services\ShippingService($dhlClient);
|
||||
|
||||
// Cancel the shipment directly
|
||||
$success = $shippingService->cancelLabel($shipment->dhl_shipment_no);
|
||||
|
||||
if ($success) {
|
||||
Log::info('[DHL Service] Shipment cancelled successfully (sync)', [
|
||||
'shipment_id' => $shipment->id,
|
||||
'dhl_shipment_no' => $shipment->dhl_shipment_no
|
||||
]);
|
||||
|
||||
return [
|
||||
'success' => true,
|
||||
'message' => 'Sendung wurde erfolgreich storniert!',
|
||||
'queued' => false,
|
||||
'shipment_id' => $shipment->id
|
||||
];
|
||||
} else {
|
||||
throw new Exception('Cancellation returned false');
|
||||
}
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
Log::warning('[DHL Service] Shipment cancellation validation failed', [
|
||||
'shipment_id' => $shipment->id,
|
||||
'error' => $e->getMessage()
|
||||
]);
|
||||
|
||||
return [
|
||||
'success' => false,
|
||||
'message' => $e->getMessage(),
|
||||
'queued' => false,
|
||||
'shipment_id' => $shipment->id
|
||||
];
|
||||
} catch (Exception $e) {
|
||||
Log::error('[DHL Service] Shipment cancellation failed (sync)', [
|
||||
'shipment_id' => $shipment->id,
|
||||
'dhl_shipment_no' => $shipment->dhl_shipment_no,
|
||||
'status' => $shipment->status,
|
||||
'error' => $e->getMessage(),
|
||||
'error_trace' => $e->getTraceAsString()
|
||||
]);
|
||||
|
||||
// Check if it's an API authentication/resource error
|
||||
$errorMessage = $e->getMessage();
|
||||
if (strpos($errorMessage, 'RF-UndefinedResource') !== false) {
|
||||
return [
|
||||
'success' => false,
|
||||
'message' => 'Die Sendung konnte bei DHL nicht gefunden werden. Mögliche Ursachen: Sendung wurde bereits storniert, ist zu alt, oder wurde in einem anderen Modus (Sandbox/Production) erstellt.',
|
||||
'queued' => false,
|
||||
'shipment_id' => $shipment->id,
|
||||
'technical_error' => $errorMessage
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'success' => false,
|
||||
'message' => 'Fehler beim Stornieren der Sendung: ' . $errorMessage,
|
||||
'queued' => false,
|
||||
'shipment_id' => $shipment->id
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue