| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- <?php
- namespace App\Services\Pagarme;
- use App\Data\Pagarme\Request\PagarmeTransferRequestData;
- use App\Models\Provider;
- use App\Models\ProviderWithdrawal;
- use App\Services\Pagarme\Concerns\SendsPagarmeRequests;
- use Carbon\Carbon;
- class PagarmeTransferService
- {
- use SendsPagarmeRequests;
- public function createTransfer(int $amountInCents, string $recipientId, string $idempotencyKey): array
- {
- if ($this->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));
- }
- }
|