baks-dev/ozon-support
Модуль техподдержки Ozon для PHP 8.4+/Laravel-экосистемы BaksDev: установка через Composer, добавление типа профиля Ozon Support командой baks:users-profile-type:ozon-support, есть тесты PHPUnit (group=ozon-support).
Install the Package
composer require baks-dev/ozon-support
Register the Ozon Support Profile Type Run the artisan command to add the Ozon-specific user profile type:
php artisan baks:users-profile-type:ozon-support
This creates the necessary database tables and migrations for Ozon-related user profiles.
Configure Ozon API Credentials
Add the following to your .env file:
OZON_API_KEY=your_ozon_api_key
OZON_API_SECRET=your_ozon_api_secret
OZON_API_SANDBOX=false # Set to true for sandbox testing
Publish Configuration (if needed) If the package requires additional configuration, publish and edit the config file:
php artisan vendor:publish --provider="BaksDev\OzonSupport\OzonSupportServiceProvider"
Edit the published config file at config/ozon-support.php.
First Use Case: Fetch Ozon Orders Use the provided service to fetch orders from Ozon:
use BaksDev\OzonSupport\Facades\Ozon;
$orders = Ozon::orders()->fetch();
Service Facade
The package provides a facade (Ozon) for easy access to Ozon API services:
// Fetch orders
$orders = Ozon::orders()->fetch();
// Create an order
$order = Ozon::orders()->create([
'external_id' => 'order_123',
'items' => [...],
]);
Event Listeners
The package includes webhook listeners for Ozon events (e.g., order status updates). Register them in EventServiceProvider:
protected $listen = [
'ozon.order.created' => [
\App\Listeners\HandleOzonOrderCreated::class,
],
];
CLI Commands Use the provided artisan commands for common tasks:
# Sync inventory with Ozon
php artisan ozon:inventory:sync
# Test Ozon API connection
php artisan ozon:test-connection
User Profile Integration Attach Ozon profiles to users:
$user->ozonProfile()->create([
'api_key' => 'user_specific_key',
'api_secret' => 'user_specific_secret',
]);
Order Management
Ozon::orders()->fetch()Ozon::orders()->create($data)Ozon::orders()->update($orderId, $status)Inventory Sync
php artisan ozon:inventory:syncOzon::inventory()->fetch()Webhook Handling
/ozon/webhook).public function handleWebhook(Request $request)
{
$payload = $request->json()->all();
event(new OzonWebhookReceived($payload));
}
Payment Processing
Ozon::payments()->fetch()Ozon::payments()->refund($paymentId, $amount)Queue Delayed Tasks Offload API calls to queues to avoid timeouts:
Ozon::orders()->create($data)->onQueue('ozon');
Logging Enable logging for debugging:
config(['ozon-support.log_enabled' => true]);
Testing Use the sandbox mode for testing:
OZON_API_SANDBOX=true
Customizing Responses Extend the package’s models or services to add custom logic:
namespace App\Services;
use BaksDev\OzonSupport\Services\OzonOrderService;
class CustomOzonOrderService extends OzonOrderService
{
public function create(array $data)
{
// Custom logic before/after parent call
return parent::create($data);
}
}
Bind your custom service in a service provider:
$this->app->bind(
\BaksDev\OzonSupport\Contracts\OzonOrderService::class,
App\Services\CustomOzonOrderService::class
);
OAuth2 Token Management
.env has the correct OAuth2 credentials and that tokens are refreshed automatically. Monitor token expiration errors in logs.Webhook Signatures
use BaksDev\OzonSupport\Helpers\OzonWebhook;
$isValid = OzonWebhook::validateSignature(
$request->header('X-Ozon-Signature'),
$request->getContent()
);
Rate Limiting
use Illuminate\Support\Facades\Http;
$response = Http::withOptions(['timeout' => 30])
->retry(3, 100)
->get('https://api.ozon.ru/v3/orders');
Time Zone Handling
$order->created_at->setTimezone('Europe/Moscow');
Database Conflicts
php artisan migrate:fresh --env=testing
in a test environment to reset the database.Enable Debug Mode
Set debug to true in config/ozon-support.php to get detailed API response logs.
Check Logs
Look for errors in storage/logs/laravel.log or enable monolog for more granular logging.
Test in Sandbox Always test critical operations in Ozon’s sandbox environment before going live.
Validate API Responses Use tools like Postman to manually test API endpoints and compare responses with the package’s output.
Sandbox vs. Production
Ensure OZON_API_SANDBOX is set correctly. Mixing sandbox and production credentials can cause unexpected errors.
Custom Endpoints
If you need to override Ozon’s API endpoints (e.g., for testing), configure them in config/ozon-support.php:
'api' => [
'base_uri' => env('OZON_API_BASE_URI', 'https://api.ozon.ru'),
],
User Profile Scoping The package uses user profiles to scope Ozon API calls. Ensure users have the correct profile attached:
$user->ozonProfile()->firstOrFail();
Custom Services Override the package’s services by binding your custom implementations in a service provider:
$this->app->bind(
\BaksDev\OzonSupport\Contracts\OzonOrderService::class,
App\Services\CustomOzonOrderService::class
);
Event Extensions Listen to Ozon events and extend their behavior:
Ozon::orders()->created(function ($order) {
// Custom logic after order creation
});
Webhook Extensions
Add custom webhook handlers by extending the OzonWebhookHandler class:
namespace App\Handlers;
use BaksDev\OzonSupport\Handlers\OzonWebhookHandler;
class CustomOzonWebhookHandler extends OzonWebhookHandler
{
public function handleOrderCreated(array $payload)
{
// Custom logic
}
}
Bind your custom handler in a service provider.
API Response Transformers
Modify how API responses are transformed into Laravel models by extending the OzonTransformer class:
namespace App\Transformers;
use BaksDev\OzonSupport\Transformers\OzonTransformer;
class CustomOzonTransformer extends OzonTransformer
{
public function transformOrder(array $data)
{
// Custom transformation logic
return parent::transformOrder($data);
}
}
Bind your custom transformer in a service provider.
CLI Command Extensions Extend existing artisan commands or create new ones by publishing the package’s commands and customizing them:
How can I help you explore Laravel packages today?