| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
- namespace App\Jobs;
- use App\Enums\ReceivableStatus;
- use App\Models\FranchiseeAccountReceive;
- use App\Models\StudentContractInstallment;
- use Illuminate\Bus\Queueable;
- use Illuminate\Contracts\Queue\ShouldQueue;
- use Illuminate\Foundation\Bus\Dispatchable;
- use Illuminate\Queue\InteractsWithQueue;
- use Illuminate\Queue\SerializesModels;
- use Illuminate\Support\Facades\Log;
- class ProcessAsaasWebhookJob implements ShouldQueue
- {
- use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
- private const EVENT_STATUS_MAP = [
- 'PAYMENT_RECEIVED' => ReceivableStatus::PAID,
- 'PAYMENT_CONFIRMED' => ReceivableStatus::PAID,
- 'PAYMENT_OVERDUE' => ReceivableStatus::OVERDUE,
- 'PAYMENT_DELETED' => ReceivableStatus::CANCELLED,
- 'PAYMENT_REFUNDED' => ReceivableStatus::CANCELLED,
- ];
- public array $payload;
- public function __construct(array $payload)
- {
- $this->payload = $payload;
- }
- public function handle(): void
- {
- $event = $this->payload['event'] ?? null;
- $payment = $this->payload['payment'] ?? [];
- if (!$event || empty($payment)) {
- Log::warning('Asaas Webhook Job: payload incompleto', $this->payload);
- return;
- }
- $newStatus = self::EVENT_STATUS_MAP[$event] ?? null;
- if (!$newStatus) {
- Log::info("Asaas Webhook Job: evento '{$event}' ignorado (não mapeado).");
- return;
- }
- $externalReference = $payment['externalReference'] ?? null;
- $asaasId = $payment['id'] ?? null;
- $receive = null;
- $type = 'unknown';
- if ($externalReference) {
- // Se for um recebível de Franquia
- if (str_starts_with($externalReference, 'TBR_')) {
- $id = str_replace('TBR_', '', $externalReference);
- $receive = FranchiseeAccountReceive::find($id);
- $type = 'FranchiseeAccountReceive';
- }
- // Se for parcela de aluno
- elseif (str_starts_with($externalReference, 'STU_')) {
- $id = str_replace('STU_', '', $externalReference);
- $receive = StudentContractInstallment::find($id);
- $type = 'StudentContractInstallment';
- }
- // Backward compatibility para faturas de teste geradas antes do prefixo
- elseif (is_numeric($externalReference)) {
- $receive = FranchiseeAccountReceive::find($externalReference);
- $type = 'FranchiseeAccountReceive (legacy)';
- }
- }
- // Fallback por asaas_id caso externalReference venha nulo
- if (!$receive && $asaasId) {
- $receive = FranchiseeAccountReceive::where('asaas_id', $asaasId)->first();
-
- if ($receive) {
- $type = 'FranchiseeAccountReceive (by asaas_id)';
- } else {
- $receive = StudentContractInstallment::where('asaas_id', $asaasId)->first();
- if ($receive) {
- $type = 'StudentContractInstallment (by asaas_id)';
- }
- }
- }
- if (!$receive) {
- Log::warning("Asaas Webhook Job: registro não encontrado", [
- 'externalReference' => $externalReference,
- 'asaas_id' => $asaasId,
- 'event' => $event,
- ]);
- return;
- }
- // Conversão de status: se a model usa string (como StudentContractInstallment)
- // Precisamos comparar de forma segura
- $currentStatus = $receive->status instanceof ReceivableStatus ? $receive->status->value : $receive->status;
- $newStatusValue = $newStatus instanceof ReceivableStatus ? $newStatus->value : $newStatus;
- // Idempotência
- if ($currentStatus === ReceivableStatus::PAID->value && $newStatusValue === ReceivableStatus::PAID->value) {
- Log::info("Asaas Webhook Job: registro {$type} #{$receive->id} já está pago. Ignorando duplicata.");
- return;
- }
- $updateData = [
- 'status' => $newStatusValue,
- 'asaas_status' => $payment['status'] ?? $event,
- ];
- if ($newStatusValue === ReceivableStatus::PAID->value) {
- $updateData['payment_date'] = $payment['paymentDate'] ?? $payment['confirmedDate'] ?? now();
- // A tabela do Aluno usa 'paid_value', a tabela da Franquia também.
- $updateData['paid_value'] = $payment['value'] ?? $receive->value;
- }
- $receive->update($updateData);
- Log::info("Asaas Webhook Job: {$type} #{$receive->id} atualizado para '{$newStatusValue}'", [
- 'event' => $event,
- 'asaas_id' => $asaasId,
- ]);
- }
- }
|