Installation
composer require amorebietakoudala/mipago-bundle
Ensure your project meets PHP (≥7.1 or ≥8.1) and Symfony (6.4+) requirements.
Enable the Bundle
Add to config/bundles.php (Symfony 5.1+):
return [
// ...
MiPago\Bundle\MiPagoBundle::class => ['all' => true],
];
Configuration Publish the default config:
php bin/console config:dump-reference MiPagoBundle
Configure in config/packages/mipago.yaml:
mipago:
merchant_id: 'YOUR_MERCHANT_ID'
secret_key: '%env(MIPAGO_SECRET_KEY)%'
environment: '%kernel.environment%' # 'test' or 'prod'
success_url: 'https://your-app.com/payment/success'
failure_url: 'https://your-app.com/payment/failure'
First Use Case Trigger a payment in a controller:
use MiPago\Bundle\MiPagoBundle\Service\MiPagoService;
class PaymentController extends AbstractController
{
public function initiatePayment(MiPagoService $miPagoService): Response
{
$paymentData = [
'amount' => 100.00,
'currency' => 'EUR',
'description' => 'Order #12345',
'reference' => 'ORDER_12345',
'buyer' => [
'name' => 'John Doe',
'email' => 'john@example.com',
],
];
return $miPagoService->createPayment($paymentData);
}
}
Payment Initiation
Use MiPagoService to create payments:
$payment = $miPagoService->createPayment([
'amount' => 50.00,
'currency' => 'EUR',
'reference' => 'INV_67890',
'buyer' => ['name' => 'Jane Smith', 'email' => 'jane@example.com'],
'additional_data' => ['tax_id' => '12345678A'], // Optional
]);
Webhook Handling Configure a controller to handle MiPago’s notifications:
#[Route('/mipago/webhook', name: 'mipago_webhook', methods: ['POST'])]
public function handleWebhook(Request $request, MiPagoService $miPagoService): Response
{
$notification = $miPagoService->processNotification($request->getContent());
// Validate signature and process payment status
if ($notification->isValid()) {
// Update your database, send emails, etc.
}
return new Response('OK');
}
MiPagoNotification to inspect payment status (e.g., success, failed, pending).Testing Payments
environment: 'test' in config.Twig Integration Render payment buttons or statuses:
{% if payment.status == 'pending' %}
<a href="{{ path('mipago_initiate', {'reference': payment.reference}) }}">
Pay Now ({{ payment.amount }} EUR)
</a>
{% endif %}
Retry Logic for Failed Payments Implement a queue job to retry failed payments after a delay:
use MiPago\Bundle\MiPagoBundle\Service\MiPagoService;
class RetryFailedPaymentJob implements ShouldQueue
{
public function handle(MiPagoService $miPagoService)
{
$payment = $miPagoService->retryPayment('FAILED_REFERENCE');
// Log retry attempts
}
}
Dynamic Configuration Override config per environment:
# config/packages/dev/mipago.yaml
mipago:
environment: 'test'
success_url: 'http://localhost:8000/success'
Custom Validation
Extend MiPagoNotification to add business logic:
class CustomMiPagoNotification extends MiPagoNotification
{
public function isValid(): bool
{
if (!parent::isValid()) return false;
// Add custom validation (e.g., check amount against your DB)
return $this->amount <= $this->getMaxAllowedAmount();
}
}
Bind it in services.yaml:
services:
MiPago\Bundle\MiPagoBundle\Service\MiPagoService:
arguments:
$notificationClass: App\Service\CustomMiPagoNotification
Routing Attribute Changes (v2.0+)
@Route) with attributes (#[Route]).// Before (v1.x)
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
#[Route('/webhook', name: 'mipago_webhook')]
public function handleWebhook() { ... }
UTF-8 Response Handling
$content = mb_convert_encoding($request->getContent(), 'UTF-8');
SSL Certificate Errors (Basque Gov)
// In config/packages/monolog.yaml (if needed)
handlers:
main:
type: stream
path: '%kernel.logs_dir%/%kernel.environment%.log'
level: debug
channels: ['!event']
handler: rotated_file
processor: [Monolog\Processor\MemoryPeakUsageProcessor]
Environment-Specific URLs
mipago:
environment: 'test' # Uses sandbox URL
Dependency Conflicts
composer why-not to check compatibility.composer.json if needed:
"require": {
"symfony/http-foundation": "^6.0"
}
Enable Verbose Logging
Add to config/packages/monolog.yaml:
monolog:
handlers:
main:
level: debug
channels: ['!event']
Check logs for MiPago API responses:
grep -i "MiPago" var/log/dev.log
Validate Webhook Signatures Always verify notifications:
$notification = $miPagoService->processNotification($rawContent);
if (!$notification->isValid()) {
throw new \RuntimeException('Invalid MiPago notification signature');
}
Test Locally with Ngrok
Expose your success_url/failure_url locally:
ngrok http 8000
Update config to use the Ngrok URL during testing.
Custom Payment Data
Extend the MiPagoService to add fields:
// In your service
public function createPayment(array $data): Response
{
$data['custom_field'] = 'your_value';
return parent::createPayment($data);
}
Override Notification Processing
Replace the default MiPagoNotification class (see Advanced Patterns above).
Add Middleware for Payments Secure payment endpoints:
// config/routes/annotations.yaml
resources:
- { path: '/payment', type: 'annotation', prefix: '/payment', middleware: ['auth'] }
Localization Support Use Symfony’s translation component
How can I help you explore Laravel packages today?