ProcessAsaasWebhookJob.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace App\Jobs;
  3. use App\Enums\ReceivableStatus;
  4. use App\Models\FranchiseeAccountReceive;
  5. use Illuminate\Bus\Queueable;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Foundation\Bus\Dispatchable;
  8. use Illuminate\Queue\InteractsWithQueue;
  9. use Illuminate\Queue\SerializesModels;
  10. use Illuminate\Support\Facades\Log;
  11. class ProcessAsaasWebhookJob implements ShouldQueue
  12. {
  13. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  14. private const EVENT_STATUS_MAP = [
  15. 'PAYMENT_RECEIVED' => ReceivableStatus::PAID,
  16. 'PAYMENT_CONFIRMED' => ReceivableStatus::PAID,
  17. 'PAYMENT_OVERDUE' => ReceivableStatus::OVERDUE,
  18. 'PAYMENT_DELETED' => ReceivableStatus::CANCELLED,
  19. 'PAYMENT_REFUNDED' => ReceivableStatus::CANCELLED,
  20. ];
  21. public array $payload;
  22. public function __construct(array $payload)
  23. {
  24. $this->payload = $payload;
  25. }
  26. public function handle(): void
  27. {
  28. $event = $this->payload['event'] ?? null;
  29. $payment = $this->payload['payment'] ?? [];
  30. if (!$event || empty($payment)) {
  31. Log::warning('Asaas Webhook Job: payload incompleto', $this->payload);
  32. return;
  33. }
  34. $newStatus = self::EVENT_STATUS_MAP[$event] ?? null;
  35. if (!$newStatus) {
  36. Log::info("Asaas Webhook Job: evento '{$event}' ignorado (não mapeado).");
  37. return;
  38. }
  39. $externalReference = $payment['externalReference'] ?? null;
  40. $asaasId = $payment['id'] ?? null;
  41. $receive = null;
  42. if ($externalReference) {
  43. $receive = FranchiseeAccountReceive::find($externalReference);
  44. }
  45. if (!$receive && $asaasId) {
  46. $receive = FranchiseeAccountReceive::where('asaas_id', $asaasId)->first();
  47. }
  48. if (!$receive) {
  49. Log::warning("Asaas Webhook Job: recebível não encontrado", [
  50. 'externalReference' => $externalReference,
  51. 'asaas_id' => $asaasId,
  52. 'event' => $event,
  53. ]);
  54. return;
  55. }
  56. // Se já está pago, não processa de novo
  57. if ($receive->status === ReceivableStatus::PAID && $newStatus === ReceivableStatus::PAID) {
  58. Log::info("Asaas Webhook Job: recebível #{$receive->id} já está pago. Ignorando duplicata.");
  59. return;
  60. }
  61. // Atualizar o status
  62. $updateData = [
  63. 'status' => $newStatus,
  64. 'asaas_status' => $payment['status'] ?? $event,
  65. ];
  66. // Se foi pago, registrar a data de pagamento e o valor pago
  67. if ($newStatus === ReceivableStatus::PAID) {
  68. $updateData['payment_date'] = $payment['paymentDate'] ?? $payment['confirmedDate'] ?? now();
  69. $updateData['paid_value'] = $payment['value'] ?? $receive->value;
  70. }
  71. $receive->update($updateData);
  72. Log::info("Asaas Webhook Job: recebível #{$receive->id} atualizado para '{$newStatus->value}'", [
  73. 'event' => $event,
  74. 'asaas_id' => $asaasId,
  75. ]);
  76. }
  77. }