PaymentResource.php 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace App\Http\Resources;
  3. use App\Models\Payment;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
  6. use Illuminate\Http\Resources\Json\JsonResource;
  7. class PaymentResource extends JsonResource
  8. {
  9. /**
  10. * Transform the resource into an array.
  11. *
  12. * @return array<string, mixed>
  13. */
  14. public function toArray(Request $request): array
  15. {
  16. return [
  17. 'id' => $this->id,
  18. 'schedule_id' => $this->schedule_id,
  19. 'schedule' => new ScheduleResource($this->whenLoaded('schedule')),
  20. 'schedule_ids' => $this->whenLoaded('schedules', fn () => $this->schedules->pluck('id')->values()),
  21. 'schedules' => $this->whenLoaded('schedules', fn () => ScheduleResource::collection($this->schedules)),
  22. 'client_id' => $this->client_id,
  23. 'provider_id' => $this->provider_id,
  24. 'client_name' => $this->client?->user?->name,
  25. 'provider_name' => $this->provider?->user?->name,
  26. 'client_payment_method_id' => $this->client_payment_method_id,
  27. 'gateway_provider' => $this->gateway_provider,
  28. 'gateway_entity_reference' => $this->gateway_entity_reference,
  29. 'gateway_entity_label' => $this->gateway_entity_label,
  30. 'gateway_operation_reference' => $this->gateway_operation_reference,
  31. 'gateway_operation_label' => $this->gateway_operation_label,
  32. 'payment_method' => $this->payment_method,
  33. 'status' => $this->status,
  34. 'gross_amount' => $this->gross_amount,
  35. 'gateway_fee_amount' => $this->gateway_fee_amount,
  36. 'platform_fee_amount' => $this->platform_fee_amount,
  37. 'net_amount' => $this->net_amount,
  38. 'currency' => $this->currency,
  39. 'installments' => $this->installments,
  40. 'authorized_at' => $this->authorized_at?->toISOString(),
  41. 'paid_at' => $this->paid_at?->toISOString(),
  42. 'failed_at' => $this->failed_at?->toISOString(),
  43. 'cancelled_at' => $this->cancelled_at?->toISOString(),
  44. 'expires_at' => $this->expires_at?->toISOString(),
  45. 'failure_code' => $this->failure_code,
  46. 'failure_message' => $this->failure_message,
  47. 'pix' => $this->pixData(),
  48. 'gateway_payload' => $this->gateway_payload,
  49. 'metadata' => $this->metadata,
  50. 'created_at' => $this->created_at?->toISOString(),
  51. 'updated_at' => $this->updated_at?->toISOString(),
  52. ];
  53. }
  54. /**
  55. * @param \Illuminate\Database\Eloquent\Collection<Payment> $resource
  56. * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection<PaymentResource>
  57. */
  58. public static function collection($resource): AnonymousResourceCollection
  59. {
  60. return parent::collection($resource);
  61. }
  62. private function pixData(): ?array
  63. {
  64. if ($this->payment_method !== 'pix') {
  65. return null;
  66. }
  67. $charge = $this->gateway_payload['charges'][0] ?? [];
  68. $transaction = $charge['last_transaction'] ?? [];
  69. $expiresAt = $charge['expires_at']
  70. ?? $transaction['expires_at']
  71. ?? $this->expires_at?->toISOString();
  72. return [
  73. 'qr_code' => $transaction['qr_code'] ?? null,
  74. 'qr_code_url' => $transaction['qr_code_url'] ?? null,
  75. 'expires_at' => $expiresAt,
  76. 'status' => $transaction['status'] ?? null,
  77. ];
  78. }
  79. }