Files
portal-api/app/Services/Hosting/HostingProvisionFailure.php
baodev0andClaude Sonnet 5.5 55a834a2f8 feat: nhắc gia hạn QR theo dịch vụ, failover server hosting, CCCD admin, log cấp phát dịch vụ
- Mail campaign: mẫu nhắc gia hạn thanh toán QR, chọn dịch vụ + chu kỳ từng dòng (renewal_services)
- Hosting: nhiều server Plesk cho một gói (service_plan_servers), chuyển server khi server chết
- Admin xem/sửa giấy tờ định danh khách, tuỳ chọn gia hạn cho khách, nhật ký cấp phát dịch vụ
- Đánh dấu mẫu email dịch vụ là non-marketing

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-30 18:36:41 +07:00

101 lines
3.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services\Hosting;
use App\Http\Services\Telegram;
use App\Jobs\Mail\Customer\VMCreationFailedJob;
use App\Models\DnsServer;
use App\Models\OrderItem;
use App\Models\Service;
use App\Services\ActivityLogger;
use App\Services\VirtualMachine\VmProvisionRefund;
use Illuminate\Support\Facades\Log;
use Throwable;
class HostingProvisionFailure
{
public function __construct(
private readonly VmProvisionRefund $refund,
private readonly Telegram $telegram,
private readonly ActivityLogger $activity,
) {
}
public function handle(OrderItem $item, string $reason): void
{
Log::channel('Custom')->error('ProvisionHostingAccountJob failed', [
'order_item_id' => $item->id,
'reason' => $reason,
]);
$service = Service::where('order_item_id', $item->id)->first();
Service::where('order_item_id', $item->id)
->whereIn('status', ['pending', 'provisioning'])
->update(['status' => 'failed']);
if ($service) {
$this->activity->log('provision_failed', 'service', 'Khởi tạo hosting thất bại — ' . $reason, $service);
}
$refunded = $this->refund->refund($item, $reason);
$this->notifyAdmin($item, $reason, $refunded);
$this->notifyCustomer($item);
}
public function serverFailedOver(OrderItem $item, DnsServer $server, string $error): void
{
Log::channel('Custom')->warning('ProvisionHostingAccountJob server failed over', [
'order_item_id' => $item->id,
'server_id' => $server->id,
'error' => $error,
]);
$this->telegram->SendErrorMessage([
'job' => 'ProvisionHostingAccountJob',
'title' => 'Server Plesk lỗi, chuyển sang server dự phòng',
'order' => $item->order?->order_num,
'domain' => $item->server_name,
'server' => "{$server->name} ({$server->ip_address})",
'error' => $error,
]);
}
private function notifyAdmin(OrderItem $item, string $reason, bool $refunded): void
{
$order = $item->order;
$this->telegram->SendErrorMessage([
'job' => 'ProvisionHostingAccountJob',
'title' => 'Khởi tạo hosting thất bại',
'order' => $order?->order_num,
'domain' => $item->server_name,
'customer' => $order?->customer?->email,
'error' => $reason,
'refunded' => $refunded ? 'đã hoàn tiền tự động' : 'chưa hoàn tiền, cần xử lý thủ công',
]);
}
private function notifyCustomer(OrderItem $item): void
{
try {
$order = $item->order;
$customer = $order->customer;
VMCreationFailedJob::dispatch([
'customer_email' => $customer->email,
'customer_name' => (string) $customer->fullname,
'product_name' => $item->getProductAttribute()?->name ?? 'Hosting',
'order_id' => $order->id,
'order_num' => $order->order_num,
'order_item_id' => $item->id,
]);
} catch (Throwable $e) {
Log::error('HostingProvisionFailure: cannot dispatch failure email: ' . $e->getMessage());
}
}
}