validate([ 'shop_id' => ['required', 'string', 'max:255'], 'secret_key' => ['required', 'string', 'max:255'], 'is_active' => ['required', 'boolean'], 'legal_entity_id' => ['nullable', 'integer', 'exists:legal_entities,id'], ]); $gw = PaymentGateway::firstOrNew(['code' => $code]); // legal_entity_id обязателен (NOT NULL FK). Берём из запроса, иначе первое юрлицо. if ($gw->legal_entity_id === null) { $legalEntityId = $validated['legal_entity_id'] ?? LegalEntity::query()->min('id'); if ($legalEntityId === null) { return response()->json([ 'message' => 'Сначала заведите юридическое лицо (реквизиты получателя платежей).', ], 422); } $gw->legal_entity_id = (int) $legalEntityId; } $gw->name ??= 'ЮKassa'; $gw->driver ??= $code; $gw->config = Crypt::encrypt([ 'shop_id' => $validated['shop_id'], 'secret_key' => $validated['secret_key'], ]); $gw->is_active = $validated['is_active']; $gw->min_amount_rub ??= '100.00'; $gw->save(); return response()->json(['status' => 'ok', 'code' => $gw->code, 'is_active' => $gw->is_active]); } }