PaymentResource.php 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. 'client_id' => $this->client_id,
  21. 'provider_id' => $this->provider_id,
  22. 'client_name' => $this->client?->user?->name,
  23. 'provider_name' => $this->provider?->user?->name,
  24. 'client_payment_method_id' => $this->client_payment_method_id,
  25. 'gateway_provider' => $this->gateway_provider,
  26. 'gateway_entity_reference' => $this->gateway_entity_reference,
  27. 'gateway_entity_label' => $this->gateway_entity_label,
  28. 'gateway_operation_reference' => $this->gateway_operation_reference,
  29. 'gateway_operation_label' => $this->gateway_operation_label,
  30. 'payment_method' => $this->payment_method,
  31. 'status' => $this->status,
  32. 'gross_amount' => $this->gross_amount,
  33. 'gateway_fee_amount' => $this->gateway_fee_amount,
  34. 'platform_fee_amount' => $this->platform_fee_amount,
  35. 'net_amount' => $this->net_amount,
  36. 'currency' => $this->currency,
  37. 'installments' => $this->installments,
  38. 'authorized_at' => $this->authorized_at?->toISOString(),
  39. 'paid_at' => $this->paid_at?->toISOString(),
  40. 'failed_at' => $this->failed_at?->toISOString(),
  41. 'cancelled_at' => $this->cancelled_at?->toISOString(),
  42. 'expires_at' => $this->expires_at?->toISOString(),
  43. 'failure_code' => $this->failure_code,
  44. 'failure_message' => $this->failure_message,
  45. 'pix' => $this->pixData(),
  46. 'gateway_payload' => $this->gateway_payload,
  47. 'metadata' => $this->metadata,
  48. 'created_at' => $this->created_at?->toISOString(),
  49. 'updated_at' => $this->updated_at?->toISOString(),
  50. ];
  51. }
  52. /**
  53. * @param \Illuminate\Database\Eloquent\Collection<Payment> $resource
  54. * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection<PaymentResource>
  55. */
  56. public static function collection($resource): AnonymousResourceCollection
  57. {
  58. return parent::collection($resource);
  59. }
  60. private function pixData(): ?array
  61. {
  62. if ($this->payment_method !== 'pix') {
  63. return null;
  64. }
  65. $charge = $this->gateway_payload['charges'][0] ?? [];
  66. $transaction = $charge['last_transaction'] ?? [];
  67. $expiresAt = $charge['expires_at']
  68. ?? $transaction['expires_at']
  69. ?? $this->expires_at?->toISOString();
  70. return [
  71. 'qr_code' => $transaction['qr_code'] ?? null,
  72. 'qr_code_url' => $transaction['qr_code_url'] ?? null,
  73. 'expires_at' => $expiresAt,
  74. 'status' => $transaction['status'] ?? null,
  75. ];
  76. }
  77. }