Install the Bundle Add the package via Composer:
composer require antilop/paybox-bundle
Register the bundle in config/bundles.php:
return [
// ...
Antilop\Bundle\PayboxBundle\PayboxBundle::class => ['all' => true],
];
Configure Paybox
Create config/packages/paybox.yml (Symfony 5+) or app/config/paybox.yml (older versions):
paybox:
parameters:
production: false # Set to `true` for live environment
site: 'YOUR_SITE_ID'
rank: 'YOUR_RANK'
login: 'YOUR_LOGIN'
hmac:
key: 'YOUR_HMAC_KEY'
First Use Case: Payment Request
Inject the PayboxClient service and trigger a payment:
use Antilop\Bundle\PayboxBundle\Client\PayboxClient;
class PaymentController extends AbstractController
{
public function initiatePayment(PayboxClient $paybox): Response
{
$payment = $paybox->createPayment([
'amount' => 1000, // Amount in cents
'currency' => '978', // EUR
'order_id' => 'ORDER_123',
'return_url' => $this->generateUrl('payment_return'),
'cancel_url' => $this->generateUrl('payment_cancel'),
'notification_url' => $this->generateUrl('payment_notification'),
]);
return $this->redirect($payment->getPaymentUrl());
}
}
Payment Initiation
Use PayboxClient::createPayment() to generate a payment URL. Pass an associative array with:
amount, currency, order_id, return_url, cancel_url, notification_url.description, customer_email, customer_ip, template (if configured).$payment = $paybox->createPayment([
'amount' => 5000,
'currency' => '978',
'order_id' => 'INV-456',
'return_url' => '/payment/success',
'cancel_url' => '/payment/failed',
'notification_url' => '/payment/webhook',
'template' => 'YOUR_TEMPLATE_ID', // If using Paybox templates
]);
Handling Callbacks
Paybox sends notifications to notification_url. Validate the HMAC signature before processing:
use Antilop\Bundle\PayboxBundle\Validator\PayboxValidator;
public function handleNotification(Request $request, PayboxValidator $validator): Response
{
if (!$validator->validateNotification($request)) {
return new Response('Invalid signature', 403);
}
$data = $request->request->all();
// Process payment status (e.g., update database)
return new Response('OK');
}
Refunds
Use PayboxClient::createRefund() for partial/full refunds:
$refund = $paybox->createRefund('ORDER_123', 2500); // Refund 25€
Subscription Management
For recurring payments, use PayboxClient::createSubscription():
$subscription = $paybox->createSubscription([
'amount' => 1000,
'currency' => '978',
'order_id' => 'SUB_789',
'cycle' => 'monthly',
'trial_days' => 7,
]);
paybox.notification events to decouple validation logic:
// config/services.yaml
services:
App\EventListener\PayboxListener:
tags:
- { name: kernel.event_listener, event: paybox.notification, method: onNotification }
production: false flag to test against Paybox’s sandbox environment.paybox.yml to log requests/responses:
paybox:
debug: true
HMAC Validation
403 explicitly:
if (!$validator->validateNotification($request)) {
$this->addFlash('error', 'Payment notification failed validation');
return $this->redirectToRoute('home');
}
Amount Formatting
1000 for €10.00). Forgetting this causes silent failures.URL Requirements
return_url, cancel_url, and notification_url must be absolute URLs (include https://).generateUrl() with full domain or hardcode URLs in production.Template Dependencies
Deprecated Methods
Enable Debug Mode
Add debug: true to paybox.yml to log raw requests/responses to var/log/paybox.log.
Sandbox Testing
production: false to test payments without real transactions.Common Errors
401 Unauthorized: Invalid site, rank, or login in config.400 Bad Request: Missing required fields (e.g., order_id).500 Server Error: HMAC mismatch (check hmac.key and request data).Extension Points
PayboxValidator to add business logic (e.g., fraud checks).$paymentStatus = $paybox->getPaymentStatus('ORDER_123');
$this->paymentRepository->updateFromPaybox($paymentStatus);
Environment Variables
Avoid hardcoding sensitive data (e.g., hmac.key). Use Symfony’s param bag or .env:
# config/packages/paybox.yml
paybox:
parameters:
hmac:
key: '%env(PAYBOX_HMAC_KEY)%'
Caching
The bundle doesn’t cache Paybox responses by default. For high-volume apps, cache getPaymentStatus() results:
$cacheKey = 'paybox_status_' . $orderId;
$status = $cache->get($cacheKey);
if (!$status) {
$status = $paybox->getPaymentStatus($orderId);
$cache->set($cacheKey, $status, 300); // Cache for 5 mins
}
Asynchronous Notifications
If notification_url is slow to respond, Paybox may retry. Implement idempotency (e.g., track processed order_ids in a database).
How can I help you explore Laravel packages today?