anripuankare/atol-client-bundle
Install the Bundle
composer require anripuankare/atol-client-bundle
Register Bundles
Add to config/bundles.php:
return [
// ...
Lamoda\AtolClientBundle\AtolClientBundle::class => ['all' => true],
JMS\SerializerBundle\JMSSerializerBundle::class => ['all' => true],
];
Configure Guzzle and ATOL
In config/services.yaml:
services:
guzzle:
class: GuzzleHttp\Client
In config/packages/atol_client.yaml:
atol_client:
clients:
default:
version: !php/const Lamoda\AtolClientBundle\AtolClientBundle::API_CLIENT_VERSION_4
guzzle_client: guzzle
base_url: '%env(ATOL_API_URL)%'
callback_url: '%env(ATOL_CALLBACK_URL)%'
First Use Case Inject the client in a service and call an API method:
use Lamoda\AtolClientBundle\Client\AtolClientInterface;
class MyService
{
public function __construct(private AtolClientInterface $atolClient) {}
public function fetchOrderStatus(string $orderId): array
{
return $this->atolClient->getOrderStatus($orderId);
}
}
Inject the Client
Use @atol_client.v4 (or .v3) in services or controllers:
public function __construct(
private AtolClientInterface $atolClient,
private AtolClientV4Interface $atolClientV4
) {}
Version-Specific Clients
The bundle auto-registers AtolClientV3Interface and AtolClientV4Interface as separate services.
Order Processing
$order = $this->atolClient->createOrder([
'order_id' => '123',
'items' => [['product_id' => '456', 'quantity' => 2]],
]);
Webhook Handling
Validate ATOL callbacks using the validateCallback method:
$isValid = $this->atolClient->validateCallback($request->getContent());
Batch Operations
Use createOrders or updateOrders for bulk requests:
$results = $this->atolClient->createOrders([$order1, $order2]);
Environment Variables
Store base_url, callback_url, and credentials in .env:
ATOL_API_URL=https://api.atol.ru/v4
ATOL_CALLBACK_URL=https://yourdomain.com/atol/callback
ATOL_LOGIN=your_login
ATOL_PASSWORD=your_password
Logging Enable Guzzle middleware for logging:
atol_client:
clients:
default:
guzzle_client_options:
middleware: ['GuzzleHttp\Middleware\LogRequest']
Validation Leverage Symfony’s validator for request payloads:
use Symfony\Component\Validator\Validator\ValidatorInterface;
public function __construct(private ValidatorInterface $validator) {}
public function validateOrder(array $order): void
{
$errors = $this->validator->validate($order);
if ($errors->count() > 0) {
throw new \InvalidArgumentException('Invalid order data');
}
}
Deprecated API (v3)
AtolClientV3Interface unless maintaining legacy systems.AtolClientV4Interface for new projects.Callback Validation
validateCallback() and verify signatures:
$isValid = $this->atolClient->validateCallback($rawPayload);
if (!$isValid) {
throw new \RuntimeException('Invalid ATOL callback');
}
Guzzle Client Configuration
guzzle_client_options (e.g., wrong base_uri) will cause silent failures.curl first or enable Guzzle logging.JMS Serializer Dependencies
jms/serializer-bundle for request/response serialization.bundles.php.Enable API Debugging Add debug middleware to Guzzle:
guzzle_client_options:
middleware: ['GuzzleHttp\Middleware\LogRequest', 'GuzzleHttp\Middleware\LogResponse']
Check ATOL API Responses
ATOL returns structured errors (e.g., {"errors": ["Invalid order_id"]}). Parse responses carefully:
try {
$response = $this->atolClient->getOrderStatus($orderId);
} catch (\Lamoda\AtolClientBundle\Exception\AtolClientException $e) {
// Handle ATOL-specific errors
$errors = json_decode($e->getMessage(), true);
}
Custom Clients Extend the base client for domain-specific logic:
class CustomAtolClient implements AtolClientInterface
{
private AtolClientV4Interface $client;
public function __construct(AtolClientV4Interface $client) {
$this->client = $client;
}
public function getOrderStatus(string $orderId): array
{
$response = $this->client->getOrderStatus($orderId);
return $this->transformResponse($response);
}
private function transformResponse(array $data): array
{
// Add custom logic (e.g., mapping fields)
return $data;
}
}
Override Configuration Use Symfony’s compiler passes or decorators to modify the client behavior at runtime.
Add Middleware Inject custom middleware into Guzzle for pre/post-processing:
atol_client:
clients:
default:
guzzle_client_options:
middleware: ['App\Middleware\AddAuthHeader']
Base URL
Ensure base_url ends with / (e.g., https://api.atol.ru/v4/). Missing slashes may cause 404s.
Callback URL Must be publicly accessible and HTTPS. ATOL will POST callbacks to this URL.
Credentials
Login/password are not configurable via services.yaml. Use environment variables or a custom factory:
// config/services.yaml
services:
atol_client.factory:
class: App\Factory\AtolClientFactory
arguments:
- '%env(ATOL_LOGIN)%'
- '%env(ATOL_PASSWORD)%'
How can I help you explore Laravel packages today?