- Nạp tiền: mã thanh toán sống 30 phút (ManualDepositDraft), khôi phục/huỷ mã, lệnh dọn mã hết hạn; total_pending chỉ tính giao dịch đã nộp chứng từ, admin duyệt/từ chối/xoá giao dịch - Gia hạn dịch vụ: phát hành invoice gia hạn trên order gốc và trừ số dư ngay - Đồng bộ domain từ Plesk theo server, ServiceOrderRecorder tạo order/invoice cho dịch vụ admin dựng - Danh sách dịch vụ trả field theo loại (service_plan, dns_records_count), đếm dịch vụ theo loại Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
159 lines
5.0 KiB
PHP
159 lines
5.0 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Plesk;
|
|
|
|
use App\Models\DnsServer;
|
|
use Illuminate\Http\Client\ConnectionException;
|
|
use Illuminate\Http\Client\RequestException;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Str;
|
|
|
|
/**
|
|
* Plesk `/domains` entry point — shared by the DNS module (ServerService)
|
|
* and the Hosting module (PleskHostingService). Extracted from
|
|
* ServerService so both stop drifting apart on the same Plesk call.
|
|
*/
|
|
class PleskDomainService
|
|
{
|
|
private ?string $lastError = null;
|
|
|
|
public function __construct(private PleskService $plesk)
|
|
{
|
|
}
|
|
|
|
/** Message from the most recent failed call — null if the last call succeeded or hasn't run yet. */
|
|
public function getLastError(): ?string
|
|
{
|
|
return $this->lastError;
|
|
}
|
|
|
|
public function createDomain(DnsServer $server, array $data = []): array|false
|
|
{
|
|
$this->lastError = null;
|
|
|
|
if (empty($data['domain_name'])) {
|
|
$this->lastError = 'Domain name is required';
|
|
return false;
|
|
}
|
|
|
|
$param = [
|
|
'name' => $data['domain_name'],
|
|
'description' => '',
|
|
'hosting_type' => 'virtual',
|
|
'hosting_settings' => [
|
|
'ftp_login' => $data['ftp_login'] ?? str_replace('.', '_', $data['domain_name']),
|
|
'ftp_password' => $data['ftp_password'] ?? Str::random(12),
|
|
],
|
|
'plan' => ['name' => $data['plan'] ?? 'Default Domain'],
|
|
];
|
|
|
|
// Omit ip_addresses/ipv4 unless explicitly given — Plesk assigns its
|
|
// own default IP for the domain, and a wrong hardcoded IP here gets
|
|
// rejected outright ("IP address 'x.x.x.x' is not available").
|
|
if (!empty($data['ip_addresses'])) {
|
|
$param['ip_addresses'] = $data['ip_addresses'];
|
|
$param['ipv4'] = $data['ip_addresses'];
|
|
}
|
|
|
|
if (!empty($data['owner_client_id'])) {
|
|
$param['owner_client'] = ['id' => $data['owner_client_id']];
|
|
}
|
|
|
|
try {
|
|
$response = $this->plesk->client($server, 30)
|
|
->post($this->plesk->url($server, 'domains'), $param);
|
|
|
|
$responseData = $response->json();
|
|
|
|
Log::info('Response Plesk API Created', ['data' => $responseData]);
|
|
|
|
if ($response->successful() && isset($responseData['id'], $responseData['guid'])) {
|
|
return $responseData + ['hosting_settings' => $param['hosting_settings']];
|
|
}
|
|
|
|
$this->lastError = $responseData['message'] ?? 'Plesk domain creation failed';
|
|
} catch (RequestException $e) {
|
|
$this->lastError = $e->response?->json('message') ?? $e->getMessage();
|
|
Log::error('Plesk domain creation failed', [
|
|
'error' => $e->getMessage(),
|
|
'response' => $e->response?->body(),
|
|
'data' => $param,
|
|
]);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function suspend(DnsServer $server, string $domain): bool
|
|
{
|
|
$this->lastError = null;
|
|
|
|
try {
|
|
$response = $this->plesk->client($server, 30)
|
|
->post($this->plesk->url($server, 'cli', '/subscription/call'), ['params' => ['--webspace-off', $domain]]);
|
|
} catch (ConnectionException $e) {
|
|
$this->lastError = $e->getMessage();
|
|
|
|
return false;
|
|
}
|
|
|
|
if ($response->successful() && (int) $response->json('code', 1) === 0) {
|
|
return true;
|
|
}
|
|
|
|
$this->lastError = trim((string) ($response->json('stderr') ?: $response->json('message') ?: $response->body()));
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* @return array<int, array<string, mixed>>|false
|
|
*/
|
|
public function listDomains(DnsServer $server): array|false
|
|
{
|
|
$this->lastError = null;
|
|
|
|
try {
|
|
$response = $this->plesk->client($server, 30)
|
|
->get($this->plesk->url($server, 'domains'));
|
|
} catch (ConnectionException $e) {
|
|
$this->lastError = $e->getMessage();
|
|
|
|
return false;
|
|
}
|
|
|
|
if (!$response->successful() || !is_array($response->json())) {
|
|
$this->lastError = $response->json('message') ?? $response->body();
|
|
|
|
return false;
|
|
}
|
|
|
|
return $response->json();
|
|
}
|
|
|
|
public function getDomainInfo(DnsServer $server, string $domain = ''): array|false
|
|
{
|
|
if (empty($domain)) {
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
$response = $this->plesk->client($server, 10)
|
|
->get($this->plesk->url($server, 'domains'), ['name' => $domain]);
|
|
|
|
if ($response->successful()) {
|
|
foreach ($response->json() ?? [] as $domainInfo) {
|
|
if (isset($domainInfo['name']) && $domainInfo['name'] === $domain) {
|
|
return $domainInfo;
|
|
}
|
|
}
|
|
}
|
|
|
|
return false;
|
|
} catch (RequestException $e) {
|
|
Log::error($e->getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
}
|