-
Notifications
You must be signed in to change notification settings - Fork 0
Notificação de Pagamento (Exemplo completo)
Vinicius Silva edited this page Aug 24, 2023
·
10 revisions
Este exemplo parte do princípio que você já visitou a seção Criando pagamentos exemplo completo e já está com o Laravel e o pacote devidamente configurados.
Este pacote já implementa todo fluxo de recebimento, validação e tratamento dos dados recebidos através de Eventos e Listeners.
Se você rodar o comando php artisan route:list
verá que existe um rota pagtesouro/webhook
- Edite o arquivo
.env
e inclua no final para ativar a notificação de status de pagamento
PAGTESOURO_URL_NOTIFICACAO=http://<dominio>/pagtesouro/webhook
<dominio>
deve ter um dos sufixos da tabela abaixo.
Sufixo | Descrição |
---|---|
.gov.br | (Instituições do Governo Federal) |
.def.br | (Defensorias Públicas) |
.jus.br | (Instituições do Poder Judiciário) |
.leg.br | (Instituições do Poder Legislativo) |
.mp.br | (Instituições do Ministério Público) |
.tc.br | (Tribunais de Contas) |
.mil.br ou .eb.br | (Forças Armadas Brasileiras) |
.edu.br ou .br | (Instituições de Ensino Públicas Federais) |
- Crie um listener para o evento Vsilva472\PagTesouro\Events\PaymentStatusChanged::class
php artisan make:listener AtualizarPagamento
- Edite o arquivo
app/Listeners/AtualizarPagamento.php
<?php
namespace App\Listeners;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Log;
use Vsilva472\PagTesouro\Events\PaymentStatusChanged;
class AtualizarPagamento
{
/**
* Handle the event.
*
* @param \Vsilva472\PagTesouro\Events\PaymentStatusChanged $event
* @return void
*/
public function handle(PaymentStatusChanged $event)
{
// @TODO: Update payment in database
Log::info($event->payment);
}
}
- Edite o arquivo
app\Providers\EventServiceProvider.php
use App\Listeners\AtualizarPagamento;
use Vsilva472\PagTesouro\Events\PaymentStatusChanged;
class EventServiceProvider extends ServiceProvider
{
//(...)
protected $listen = [
//(...)
PaymentStatusChanged::class => [
AtualizarPagamento::class
]
];
//(...)
}
- Desativar a verificação do CSRF Token na rota de notificação no arquivo
app\Http\Middleware\VerifyCsrfToken.php
//(...)
protected $except = [
'pagtesouro/webhook',
];
- Feito! Veja como simular uma notificação em localhost