Files
portal/app/app/Http/Controllers/Api/AdminPaymentGatewayController.php
T

58 lines
2.2 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\LegalEntity;
use App\Models\PaymentGateway;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;
/**
* SaaS-admin: ввод секретных ключей платёжного шлюза.
*
* config хранится Crypt::encrypt — ключи не попадают в БД в открытом виде и
* не возвращаются обратно клиенту. На MVP без auth-middleware (как остальные
* /api/admin/* эндпоинты); production — middleware('auth:saas-admin').
*/
class AdminPaymentGatewayController extends Controller
{
public function update(Request $request, string $code): JsonResponse
{
$validated = $request->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]);
}
}