feat(service): huỷ dịch vụ, SSL, đổi mật khẩu hosting, lịch sử thanh toán theo hình thức

- Lịch sử thanh toán hoá đơn trả thêm `method` (tự động gia hạn / số dư / admin / chuyển khoản / hoàn tiền), bỏ đuôi `(method)` khỏi ghi chú
- `DnsServer::panelUrl()` = https://<domain>/ dùng cho email cấp hosting và nút Plesk của khách
- Order item trả thêm `uuid`/`type` của dịch vụ để FE dẫn tới trang quản lý
- Yêu cầu huỷ dịch vụ, chứng chỉ SSL, lịch sử gia hạn, đổi mật khẩu hosting cho khách

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
baodev0
2026-10-28 16:40:24 +07:00
co-authored by Claude Sonnet 5.5
parent a6b641015e
commit b44a88bb52
72 changed files with 1701 additions and 231 deletions
+1 -1
View File
@@ -220,7 +220,7 @@ class OrderQueryService
->with(['orderItem.product', 'customProduct'])
->where('id', $serviceId)
->where('customer_id', $customer->id)
->whereNotIn('status', ['terminated', 'cancelled'])
->where('status', '!=', 'terminated')
->first();
if (! $service) {
@@ -18,9 +18,9 @@ class ServiceClientService
/**
* Lấy danh sách dịch vụ đang hoạt động
*/
public function getActiveServices(int $customerId, ?string $productType = null, ?int $limit = 10)
public function getActiveServices(int $customerId, ?string $productType = null, ?int $limit = 10, ?string $group = null)
{
return $this->serviceClientRepository->getActiveServices($customerId, $productType, $limit);
return $this->serviceClientRepository->getActiveServices($customerId, $productType, $limit, $group);
}
/**
@@ -15,7 +15,9 @@ use App\Models\OrderItem;
use App\Models\Service;
use App\Models\ServiceAddon;
use App\Services\ActivityLogger;
use App\Services\CustomerRenewalNotifier;
use Carbon\Carbon;
use Carbon\CarbonInterface;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
@@ -94,13 +96,30 @@ trait ActivatesPaidInvoices
: now();
$expiresAt = Carbon::parse($baseDate)->addMonths($months);
$previousExpiresAt = $service->expires_at;
$previousStatus = $service->status;
$wasCancelled = $previousStatus === 'cancelled';
$wasScheduledCancel = (bool) $service->cancel_at_expiry;
$service->update([
'status' => 'active',
'expires_at' => $expiresAt,
'suspended_at' => null,
'cancel_at_expiry' => false,
'cancelled_at' => null,
]);
if ($wasCancelled || $wasScheduledCancel) {
app(ActivityLogger::class)->log(
event: 'cancellation_revoked',
logName: 'Service',
description: "Dịch vụ #{$service->id} được gia hạn nên huỷ bỏ việc hủy dịch vụ"
.($wasCancelled ? ' (đã hủy → hoạt động lại)' : ' (hủy cuối kỳ)'),
subject: $service,
oldValues: ['status' => $previousStatus, 'cancel_at_expiry' => $wasScheduledCancel],
newValues: ['status' => 'active', 'cancel_at_expiry' => false, 'invoice_number' => $invoice->invoice_number],
);
}
app(ActivityLogger::class)->log(
event: 'renewed',
logName: 'Service',
@@ -119,6 +138,21 @@ trait ActivatesPaidInvoices
if ($service->vm_id) {
dispatch(new VMJobs((int) $service->vm_id, 'Start', false))->delay(now()->addSeconds(5));
}
$this->notifyAdminsOfRenewal($service, $invoice, $months, $previousExpiresAt);
}
private function notifyAdminsOfRenewal(Service $service, Invoice $invoice, int $months, ?CarbonInterface $previousExpiresAt): void
{
if (auth()->guard('admin')->check()) {
return;
}
$source = auth()->guard('client')->check()
? CustomerRenewalNotifier::SOURCE_CUSTOMER
: CustomerRenewalNotifier::SOURCE_SYSTEM;
DB::afterCommit(fn () => app(CustomerRenewalNotifier::class)->notify($service->fresh(), $invoice, $months, $previousExpiresAt, $source));
}
/**
@@ -51,6 +51,7 @@ class CustomerServiceProvisioningService
);
ModuleVmware::resolveVirtualMachineIds($services->pluck('vmware'));
\App\Support\HostingCredentials::attach($services);
return $services;
}
+69
View File
@@ -0,0 +1,69 @@
<?php
namespace App\Services;
use App\Mail\ServiceRenewedMail;
use App\Models\Invoice;
use App\Models\Notification;
use App\Models\Service;
use App\Models\Setting;
use App\Support\AutoEmailGate;
use Carbon\CarbonInterface;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
class CustomerRenewalNotifier
{
public const SCENARIO = 'admin_customer_service_renewed';
public const SOURCE_CUSTOMER = 'customer';
public const SOURCE_SYSTEM = 'system';
public function notify(Service $service, Invoice $invoice, int $months, ?CarbonInterface $previousExpiresAt, string $source): void
{
$service->loadMissing(['customer', 'product']);
$customer = $service->customer;
if (! $customer) {
return;
}
$productName = $service->product?->name ?? 'Dịch vụ';
$serverName = (string) $service->server_name;
$expiresAt = $service->expires_at?->format('d/m/Y') ?? 'N/A';
$auto = $source === self::SOURCE_SYSTEM;
$amount = number_format((float) $invoice->total, 0, ',', '.') . ' VNĐ';
Notification::notifyAdmins([
'type' => 'service_renewed_by_customer',
'title' => $auto ? 'Dịch vụ được tự động gia hạn' : 'Khách hàng gia hạn dịch vụ',
'body' => "{$customer->fullname} " . ($auto ? 'được tự động gia hạn' : 'đã gia hạn') . " {$productName}" . ($serverName !== '' ? " ({$serverName})" : '') . " thêm {$months} tháng ({$amount}), hạn mới {$expiresAt}.",
'link' => '/services/' . $service->id,
'data' => ['service_id' => $service->id, 'invoice_id' => $invoice->id, 'months' => $months],
]);
$adminEmail = json_decode((string) Setting::where('key', 'setting')->value('value'), true)['admin_email'] ?? null;
if (! $adminEmail || ! AutoEmailGate::allows(self::SCENARIO)) {
return;
}
try {
Mail::to($adminEmail)->queue(new ServiceRenewedMail(self::SCENARIO, [
'customer_name' => (string) $customer->fullname,
'customer_email' => (string) $customer->email,
'product_name' => $productName,
'server_name_suffix' => $serverName !== '' ? ' (' . e($serverName) . ')' : '',
'months_label' => "{$months} tháng",
'old_expires_at' => $previousExpiresAt?->format('d/m/Y') ?? 'N/A',
'expires_at' => $expiresAt,
'invoice_number' => (string) $invoice->invoice_number,
'amount_formatted' => $amount,
'payment_method_label' => $invoice->payment_method === 'balance' ? 'Trừ số dư tài khoản' : (string) ($invoice->payment_method ?: 'N/A'),
'renewal_source_label' => $auto ? 'Tự động gia hạn (hệ thống)' : 'Khách hàng tự gia hạn',
'admin_url' => rtrim(env('ADMIN_URL', config('app.url')), '/') . '/services/' . $service->id,
]));
} catch (\Throwable $e) {
Log::error('CustomerRenewalNotifier: không gửi được email admin', ['service_id' => $service->id, 'error' => $e->getMessage()]);
}
}
}
+11
View File
@@ -129,6 +129,17 @@ class PleskClientService
}
}
public function changePassword(DnsServer $server, ModuleHostingPleskClient $client, string $password): bool
{
if (!$this->resetPassword($server, (int) $client->plesk_client_id, $password)) {
return false;
}
$client->update(['password' => Crypt::encryptString($password)]);
return true;
}
private function resetPassword(DnsServer $server, int $pleskClientId, string $password): bool
{
try {
+1 -2
View File
@@ -138,8 +138,7 @@ class PleskHostingService
'ftp_password' => $ftpPassword,
'plesk_login' => $pleskLogin,
'plesk_password' => $pleskPassword,
// Plesk's actual login page — not the :8443 REST API entry point.
'plesk_panel_url' => 'https://' . $server->publicHost() . '/login_up.php',
'plesk_panel_url' => $server->panelUrl(),
'created_at' => now()->format('d/m/Y H:i:s'),
];
@@ -0,0 +1,51 @@
<?php
namespace App\Services;
use App\Mail\Admin\ServiceCancellationRequestMail;
use App\Models\Notification;
use App\Models\ServiceCancellationRequest;
use App\Models\Setting;
use App\Support\AutoEmailGate;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
class ServiceCancellationNotifier
{
public function notify(ServiceCancellationRequest $request, ?string $clientIp): void
{
$request->loadMissing(['service.product', 'customer']);
$service = $request->service;
$customer = $request->customer;
$serviceName = $service->server_name ?: ($service->product?->name ?? 'Dịch vụ');
Notification::notifyAdmins([
'type' => 'service_cancellation_request',
'title' => 'Khách hàng yêu cầu hủy dịch vụ',
'body' => "{$customer->fullname} yêu cầu hủy dịch vụ {$serviceName}. Lý do: {$request->reason}",
'link' => '/services/' . $service->id,
'data' => ['service_id' => $service->id, 'request_id' => $request->id],
]);
$adminEmail = json_decode((string) Setting::where('key', 'setting')->value('value'), true)['admin_email'] ?? null;
if (! $adminEmail || ! AutoEmailGate::allows('admin_service_cancellation_request')) {
return;
}
try {
Mail::to($adminEmail)->queue(new ServiceCancellationRequestMail([
'customer_name' => (string) $customer->fullname,
'customer_email' => (string) $customer->email,
'product_name' => (string) ($service->product?->name ?? 'Dịch vụ'),
'server_name' => (string) $service->server_name,
'expires_at' => $service->expires_at?->format('d/m/Y') ?? 'N/A',
'cancel_reason' => $request->reason,
'client_ip' => (string) $clientIp,
'created_at' => $request->created_at->format('d/m/Y H:i:s'),
'admin_url' => rtrim(env('ADMIN_URL', config('app.url')), '/') . '/services/' . $service->id,
]));
} catch (\Throwable $e) {
Log::error('Không gửi được email yêu cầu hủy dịch vụ', ['service_id' => $service->id, 'error' => $e->getMessage()]);
}
}
}
@@ -27,6 +27,7 @@ class ServiceRenewalReminderService
{
return Service::with(['customer:id,uuid,fullname,email', 'product:id,name'])
->where('status', 'active')
->where('cancel_at_expiry', false)
->whereHas('customer')
->whereBetween('expires_at', [now()->startOfDay(), now()->addDays($days)->endOfDay()])
->orderBy('expires_at')