319 lines
11 KiB
PHP
319 lines
11 KiB
PHP
<?php
|
|
|
|
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;
|
|
|
|
/**
|
|
* DHL Shipment Service
|
|
*
|
|
* Handles both synchronous and asynchronous shipment creation
|
|
* based on configuration settings
|
|
*/
|
|
class DhlShipmentService
|
|
{
|
|
/**
|
|
* Create a DHL shipment (sync or async based on config)
|
|
*
|
|
* @param ShoppingOrder $order
|
|
* @param float $weight
|
|
* @param array $options
|
|
* @return array
|
|
*/
|
|
public function createShipment(ShoppingOrder $order, float $weight = 1.0, array $options = []): array
|
|
{
|
|
// Get DHL configuration
|
|
$settingController = new SettingController();
|
|
$dhlConfig = $settingController->getDhlConfig();
|
|
\Log::info('dhlConfig', $dhlConfig);
|
|
// Check if queue should be used
|
|
$useQueue = $dhlConfig['use_queue'] ?? false;
|
|
|
|
if ($useQueue) {
|
|
return $this->createShipmentAsync($order, $weight, $options, $dhlConfig);
|
|
} else {
|
|
return $this->createShipmentSync($order, $weight, $options, $dhlConfig);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create shipment asynchronously using queue
|
|
*
|
|
* @param ShoppingOrder $order
|
|
* @param float $weight
|
|
* @param array $options
|
|
* @param array $dhlConfig
|
|
* @return array
|
|
*/
|
|
private function createShipmentAsync(ShoppingOrder $order, float $weight, array $options, array $dhlConfig): array
|
|
{
|
|
try {
|
|
// Dispatch job with pre-loaded config
|
|
CreateShipmentJob::dispatch($order, $weight, $options, $dhlConfig);
|
|
|
|
Log::info('[DHL Service] Shipment creation dispatched to queue', [
|
|
'order_id' => $order->id,
|
|
'weight' => $weight
|
|
]);
|
|
|
|
return [
|
|
'success' => true,
|
|
'message' => 'Sendung wird erstellt. Sie erhalten eine Benachrichtigung, sobald das Versandlabel verfügbar ist.',
|
|
'queued' => true,
|
|
'order_id' => $order->id
|
|
];
|
|
} catch (Exception $e) {
|
|
Log::error('[DHL Service] Failed to dispatch shipment creation', [
|
|
'error' => $e->getMessage(),
|
|
'order_id' => $order->id,
|
|
]);
|
|
|
|
return [
|
|
'success' => false,
|
|
'message' => 'Fehler beim Einreihen der Sendungserstellung: ' . $e->getMessage(),
|
|
'queued' => false
|
|
];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create shipment synchronously
|
|
*
|
|
* @param ShoppingOrder $order
|
|
* @param float $weight
|
|
* @param array $options
|
|
* @param array $dhlConfig
|
|
* @return array
|
|
*/
|
|
private function createShipmentSync(ShoppingOrder $order, float $weight, array $options, array $dhlConfig): array
|
|
{
|
|
try {
|
|
Log::info('[DHL Service] Creating shipment synchronously', [
|
|
'order_id' => $order->id,
|
|
'weight' => $weight
|
|
]);
|
|
|
|
// Create DHL client directly with correct base URL
|
|
$dhlClient = new \Acme\Dhl\Support\DhlClient(
|
|
$dhlConfig['base_url'],
|
|
$dhlConfig['api_key'],
|
|
$dhlConfig['username'],
|
|
$dhlConfig['password']
|
|
);
|
|
|
|
$shippingService = new \Acme\Dhl\Services\ShippingService($dhlClient);
|
|
|
|
// Prepare order data using helper
|
|
$orderData = DhlDataHelper::prepareOrderData($order, $weight, $options, $dhlConfig);
|
|
Log::info('orderData', $orderData);
|
|
// Create the shipment directly
|
|
$result = $shippingService->createLabel($orderData);
|
|
|
|
Log::info('[DHL Service] Shipment created successfully (sync)', [
|
|
'order_id' => $order->id,
|
|
'shipment_number' => $result['shipmentNumber'] ?? 'N/A',
|
|
'label_path' => $result['labelPath'] ?? 'N/A',
|
|
]);
|
|
|
|
return [
|
|
'success' => true,
|
|
'message' => 'Versandlabel erfolgreich erstellt!',
|
|
'queued' => false,
|
|
'order_id' => $order->id,
|
|
'shipment_number' => $result['shipmentNumber'] ?? null,
|
|
'tracking_number' => $result['trackingNumber'] ?? null,
|
|
'label_path' => $result['labelPath'] ?? null,
|
|
'label_url' => $result['labelUrl'] ?? null,
|
|
];
|
|
} catch (Exception $e) {
|
|
Log::error('[DHL Service] Shipment creation failed (sync)', [
|
|
'order_id' => $order->id,
|
|
'error' => $e->getMessage()
|
|
]);
|
|
|
|
return [
|
|
'success' => false,
|
|
'message' => 'Fehler beim Erstellen des Versandlabels: ' . $e->getMessage(),
|
|
'queued' => false,
|
|
'order_id' => $order->id
|
|
];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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
|
|
];
|
|
}
|
|
}
|
|
}
|