Install via Composer
composer require ekyna/dpd-bundle
(Note: The package is outdated; verify compatibility with your Laravel/EkynaCommerce version.)
Enable the Bundle
In config/app.php, add to providers:
Ekyna\DpdBundle\DpdBundle::class,
And to aliases:
'Dpd' => Ekyna\DpdBundle\DpdService::class,
Configure DPD Credentials
Add to config/services.php or config/packages/ekyna_dpd.yaml:
dpd:
api_key: '%env(DPD_API_KEY)%'
account_number: '%env(DPD_ACCOUNT_NUMBER)%'
test_mode: '%env(bool:DPD_TEST_MODE)%'
First Use Case: Create a Shipment
Inject DpdService into a controller/service and use:
$shipment = $this->dpd->createShipment([
'sender' => ['name' => 'Sender', 'address' => '123 Sender St'],
'recipient' => ['name' => 'Recipient', 'address' => '456 Recipient Ave'],
'parcels' => [
['weight' => 1.5, 'dimensions' => ['length' => 30, 'width' => 20, 'height' => 15]],
],
'service' => 'DPDStandard', // Check available services in docs
]);
Hook into EkynaCommerce Events
Listen for ekyna_commerce.order.shipped to trigger DPD shipment creation:
use Ekyna\CommerceBundle\Event\OrderEvent;
public function onOrderShipped(OrderEvent $event) {
$order = $event->getOrder();
$shipmentData = $this->mapOrderToDpdShipment($order);
$this->dpd->createShipment($shipmentData);
}
Batch Processing For bulk shipments (e.g., nightly exports), use:
$shipments = $this->orderRepository->getPendingShipments();
foreach ($shipments as $order) {
$this->dpd->createShipment($this->mapOrderToDpdShipment($order));
}
Tracking Integration Store DPD tracking numbers in your order model:
$trackingNumber = $shipment->getTrackingNumber();
$order->setTrackingNumber($trackingNumber)->save();
DpdService in a repository to handle business logic:
class DpdShipmentService {
public function __construct(private DpdService $dpd) {}
public function createFromOrder(Order $order) {
// Transform order data to DPD format
$data = [...];
return $this->dpd->createShipment($data);
}
}
try {
$shipment = $this->dpd->createShipment($data);
} catch (DpdException $e) {
$this->logger->error('DPD API failed', ['error' => $e->getMessage()]);
throw new OrderShipmentException('Failed to create DPD shipment');
}
Deprecated Package
ekyna/dpd library directly if the bundle is incompatible.Missing Documentation
service types (e.g., DPDStandard, DPDExpress).sender/recipient/parcels.Test Mode Quirks
test_mode: true may not simulate all API responses accurately.$this->dpd->setLogger(function ($message) {
\Log::debug('DPD API', ['message' => $message]);
});
Error Handling
DpdException may obscure issues (e.g., invalid credentials vs. rate limits).class DpdValidationException extends DpdException {}
Environment Variables
Use .env for sensitive data:
DPD_API_KEY=your_api_key_here
DPD_ACCOUNT_NUMBER=123456
DPD_TEST_MODE=true
Retry Logic Implement exponential backoff for transient failures:
use Symfony\Component\Process\Exception\ProcessFailedException;
try {
$this->dpd->createShipment($data);
} catch (ProcessFailedException $e) {
if ($e->getCode() === 500) { // Server error
sleep(2 ** $attempt); // Exponential backoff
retry();
}
}
Logging Enable debug logging for API calls:
# config/packages/monolog.yaml
handlers:
dpd:
type: stream
path: "%kernel.logs_dir%/dpd.log"
level: debug
Extending Functionality
DpdService to add methods like:
public function getAvailableServices() {
return $this->client->get('/services'); // Hypothetical endpoint
}
How can I help you explore Laravel packages today?