PaymentResource.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. 'service_package_id' => $this->service_package_id,
  21. 'schedule_ids' => $this->whenLoaded('servicePackage', fn () => $this->servicePackage?->items->pluck('schedule_id')->values()),
  22. 'schedules' => $this->whenLoaded('servicePackage', fn () => ScheduleResource::collection(
  23. $this->servicePackage?->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->resource->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. public static function collection($resource): AnonymousResourceCollection
  58. {
  59. return parent::collection($resource);
  60. }
  61. }