Procházet zdrojové kódy

feat: add console command para verificar saldo de um recipient

Gustavo Mantovani před 1 týdnem
rodič
revize
20c52cb04e

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

@@ -54,6 +54,19 @@ class PagarmeTransferService
 
     //
 
+    public function getRecipientBalance(string $recipientId): array
+    {
+        return $this->pagarmeRequest(
+            method:         'GET',
+            path:           "/recipients/{$recipientId}/balance",
+            payload:        [],
+            idempotencyKey: "get-recipient-balance-{$recipientId}",
+            errorMessage:   'Erro ao consultar saldo do recebedor no Pagar.me.',
+        );
+    }
+
+    //
+
     private function mockTransferId(string $idempotencyKey): string
     {
         return (string) abs(crc32($idempotencyKey));

+ 34 - 2
routes/console.php

@@ -1,10 +1,13 @@
 <?php
 
-use Illuminate\Foundation\Inspiring;
-use Illuminate\Support\Facades\Artisan;
 use App\Commands\CreateCrud;
 use App\Commands\RefreshPermissions;
 use App\Commands\TestWebsocketEvent;
+use App\Models\Provider;
+use App\Services\Pagarme\PagarmeTransferService;
+use App\Services\ProviderWithdrawalService;
+use Illuminate\Foundation\Inspiring;
+use Illuminate\Support\Facades\Artisan;
 
 Artisan::command('inspire', function () {
     $this->comment(Inspiring::quote());
@@ -35,3 +38,32 @@ Artisan::command('websocket:test {room} {--event=test-event} {--data=}', functio
         '--data' => $this->option('data'),
     ]);
 })->purpose('Test websocket broadcasting by emitting an event');
+
+//
+
+Artisan::command('pagarme:recipient-balance {recipient_id} {--skip-local}', function (
+    PagarmeTransferService $pagarmeTransfer,
+    ProviderWithdrawalService $withdrawals,
+) {
+    $recipientId = $this->argument('recipient_id');
+
+    if (! $this->option('skip-local')) {
+        $provider = Provider::query()
+            ->where('recipient_id', $recipientId)
+            ->first();
+
+        if ($provider) {
+            $this->info('Saldo calculado localmente:');
+            $this->line(sprintf('  available: R$ %s', number_format($withdrawals->getAvailableBalance($provider), 2, ',', '.')));
+            $this->line(sprintf('  pending:   R$ %s', number_format($withdrawals->getPendingBalance($provider), 2, ',', '.')));
+            $this->newLine();
+        } else {
+            $this->warn('Nenhum provider local encontrado para esse recipient_id.');
+            $this->newLine();
+        }
+    }
+
+    $this->info('Saldo retornado pelo Pagar.me:');
+
+    $this->line(json_encode($pagarmeTransfer->getRecipientBalance($recipientId), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
+})->purpose('Consult Pagar.me recipient balance and compare it with local withdrawal balance');