baks-dev/ozon-support
Модуль техподдержки Ozon для Laravel/Symfony: установка через Composer, добавление типа профиля Ozon Support командой baks:users-profile-type:ozon-support, запуск тестов PHPUnit (группа ozon-support). Требуется PHP 8.4+.
Installation
composer require baks-dev/ozon-support
Ensure your project uses PHP 8.4+ and Laravel/Symfony.
Register Ozon Support Profile Type Run the console command to add the Ozon-specific user profile:
php artisan baks:users-profile-type:ozon-support
This creates the necessary database tables and migrations.
Configure Ozon API Credentials
Add your Ozon API credentials (client ID, client secret, etc.) to your .env file or Laravel config. Example:
OZON_CLIENT_ID=your_client_id
OZON_CLIENT_SECRET=your_client_secret
OZON_REDIRECT_URI=http://your-app.com/ozon/callback
First Use Case: OAuth Authentication Use the provided OAuth service to authenticate with Ozon:
use Baks\OzonSupport\Facades\OzonAuth;
$authUrl = OzonAuth::getAuthorizationUrl();
// Redirect user to $authUrl to start OAuth flow.
// After callback, exchange code for token:
$token = OzonAuth::exchangeCodeForToken($authorizationCode);
Test the Integration Run the package-specific tests to verify functionality:
php artisan phpunit --group=ozon-support
OzonAuth facade to handle OAuth2 authentication.$authUrl = OzonAuth::getAuthorizationUrl(['scope' => 'Orders.ReadWrite']);
Route::get('/ozon/callback', function ($request) {
$token = OzonAuth::exchangeCodeForToken($request->query('code'));
// Store token in session/database.
});
$refreshedToken = OzonAuth::refreshToken($refreshToken);
Pattern: Use the OzonOrderService to create, update, and fetch orders.
Example: Create an Order
use Baks\OzonSupport\Services\OzonOrderService;
$orderService = app(OzonOrderService::class);
$orderData = [
'external_id' => 'ORDER_123',
'items' => [
['external_id' => 'PROD_456', 'price' => 1000, 'quantity' => 2],
],
];
$createdOrder = $orderService->create($orderData);
Example: Fetch Orders
$orders = $orderService->getOrders(['limit' => 10]);
use Baks\OzonSupport\Events\OzonWebhookReceived;
Event::listen(OzonWebhookReceived::class, function ($event) {
$payload = $event->payload;
if ($payload['event_type'] === 'order_cancelled') {
// Handle cancellation logic.
}
});
$isValid = OzonAuth::validateWebhookSignature($payload, $signature);
use Baks\OzonSupport\Models\OzonProfile;
$user = auth()->user();
$ozonProfile = OzonProfile::create([
'user_id' => $user->id,
'ozon_shop_id' => '12345',
'access_token' => $token,
]);
php artisan ozon:sync-inventory --shop-id=12345
php artisan ozon:generate-report --type=sales --days=7
Laravel Service Providers
$this->app->bind(
CustomOzonService::class,
function ($app) {
return new CustomOzonService(
$app->make(OzonOrderService::class),
$app->make(OzonAuth::class)
);
}
);
Event-Driven Extensions
OzonOrderCreated) to trigger custom logic:
Event::listen(OzonOrderCreated::class, function ($event) {
// Send notification or update ERP system.
});
Testing Strategies
$this->mock(OzonOrderService::class)
->shouldReceive('create')
->once()
->andReturn($mockOrder);
Localization
php artisan vendor:publish --provider="Baks\OzonSupport\OzonSupportServiceProvider" --tag=lang
resources/lang/ru/ozon.php file.Error Handling
public function handle($request, Closure $next)
{
try {
return $next($request);
} catch (OzonApiException $e) {
Log::error('Ozon API Error: ' . $e->getMessage());
return response()->json(['error' => 'Ozon service unavailable'], 503);
}
}
OAuth Token Management
public function handle($request, Closure $next)
{
if (OzonAuth::isTokenExpired()) {
$token = OzonAuth::refreshToken($request->user()->ozonProfile->refresh_token);
$request->user()->ozonProfile->update(['access_token' => $token]);
}
return $next($request);
}
Webhook Signature Validation
OZON_WEBHOOK_SECRET in .env matches Ozon’s configured secret.Database Schema Conflicts
php artisan migrate:status
Roll back and re-run if needed:
php artisan migrate:rollback --step=1
php artisan migrate
Rate Limiting
use Symfony\Component\HttpClient\RetryableHttpClient;
$client = new RetryableHttpClient(
new HttpClient(),
[
'max_retries' => 3,
'delay' => 1000,
'multiplier' => 2,
'statuses' => [429],
]
);
Hardcoded Values
.env or publish the config:
php artisan vendor:publish --provider="Baks\OzonSupport\OzonSupportServiceProvider" --tag=config
Enable Debug Logging
Add to config/logging.php:
'channels' => [
'ozon' => [
'driver' => 'single',
'path' => storage_path('logs/ozon.log'),
'level' => 'debug',
],
],
Then log Ozon API responses:
Log::channel('ozon')->debug('Ozon API Response', ['response' => $response]);
Inspect Raw API Responses Use a debug middleware to log raw HTTP requests/responses:
How can I help you explore Laravel packages today?