Installation:
composer require allset/przelewy24-bundle
Register the bundle in config/bundles.php (Symfony 4+) or AppKernel.php (Symfony 3):
Allset\Przelewy24Bundle\AllsetPrzelewy24Bundle::class => ['all' => true],
Routing:
Add to config/routes.yaml:
allset_przelewy24:
resource: "@AllsetPrzelewy24Bundle/Resources/config/routing.xml"
Configuration:
Add to config/packages/allset_przelewy24.yaml:
allset_przelewy24:
sandbox: true # Use true for testing
merchant_id: "YOUR_MERCHANT_ID"
crc_key: "YOUR_CRC_KEY"
First Use Case: Redirect a user to Przelewy24 payment form:
use Allset\Przelewy24Bundle\Factory\ProcessFactory;
use Allset\Przelewy24Bundle\Model\Payment;
public function initiatePayment(ProcessFactory $processFactory)
{
$payment = (new Payment())
->setCurrency('PLN')
->setSessionId('unique_order_token')
->setAmount(100.00)
->setDescription('Product purchase')
->setEmail('user@example.com')
->setReturnUrl($this->generateUrl('payment_return', [], UrlGeneratorInterface::ABSOLUTE_URL));
$processFactory->setPayment($payment);
return $this->redirect($processFactory->createAndGetUrl());
}
Payment Initiation:
ProcessFactory to create a payment object and generate a redirect URL.sessionId (e.g., order token) in your database for later reference.Event-Driven Success Handling:
przelewy24.event.payment_success to process successful payments.use Allset\Przelewy24Bundle\Event\PaymentEventInterface;
public function onPaymentSuccess(PaymentEventInterface $event)
{
$sessionId = $event->getPayment()->getSessionId();
$this->orderRepository->markAsPaid($sessionId);
}
config/services.yaml:
App\EventListener\Przelewy24Listener:
tags:
- { name: kernel.event_listener, event: przelewy24.event.payment_success, method: onPaymentSuccess }
Return URL Handling:
payment_return) to handle Przelewy24’s callback.PaymentValidator:
use Allset\Przelewy24Bundle\Validator\PaymentValidator;
public function returnAction(PaymentValidator $validator, Request $request)
{
$payment = $validator->validate($request->query->all());
if ($payment->isValid()) {
// Process successful payment
}
return $this->render('payment/return.html.twig');
}
sessionId to link Przelewy24 payments to your orders.$this->logger->info('Payment success', ['sessionId' => $sessionId, 'amount' => $payment->getAmount()]);
sandbox: true) for development./p24-fake-success/{sessionId} (dev-only route).Session ID Uniqueness:
sessionId must be unique and stored in your database. Reusing IDs will cause conflicts.Return URL Validation:
returnUrl. Use UrlGeneratorInterface::ABSOLUTE_URL:
$this->generateUrl('route_name', [], UrlGeneratorInterface::ABSOLUTE_URL)
Sandbox vs. Production:
sandbox: false in production will cause payments to fail silently.Event Listener Scope:
NullReferenceException:
services:
App\EventListener\Przelewy24Listener:
arguments:
$orderRepository: '@App\Repository\OrderRepository'
CRC Key Sensitivity:
crc_key is case-sensitive. Double-check your configuration.Test Tools:
/p24-test to verify API connectivity in development./p24-fake-success/{sessionId} to test event listeners.Logging:
monolog:
handlers:
main:
level: debug
Common Errors:
merchant_id or crc_key.sessionId, amount).returnUrl is correct and accessible.Custom Payment Fields:
Payment model to add custom fields:
class CustomPayment extends Payment
{
private $customField;
public function setCustomField($value): self
{
$this->customField = $value;
return $this;
}
}
ProcessFactory to support custom fields.Webhook Handling:
public function webhookAction(Request $request)
{
$validator = new PaymentValidator();
$payment = $validator->validate($request->request->all());
if ($payment->isValid()) {
// Handle webhook (e.g., update order status)
}
}
Multi-Currency Support:
PLN, EUR):
if (!in_array($payment->getCurrency(), ['PLN', 'EUR'])) {
throw new \InvalidArgumentException('Unsupported currency');
}
How can I help you explore Laravel packages today?