Installation
composer require byhaskell/novaposhta-bundle
Register the bundle in config/bundles.php (Symfony Flex handles this automatically).
Configuration
Create config/packages/byhaskell_nova_poshta.yaml:
byhaskell_nova_poshta:
api_key: '%env(NP_API_KEY)%'
base_url: 'https://api.novaposhta.ua/v2.0/json/'
Add NP_API_KEY to .env (generate at Nova Poshta Developer Dashboard).
First Use Case
Inject the NovaPoshta service into a controller/service:
use byhaskell\NovaPoshtaBundle\NovaPoshta;
public function getAddressList(NovaPoshta $novaPoshta) {
$response = $novaPoshta->getAddressList();
return json_encode($response);
}
Address Operations
$addresses = $novaPoshta->getAddressList(['CityName' => 'Kyiv']);
$address = $novaPoshta->getAddressDetails(['AddressID' => 12345]);
Delivery Services
$services = $novaPoshta->getDeliveryServices();
$availability = $novaPoshta->getServiceAvailability([
'AddressFrom' => 12345,
'AddressTo' => 67890,
'ServiceTypeID' => 1
]);
Order Tracking
$track = $novaPoshta->getOrderTracking(['OrderNumber' => '123456789']);
NovaPoshta service.try {
$data = $novaPoshta->getSomething();
} catch (\byhaskell\NovaPoshtaBundle\Exception\NovaPoshtaException $e) {
// Log or handle error
}
$services = $novaPoshta->getDeliveryServices(['Cache' => true]);
Checkout Flow
Admin Dashboard
Data Migration
API Key Leaks
.env or expose NP_API_KEY in client-side code.%env() syntax for sensitive data.Rate Limiting
use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Component\Stopwatch\StopwatchEvent;
$stopwatch = new Stopwatch();
$event = $stopwatch->start('novaposhta_request');
try {
$response = $novaPoshta->getSomething();
} catch (\byhaskell\NovaPoshtaBundle\Exception\RateLimitException $e) {
$event->stop();
$duration = $event->getDuration();
sleep(max(1, $duration / 1000)); // Retry after delay
retry();
}
Deprecated Methods
Character Encoding
Enable Debug Mode
Set debug: true in config:
byhaskell_nova_poshta:
debug: true
Logs raw API requests/responses to var/log/dev.log.
Validate Parameters
Use getMethodInfo() to inspect required parameters:
$methodInfo = $novaPoshta->getMethodInfo('getAddressList');
dd($methodInfo['parameters']); // Check expected params
Custom Responses
Override response handling by extending the bundle’s NovaPoshta class:
namespace App\Service;
use byhaskell\NovaPoshtaBundle\NovaPoshta as BaseNovaPoshta;
class CustomNovaPoshta extends BaseNovaPoshta {
protected function processResponse($response) {
// Custom logic (e.g., transform data)
return parent::processResponse($response);
}
}
Register the service in services.yaml:
services:
App\Service\CustomNovaPoshta: '@byhaskell_nova_poshta.nova_poshta'
Event Listeners
Subscribe to novaposhta.response events to intercept responses:
use Symfony\Component\EventDispatcher\GenericEvent;
$dispatcher->addListener('novaposhta.response', function (GenericEvent $event) {
$response = $event->getSubject();
// Modify or log response
});
Testing
Mock the NovaPoshta service in tests:
$this->container->set('byhaskell_nova_poshta.nova_poshta', $this->createMock(NovaPoshta::class));
base_url for sandbox testing:
base_url: 'https://sandbox.novaposhta.ua/v2.0/json/'
config/packages/http_client.yaml:
framework:
http_client:
timeout: 30 # seconds
How can I help you explore Laravel packages today?