ProviderBankAccount.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Builder;
  4. use Illuminate\Database\Eloquent\Factories\HasFactory;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  7. use Illuminate\Database\Eloquent\SoftDeletes;
  8. /**
  9. * @property int $id
  10. * @property int $provider_id
  11. * @property int|null $bank_id
  12. * @property string $bank_code
  13. * @property string|null $bank_name
  14. * @property string $holder_name
  15. * @property string $holder_document
  16. * @property string $holder_type
  17. * @property string $type
  18. * @property string $branch_number
  19. * @property string|null $branch_check_digit
  20. * @property string $account_number
  21. * @property string $account_check_digit
  22. * @property bool $is_primary
  23. * @property bool $is_active
  24. * @property \Illuminate\Support\Carbon|null $gateway_synced_at
  25. * @property string|null $gateway_sync_hash
  26. * @property array|null $gateway_payload
  27. * @property \Illuminate\Support\Carbon|null $created_at
  28. * @property \Illuminate\Support\Carbon|null $updated_at
  29. * @property \Illuminate\Support\Carbon|null $deleted_at
  30. * @property-read Bank|null $bank
  31. * @property-read Provider $provider
  32. * @method static \Illuminate\Database\Eloquent\Builder<static>|ProviderBankAccount newModelQuery()
  33. * @method static \Illuminate\Database\Eloquent\Builder<static>|ProviderBankAccount newQuery()
  34. * @method static \Illuminate\Database\Eloquent\Builder<static>|ProviderBankAccount onlyTrashed()
  35. * @method static \Illuminate\Database\Eloquent\Builder<static>|ProviderBankAccount query()
  36. * @method static \Illuminate\Database\Eloquent\Builder<static>|ProviderBankAccount active()
  37. * @method static \Illuminate\Database\Eloquent\Builder<static>|ProviderBankAccount primary()
  38. * @method static \Illuminate\Database\Eloquent\Builder<static>|ProviderBankAccount withTrashed(bool $withTrashed = true)
  39. * @method static \Illuminate\Database\Eloquent\Builder<static>|ProviderBankAccount withoutTrashed()
  40. * @mixin \Eloquent
  41. */
  42. class ProviderBankAccount extends Model
  43. {
  44. use HasFactory, SoftDeletes;
  45. protected $table = 'provider_bank_accounts';
  46. protected $fillable = [
  47. 'provider_id',
  48. 'bank_id',
  49. 'bank_code',
  50. 'bank_name',
  51. 'holder_name',
  52. 'holder_document',
  53. 'holder_type',
  54. 'type',
  55. 'branch_number',
  56. 'branch_check_digit',
  57. 'account_number',
  58. 'account_check_digit',
  59. 'is_primary',
  60. 'is_active',
  61. 'gateway_synced_at',
  62. 'gateway_sync_hash',
  63. 'gateway_sync_count',
  64. 'gateway_payload',
  65. ];
  66. protected function casts(): array
  67. {
  68. return [
  69. 'is_primary' => 'boolean',
  70. 'is_active' => 'boolean',
  71. 'gateway_synced_at' => 'datetime',
  72. 'gateway_sync_count' => 'integer',
  73. 'gateway_payload' => 'array',
  74. ];
  75. }
  76. public function provider(): BelongsTo
  77. {
  78. return $this->belongsTo(Provider::class);
  79. }
  80. public function bank(): BelongsTo
  81. {
  82. return $this->belongsTo(Bank::class);
  83. }
  84. //
  85. public function scopePrimary(Builder $query): Builder
  86. {
  87. return $query->where('is_primary', true);
  88. }
  89. public function scopeActive(Builder $query): Builder
  90. {
  91. return $query->where('is_active', true);
  92. }
  93. //
  94. /**
  95. * Formato esperado pelo Pagar.me em default_bank_account / bank_account.
  96. * E tambem o formato espelhado em providers.recipient_default_bank_account.
  97. */
  98. public function toGatewayBankAccount(): array
  99. {
  100. return [
  101. 'holder_name' => $this->holder_name,
  102. 'holder_type' => $this->holder_type,
  103. 'holder_document' => $this->holder_document,
  104. 'bank' => $this->bank_code,
  105. 'branch_number' => $this->branch_number,
  106. 'branch_check_digit' => $this->branch_check_digit,
  107. 'account_number' => $this->account_number,
  108. 'account_check_digit' => $this->account_check_digit,
  109. 'type' => $this->type,
  110. ];
  111. }
  112. /**
  113. * Identifica o conteudo da conta enviado ao gateway, para detectar divergencia
  114. * entre o que esta salvo aqui e o que o Pagar.me tem como conta padrao.
  115. */
  116. public function gatewayHash(): string
  117. {
  118. return sha1(json_encode($this->toGatewayBankAccount()));
  119. }
  120. /**
  121. * Avanca o contador usado na chave de idempotencia, para que cada envio ao Pagar.me
  122. * seja uma operacao nova - inclusive ao voltar para uma conta ja usada antes.
  123. */
  124. public function markGatewaySyncAttempt(): int
  125. {
  126. $this->forceFill(['gateway_sync_count' => $this->gateway_sync_count + 1])->save();
  127. return $this->gateway_sync_count;
  128. }
  129. public function isSyncedWithGateway(): bool
  130. {
  131. return $this->gateway_synced_at !== null
  132. && $this->gateway_sync_hash === $this->gatewayHash();
  133. }
  134. }