Install the Bundle
composer require ddb/stuart-api-bundle
Register the bundle in config/bundles.php:
return [
// ...
DdB\StuartApiBundle\StuartApiBundle::class => ['all' => true],
];
Configure Stuart API
Add your credentials to config/packages/ddb_stuart_api.yaml:
stuart_api:
private_key: "%env(STUART_PRIVATE_KEY)%"
public_key: "%env(STUART_PUBLIC_KEY)%"
environment: "%env(STUART_ENV)%" # "SANDBOX" or "PRODUCTION"
vat_rate: 20
First Use Case: Fetch Next Pickup Slot Call the pre-configured route:
curl http://your-app.test/api/stuart/next-pickup-slot/Bordeaux
Or use the controller directly in your code:
$nextSlot = $this->get('ddb_stuart_api.controller.stuart_api')->nextPickupSlot('Paris');
Job Creation & Dispatch
StuartApi service to create and add jobs:
$job = $this->get('ddb_stuart_api.stuart_api')->createJobObjectFromRequest($request);
$this->get('ddb_stuart_api.stuart_api')->addJob($job);
$job = $this->get('ddb_stuart_api.stuart_api')->createJobObject([
'pickup_address' => '123 Rue de Paris',
'delivery_address' => '456 Av. des Champs',
'pickup_date' => '2023-12-31T10:00:00',
]);
Webhook Handling
StuartApiEvents::WEBHOOK_RECEIVED to process Stuart’s webhook payloads:
// src/EventSubscriber/StuartWebhookSubscriber.php
class StuartWebhookSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
StuartApiEvents::WEBHOOK_RECEIVED => 'onWebhookReceived',
];
}
public function onWebhookReceived(WebhookEvent $event)
{
$data = $event->getData();
// Update your order status, notify users, etc.
}
}
Price & Validate Jobs
$price = $this->get('ddb_stuart_api.controller.stuart_api')->priceJob($request);
$validation = $this->get('ddb_stuart_api.controller.stuart_api')->validateJob($request);
SANDBOX first. The bundle auto-validates webhook IPs based on the environment.StuartApi service over instantiating the controller directly for better testability.Stuart\Exception\ApiException).Webhook IP Validation
config/packages/ddb_stuart_api.yaml match Stuart’s production/sandbox IPs.WebhookEvent subscriber to confirm:
$ip = $event->getRequest()->getClientIp();
Private/Public Key Sensitivity
.env or a secrets manager:
# config/packages/ddb_stuart_api.yaml
private_key: "%env(STUART_PRIVATE_KEY)%"
Job Object Requirements
createJobObject() expects a strict array structure. Missing fields (e.g., pickup_date) will cause silent failures. Refer to Stuart’s API docs for required fields.Event Dispatching
$client = $this->get('ddb_stuart_api.stuart_api')->getClient();
$client->setDebug(true); // Logs requests/responses to `var/log/stuart.log`
file_put_contents(
'var/log/stuart_webhook.log',
print_r($event->getData(), true),
FILE_APPEND
);
Custom Job Fields
Job class or using a decorator pattern:
// src/Service/ExtendedJob.php
class ExtendedJob extends \DdB\StuartApiBundle\Model\Job
{
public function setCustomField(string $key, $value): self
{
$this->customFields[$key] = $value;
return $this;
}
}
services.yaml:
DdB\StuartApiBundle\Model\Job: '@App\Service\ExtendedJob'
Webhook Payload Processing
WebhookEvent logic by creating a custom subscriber that implements onWebhookReceived with your business logic.Environment-Specific Config
# config/packages/ddb_stuart_api.yaml
authorized_webhook_ips: "%kernel.environment%_webhook_ips%"
# config/packages/dev/ddb_stuart_api.yaml
parameters:
dev_webhook_ips:
- "127.0.0.1" # Local dev IP
How can I help you explore Laravel packages today?