PaymentResource.php 4.1 KB

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