| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?php
- namespace App\Http\Resources;
- use App\Models\Payment;
- use Illuminate\Http\Request;
- use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
- use Illuminate\Http\Resources\Json\JsonResource;
- class PaymentResource extends JsonResource
- {
- /**
- * Transform the resource into an array.
- *
- * @return array<string, mixed>
- */
- public function toArray(Request $request): array
- {
- return [
- 'id' => $this->id,
- 'schedule_id' => $this->schedule_id,
- 'client_id' => $this->client_id,
- 'provider_id' => $this->provider_id,
- 'client_payment_method_id' => $this->client_payment_method_id,
- 'gateway_provider' => $this->gateway_provider,
- 'gateway_entity_reference' => $this->gateway_entity_reference,
- 'gateway_entity_label' => $this->gateway_entity_label,
- 'gateway_operation_reference' => $this->gateway_operation_reference,
- 'gateway_operation_label' => $this->gateway_operation_label,
- 'payment_method' => $this->payment_method,
- 'status' => $this->status,
- 'gross_amount' => $this->gross_amount,
- 'gateway_fee_amount' => $this->gateway_fee_amount,
- 'platform_fee_amount' => $this->platform_fee_amount,
- 'net_amount' => $this->net_amount,
- 'currency' => $this->currency,
- 'installments' => $this->installments,
- 'authorized_at' => $this->authorized_at?->toISOString(),
- 'paid_at' => $this->paid_at?->toISOString(),
- 'failed_at' => $this->failed_at?->toISOString(),
- 'cancelled_at' => $this->cancelled_at?->toISOString(),
- 'expires_at' => $this->expires_at?->toISOString(),
- 'failure_code' => $this->failure_code,
- 'failure_message' => $this->failure_message,
- 'pix' => $this->pixData(),
- 'gateway_payload' => $this->gateway_payload,
- 'metadata' => $this->metadata,
- 'created_at' => $this->created_at?->toISOString(),
- 'updated_at' => $this->updated_at?->toISOString(),
- ];
- }
- /**
- * @param \Illuminate\Database\Eloquent\Collection<Payment> $resource
- * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection<PaymentResource>
- */
- public static function collection($resource): AnonymousResourceCollection
- {
- return parent::collection($resource);
- }
- private function pixData(): ?array
- {
- if ($this->payment_method !== 'pix') {
- return null;
- }
- $transaction = $this->gateway_payload['charges'][0]['last_transaction'] ?? [];
- return [
- 'qr_code' => $transaction['qr_code'] ?? null,
- 'qr_code_url' => $transaction['qr_code_url'] ?? null,
- 'expires_at' => $transaction['expires_at'] ?? $this->expires_at?->toISOString(),
- 'status' => $transaction['status'] ?? null,
- ];
- }
- }
|