Installation Add the bundle via Composer:
composer require bsadnu/payex-bundle
Enable it in config/bundles.php:
return [
// ...
Bsadnu\PayexBundle\BsadnuPayexBundle::class => ['all' => true],
];
Configuration Publish the default config:
php bin/console bsadnu:payex:install
Update config/packages/bsadnu_payex.yaml with your PayEx credentials (e.g., merchant_id, test_mode, secret_key).
First Use Case: Redirect Payment Create a controller to initiate a payment:
use Bsadnu\PayexBundle\Service\PayexService;
class PaymentController extends AbstractController
{
public function createPayment(PayexService $payex): Response
{
$payment = $payex->createPayment(
'123', // Order ID
1000, // Amount (in lowest currency unit, e.g., 1000 = 10.00 SEK)
'SEK', // Currency
'https://your-site.com/payment/callback' // Callback URL
);
return $this->redirect($payment->getPaymentUrl());
}
}
Callback Handling
Define a route for PayEx’s callback (e.g., /payment/callback) and verify the response:
public function handleCallback(PayexService $payex, Request $request): Response
{
$response = $payex->verifyCallback($request->getContent());
if ($response->isSuccessful()) {
// Process successful payment (e.g., update order status)
}
return new Response('OK');
}
Payment Flow
PayexService::createPayment() to generate a redirect URL.verifyCallback().Recurring Payments
Use createRecurringPayment() for subscriptions:
$recurring = $payex->createRecurringPayment(
'sub_123',
500,
'SEK',
'monthly',
'https://your-site.com/recurring/callback'
);
Refunds Refund a payment via:
$payex->createRefund('payment_id_123', 500, 'SEK');
payex.payment.created or payex.payment.callback events for custom logic.bsadnu_payex:
debug: true
| Use Case | Method/Service | Notes |
|---|---|---|
| One-time payment | createPayment() |
Redirect or direct (tokenized) flow. |
| Subscription | createRecurringPayment() |
Requires PayEx subscription setup. |
| Refund | createRefund() |
Must match original currency/amount. |
| Invoice payment | createInvoicePayment() |
For invoice-based payments. |
| Verify callback | verifyCallback() |
Critical: Always verify signatures. |
Callback Verification
verifyCallback() and check:
$response = $payex->verifyCallback($rawContent);
if (!$response->isValidSignature()) {
throw new \RuntimeException('Invalid callback signature!');
}
test_mode: true in config.Amount Handling
1000).bcmath for floats).Timeouts
payment_id in DB before processing).Deprecated Methods
Direct Payments (Tokenization)
Enable Debug Mode:
bsadnu_payex:
debug: true
Logs requests/responses to var/log/payex.log.
Test Mode:
Use PayEx’s test environment (test_mode: true) to simulate payments. Test cards:
4111111111111111 (Visa)4000000000000002 (Declined)Common Errors:
| Error | Cause | Fix |
|---|---|---|
Invalid signature |
Callback tampering or wrong secret. | Verify secret_key in config. |
Payment not found |
Wrong payment_id or test mode issue. |
Check PayEx dashboard for test payments. |
Amount mismatch |
Currency/amount mismatch. | Ensure amounts match PayEx’s precision. |
Callback URL unreachable |
Incorrect URL in PayEx config. | Test the callback URL manually. |
Custom Responses
Extend Bsadnu\PayexBundle\Response\PaymentResponse to add custom fields:
class CustomPaymentResponse extends PaymentResponse
{
public function getCustomField(): string
{
return $this->data['custom_field'] ?? '';
}
}
Override the service to use your class.
Event Listeners Subscribe to events for custom logic:
// config/services.yaml
services:
App\EventListener\PayexListener:
tags:
- { name: kernel.event_listener, event: payex.payment.created, method: onPaymentCreated }
API Versioning
If PayEx updates their API, fork the bundle and override the PayexClient class to adapt to new endpoints.
Multi-Currency Support Dynamically set currency based on user locale:
$currency = $user->getPreferredCurrency(); // e.g., 'EUR'
$payex->createPayment($orderId, $amount, $currency, $callbackUrl);
secret_key in bsadnu_payex.yaml matches PayEx’s merchant settings.curl:
curl -X POST -H "Content-Type: application/json" --data '{"payment":"..."}' https://your-site.com/payment/callback
test_mode before switching to live. Use PayEx’s test cards.How can I help you explore Laravel packages today?