shouldMockTransferRequest()) { return $this->mockTransferResponse( amountInCents: $amountInCents, recipientId: $recipientId, idempotencyKey: $idempotencyKey, ); } return $this->pagarmeRequest( method: 'POST', path: '/transfers', payload: new PagarmeTransferRequestData( amount: $amountInCents, recipientId: $recipientId, ), idempotencyKey: $idempotencyKey, errorMessage: 'Erro ao criar transferencia (saque) no Pagar.me.', ); } public function getTransfer(string $transferId): array { if ($this->shouldMockTransferRequest()) { return $this->mockTransferLookupResponse($transferId); } return $this->pagarmeRequest( method: 'GET', path: "/transfers/{$transferId}", payload: [], idempotencyKey: "get-transfer-{$transferId}", errorMessage: 'Erro ao consultar transferencia no Pagar.me.', ); } // private function shouldMockTransferRequest(): bool { return app()->environment('local', 'development'); } private function mockTransferResponse(int $amountInCents, string $recipientId, string $idempotencyKey): array { $provider = Provider::query() ->where('recipient_id', $recipientId) ->first(); $createdAt = Carbon::now(); return [ 'id' => $this->mockTransferId($idempotencyKey), 'amount' => $amountInCents, 'type' => 'credito_em_conta', 'status' => 'pending_transfer', 'fee' => 0, 'funding_date' => null, 'funding_estimated_date' => $createdAt->copy()->addWeekday()->toISOString(), 'transaction_id' => null, 'bank_account' => $provider?->recipient_default_bank_account, 'date_created' => $createdAt->toISOString(), 'created_at' => $createdAt->toISOString(), 'metadata' => [ 'mocked' => true, 'environment' => app()->environment(), 'recipient_id' => $recipientId, 'idempotency_key' => $idempotencyKey, 'provider_id' => $provider?->id, ], 'bank_response' => null, ]; } private function mockTransferLookupResponse(string $transferId): array { $withdrawal = ProviderWithdrawal::query() ->where('transfer_id', $transferId) ->first(); if ($withdrawal?->gateway_payload) { return $withdrawal->gateway_payload; } $createdAt = Carbon::now(); return [ 'id' => $transferId, 'amount' => $withdrawal ? (int) round((float) $withdrawal->gross_amount * 100) : 0, 'type' => $withdrawal?->type ?? 'credito_em_conta', 'status' => $withdrawal?->status?->value ?? 'pending_transfer', 'fee' => $withdrawal ? (int) round((float) $withdrawal->gateway_fee_amount * 100) : 0, 'funding_date' => $withdrawal?->completed_at?->toISOString(), 'funding_estimated_date' => $createdAt->copy()->addWeekday()->toISOString(), 'transaction_id' => null, 'bank_account' => $withdrawal?->bank_account, 'date_created' => $withdrawal?->created_at?->toISOString() ?? $createdAt->toISOString(), 'created_at' => $withdrawal?->created_at?->toISOString() ?? $createdAt->toISOString(), 'metadata' => $withdrawal?->metadata ?? [ 'mocked' => true, 'environment' => app()->environment(), ], 'bank_response' => $withdrawal?->bank_response, ]; } private function mockTransferId(string $idempotencyKey): string { return (string) abs(crc32($idempotencyKey)); } }