| 12345678910111213141516171819202122232425262728293031323334 |
- <?php
- namespace App\Services;
- use App\Models\UnitPaymentAccount;
- class UnitPaymentAccountService
- {
- public function getByUnitId(int $unitId): ?UnitPaymentAccount
- {
- return UnitPaymentAccount::where('unit_id', $unitId)->first();
- }
- /**
- * Cria/atualiza a conta de pagamento da unidade.
- *
- * Quando a chave vem vazia, a integração é considerada limpa (status pending).
- */
- public function upsertAsaasApiKey(int $unitId, ?string $apiKey): UnitPaymentAccount
- {
- $apiKey = $apiKey !== null ? trim($apiKey) : null;
- $hasKey = $apiKey !== null && $apiKey !== '';
- $model = UnitPaymentAccount::firstOrNew(['unit_id' => $unitId]);
- $model->asaas_api_key = $hasKey ? $apiKey : null;
- $model->status = $hasKey ? UnitPaymentAccount::STATUS_ACTIVE : UnitPaymentAccount::STATUS_PENDING;
- $model->onboarded_at = $hasKey ? ($model->onboarded_at ?? now()) : null;
- $model->save();
- return $model->fresh();
- }
- }
|