PaymentResource.php 3.4 KB

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