feat: add domain registration form and email notifications

- Create a new Blade view for domain registration (`registrant-tochuc.blade.php`) to capture organization and manager details.
- Implement email templates for new domain orders for both admin (`domain-order-new.blade.php`) and customers (`domain-order-received.blade.php`).
- Add CSV exports for client and domain data.
- Introduce tests for customer mass assignment, password reset, login throttling, and permission gating.
- Ensure service renewals use current pricing and validate ownership for virtual machines.
- Validate mass assignment behavior in service model.
This commit is contained in:
baodev0
2026-08-22 17:30:55 +07:00
parent eec2c5906e
commit c377c7e195
58 changed files with 3063 additions and 418 deletions
+6 -24
View File
@@ -13,11 +13,11 @@ use App\Models\DnsDomain;
use App\Models\Invoice;
use App\Models\InvoiceItem;
use App\Models\Log as CustomerActivityLog;
use App\Models\DomainPricing;
use App\Models\Order;
use App\Models\OrderItem;
use App\Models\Product;
use App\Models\Service;
use App\Models\Setting;
use App\Models\Transaction;
use App\Repositories\Api\Domain\DomainOrderRepository;
use Illuminate\Support\Facades\DB;
@@ -53,6 +53,10 @@ class DomainOrderService
return DB::transaction(function () use ($customer, $product, $domainName, $price, $months, $years) {
$lockedCustomer = Customer::lockForUpdate()->find($customer->id);
if (!$lockedCustomer->front_identity_number || !$lockedCustomer->back_identity_number) {
throw new \InvalidArgumentException('Vui lòng bổ sung ảnh CMND/CCCD (2 mặt) trước khi đặt mua tên miền.');
}
if ((int) $lockedCustomer->total < $price) {
throw new \InvalidArgumentException('Số dư không đủ để đặt mua tên miền. Vui lòng nạp thêm tiền vào tài khoản.');
}
@@ -367,29 +371,7 @@ class DomainOrderService
*/
private function findDomainPricing(string $domain): array
{
$setting = Setting::where('key', 'domain_pricing')->first();
if (!$setting || empty($setting->value)) {
throw new \InvalidArgumentException('Không tìm thấy cấu hình giá tên miền.');
}
$pricingData = is_string($setting->value)
? json_decode($setting->value, true)
: (array) $setting->value;
$domains = $pricingData['domains'] ?? [];
// Longest-match: sort TLDs by length descending so ".com.vn" beats ".vn"
usort($domains, fn ($a, $b) => strlen($b['tld']) - strlen($a['tld']));
foreach ($domains as $entry) {
$tld = ltrim($entry['tld'], '.');
if (str_ends_with($domain, '.' . $tld)) {
return $entry;
}
}
throw new \InvalidArgumentException("Tên miền không được hỗ trợ. Vui lòng chọn đuôi tên miền có trong danh sách.");
return DomainPricing::findByDomain($domain)->toDomainArray();
}
/**
@@ -0,0 +1,131 @@
<?php
namespace App\Services\Api\Domain;
use App\Models\Customer;
use App\Models\Service;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\Html;
use PhpOffice\PhpWord\Writer\Word2007;
class DomainRegistrantService
{
/**
* Registrant info for `$service`, pre-filled from the customer's profile
* on first view. Once saved on the service, the saved data always wins.
*
* @return array{type: string, info: array<string, mixed>}
*/
public function get(Service $service, ?Customer $customer): array
{
if ($service->registrant_info) {
return [
'type' => $service->registrant_type ?? 'individual',
'info' => $service->registrant_info,
];
}
$isOrganization = $customer && filled($customer->company);
return [
'type' => $isOrganization ? 'organization' : 'individual',
'info' => $isOrganization ? $this->defaultOrganizationInfo($customer) : $this->defaultIndividualInfo($customer),
];
}
public function update(Service $service, string $type, array $info): Service
{
$service->update([
'registrant_type' => $type,
'registrant_info' => $info,
]);
return $service->fresh();
}
/**
* Render the VNNIC domain registration form as a .docx and return its binary contents.
*/
public function renderDocx(string $domainName, string $type, array $info): string
{
$view = $type === 'organization' ? 'domain.registrant-tochuc' : 'domain.registrant-canhan';
$html = view($view, ['domain' => $domainName, 'r' => $info])->render();
$phpWord = new PhpWord();
$phpWord->setDefaultFontName('Times New Roman');
$phpWord->setDefaultFontSize(13);
$section = $phpWord->addSection(['marginLeft' => 1000, 'marginRight' => 1000]);
Html::addHtml($section, $html, false, false);
$writer = new Word2007($phpWord);
$tmp = tempnam(sys_get_temp_dir(), 'domain_reg_');
$writer->save($tmp);
$contents = file_get_contents($tmp);
unlink($tmp);
return $contents;
}
private function defaultIndividualInfo(?Customer $customer): array
{
return [
'fullname' => $customer?->fullname,
'birth_date' => null,
'identity_number' => $customer?->identity_number,
'perm_address' => $customer?->address,
'perm_ward' => $customer?->ward?->name,
'perm_province' => $customer?->province?->name,
'temp_address' => null,
'temp_ward' => null,
'temp_province' => null,
'phone' => $customer?->phone,
'email' => $customer?->email,
'sign_place' => $customer?->province?->name,
'sign_date' => now()->toDateString(),
];
}
private function defaultOrganizationInfo(?Customer $customer): array
{
$address = $customer?->address;
$ward = $customer?->ward?->name;
$province = $customer?->province?->name;
return [
'org_name' => $customer?->company,
'org_tax_code' => null,
'org_address' => $address,
'org_ward' => $ward,
'org_province' => $province,
'org_phone' => $customer?->phone,
'org_email' => $customer?->email,
'manager_name' => $customer?->fullname,
'manager_identity_number' => $customer?->identity_number,
'manager_address' => $address,
'manager_ward' => $ward,
'manager_province' => $province,
'manager_phone' => $customer?->phone,
'manager_email' => $customer?->email,
'tech_name' => $customer?->fullname,
'tech_address' => $address,
'tech_ward' => $ward,
'tech_province' => $province,
'tech_phone' => $customer?->phone,
'tech_email' => $customer?->email,
'payer_org_name' => $customer?->company,
'payer_name' => $customer?->fullname,
'payer_address' => $address,
'payer_ward' => $ward,
'payer_province' => $province,
'payer_phone' => $customer?->phone,
'payer_email' => $customer?->email,
'signer_title' => null,
'sign_place' => $province,
'sign_date' => now()->toDateString(),
];
}
}
+14 -4
View File
@@ -17,14 +17,17 @@ use App\Models\OrderItem;
use App\Models\Service;
use App\Models\VirtualMachines;
use App\Repositories\Api\Order\OrderRepository;
use App\Repositories\Api\Service\ServiceRepositoryInterface;
use Carbon\Carbon;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\DB;
class OrderQueryService
{
public function __construct(private readonly OrderRepository $orderRepository)
{
public function __construct(
private readonly OrderRepository $orderRepository,
private readonly ServiceRepositoryInterface $productRepo,
) {
}
public function getOrdersForCustomer(int $customerId, ClientOrderFilters $filters): LengthAwarePaginator
@@ -198,11 +201,18 @@ class OrderQueryService
}
$orderItem = $service->orderItem;
if (! $orderItem || (int) $orderItem->price <= 0) {
if (! $orderItem) {
return ['ok' => false, 'message' => 'Không thể xác định giá gia hạn dịch vụ.', 'code' => 400];
}
$price = (int) $orderItem->price * $months;
// Gia hạn luôn dùng giá sản phẩm hiện tại trong bảng giá, không dùng giá đã
// chốt lúc đặt mua ban đầu — giá cũ chỉ để hiển thị trên đơn hàng/hoá đơn gốc.
$currentPrice = $this->productRepo->getPricing((int) $service->product_id, $orderItem->billing_cycle);
if ($currentPrice === null) {
return ['ok' => false, 'message' => 'Sản phẩm hiện không còn bán, không thể gia hạn.', 'code' => 400];
}
$price = (int) $currentPrice * $months;
if ($service->invoice_id && Invoice::where('id', $service->invoice_id)
->where('invoice_type', 2)