RefreshPagarmeEntities.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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', 'primaryBankAccount'])
  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->primaryBankAccount?->toGatewayBankAccount()
  75. ?? $provider->recipient_default_bank_account
  76. ?? [],
  77. 'birth_date' => $provider->birth_date?->format('Y-m-d'),
  78. 'professional_occupation' => 'autonomo',
  79. 'phone' => $provider->user?->phone,
  80. 'address' => $address?->address ?? '',
  81. 'number' => $address?->number ?? '',
  82. 'district' => $address?->district ?? '',
  83. 'city' => $address?->city?->name ?? '',
  84. 'state' => $address?->state?->code ?? '',
  85. 'zip_code' => $address?->zip_code ?? '',
  86. 'complement' => $address?->complement ?? '',
  87. ];
  88. $provider->forceFill([
  89. 'recipient_id' => null,
  90. 'idempotency_key' => null,
  91. 'recipient_code' => null,
  92. ])->save();
  93. try {
  94. $this->recipientService->createRecipientForProvider($provider, $data);
  95. } catch (\Throwable $e) {
  96. $this->newLine();
  97. $this->error("Erro ao recriar recipient do provider {$provider->id}: {$e->getMessage()}");
  98. $provider->forceFill([
  99. 'recipient_id' => null,
  100. 'idempotency_key' => null,
  101. 'recipient_code' => null,
  102. ])->save();
  103. }
  104. $bar->advance();
  105. }
  106. $bar->finish();
  107. $this->newLine();
  108. $this->newLine();
  109. }
  110. private function processClients(): void
  111. {
  112. $this->info('--- Processando clients ---');
  113. $this->newLine();
  114. $clients = Client::query()
  115. ->whereNotNull('gateway_customer_id')
  116. ->with('user')
  117. ->get();
  118. $bar = $this->output->createProgressBar($clients->count());
  119. $bar->start();
  120. foreach ($clients as $client) {
  121. $address = Address::query()
  122. ->with(['city', 'state'])
  123. ->where('source', 'client')
  124. ->where('source_id', $client->id)
  125. ->where('is_primary', true)
  126. ->first()
  127. ?? Address::query()
  128. ->with(['city', 'state'])
  129. ->where('source', 'client')
  130. ->where('source_id', $client->id)
  131. ->first();
  132. $data = [
  133. 'name' => $client->user?->name,
  134. 'email' => $client->user?->email,
  135. 'phone' => $client->user?->phone,
  136. 'document' => $client->document,
  137. 'address' => $address?->address ?? '',
  138. 'number' => $address?->number ?? '',
  139. 'district' => $address?->district ?? '',
  140. 'city' => $address?->city?->name ?? '',
  141. 'state' => $address?->state?->code ?? '',
  142. 'zip_code' => $address?->zip_code ?? '',
  143. 'complement' => $address?->complement ?? '',
  144. ];
  145. $client->forceFill([
  146. 'gateway_customer_id' => null,
  147. 'gateway_customer_code' => null,
  148. 'idempotency_key' => null,
  149. ])->save();
  150. try {
  151. $this->customerService->createCustomerForClient($client, $data);
  152. } catch (\Throwable $e) {
  153. $this->newLine();
  154. $this->error("Erro ao recriar customer do client {$client->id}: {$e->getMessage()}");
  155. $client->forceFill([
  156. 'gateway_customer_id' => null,
  157. 'gateway_customer_code' => null,
  158. 'idempotency_key' => null,
  159. ])->save();
  160. }
  161. $bar->advance();
  162. }
  163. $bar->finish();
  164. $this->newLine();
  165. $this->newLine();
  166. }
  167. }