Installation
composer require baks-dev/yandex-support
Verify PHP 8.4+ compatibility with:
php -v
Publish Configuration Run the publish command to generate the config file:
php artisan vendor:publish --provider="BaksDev\YandexSupport\YandexSupportServiceProvider" --tag="config"
Update config/yandex-support.php with your Yandex API credentials (e.g., client_id, client_secret, redirect_uri).
First Use Case: OAuth Authentication Register a route for Yandex OAuth redirection:
use BaksDev\YandexSupport\Facades\YandexAuth;
Route::get('/auth/yandex', [YandexAuth::class, 'redirectToYandex']);
Route::get('/auth/yandex/callback', [YandexAuth::class, 'handleCallback']);
Test the Flow
/auth/yandex to trigger the OAuth redirect.Use the facade for seamless OAuth handling:
// Redirect user to Yandex for authentication
$authUrl = YandexAuth::getAuthorizationUrl();
return redirect()->to($authUrl);
// Handle callback after authorization
$user = YandexAuth::handleCallback($request);
Initialize a Yandex API client (e.g., for Marketplace or Cloud):
use BaksDev\YandexSupport\Clients\MarketplaceClient;
$client = new MarketplaceClient(config('yandex-support.marketplace.api_key'));
$products = $client->getProducts(['limit' => 10]);
Bind the Yandex client to Laravel’s container for dependency injection:
// app/Providers/YandexSupportServiceProvider.php
public function register(): void
{
$this->app->singleton(MarketplaceClient::class, function ($app) {
return new MarketplaceClient(config('yandex-support.marketplace.api_key'));
});
}
Register a webhook route for Yandex Payments or Cloud events:
Route::post('/yandex/webhook', function (Request $request) {
$payload = YandexWebhook::verifyAndProcess($request->getContent());
// Handle payload (e.g., payment confirmation, VM status change)
});
Offload long-running Yandex API calls to Laravel queues:
use BaksDev\YandexSupport\Jobs\ProcessYandexOrder;
ProcessYandexOrder::dispatch($orderId)
->onQueue('yandex');
Use Facades for Simplicity
Leverage the package’s facades (e.g., YandexAuth, YandexMarketplace) to reduce boilerplate in controllers:
public function syncProducts()
{
$products = YandexMarketplace::getProducts();
// Process products...
}
Middleware for Authenticated Requests Protect routes requiring Yandex authentication:
Route::middleware(['auth.yandex'])->group(function () {
Route::get('/dashboard', 'DashboardController@index');
});
Event Listeners for Yandex Events Listen for Yandex webhook events and trigger Laravel events:
// app/Listeners/HandleYandexPaymentEvent.php
public function handle(YandexPaymentEvent $event)
{
event(new PaymentReceived($event->payload));
}
Mock Yandex API Responses Use Laravel’s HTTP testing to mock Yandex API calls:
$response = Http::fake([
'api.yandex.ru/*' => Http::response(['data' => 'test'], 200),
]);
$this->get('/sync-products')->assertOk();
Test OAuth Flow Simulate OAuth callbacks in tests:
$callbackResponse = Http::fake([
'your-app.com/auth/yandex/callback' => Http::response($userData),
]);
$this->get('/auth/yandex/callback?code=test')->assertRedirect('/dashboard');
.env for environment-specific Yandex credentials:
YANDEX_MARKETPLACE_API_KEY=your_key_prod
YANDEX_MARKETPLACE_API_KEY_TEST=your_key_test
Load the correct key in config/yandex-support.php:
'api_key' => env('YANDEX_MARKETPLACE_API_KEY_' . config('app.env')),
use Symfony\Component\HttpClient\RetryStrategy;
$client = HttpClient::create([
'base_uri' => 'https://api.yandex.ru',
'options' => [
'retries' => 3,
'timeout' => 30,
'retry_strategy' => new RetryStrategy(3, 1000),
],
]);
public function handle($request, Closure $next)
{
if (YandexAuth::isTokenExpired()) {
YandexAuth::refreshToken();
}
return $next($request);
}
config/app.php:
'providers' => [
// ...
BaksDev\YandexSupport\YandexSupportServiceProvider::class,
],
use Illuminate\Support\Facades\Http;
public function handleWebhook(Request $request)
{
$signature = $request->header('X-Yandex-Signature');
$payload = $request->getContent();
if (!YandexWebhook::verifySignature($payload, $signature)) {
abort(403, 'Invalid signature');
}
// Process payload...
}
Configure Laravel to log Yandex API requests/responses:
'logging' => [
'enabled' => env('YANDEX_DEBUG', false),
'channel' => 'single',
],
Add to AppServiceProvider:
if (config('yandex-support.logging.enabled')) {
\Monolog\Logger::addHandler(new \Monolog\Handler\StreamHandler(storage_path('logs/yandex.log')));
}
Use Laravel’s tap method to debug API responses:
$response = YandexMarketplace::getProducts()->tap(function ($response) {
\Log::debug('Raw response:', ['data' => $response->getData()]);
});
Use Yandex’s sandbox environments (e.g., api.sb.yandex.ru) to avoid hitting rate limits or real data:
$client = new MarketplaceClient(config('yandex-support.marketplace.sandbox_key'));
How can I help you explore Laravel packages today?