brazilianfriendsofsymfony/pagamento-bundle
Bundle Symfony para agregar funcionalidades comuns a meios de pagamento no Brasil, inspirado no JMSPaymentCoreBundle. Inclui utilitários e Twig helper para opções de parcelamento (juros, limites, templates) e suporte a formulários de escolha de pagamento.
Installation:
composer require brazilianfriendsofsymfony/pagamento-bundle
Add the bundle to config/bundles.php:
return [
// ...
BFOS\PagamentoBundle\BFOSPagamentoBundle::class => ['all' => true],
];
Configure Encryption Service (in .env or config/packages/bfos_pagamento.yaml):
bfos_pagamento:
servico_de_criptografia:
secret: 'your-secret-key-here'
First Use Case:
ParcelamentoUtils to generate payment options for a checkout flow:
use BFOS\PagamentoBundle\Utils\ParcelamentoConfiguracao;
use BFOS\PagamentoBundle\Utils\ParcelamentoUtils;
$config = new ParcelamentoConfiguracao();
$config->setParcelamentoHabilitado(true)
->setJurosParcelamento(1.99)
->setQuantidadeMaximaParcelas(12);
$options = ParcelamentoUtils::obterOpcoesDeParcelamento($config, 500);
Frontend Dependencies:
Ensure jquery and requirejs are included in your project (e.g., via Webpack Encore or standalone).
Form Setup:
Create a custom form type extending AbstractType to integrate payment options:
use BFOS\PagamentoBundle\Parcelamento\Form\Type\ParcelamentoType;
class CheckoutFormType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('payment_method', 'bfos_pagamento_forma_pagamento_checkout_choice', [
'configuracoes' => [
'pagseguro' => [
'configuracao_checkout_form' => new ParcelamentoType($config, $orderTotal)
]
]
]);
}
}
Twig Integration: Render parcel options dynamically in templates:
{{ bfos_pagamento_opcoes_parcelamento(config, 500, {
'colunas': 3,
'mostrarParcelas': [1, 2, 3, 6, 12],
'mostrarLinkVerTudo': true
}) }}
JavaScript Handling:
Leverage Resources/assets/ for dynamic form switching (e.g., pagseguro.js for gateway-specific logic).
Gateway-Specific Config:
Extend the bundle for new gateways (e.g., MercadoPago, PicPay) by:
FormType for each gateway.bfos_pagamento_forma_pagamento_checkout_choice in your theme.Encryption/Decryption:
Use the bundle’s ServicoDeCriptografia for sensitive data (e.g., tokens):
$service = $this->container->get('bfos_pagamento.servico_de_criptografia');
$encrypted = $service->encrypt('token123');
Webhook Handling:
Implement a controller to process gateway callbacks (e.g., PagSeguroWebhookController).
| Use Case | Implementation Pattern |
|---|---|
| Dynamic Parcel Calculation | ParcelamentoUtils::obterOpcoesDeParcelamento() |
| Gateway-Specific Forms | Custom FormType + JS integration |
| Token Storage | Encrypt via ServicoDeCriptografia |
| Order Validation | Extend PaymentValidator trait |
JavaScript Dependencies:
jquery/requirejs breaks frontend rendering.base.html.twig:
{{ encore_entry_link_tags('app') }} {# If using Webpack #}
<script src="{{ asset('js/jquery.js') }}"></script>
Encryption Key:
.env and validate in config/packages/bfos_pagamento.yaml:
secret: '%env(PAYMENT_SECRET)%'
Parcelamento Logic:
ParcelamentoConfiguracao values (e.g., jurosParcelamento as decimal vs. percentage).ParcelamentoUtils::obterOpcoesDeParcelamento() in a console command.Gateway-Specific Quirks:
bfos_pagamento_forma_pagamento_checkout_choice expects exact gateway names (e.g., 'pagseguro').Resources/config/gateways.yml for supported names.Form Rendering:
$form->getConfig()->getOption('configuracoes');
requirejs not loading).Parcelamento Calculation:
\Symfony\Component\VarDumper\VarDumper::dump($config);
valorMinimoParcela vs. total amount.Encryption:
$service->decrypt($encryptedToken); // Should return original value
Custom Gateways:
BFOS\PagamentoBundle\Resources/config/gateways.yml to add new gateways.mercado_pago:
label: 'Mercado Pago'
form_type: 'app.form.type.MercadoPagoType'
Twig Extensions:
bfos_pagamento_opcoes_parcelamento() by creating a custom Twig filter:
// src/Twig/AppExtension.php
public function getFilters() {
return [
new \Twig\TwigFilter('custom_parcel_options', [$this, 'customParcelOptions']),
];
}
Validation:
PaymentValidator trait to add custom rules:
use BFOS\PagamentoBundle\Validator\Constraints\PaymentValidator;
class CustomPaymentValidator extends PaymentValidator {
public function validateCustomRule($value, Constraint $constraint) {
// ...
}
}
ParcelamentoUtils results if used frequently (e.g., in a checkout session).data-attribute triggers).How can I help you explore Laravel packages today?