RefreshPagarmeEntities.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace App\Commands;
  3. use App\Models\Address;
  4. use App\Models\Client;
  5. use App\Models\Provider;
  6. use App\Services\Pagarme\PagarmeCustomerService;
  7. use App\Services\Pagarme\PagarmeRecipientService;
  8. use Illuminate\Console\Command;
  9. class RefreshPagarmeEntities extends Command
  10. {
  11. protected $signature = 'pagarme:refresh-entities';
  12. protected $description = 'Limpa os recipients e clients locais e os recria no Pagar.me com os dados do banco local';
  13. public function __construct(
  14. protected PagarmeRecipientService $recipientService,
  15. protected PagarmeCustomerService $customerService,
  16. ) {
  17. parent::__construct();
  18. }
  19. public function handle(): int
  20. {
  21. $this->warn('ATENCAO: Este comando vai limpar os IDs de gateway dos recipients (providers) e customers (clients)');
  22. $this->warn('e recria-los no Pagar.me com os dados do banco local. Os registros antigos no Pagar.me');
  23. $this->warn('ficarao orfaos (nao serao deletados).');
  24. $this->newLine();
  25. $providers = Provider::query()
  26. ->whereNotNull('recipient_id')
  27. ->count();
  28. $clients = Client::query()
  29. ->whereNotNull('gateway_customer_id')
  30. ->count();
  31. $this->info("Providers com recipient_id: {$providers}");
  32. $this->info("Clients com gateway_customer_id: {$clients}");
  33. $this->newLine();
  34. if ($providers + $clients === 0) {
  35. $this->info('Nenhum registro para processar.');
  36. return Command::SUCCESS;
  37. }
  38. if (! $this->confirm('Deseja continuar com a limpeza e recriacao?')) {
  39. $this->info('Operacao cancelada.');
  40. return Command::SUCCESS;
  41. }
  42. $this->newLine();
  43. if ($providers > 0) {
  44. $this->processProviders();
  45. }
  46. if ($clients > 0) {
  47. $this->processClients();
  48. }
  49. $this->newLine();
  50. $this->info('Operacao concluida.');
  51. return Command::SUCCESS;
  52. }
  53. private function processProviders(): void
  54. {
  55. $this->info('--- Processando providers ---');
  56. $this->newLine();
  57. $providers = Provider::query()
  58. ->whereNotNull('recipient_id')
  59. ->with(['user', 'addresses.city.state', 'addresses.state'])
  60. ->get();
  61. $bar = $this->output->createProgressBar($providers->count());
  62. $bar->start();
  63. foreach ($providers as $provider) {
  64. $address = $provider->addresses->where('is_primary', true)->first()
  65. ?? $provider->addresses->first();
  66. $data = [
  67. 'recipient_name' => $provider->recipient_name,
  68. 'recipient_email' => $provider->recipient_email,
  69. 'recipient_document' => $provider->recipient_document,
  70. 'recipient_type' => $provider->recipient_type,
  71. 'recipient_payment_mode' => $provider->recipient_payment_mode,
  72. 'recipient_description' => $provider->recipient_description,
  73. 'recipient_metadata' => $provider->recipient_metadata ?? [],
  74. 'recipient_default_bank_account' => $provider->recipient_default_bank_account ?? [],
  75. 'birth_date' => $provider->birth_date?->format('Y-m-d'),
  76. 'professional_occupation' => 'autonomo',
  77. 'phone' => $provider->user?->phone,
  78. 'address' => $address?->address ?? '',
  79. 'number' => $address?->number ?? '',
  80. 'district' => $address?->district ?? '',
  81. 'city' => $address?->city?->name ?? '',
  82. 'state' => $address?->state?->code ?? '',
  83. 'zip_code' => $address?->zip_code ?? '',
  84. 'complement' => $address?->complement ?? '',
  85. ];
  86. $provider->forceFill([
  87. 'recipient_id' => null,
  88. 'idempotency_key' => null,
  89. 'recipient_code' => null,
  90. ])->save();
  91. try {
  92. $this->recipientService->createRecipientForProvider($provider, $data);
  93. } catch (\Throwable $e) {
  94. $this->newLine();
  95. $this->error("Erro ao recriar recipient do provider {$provider->id}: {$e->getMessage()}");
  96. $provider->forceFill([
  97. 'recipient_id' => null,
  98. 'idempotency_key' => null,
  99. 'recipient_code' => null,
  100. ])->save();
  101. }
  102. $bar->advance();
  103. }
  104. $bar->finish();
  105. $this->newLine();
  106. $this->newLine();
  107. }
  108. private function processClients(): void
  109. {
  110. $this->info('--- Processando clients ---');
  111. $this->newLine();
  112. $clients = Client::query()
  113. ->whereNotNull('gateway_customer_id')
  114. ->with('user')
  115. ->get();
  116. $bar = $this->output->createProgressBar($clients->count());
  117. $bar->start();
  118. foreach ($clients as $client) {
  119. $address = Address::query()
  120. ->with(['city', 'state'])
  121. ->where('source', 'client')
  122. ->where('source_id', $client->id)
  123. ->where('is_primary', true)
  124. ->first()
  125. ?? Address::query()
  126. ->with(['city', 'state'])
  127. ->where('source', 'client')
  128. ->where('source_id', $client->id)
  129. ->first();
  130. $data = [
  131. 'name' => $client->user?->name,
  132. 'email' => $client->user?->email,
  133. 'phone' => $client->user?->phone,
  134. 'document' => $client->document,
  135. 'address' => $address?->address ?? '',
  136. 'number' => $address?->number ?? '',
  137. 'district' => $address?->district ?? '',
  138. 'city' => $address?->city?->name ?? '',
  139. 'state' => $address?->state?->code ?? '',
  140. 'zip_code' => $address?->zip_code ?? '',
  141. 'complement' => $address?->complement ?? '',
  142. ];
  143. $client->forceFill([
  144. 'gateway_customer_id' => null,
  145. 'gateway_customer_code' => null,
  146. 'idempotency_key' => null,
  147. ])->save();
  148. try {
  149. $this->customerService->createCustomerForClient($client, $data);
  150. } catch (\Throwable $e) {
  151. $this->newLine();
  152. $this->error("Erro ao recriar customer do client {$client->id}: {$e->getMessage()}");
  153. $client->forceFill([
  154. 'gateway_customer_id' => null,
  155. 'gateway_customer_code' => null,
  156. 'idempotency_key' => null,
  157. ])->save();
  158. }
  159. $bar->advance();
  160. }
  161. $bar->finish();
  162. $this->newLine();
  163. $this->newLine();
  164. }
  165. }