Prechádzať zdrojové kódy

feat: add mock do saque para ambiente de teste

Gustavo Mantovani 2 týždňov pred
rodič
commit
1433ee7acd

+ 94 - 0
app/Services/Pagarme/PagarmeTransferService.php

@@ -3,7 +3,10 @@
 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
 {
@@ -11,6 +14,14 @@ class PagarmeTransferService
 
     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',
@@ -25,6 +36,10 @@ class PagarmeTransferService
 
     public function getTransfer(string $transferId): array
     {
+        if ($this->shouldMockTransferRequest()) {
+            return $this->mockTransferLookupResponse($transferId);
+        }
+
         return $this->pagarmeRequest(
             method: 'GET',
             path: "/transfers/{$transferId}",
@@ -33,4 +48,83 @@ class PagarmeTransferService
             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));
+    }
 }