brazilianfriendsofsymfony/pagamento-digital-bundle
Symfony bundle integrating Pagamento Digital payment services for Brazilian e-commerce apps. Provides configuration and helper services to communicate with the gateway, handle checkout requests and notifications, and streamline payment workflow integration within Symfony projects.
Installation Add the bundle via Composer:
composer require brazilianfriendsosymfony/pagamento-digital-bundle
Enable it in config/bundles.php:
return [
// ...
BrazilianFriendsOfSymfony\PagamentoDigitalBundle\BFOSPagamentoDigitalBundle::class => ['all' => true],
];
Configuration Publish the default config:
php bin/console bfos:pagamento-digital:install
Update config/packages/bfos_pagamento_digital.yaml with your Pagamento Digital API credentials (e.g., api_key, merchant_id).
First Use Case: Creating a Payment
Use the Payment service to generate a transaction:
use BrazilianFriendsOfSymfony\PagamentoDigitalBundle\Service\PaymentService;
$paymentService = $this->container->get(PaymentService::class);
$payment = $paymentService->createPayment(
100.00, // Amount
'BRL', // Currency
'customer@example.com', // Customer email
'Order #123' // Description
);
The response will include a payment_id and payment_url for redirecting the user.
Payment Creation & Redirection
PaymentService to create a payment and redirect users to the Pagamento Digital gateway:
$payment = $paymentService->createPayment($amount, $currency, $email, $description);
return $this->redirect($payment->getPaymentUrl());
Webhook Handling
POST /webhook/payment):
use BrazilianFriendsOfSymfony\PagamentoDigitalBundle\Event\PaymentEvent;
#[Route('/webhook/payment', name: 'pagamento_digital_webhook', methods: ['POST'])]
public function handleWebhook(Request $request, PaymentService $paymentService): Response
{
$data = json_decode($request->getContent(), true);
$paymentService->handleWebhook($data); // Validates and processes events (e.g., PAYMENT_CONFIRMED)
return new Response('OK');
}
PaymentConfirmedEvent) in your services.yaml:
services:
App\EventListener\PaymentListener:
tags:
- { name: 'kernel.event_listener', event: 'bfos.pagamento_digital.payment.confirmed', method: 'onPaymentConfirmed' }
Refunds & Cancellations
RefundService to process refunds:
$refundService = $this->container->get(RefundService::class);
$refund = $refundService->createRefund($paymentId, 50.00, 'Partial refund');
Subscription Management
SubscriptionService:
$subscriptionService = $this->container->get(SubscriptionService::class);
$subscription = $subscriptionService->createSubscription(
$customerId,
'monthly_plan_id',
'customer@example.com'
);
$builder->add('amount', MoneyType::class, ['currency' => 'BRL']);
$builder->add('email', EmailType::class);
Payment entity or create a custom one to map to your database:
#[ORM\Entity]
class AppPayment {
#[ORM\OneToOne(targetEntity: \BrazilianFriendsOfSymfony\PagamentoDigitalBundle\Entity\Payment::class)]
private ?Payment $pagamentoDigitalPayment = null;
}
PaymentService in tests with mocked API responses:
$paymentService->setClient($mockedClient); // Inject a mocked Guzzle client
API Rate Limits
$client->setDefaultOption('timeout', 30);
$client->setDefaultOption('connect_timeout', 10);
Webhook Verification
api_secret from your config:
$isValid = $paymentService->validateWebhookSignature($request->getContent(), $request->headers->get('X-Signature'));
if (!$isValid) throw new \RuntimeException('Invalid webhook signature');
Currency & Amount Precision
100.00, not 100). Use number_format() or bcdiv() for calculations.Idempotency
$paymentService->createPayment(..., null, 'unique-idempotency-key');
Enable API Logging Configure the Guzzle client to log requests/responses:
# config/packages/bfos_pagamento_digital.yaml
services:
BrazilianFriendsOfSymfony\PagamentoDigitalBundle\Client\PagamentoDigitalClient:
arguments:
$logger: '@logger'
Check logs at var/log/dev.log for API errors.
Test Mode
Use the test_mode: true config to avoid real charges during development.
Custom Events
Extend the bundle’s events (e.g., PaymentFailedEvent) to trigger custom logic:
// src/EventListener/CustomPaymentListener.php
class CustomPaymentListener {
public function onPaymentFailed(PaymentFailedEvent $event): void {
// Send email, update CRM, etc.
}
}
API Client Overrides
Replace the default PagamentoDigitalClient with a custom implementation for additional features (e.g., logging, metrics):
# config/services.yaml
services:
App\Client\CustomPagamentoDigitalClient:
decorates: 'BrazilianFriendsOfSymfony\PagamentoDigitalBundle\Client\PagamentoDigitalClient'
arguments:
$decorated: '@.inner'
Database Migrations
The bundle doesn’t include migrations. Manually create tables for Payment, Refund, and Subscription entities or use Doctrine migrations:
php bin/console make:migration
php bin/console doctrine:migrations:migrate
PaymentConfirmedEvent) to a queue for background processing:
$eventDispatcher->dispatch(new PaymentConfirmedEvent($payment));
translations/:
# config/packages/bfos_pagamento_digital.yaml
twig:
default_locale: 'pt_BR'
How can I help you explore Laravel packages today?