PaymentResource.php 3.5 KB

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