andchir/omnipay3-yandexkassa
Omnipay 3 payment gateway driver for Yandex.Kassa (now YooKassa) in Laravel/PHP. Adds YandexKassa support to Omnipay with request/response handling for payments and related transactions.
Installation
composer require andchir/omnipay3-yandexkassa
Ensure your composer.json has "minimum-stability": "dev" if using a dev package.
Service Provider
Register the gateway in config/app.php under providers:
Omnipay\Omnipay::create('YandexKassa')
Or publish the config (if available) via:
php artisan vendor:publish --provider="Andchir\Omnipay3YandexKassa\YandexKassaServiceProvider"
First Use Case: Basic Payment
use Omnipay\Omnipay;
$gateway = Omnipay::create('YandexKassa');
$gateway->setShopId('your_shop_id');
$gateway->setSecretKey('your_secret_key');
$response = $gateway->purchase([
'amount' => '10.00',
'currency' => 'RUB',
'description' => 'Test Payment',
'method' => 'bank_card', // or 'wallet'
'capture' => true,
])->send();
if ($response->isSuccessful()) {
// Redirect to Yandex.Kassa payment page
$paymentUrl = $response->getPaymentUrl();
return redirect($paymentUrl);
}
Key Config
Check config/omnipay.php (if published) for:
shopId (Yandex.Kassa merchant ID)secretKey (API secret)testMode (set to true for sandbox testing)Authorization + Capture For deferred payments (e.g., subscriptions):
// Authorize (hold funds)
$authResponse = $gateway->authorize(['amount' => '20.00'])->send();
if ($authResponse->isSuccessful()) {
$transactionId = $authResponse->getTransactionReference();
// Later...
$captureResponse = $gateway->capture([
'amount' => '20.00',
'transactionReference' => $transactionId,
])->send();
}
Webhook Handling Validate Yandex.Kassa webhook signatures and process events:
use Andchir\Omnipay3YandexKassa\Webhook\WebhookValidator;
$validator = new WebhookValidator($request->getContent(), $request->get('signature'));
if ($validator->isValid()) {
$event = $validator->getEvent();
// Handle event (e.g., payment.succeeded)
}
Refunds
$refundResponse = $gateway->refund([
'amount' => '5.00',
'transactionReference' => 'YK_12345',
])->send();
/yandex-kassa/webhook to a controller handling POST requests.web middleware for webhook routes to access $request.\Log::error('Yandex.Kassa Error', ['response' => $response->getMessage()]);
testMode: true.Signature Validation
sha1 hash of the payload + secretKey.$calculatedSignature = sha1($payload . $secretKey);
if ($calculatedSignature !== $request->get('signature')) {
abort(403, 'Invalid signature');
}
Currency Mismatch
currency: 'RUB' is recommended.Transaction Reference
transactionReference (e.g., YK_12345) for future actions (capture/refund).YK_ prefix indicates Yandex.Kassa transactions.Test Mode Quirks
config/omnipay.php:
'log' => [
'enabled' => true,
'path' => storage_path('logs/omnipay.log'),
],
InvalidShopId: Verify shopId in config.InvalidAmount: Ensure amount is a string (e.g., '10.00'), not integer.403 Forbidden: Check secretKey and webhook signature.Custom Fields
Pass additional Yandex.Kassa parameters via data array:
$gateway->purchase([
'amount' => '10.00',
'data' => [
'confirmation' => ['type' => 'redirect', 'return_url' => url('/payment/confirm')],
'receipt' => ['customer' => ['phone' => '+79991234567']],
],
]);
Event Handling
Extend Andchir\Omnipay3YandexKassa\Webhook\WebhookValidator to add custom logic for specific events (e.g., payment.cancelled).
Fallbacks Implement retry logic for failed transactions:
if (!$response->isSuccessful() && $attempts < 3) {
sleep(2);
$attempts++;
retry();
}
How can I help you explore Laravel packages today?