DHL Modul v0.5 Shipping Label ok
This commit is contained in:
parent
480fdc65ed
commit
8fdaa0ba1d
122 changed files with 17938 additions and 2239 deletions
147
app/Services/DhlShipmentService.php
Normal file
147
app/Services/DhlShipmentService.php
Normal file
|
|
@ -0,0 +1,147 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\ShoppingOrder;
|
||||
use App\Http\Controllers\SettingController;
|
||||
use App\Jobs\CreateShipmentJob;
|
||||
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
|
||||
$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
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue