feat: nạp tiền thủ công mã 30 phút, gia hạn trừ số dư, đồng bộ domain từ Plesk

- 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>
This commit is contained in:
baodev0
2026-10-11 23:07:51 +07:00
co-authored by Claude Opus 5.5
parent 10af41c5cf
commit c5115b52dd
53 changed files with 1887 additions and 613 deletions
@@ -0,0 +1,30 @@
<?php
namespace App\Console\Commands;
use App\Models\Transaction;
use App\Support\ManualDepositDraft;
use Illuminate\Console\Command;
class PurgeExpiredManualDeposits extends Command
{
protected $signature = 'deposits:purge-expired-drafts';
protected $description = 'Xoá mã thanh toán nạp tiền thủ công quá hạn chưa nộp chứng từ.';
public function handle(): int
{
$purged = 0;
ManualDepositDraft::expiredQuery()->chunkById(200, function ($drafts) use (&$purged) {
$drafts->each(function (Transaction $draft) use (&$purged) {
if (ManualDepositDraft::discard($draft)) {
$purged++;
}
});
});
$this->info("deposits:purge-expired-drafts | Purged {$purged} draft(s).");
return self::SUCCESS;
}
}
+4
View File
@@ -45,6 +45,10 @@ class Kernel extends ConsoleKernel
->everyFiveMinutes()
->withoutOverlapping();
$schedule->command('deposits:purge-expired-drafts')
->everyFiveMinutes()
->withoutOverlapping();
/* Sync all VM packages */
$schedule->job(new SyncAllPackage())
->everyThreeHours()
+5 -1
View File
@@ -15,6 +15,7 @@ class DnsSyncCompleted implements ShouldBroadcastNow
public function __construct(
public readonly int $synced,
public readonly array $failedDomains,
public readonly int $imported = 0,
) {}
public function broadcastOn(): array
@@ -32,7 +33,10 @@ class DnsSyncCompleted implements ShouldBroadcastNow
return [
'synced' => $this->synced,
'failed_domains' => $this->failedDomains,
'message' => "Đã đồng bộ {$this->synced} bản ghi DNS từ Plesk",
'imported' => $this->imported,
'message' => $this->imported > 0
? "Đã nhập {$this->imported} tên miền và đồng bộ {$this->synced} bản ghi DNS từ Plesk"
: "Đã đồng bộ {$this->synced} bản ghi DNS từ Plesk",
];
}
}
@@ -4,7 +4,11 @@ namespace App\Http\Controllers\Api\Admin\Customer\Concerns;
use App\Models\DnsDomain;
use App\Models\DnsRecord;
use App\Models\Service;
use App\Services\Domain\CustomerDomainServiceSync;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Collection;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
trait ManagesCustomerDomains
@@ -21,6 +25,7 @@ trait ManagesCustomerDomains
public function getDnsDomains(string $uuid): JsonResponse
{
$customer = $this->resolveCustomer($uuid);
$domainServices = $this->customerDomainServices($customer->id);
$domains = DnsDomain::query()
->with(['server:id,name,ip_address', 'registrar:id,name,short_name,website'])
@@ -30,6 +35,7 @@ trait ManagesCustomerDomains
->get()
->map(fn($d) => [
'id' => $d->id,
'service_id' => $domainServices->get($this->domainKey($d->domain_name))?->id,
'domain_name' => $d->domain_name,
'is_created' => (int) $d->is_created,
'plesk_id' => $d->plesk_id,
@@ -46,6 +52,7 @@ trait ManagesCustomerDomains
'short_name' => $d->registrar->short_name,
'website' => $d->registrar->website,
] : null,
'expires_at' => $domainServices->get($this->domainKey($d->domain_name))?->expires_at,
'created_at' => $d->created_at,
'updated_at' => $d->updated_at,
]);
@@ -53,6 +60,84 @@ trait ManagesCustomerDomains
return response()->json(['error' => false, 'data' => $domains]);
}
public function updateDnsDomain(string $uuid, int $domainId, Request $request): JsonResponse
{
$domain = $this->resolveCustomerDnsDomain($uuid, $domainId);
$data = $request->validate([
'is_created' => 'required|integer|in:0,1,2,3',
'created_at' => 'required|date',
'expires_at' => 'nullable|date',
]);
$customer = $this->resolveCustomer($uuid);
DB::transaction(function () use ($domain, $customer, $data) {
$domain->forceFill(['is_created' => $data['is_created'], 'created_at' => $data['created_at']])->save();
app(CustomerDomainServiceSync::class)->updateCustomerDomain($domain, $customer->id, $data['expires_at']);
});
return response()->json(['error' => false, 'message' => 'Cập nhật domain thành công']);
}
public function detachDnsDomain(string $uuid, int $domainId): JsonResponse
{
$customer = $this->resolveCustomer($uuid);
$domain = $this->resolveCustomerDnsDomain($uuid, $domainId);
$remainingCustomerIds = $domain->customer()->pluck('customers.id')->reject(
fn ($customerId) => (int) $customerId === (int) $customer->id,
)->all();
app(CustomerDomainServiceSync::class)->syncCustomers($domain, $remainingCustomerIds);
return response()->json(['error' => false, 'message' => 'Đã gỡ domain khỏi khách hàng']);
}
public function assignDnsDomain(string $uuid, Request $request): JsonResponse
{
$customer = $this->resolveCustomer($uuid);
$data = $request->validate([
'domain_id' => ['required', 'integer', 'exists:dns_manager_domain,id'],
'service_id' => ['required', 'integer', 'exists:services,id'],
]);
abort_unless(
Service::query()->whereKey($data['service_id'])->where('customer_id', $customer->id)->exists(),
422,
'Dịch vụ không thuộc khách hàng này',
);
$domain = DnsDomain::query()->findOrFail($data['domain_id']);
$customerIds = $domain->customer()->pluck('customers.id')->push($customer->id)->all();
app(CustomerDomainServiceSync::class)->syncCustomers($domain, $customerIds);
return response()->json(['error' => false, 'message' => 'Gán domain thành công']);
}
private function customerDomainServices(int $customerId): Collection
{
return Service::query()
->where('customer_id', $customerId)
->where('type', 'domain')
->orderBy('id')
->get()
->keyBy(fn(Service $service) => $this->domainKey($service->server_name));
}
private function domainKey(?string $domainName): string
{
return mb_strtolower(trim((string) $domainName));
}
private function resolveCustomerDnsDomain(string $uuid, int $domainId): DnsDomain
{
$customer = $this->resolveCustomer($uuid);
return DnsDomain::query()
->whereHas('customer', fn($q) => $q->where('customers.id', $customer->id))
->findOrFail($domainId);
}
public function getDnsDomainRecords(string $uuid, int $domainId): JsonResponse
{
$customer = $this->resolveCustomer($uuid);
@@ -10,6 +10,7 @@ use App\Jobs\DnsManager\SyncDnsFromPlesk;
use App\Repositories\DnsManager\Domain\DnsDomainRepositoryInterface;
use App\Repositories\DnsManager\Record\DnsRecordRepositoryInterface;
use App\Services\ServerService;
use App\Services\Domain\CustomerDomainServiceSync;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
@@ -32,6 +33,7 @@ class DomainController extends Controller
private readonly DnsDomainRepositoryInterface $domainRepository,
private readonly DnsRecordRepositoryInterface $recordRepository,
private readonly ServerService $serverService,
private readonly CustomerDomainServiceSync $customerDomainServiceSync,
) {}
// ─── Domains ─────────────────────────────────────────────────────────────
@@ -114,7 +116,7 @@ class DomainController extends Controller
]);
if (!empty($request->customer_ids)) {
$domain->customer()->sync($request->customer_ids);
$this->customerDomainServiceSync->syncCustomers($domain, $request->customer_ids);
}
DB::commit();
@@ -153,6 +155,7 @@ class DomainController extends Controller
DB::beginTransaction();
try {
$previousDomainName = $domain->domain_name;
$this->domainRepository->update($id, $request->only([
'domain_name',
'dns_manager_server_id',
@@ -161,8 +164,19 @@ class DomainController extends Controller
]));
// Sync customers (empty array = detach all)
$freshDomain = $domain->fresh();
if ($request->has('customer_ids')) {
$domain->customer()->sync($request->customer_ids ?? []);
$this->customerDomainServiceSync->syncCustomers(
$freshDomain,
$request->customer_ids ?? [],
$previousDomainName,
);
} elseif ($previousDomainName !== $freshDomain->domain_name) {
$this->customerDomainServiceSync->syncCustomers(
$freshDomain,
$freshDomain->customer()->pluck('customers.id')->all(),
$previousDomainName,
);
}
DB::commit();
@@ -270,9 +284,13 @@ class DomainController extends Controller
// ─── Sync from Plesk ─────────────────────────────────────────────────────
public function syncDns(): JsonResponse
public function syncDns(Request $request): JsonResponse
{
dispatch(new SyncDnsFromPlesk());
$validated = $request->validate([
'dns_manager_server_id' => ['nullable', 'integer', 'exists:dns_manager_servers,id'],
]);
dispatch(new SyncDnsFromPlesk($validated['dns_manager_server_id'] ?? null));
return response()->json([
'error' => false,
@@ -373,6 +373,7 @@ class AdminDomainOrderController extends Controller
'refunded_at' => $service->refunded_at?->toIso8601String(),
'cancelled_at' => $service->cancelled_at?->toIso8601String(),
'created_at' => $service->created_at?->toIso8601String(),
'updated_at' => $service->updated_at?->toIso8601String(),
];
}
}
@@ -84,8 +84,12 @@ class ProductCategoryController extends Controller
'name' => 'required|string|max:255',
'parent_id' => 'nullable|integer|exists:product_categories,id',
'is_active' => 'boolean',
'sort_order' => 'nullable|integer',
'sort_order' => 'required|integer|min:0',
'type' => 'nullable|string',
], [
'sort_order.required' => 'Vui lòng nhập thứ tự hiển thị (sort_order).',
'sort_order.integer' => 'Thứ tự hiển thị phải là số nguyên.',
'sort_order.min' => 'Thứ tự hiển thị không được âm.',
]);
$validated['slug'] = Str::slug($validated['name']);
@@ -119,8 +123,12 @@ class ProductCategoryController extends Controller
'name' => 'sometimes|required|string|max:255',
'parent_id' => 'nullable|integer|exists:product_categories,id',
'is_active' => 'boolean',
'sort_order' => 'nullable|integer',
'sort_order' => 'sometimes|required|integer|min:0',
'type' => 'nullable|string',
], [
'sort_order.required' => 'Vui lòng nhập thứ tự hiển thị (sort_order).',
'sort_order.integer' => 'Thứ tự hiển thị phải là số nguyên.',
'sort_order.min' => 'Thứ tự hiển thị không được âm.',
]);
if (isset($validated['name']) && $validated['name'] !== $category->name) {
@@ -2,8 +2,10 @@
namespace App\Http\Controllers\Api\Admin\Service;
use App\Helpers\OrderHelper;
use App\Http\Controllers\Controller;
use App\Jobs\VirtualMachine\Provision\ResendVmCredentialsEmailJob;
use App\Models\DomainPricing;
use App\Models\ModuleVmware;
use App\Models\Service;
use Illuminate\Http\JsonResponse;
@@ -16,6 +18,7 @@ class ServiceController extends Controller
$perPage = min((int) ($request->per_page ?? 15), 100);
$query = Service::with(['customer:id,uuid,fullname,email', 'product:id,uuid,name', 'invoice:id,invoice_number', 'vmware:id,name,module_ip_address'])
->where(fn($q) => $q->whereNull('order_item_id')->orWhereNull('type')->orWhere('type', '!=', 'domain'))
->when($request->filter, fn($q, $v) => $q->where('server_name', 'like', "%{$v}%"))
->when($request->status, fn($q, $v) => $q->where('status', $v))
->when($request->customer_id, fn($q, $v) => $q->where('customer_id', $v));
@@ -34,6 +37,33 @@ class ServiceController extends Controller
]);
}
public function domainQuote(Request $request): JsonResponse
{
$data = $request->validate([
'domain' => 'required|string|max:255',
'billing_cycle' => 'nullable|in:monthly,quarterly,semi_annually,annually,biennially,triennially',
]);
$pricing = DomainPricing::matchDomain($data['domain']);
if (!$pricing) {
return response()->json(['error' => false, 'data' => null]);
}
$years = DomainPricing::yearsForMonths(OrderHelper::cycleToMonths($data['billing_cycle'] ?? 'annually'));
return response()->json([
'error' => false,
'data' => [
'tld' => $pricing->tld,
'setup_fee' => is_numeric($pricing->setup_fee) ? (int) $pricing->setup_fee : 0,
'first_year' => (int) $pricing->first_year,
'renewal' => (int) $pricing->recurring_fee,
'years' => $years,
'price' => $pricing->orderPrice($years),
],
]);
}
public function show(int $id): JsonResponse
{
$service = Service::with(['customer', 'product', 'orderItem', 'invoice', 'vmware.os.osGroup', 'addons', ...Service::HOSTING_ACCOUNT_RELATIONS])
@@ -137,8 +137,7 @@ class AdminTransactionController extends Controller
$creditEarned = Customer::creditFor($received);
$customer->total += $received;
$customer->credit += $creditEarned;
// Trừ pending theo số khách khai — đó chính là số đã được giữ chỗ lúc tạo yêu cầu.
$customer->total_pending = max(0, $customer->total_pending - $declared);
$this->releasePending($customer, $transaction, $declared);
$customer->save();
Log::create([
@@ -158,7 +157,7 @@ class AdminTransactionController extends Controller
'type' => 'deposit_success',
'title' => 'Nạp tiền thành công',
'body' => 'Giao dịch ' . $transaction->public_code . ' đã được duyệt.',
'link' => '/lich-su-giao-dich/' . $transaction->id,
'link' => '/lich-su-giao-dich/' . $transaction->uuid,
'data' => [
'transaction_uuid' => $transaction->uuid,
'amount' => $received,
@@ -231,7 +230,7 @@ class AdminTransactionController extends Controller
$transaction->update(['status' => TransactionStatus::FAILED->value]);
$customer = Customer::where('id', $transaction->customer_id)->lockForUpdate()->first();
$customer->total_pending = max(0, $customer->total_pending - $transaction->amount);
$this->releasePending($customer, $transaction, (int) $transaction->amount);
$customer->save();
Log::create([
@@ -288,6 +287,57 @@ class AdminTransactionController extends Controller
]);
}
public function destroy(string $uuid): JsonResponse
{
$transaction = DB::transaction(function () use ($uuid) {
$transaction = Transaction::where('uuid', $uuid)
->where('type', 'manual')
->lockForUpdate()
->first();
if (! $transaction) {
return null;
}
if ((int) $transaction->status === TransactionStatus::PENDING->value) {
$customer = Customer::where('id', $transaction->customer_id)->lockForUpdate()->first();
if ($customer) {
$this->releasePending($customer, $transaction, (int) $transaction->amount);
$customer->save();
}
}
$transaction->delete();
Log::create([
'agent' => 'admin',
'client_ip' => request()->ip(),
'description' => sprintf(
'Admin xoá giao dịch nạp tiền thủ công %s: %s VNĐ (%s)',
$transaction->public_code,
number_format($transaction->amount),
TransactionStatus::from((int) $transaction->status)->label(),
),
'customer_id' => $transaction->customer_id,
]);
return $transaction;
});
if (! $transaction) {
return response()->json(['error' => true, 'message' => 'Giao dịch không tồn tại'], 404);
}
return response()->json(['error' => false, 'message' => 'Đã xoá giao dịch']);
}
private function releasePending(Customer $customer, Transaction $transaction, int $amount): void
{
if ($transaction->proof_image) {
$customer->total_pending = max(0, $customer->total_pending - $amount);
}
}
public function proofImage(Transaction $transaction, int $index = 0)
{
abort_unless($transaction->proof_image, 404);
@@ -91,7 +91,7 @@ class ClientController extends Controller
return response()->json([
'error' => false,
'data' => [
'client' => new ClientResource(auth('client')->user()->loadCount(['vps', 'hosting', 'domain'])),
'client' => new ClientResource(auth('client')->user()),
],
]);
}
@@ -53,7 +53,7 @@ trait HandlesClientAuthentication
return response()->json([
'error' => false,
'data' => [
'client' => new ClientResource($client->loadCount(['vps', 'hosting', 'domain'])),
'client' => new ClientResource($client),
'token' => $result['token'],
'refresh_token' => $result['refresh_token'],
'expires_in' => config('jwt.ttl', 60) * 60, // seconds
@@ -136,7 +136,7 @@ trait HandlesClientAuthentication
return response()->json([
'error' => false,
'data' => [
'client' => new ClientResource($client->loadCount(['vps', 'hosting', 'domain'])),
'client' => new ClientResource($client),
'token' => $result['token'],
'refresh_token' => $result['refresh_token'],
'expires_in' => config('jwt.ttl', 60) * 60,
@@ -252,7 +252,7 @@ trait HandlesClientAuthentication
'token' => $result['token'],
'refresh_token' => $result['refresh_token'],
'expires_in' => config('jwt.ttl', 60) * 60,
'client' => new ClientResource($result['client']->loadCount(['vps', 'hosting', 'domain'])),
'client' => new ClientResource($result['client']),
],
]);
} catch (\Illuminate\Validation\ValidationException $e) {
@@ -30,7 +30,7 @@ trait ManagesClientProfile
'error' => false,
'message' => 'Cập nhật thông tin thành công',
'data' => [
'client' => new ClientResource($user->loadCount(['vps', 'hosting', 'domain'])),
'client' => new ClientResource($user),
],
]);
}
@@ -64,7 +64,7 @@ trait ManagesClientProfile
'error' => false,
'message' => 'Cập nhật chứng minh thư thành công',
'data' => [
'client' => new ClientResource($user->loadCount(['vps', 'hosting', 'domain'])),
'client' => new ClientResource($user),
],
]);
}
@@ -36,6 +36,22 @@ class ServiceController extends Controller
$this->activityLogger = $activityLogger;
}
public function index(Request $request): JsonResponse
{
$customer = $request->user();
$services = $this->serviceClientService->getActiveServices($customer->id, null, null);
$this->serviceClientService->attachDnsRecordCounts($services, $customer->id);
return (new VpsServiceCollection($services))
->additional([
'error' => false,
'message' => 'Lấy dữ liệu thành công',
'page_type' => 'services',
])
->response();
}
public function indexVps(Request $request): JsonResponse
{
$perPage = min((int) $request->get('per_page', 15), 100);
@@ -123,6 +139,7 @@ class ServiceController extends Controller
$customerId = (int) $request->user()->id;
$services = $this->serviceClientService->getActiveServices($customerId, null, 5);
$this->serviceClientService->attachDnsRecordCounts($services, $customerId);
$invoices = $orderQueryService->getCustomerInvoices($customerId)->take(5);
return response()->json([
@@ -131,6 +148,7 @@ class ServiceController extends Controller
'data' => [
'services' => (new VpsServiceCollection($services))->collection,
'invoices' => InvoiceSummaryResource::collection($invoices),
'counts' => $this->serviceClientService->countByType($customerId),
],
]);
}
@@ -337,12 +337,15 @@ class OrderControllerRefactor extends Controller
{
$result = $this->orderQueryService->renewService(
$id,
(int) $request->user()->id,
$request->user(),
(int) $request->validated()['months'],
);
if (!$result['ok']) {
return response()->json(['success' => false, 'message' => $result['message']], $result['code']);
return response()->json(
['success' => false, 'message' => $result['message'], 'data' => $result['data'] ?? null],
$result['code'],
);
}
return response()->json(['success' => true, 'message' => $result['message'], 'data' => $result['data']]);
@@ -5,6 +5,7 @@ namespace App\Http\Controllers\Api\Service;
use App\Enums\Product\ProductTypeEnums;
use App\Http\Controllers\Controller;
use App\Http\Resources\Api\Service\ProductCategoryResource;
use App\Http\Resources\Api\Service\ServiceResource;
use App\Models\Product;
use App\Models\ProductCategories;
use Illuminate\Http\Request;
@@ -161,11 +162,12 @@ class ProductCategoryController extends Controller
->with([
'osVersions.osGroup',
'category',
'servicePlan',
'pricings' => fn($q) => $q->where('is_active', 1)
])
->get();
$productResources = $products->map(fn($product) => $this->formatProduct($product));
$productResources = ServiceResource::collection($products)->resolve();
$categoryArray = $category->toArray();
$categoryArray['products'] = $productResources;
@@ -265,11 +267,12 @@ class ProductCategoryController extends Controller
->with([
'osVersions.osGroup',
'category',
'servicePlan',
'pricings' => fn($q) => $q->where('is_active', 1)
])
->get();
$productResources = $products->map(fn($product) => $this->formatProduct($product));
$productResources = ServiceResource::collection($products)->resolve();
$childArray = $child->toArray();
$childArray['parent'] = $child->parent ? $child->parent->toArray() : null;
@@ -281,81 +284,4 @@ class ProductCategoryController extends Controller
'data' => $childArray,
]);
}
private function formatProduct($product): array
{
$billingCycleLabels = [
'monthly' => 'Hàng tháng',
'quarterly' => 'Hàng quý',
'semi_annually' => 'Nửa năm',
'annually' => 'Hàng năm',
'biennially' => '2 năm',
'triennially' => '3 năm',
'onetime' => 'Một lần',
];
$periodMonths = [
'monthly' => 1,
'quarterly' => 3,
'semi_annually' => 6,
'annually' => 12,
'biennially' => 24,
'triennially' => 36,
'onetime' => 0,
];
$periodUnits = [
'monthly' => 'month',
'quarterly' => 'quarter',
'semi_annually' => 'semi_year',
'annually' => 'year',
'biennially' => 'biennially',
'triennially' => 'triennially',
'onetime' => 'none',
];
return [
'uuid' => $product->uuid,
'name' => $product->name,
'slug' => $product->slug,
'short_description' => $product->short_description,
'description' => $product->description,
'thumbnail' => $product->thumbnail ? asset($product->thumbnail) : null,
'product_type' => $product->product_type,
'specs' => [
'cpu' => (int) $product->cpu,
'ram' => (int) $product->ram,
'disk' => (int) $product->disk,
'bandwidth' => (int) $product->bandwidth,
'gpu' => $product->gpu,
],
'stock' => $product->stock,
'price' => (int) $product->price,
'price_sale' => $product->price_sale ? (int) $product->price_sale : null,
'group' => $product->category->name ?? null,
'os' => $product->osVersions->groupBy('os_group_id')->map(function ($versions) {
$group = $versions->first()->osGroup;
return [
'name' => $group?->name,
'thumbnail' => $group?->thumbnail,
'versions' => $versions->map(fn($os) => [
'id' => $os->id,
'version' => $os->version,
'name' => $os->name,
])->values(),
];
})->values(),
'pricing' => $product->pricings
->sortBy(fn($p) => $periodMonths[$p->billing_cycle] ?? 999)
->values()
->map(fn($p) => [
'id' => $p->id,
'name' => $billingCycleLabels[$p->billing_cycle] ?? $p->billing_cycle,
'price' => (int) $p->price,
'price_sale' => $p->price_sale ? (int) $p->price_sale : null,
'period' => $periodMonths[$p->billing_cycle] ?? 1,
'period_unit' => $periodUnits[$p->billing_cycle] ?? 'month',
]),
];
}
}
@@ -156,7 +156,8 @@ class ServiceController extends Controller
'osVersions.osGroup',
'category',
'pricings' => fn($q) => $q->where('is_active', 1),
'addons'
'addons',
'servicePlan',
]);
return response()->json([
@@ -12,6 +12,7 @@ use App\Models\Log;
use App\Models\Setting;
use App\Models\Transaction;
use App\Services\ActivityLogger;
use App\Support\ManualDepositDraft;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
@@ -77,28 +78,17 @@ class ManualDepositController extends Controller
], 422);
}
// Huỷ transaction prepare cũ (chưa có proof) nếu còn tồn tại
$old = Transaction::where('customer_id', $user->id)
->where('type', 'manual')
->where('status', TransactionStatus::PENDING->value)
->whereNull('proof_image')
->latest()
->first();
if ($old) {
DB::transaction(function () use ($old, $user) {
$user->total_pending = max(0, $user->total_pending - $old->amount);
$user->save();
$old->forceDelete();
});
}
ManualDepositDraft::query()
->where('customer_id', $user->id)
->get()
->each(fn (Transaction $draft) => ManualDepositDraft::discard($draft));
$uuid = Str::uuid()->toString();
$publicCode = Transaction::newPublicCode('MAN');
DB::beginTransaction();
try {
Transaction::create([
$draft = Transaction::create([
'uuid' => $uuid,
'public_code' => $publicCode,
'trans' => 'MAN' . time(),
@@ -108,9 +98,6 @@ class ManualDepositController extends Controller
'status' => TransactionStatus::PENDING->value,
]);
$user->total_pending += $request->amount;
$user->save();
DB::commit();
} catch (\Exception) {
DB::rollBack();
@@ -126,13 +113,43 @@ class ManualDepositController extends Controller
return response()->json([
'error' => false,
'data' => [
'uuid' => $uuid,
'public_code' => $publicCode,
],
'data' => $this->draftPayload($draft),
]);
}
/**
* GET /v1/transaction/manual-deposit/{uuid}
*/
public function show(Request $request, string $uuid): JsonResponse
{
$draft = ManualDepositDraft::query()
->where('uuid', $uuid)
->where('customer_id', $request->user()->id)
->first();
if (! $draft) {
return response()->json(['error' => true, 'message' => 'Mã thanh toán không tồn tại'], 404);
}
if (ManualDepositDraft::isExpired($draft)) {
ManualDepositDraft::discard($draft);
return response()->json(['error' => true, 'message' => 'Mã thanh toán đã hết hạn, vui lòng tạo mã mới'], 410);
}
return response()->json(['error' => false, 'data' => $this->draftPayload($draft)]);
}
private function draftPayload(Transaction $draft): array
{
return [
'uuid' => $draft->uuid,
'public_code' => $draft->public_code,
'amount' => (int) $draft->amount,
'expires_at' => ManualDepositDraft::expiresAt($draft)->toISOString(),
];
}
/**
* DELETE /v1/transaction/manual-deposit/{uuid}/cancel
* Huỷ transaction đang prepare (chưa có proof, chưa submit).
@@ -142,21 +159,15 @@ class ManualDepositController extends Controller
{
$user = $request->user();
$transaction = Transaction::where('uuid', $uuid)
$transaction = ManualDepositDraft::query()
->where('uuid', $uuid)
->where('customer_id', $user->id)
->where('type', 'manual')
->where('status', TransactionStatus::PENDING->value)
->whereNull('proof_image')
->firstOrFail();
$amount = $transaction->amount;
$publicCode = $transaction->public_code;
DB::transaction(function () use ($transaction, $user) {
$user->total_pending = max(0, $user->total_pending - $transaction->amount);
$user->save();
$transaction->forceDelete();
});
ManualDepositDraft::discard($transaction);
$this->activityLogger->log(
event: 'deposit_cancel',
@@ -194,22 +205,30 @@ class ManualDepositController extends Controller
], 422);
}
$transaction = Transaction::where('uuid', $request->uuid)
->where('customer_id', $user->id)
->where('type', 'manual')
->where('status', TransactionStatus::PENDING->value)
->whereNull('proof_image')
->first();
$transaction = DB::transaction(function () use ($request, $user) {
$draft = ManualDepositDraft::query()
->where('uuid', $request->uuid)
->where('customer_id', $user->id)
->lockForUpdate()
->first();
if (! $draft || ManualDepositDraft::isExpired($draft)) {
return null;
}
$draft->update(['proof_image' => $request->proof_image]);
$user->newQuery()->whereKey($user->getKey())->increment('total_pending', $draft->amount);
return $draft;
});
if (!$transaction) {
return response()->json([
'error' => true,
'message' => 'Giao dịch không hợp lệ hoặc đã được xử lý',
'message' => 'Mã thanh toán đã hết hạn hoặc đã được xử lý, vui lòng tạo mã mới',
], 422);
}
$transaction->update(['proof_image' => $request->proof_image]);
try {
(new Telegram())->SendManualDepositPendingApproval($user, $transaction);
$this->sendManualDepositAdminEmail($user, $transaction, $request);
@@ -266,12 +266,11 @@ class TransactionController extends Controller
'type' => 'deposit',
'public_code' => $t->public_code,
'trans' => $t->trans,
'description' => 'Nạp tiền vào tài khoản',
'description' => $t->typeLabel(),
'amount' => (float) $t->amount,
'status' => $t->status,
'status_label' => TransactionStatus::from((int) $t->status)->label(),
'reference_id' => null,
'reference_num'=> null,
'detail_key' => $t->uuid,
'created_at' => $t->created_at?->toISOString(),
]);
@@ -287,8 +286,7 @@ class TransactionController extends Controller
'amount' => (float) $inv->total,
'status' => $inv->status,
'status_label' => \App\Enums\Invoice\InvoiceStatus::tryFrom((int) $inv->status)?->label() ?? '-',
'reference_id' => $inv->order?->id,
'reference_num'=> $inv->order?->order_num,
'detail_key' => $inv->order?->order_num,
'created_at' => $inv->created_at?->toISOString(),
]);
@@ -303,6 +301,37 @@ class TransactionController extends Controller
]);
}
public function getClientTransactionDetail(Request $request, string $uuid): JsonResponse
{
$transaction = Transaction::query()
->where('uuid', $uuid)
->where('customer_id', $request->user()->id)
->first();
if (! $transaction) {
return response()->json(['error' => true, 'message' => 'Giao dịch không tồn tại'], 404);
}
return response()->json([
'error' => false,
'data' => [
'uuid' => $transaction->uuid,
'public_code' => $transaction->public_code,
'trans' => $transaction->trans,
'type' => $transaction->type,
'type_label' => $transaction->typeLabel(),
'direction' => $transaction->direction,
'amount' => (int) $transaction->amount,
'received_amount' => $transaction->received_amount !== null ? (int) $transaction->received_amount : null,
'status' => (int) $transaction->status,
'status_label' => TransactionStatus::from((int) $transaction->status)->label(),
'has_proof' => $transaction->proof_image !== null,
'created_at' => $transaction->created_at?->toISOString(),
'updated_at' => $transaction->updated_at?->toISOString(),
],
]);
}
public function getByPublicCode(Request $request)
{
$code = $request->query('code');
@@ -6,9 +6,26 @@ use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use App\Enums\Product\ProductBillingCycle;
use App\Enums\Product\ProductTypeEnums;
use App\Models\Product;
class StoreOrderRequest extends FormRequest
{
private const DOMAIN_PATTERN = '/^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\\.)+[a-z]{2,63}$/i';
private function isHostingOrder(): bool
{
return Product::where('uuid', $this->input('product_id'))
->value('product_type') === ProductTypeEnums::HOSTING->value;
}
protected function prepareForValidation(): void
{
if (is_string($this->input('server_name'))) {
$this->merge(['server_name' => trim($this->input('server_name'))]);
}
}
public function authorize(): bool
{
return true;
@@ -19,7 +36,9 @@ class StoreOrderRequest extends FormRequest
return [
'product_id' => ['required', Rule::exists('products', 'uuid')->whereNull('deleted_at')],
'billing_cycle'=> ['required', Rule::enum(ProductBillingCycle::class)],
'server_name' => 'nullable|string|max:255',
'server_name' => $this->isHostingOrder()
? ['required', 'string', 'max:253', 'regex:'.self::DOMAIN_PATTERN]
: 'nullable|string|max:255',
'os_id' => 'nullable|integer',
'addons' => 'array',
'addons.*.addon_id' => 'nullable|exists:product_addon,id',
@@ -35,6 +54,8 @@ class StoreOrderRequest extends FormRequest
'billing_cycle.required' => 'Vui lòng chọn chu kỳ thanh toán',
'billing_cycle.enum' => 'Chu kỳ thanh toán không hợp lệ',
'billing_cycle.exists' => 'Chu kỳ thanh toán không áp dụng cho sản phẩm này',
'server_name.required' => 'Vui lòng nhập tên miền cho hosting',
'server_name.regex' => 'Tên miền không hợp lệ (ví dụ: example.com)',
'os_id.exists' => 'Hệ điều hành không hợp lệ',
'addons.*.addon_id.exists' => 'Addon không tồn tại',
'addons.*.qty.min' => 'Số lượng addon phải lớn hơn 0',
@@ -2,6 +2,7 @@
namespace App\Http\Resources\Api\Client;
use App\Services\Api\ServiceClient\ServiceClientService;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\URL;
@@ -51,11 +52,8 @@ class ClientResource extends JsonResource
'point_total' => $this->point,
],
'service' => [
'vps' => $this->vps_count ?? $this->vps()->count(),
'proxy' => 0,
'server' => 0,
'hosting' => $this->hosting_count ?? $this->hosting()->count(),
'domain' => $this->domain_count ?? $this->domain()->count(),
...app(ServiceClientService::class)->countByType((int) $this->id),
]
];
}
@@ -82,7 +82,7 @@ class ServiceClientDetailResource extends JsonResource
$orderItem = $this->orderItem;
$order = $orderItem?->order;
$vm = $this->resource->vmware;
$product = $orderItem?->product;
$product = $orderItem?->product ?? $this->resource->product;
$hosting = $this->resource->hostingAccount;
$pleskCredentials = $hosting ? $this->resolvePleskCredentials($hosting) : null;
$dnsZone = $this->resolveDnsZone();
@@ -91,6 +91,7 @@ class ServiceClientDetailResource extends JsonResource
return [
'id' => $this->id,
'uuid' => $this->uuid,
'type' => $this->type,
'server_name' => $this->server_name,
'status' => $this->status,
'auto_renew' => $this->auto_renew,
@@ -100,11 +101,16 @@ class ServiceClientDetailResource extends JsonResource
'version' => $orderItem?->os?->version ?? $vm?->os?->version,
'group' => $orderItem?->os?->osGroup?->name ?? $vm?->os?->osGroup?->name,
],
'specs' => $this->resolveSpecs($vm, $product),
'specs' => $this->type === 'hosting' ? null : $this->resolveSpecs($vm, $product),
'service_plan' => $this->type === 'hosting' ? $product?->servicePlan?->summary() : null,
'order' => $order ? [
'total' => (float) $order->total,
'months' => (int) $orderItem->months,
'billing_label' => $orderItem->billing_label,
'cycle_price' => (float) ($product?->pricings()
->where('billing_cycle', $orderItem->billing_cycle)
->where('is_active', 1)
->value('price') ?? 0),
'addon_total' => \App\Support\RenewalPricing::addonTotal($this->resource, 1),
] : null,
'vm' => $vm ? [
@@ -87,6 +87,9 @@ class ServiceResource extends JsonResource
'period' => $periodMonths[$p->billing_cycle] ?? 1,
'period_unit' => $periodUnits[$p->billing_cycle] ?? 'month',
]),
$this->mergeWhen($this->product_type === 'hosting', fn () => [
'service_plan' => $this->servicePlan?->summary(),
]),
];
}
@@ -2,17 +2,13 @@
namespace App\Http\Resources\Api\Service;
use App\Models\Product;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class VpsServiceResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
private function resolveSpecs($product): ?array
private function resolveSpecs(?Product $product): ?array
{
$addons = $this->addons ?? collect();
if ($addons->isNotEmpty()) {
@@ -37,22 +33,19 @@ class VpsServiceResource extends JsonResource
$invoice = $this->invoice;
$order = $orderItem?->order ?? $invoice?->order;
$product = $orderItem?->product ?? $this->product;
$type = $this->type ?? $product?->product_type;
return [
'uuid' => $this->uuid,
'type' => $type,
'server_name' => $this->server_name,
'invoice_num' => $invoice?->invoice_number,
'order_item_id' => $this->order_item_id,
'order_num' => $order?->order_num,
'status' => $this->status,
'status_label' => $this->status_label,
'auto_renew' => (bool) $this->auto_renew,
'started_at' => $this->started_at?->toISOString(),
'expires_at' => $this->expires_at?->toISOString(),
'os' => [
'version' => $orderItem?->os?->version,
'group' => $orderItem?->os?->osGroup?->name,
],
'product' => $product ? [
'uuid' => $product->uuid,
'name' => $product->name,
@@ -60,7 +53,19 @@ class VpsServiceResource extends JsonResource
'product_type' => $product->product_type,
'page_type' => 'service_' . str_replace(' ', '_', $product->product_type),
] : null,
'specs' => $this->resolveSpecs($product),
$this->mergeWhen($type === 'vps', fn () => [
'os' => [
'version' => $orderItem?->os?->version,
'group' => $orderItem?->os?->osGroup?->name,
],
'specs' => $this->resolveSpecs($product),
]),
$this->mergeWhen($type === 'hosting', fn () => [
'service_plan' => $product?->servicePlan?->summary(),
]),
$this->mergeWhen($type === 'domain', fn () => [
'dns_records_count' => $this->dns_records_count,
]),
];
}
}
+88 -6
View File
@@ -3,8 +3,11 @@
namespace App\Jobs\DnsManager;
use App\Events\DnsSyncCompleted;
use App\Models\DnsDomain;
use App\Models\DnsServer;
use App\Repositories\DnsManager\Domain\DnsDomainRepositoryInterface;
use App\Repositories\DnsManager\Record\DnsRecordRepositoryInterface;
use App\Services\Plesk\PleskDomainService;
use App\Services\ServerService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
@@ -19,19 +22,46 @@ class SyncDnsFromPlesk implements ShouldQueue
private const PLESK_RECORD_TYPES = ['A', 'AAAA', 'CNAME', 'TXT', 'MX', 'NS', 'SRV'];
public function __construct(private readonly ?int $serverId = null)
{
}
public function handle(
DnsDomainRepositoryInterface $domainRepository,
DnsRecordRepositoryInterface $recordRepository,
ServerService $serverService,
PleskDomainService $pleskDomainService,
): void {
$synced = 0;
$imported = 0;
$failedDomains = [];
if ($this->serverId !== null) {
$server = DnsServer::find($this->serverId);
$pleskDomains = $server ? $pleskDomainService->listDomains($server) : false;
if ($pleskDomains === false) {
Log::error('DNS sync: không lấy được danh sách domain từ Plesk', [
'server_id' => $this->serverId,
'error' => $pleskDomainService->getLastError(),
]);
event(new DnsSyncCompleted(0, ['server #' . $this->serverId], 0));
return;
}
$imported = $this->importDomains($pleskDomains);
}
$domains = $domainRepository->model
->notCancelled()
->whereNotNull('dns_manager_server_id')
->when(
$this->serverId !== null,
fn ($query) => $query->where('dns_manager_server_id', $this->serverId),
fn ($query) => $query->whereNotNull('dns_manager_server_id'),
)
->get();
$synced = 0;
$failedDomains = [];
foreach ($domains as $domain) {
if (!$serverService->addServerById($domain->dns_manager_server_id)) {
$failedDomains[] = $domain->domain_name;
@@ -63,9 +93,61 @@ class SyncDnsFromPlesk implements ShouldQueue
}
}
Log::info('DNS sync from Plesk finished', ['synced' => $synced, 'failed_domains' => $failedDomains]);
Log::info('DNS sync from Plesk finished', [
'server_id' => $this->serverId,
'imported' => $imported,
'synced' => $synced,
'failed_domains' => $failedDomains,
]);
event(new DnsSyncCompleted($synced, $failedDomains));
event(new DnsSyncCompleted($synced, $failedDomains, $imported));
}
/**
* @param array<int, array<string, mixed>> $pleskDomains
*/
private function importDomains(array $pleskDomains): int
{
$imported = 0;
foreach ($pleskDomains as $item) {
$name = strtolower(trim((string) ($item['name'] ?? '')));
if ($name === '' || empty($item['id']) || !empty($item['base_domain_id'])) {
continue;
}
$domain = DnsDomain::whereRaw('LOWER(domain_name) = ?', [$name])->first();
if (!$domain) {
DnsDomain::create([
'domain_name' => $name,
'dns_manager_server_id' => $this->serverId,
'plesk_id' => $item['id'],
'is_created' => DnsDomain::STATUS_CREATED,
]);
$imported++;
continue;
}
$sameServer = in_array((int) $domain->dns_manager_server_id, [0, $this->serverId], true);
if ($domain->plesk_id || !$sameServer || (int) $domain->is_created === DnsDomain::STATUS_CANCELLED) {
continue;
}
$domain->update([
'dns_manager_server_id' => $this->serverId,
'plesk_id' => $item['id'],
'is_created' => (int) $domain->is_created === DnsDomain::STATUS_PENDING
? DnsDomain::STATUS_CREATED
: $domain->is_created,
]);
$imported++;
}
return $imported;
}
private function mapPleskRecord(array $item, string $type, string $domainName): array
+14 -2
View File
@@ -88,16 +88,28 @@ class DomainPricing extends Model
*/
public static function findByDomain(string $domain): self
{
return static::matchDomain($domain)
?? 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.');
}
public static function matchDomain(string $domain): ?self
{
$domain = mb_strtolower(trim($domain));
$candidates = static::domains()->where('is_active', true)->get()
->sortByDesc(fn (self $row) => strlen((string) $row->tld));
foreach ($candidates as $row) {
$tld = ltrim((string) $row->tld, '.');
$tld = mb_strtolower(ltrim((string) $row->tld, '.'));
if ($tld !== '' && str_ends_with($domain, '.' . $tld)) {
return $row;
}
}
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 null;
}
public static function yearsForMonths(int $months): int
{
return max(1, (int) ceil($months / 12));
}
}
+5
View File
@@ -133,6 +133,11 @@ class Product extends Model
return $this->belongsTo(ModuleIpAddress::class, 'module_ip_address');
}
public function servicePlan()
{
return $this->belongsTo(ServicePlan::class);
}
public function addons()
{
return $this->hasMany(Addon::class, 'type', 'product_type')
+17
View File
@@ -40,4 +40,21 @@ class ServicePlan extends Model
{
return $this->belongsTo(DnsServer::class, 'dns_manager_server_id');
}
public function summary(): array
{
return [
'title' => $this->title,
'vcpu' => $this->vcpu,
'ram' => $this->ram,
'disk' => $this->disk,
'bandwidth' => $this->bandwidth,
'databases' => $this->databases,
'mailboxes' => $this->mailboxes,
'ftp_accounts' => $this->ftp_accounts,
'subdomains' => $this->subdomains,
'domain_aliases' => $this->domain_aliases,
'features' => $this->features ?? [],
];
}
}
+14
View File
@@ -82,6 +82,20 @@ class Transaction extends Model
};
}
public function typeLabel(): string
{
return match ($this->type) {
'manual' => 'Nạp tiền chuyển khoản',
'vnpay' => 'Nạp tiền qua VNPay',
'invoice' => 'Thanh toán hoá đơn',
'refund' => 'Hoàn tiền',
'admin_add_total', 'admin_add_credit' => 'Cộng số dư',
'admin_deduct_credit' => 'Trừ số dư',
'admin_record' => 'Ghi nhận giao dịch',
default => 'Giao dịch',
};
}
public function getProofImageUrlsAttribute(): array
{
if (! $this->proof_image) {
+5 -5
View File
@@ -38,16 +38,16 @@ class RouteServiceProvider extends ServiceProvider
$this->configureRateLimiting();
$this->routes(function () {
Route::prefix('api')
->middleware(['api', 'json.response'])
->namespace($this->namespace)
->group(base_path('routes/api.php'));
Route::prefix('api')
->middleware(['api'])
->namespace($this->namespace)
->group(base_path('routes/admin-media.php'));
Route::prefix('api')
->middleware(['api', 'json.response'])
->namespace($this->namespace)
->group(base_path('routes/api.php'));
Route::middleware(['api', 'json.response'])
->namespace($this->namespace)
->group(base_path('routes/client.php'));
@@ -15,14 +15,14 @@ class ServiceClientRepository extends BaseApiRepository implements ServiceClient
/**
* Lấy toàn bộ dịch vụ của customer (mọi trạng thái)
*/
public function getActiveServices(int $customerId, ?string $productType = null, int $limit = 10)
public function getActiveServices(int $customerId, ?string $productType = null, ?int $limit = 10)
{
$query = $this->model
->with([
'product',
'product.servicePlan',
'addons',
'invoice.order',
'orderItem' => fn($q) => $q->with(['product', 'os.osGroup', 'order']),
'orderItem' => fn($q) => $q->with(['product.servicePlan', 'os.osGroup', 'order']),
])
->where('customer_id', $customerId)
->orderByRaw("FIELD(status, 'active', 'pending', 'suspended', 'expired', 'cancelled', 'terminated')")
@@ -32,7 +32,7 @@ class ServiceClientRepository extends BaseApiRepository implements ServiceClient
$query->where('type', $productType);
}
return $query->paginate($limit);
return $limit === null ? $query->get() : $query->paginate($limit);
}
public function findActiveBySlug(string $slug)
@@ -9,7 +9,7 @@ interface ServiceClientRepositoryInterface extends BaseRepositoryInterface
/**
* Lấy danh sách sản phẩm đ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);
public function findActiveBySlug(string $slug);
+101 -72
View File
@@ -8,7 +8,9 @@ use App\DTOs\Order\ClientOrderFilters;
use App\Enums\Invoice\InvoiceStatus;
use App\Helpers\OrderHelper;
use App\Jobs\VirtualMachine\Provision\RetryProvisioningOrchestrator;
use App\Models\Customer;
use App\Models\Invoice;
use App\Models\InvoiceItem;
use App\Models\ModuleVmware;
use App\Models\Order;
use App\Models\OrderItem;
@@ -16,6 +18,7 @@ use App\Models\Service;
use App\Models\VirtualMachines;
use App\Repositories\Api\Order\OrderRepositoryInterface;
use App\Repositories\Api\Service\ServiceRepositoryInterface;
use App\Services\InvoicePaymentService;
use App\Support\RenewalPricing;
use Carbon\Carbon;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
@@ -26,6 +29,7 @@ class OrderQueryService
public function __construct(
private readonly OrderRepositoryInterface $orderRepository,
private readonly ServiceRepositoryInterface $productRepo,
private readonly InvoicePaymentService $invoicePaymentService,
) {
}
@@ -166,12 +170,12 @@ class OrderQueryService
return ['ok' => true, 'message' => 'Đã yêu cầu khởi tạo lại dịch vụ. Hệ thống sẽ kiểm tra và xử lý trong vài phút.'];
}
public function renewService(int $serviceId, int $customerId, int $months): array
public function renewService(int $serviceId, Customer $customer, int $months): array
{
$service = Service::query()
->with(['orderItem.product', 'invoice'])
->with(['orderItem.product'])
->where('id', $serviceId)
->where('customer_id', $customerId)
->where('customer_id', $customer->id)
->whereNotIn('status', ['terminated', 'cancelled'])
->first();
@@ -197,89 +201,114 @@ class OrderQueryService
$cycles = intdiv($months, $cycleMonths);
// 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);
$currentPrice = $this->productRepo->getPricing((int) ($service->product_id ?: $orderItem->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];
}
$renewalPrice = (int) $currentPrice * $cycles;
$addonTotal = RenewalPricing::addonTotal($service, $cycles);
$price = $renewalPrice + $addonTotal;
if ($service->invoice_id && Invoice::where('id', $service->invoice_id)
->where('invoice_type', 2)
->whereIn('status', [InvoiceStatus::UNPAID->value, InvoiceStatus::OVERDUE->value])
->exists()) {
$invoice = Invoice::find($service->invoice_id);
$invoice = DB::transaction(function () use ($service, $orderItem, $renewalPrice, $cycles, $months) {
$service = Service::query()->whereKey($service->id)->lockForUpdate()->first();
$pending = $this->pendingRenewalInvoice($service);
if ($pending && ((int) $pending->amount_paid > 0 || $this->renewalMonths($pending) === $months)) {
return $pending;
}
return $this->issueRenewalInvoice($service, $orderItem, $renewalPrice, $cycles, $months, $pending);
});
try {
$invoice = $this->invoicePaymentService->payWithBalance($invoice, $customer);
} catch (\InvalidArgumentException $e) {
return [
'ok' => true,
'message' => 'Dịch vụ đã có hóa đơn gia hạn đang chờ thanh toán.',
'data' => [
'order_id' => $invoice?->order_id,
'invoice_id' => $invoice?->id,
'invoice_number' => $invoice?->invoice_number,
'total' => (float) ($invoice?->total ?? 0),
'status' => $invoice?->status,
'payment_url' => $invoice?->clientPath(),
],
'ok' => false,
'message' => $e->getMessage() . ' Hóa đơn gia hạn #' . $invoice->invoice_number . ' đang chờ thanh toán.',
'code' => 422,
'data' => $this->renewalPayload($invoice),
];
}
// Gia hạn là một billing cycle mới của CÙNG một Order/Service — không tạo Order mới.
// Order gốc chính là order đã tạo ra service này (qua order_item -> order_id).
$periodStart = $service->expires_at && $service->expires_at->isFuture() ? $service->expires_at->copy() : now();
$periodEnd = $periodStart->copy()->addMonths($months);
$invoice = DB::transaction(function () use ($service, $orderItem, $price, $renewalPrice, $cycles, $months, $periodStart, $periodEnd) {
$invoice = Invoice::create([
'invoice_number' => OrderHelper::generateOrderNumber('INV'),
'customer_id' => $service->customer_id,
'order_id' => $orderItem->order_id,
'invoice_type' => 2,
'sub_total' => $price,
'discount_amount' => 0,
'tax' => 0,
'total' => $price,
'status' => InvoiceStatus::UNPAID->value,
'due_date' => now()->addDays(7)->toDateString(),
'billing_period_start' => $periodStart->toDateString(),
'billing_period_end' => $periodEnd->toDateString(),
]);
\App\Models\InvoiceItem::create([
'invoice_id' => $invoice->id,
'product_id' => $service->product_id,
'type' => 'renewal',
'description' => 'Gia hạn dịch vụ: ' . ($orderItem->product?->name ?? $service->server_name ?? ''),
'months' => $months,
'quantity' => 1,
'unit_price' => $renewalPrice,
'total_price' => $renewalPrice,
]);
foreach (RenewalPricing::addonInvoiceItems($service, $cycles, $months) as $addonItem) {
\App\Models\InvoiceItem::create($addonItem + ['invoice_id' => $invoice->id]);
}
$service->update(['invoice_id' => $invoice->id]);
return $invoice;
});
return [
'ok' => true,
'message' => 'Đã tạo hóa đơn gia hạn. Vui lòng thanh toán để gia hạn dịch vụ.',
'data' => [
'order_id' => $invoice->order_id,
'invoice_id' => $invoice->id,
'invoice_number' => $invoice->invoice_number,
'total' => (float) $invoice->total,
'status' => $invoice->status,
'payment_url' => $invoice->clientPath(),
'message' => 'Gia hạn dịch vụ thành công.',
'data' => $this->renewalPayload($invoice) + [
'expires_at' => $service->fresh()->expires_at?->toISOString(),
],
];
}
private function pendingRenewalInvoice(Service $service): ?Invoice
{
if (! $service->invoice_id) {
return null;
}
return Invoice::query()
->where('id', $service->invoice_id)
->where('invoice_type', 2)
->whereIn('status', [InvoiceStatus::UNPAID->value, InvoiceStatus::OVERDUE->value])
->first();
}
private function renewalMonths(Invoice $invoice): int
{
return (int) $invoice->invoiceItems()->where('type', 'renewal')->value('months');
}
private function issueRenewalInvoice(Service $service, OrderItem $orderItem, int $renewalPrice, int $cycles, int $months, ?Invoice $draft): Invoice
{
$price = $renewalPrice + RenewalPricing::addonTotal($service, $cycles);
$periodStart = $service->expires_at && $service->expires_at->isFuture() ? $service->expires_at->copy() : now();
$invoice = $draft ?? new Invoice([
'invoice_number' => OrderHelper::generateOrderNumber('INV'),
'status' => InvoiceStatus::UNPAID->value,
]);
$invoice->invoiceItems()->delete();
$invoice->fill([
'customer_id' => $service->customer_id,
'order_id' => $orderItem->order_id,
'invoice_type' => 2,
'sub_total' => $price,
'discount_amount' => 0,
'tax' => 0,
'total' => $price,
'due_date' => now()->addDays(7)->toDateString(),
'billing_period_start' => $periodStart->toDateString(),
'billing_period_end' => $periodStart->copy()->addMonths($months)->toDateString(),
])->save();
InvoiceItem::create([
'invoice_id' => $invoice->id,
'product_id' => $service->product_id ?: $orderItem->product_id,
'type' => 'renewal',
'description' => 'Gia hạn dịch vụ: ' . ($orderItem->product?->name ?? $service->server_name ?? ''),
'months' => $months,
'quantity' => 1,
'unit_price' => $renewalPrice,
'total_price' => $renewalPrice,
]);
foreach (RenewalPricing::addonInvoiceItems($service, $cycles, $months) as $addonItem) {
InvoiceItem::create($addonItem + ['invoice_id' => $invoice->id]);
}
$service->update(['invoice_id' => $invoice->id]);
return $invoice;
}
private function renewalPayload(Invoice $invoice): array
{
return [
'order_id' => $invoice->order_id,
'invoice_id' => $invoice->id,
'invoice_number' => $invoice->invoice_number,
'total' => (float) $invoice->total,
'status' => $invoice->status,
'payment_url' => $invoice->clientPath(),
];
}
}
@@ -2,6 +2,8 @@
namespace App\Services\Api\ServiceClient;
use App\Models\DnsDomain;
use App\Models\Service;
use App\Repositories\Api\ServiceClient\ServiceClientRepositoryInterface;
class ServiceClientService
@@ -16,11 +18,51 @@ 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)
{
return $this->serviceClientRepository->getActiveServices($customerId, $productType, $limit);
}
/**
* @return array{vps: int, server: int, hosting: int, domain: int}
*/
public function countByType(int $customerId): array
{
$counts = Service::query()
->where('customer_id', $customerId)
->whereNotIn('status', ['cancelled', 'terminated'])
->selectRaw('type, COUNT(*) as total')
->groupBy('type')
->pluck('total', 'type');
return [
'vps' => (int) $counts->get('vps', 0),
'server' => (int) $counts->get('server', 0),
'hosting' => (int) $counts->get('hosting', 0),
'domain' => (int) $counts->get('domain', 0),
];
}
/**
* @param iterable<Service> $services
*/
public function attachDnsRecordCounts(iterable $services, int $customerId): void
{
$counts = DnsDomain::query()
->withCount('ns')
->whereHas('customer', fn ($query) => $query->where('customers.id', $customerId))
->get(['id', 'domain_name'])
->mapWithKeys(fn (DnsDomain $domain) => [
mb_strtolower(trim($domain->domain_name)) => (int) $domain->ns_count,
]);
foreach ($services as $service) {
if ($service->type === 'domain') {
$service->setAttribute('dns_records_count', $counts->get(mb_strtolower(trim((string) $service->server_name))));
}
}
}
public function findActiveBySlug(string $slug)
{
return $this->serviceClientRepository->findActiveBySlug($slug);
@@ -36,7 +36,7 @@ class CustomerImpersonationService
$token = JWTAuth::fromUser($customer);
$refreshToken = $this->clientAuthService->createRefreshToken($customer->id);
$clientResource = (new ClientResource($customer->loadCount(['vps', 'hosting', 'domain'])))->resolve($request);
$clientResource = (new ClientResource($customer))->resolve($request);
$code = Str::random(48);
Cache::put("impersonate:{$code}", [
@@ -2,16 +2,11 @@
namespace App\Services\Customer;
use App\Enums\Invoice\InvoiceStatus;
use App\Enums\Order\OrderStatus;
use App\Helpers\OrderHelper;
use App\Mail\ServiceRenewalReminderMail;
use App\Models\Customer;
use App\Models\Invoice;
use App\Models\InvoiceItem;
use App\Models\ModuleVmware;
use App\Models\Order;
use App\Models\OrderItem;
use App\Models\Product;
use App\Models\Service;
use App\Models\Transaction;
@@ -21,6 +16,7 @@ use App\Repositories\Module\Hosting\HostingRepositoryInterface as ModuleHostingR
use App\Repositories\Module\Vmware\VmwareRepositoryInterface;
use App\Repositories\Service\ServiceRepositoryInterface;
use App\Services\Customer\Concerns\ResolvesCustomer;
use App\Services\Order\ServiceOrderRecorder;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
@@ -37,6 +33,7 @@ class CustomerServiceProvisioningService
private readonly ModuleHostingRepositoryInterface $moduleHostingRepository,
private readonly VmwareRepositoryInterface $vmwareRepository,
private readonly ServiceRepositoryInterface $serviceRepository,
private readonly ServiceOrderRecorder $orderRecorder,
) {}
public function getServices(string $uuid): Collection
@@ -89,8 +86,11 @@ class CustomerServiceProvisioningService
$this->assignVirtualMachine($service, $customer, (int) $data['virtual_machine_id']);
}
if (array_key_exists('price', $data) && $data['price'] !== null && $service->orderItem) {
$service->orderItem->update(['price' => $data['price']]);
$price = isset($data['price']) ? (int) $data['price'] : null;
if (!$service->order_item_id && ($price !== null || !empty($data['billing_cycle']))) {
$this->orderRecorder->ensureForService($service, $price, $data['billing_cycle'] ?? null);
$service->refresh();
}
if (!empty($data['billing_cycle']) && $service->orderItem) {
@@ -100,6 +100,11 @@ class CustomerServiceProvisioningService
]);
}
if ($price !== null && $service->orderItem) {
$service->orderItem->update(['price' => $price]);
$this->orderRecorder->syncUnpaidInvoice($service->refresh(), $price);
}
// Đồng bộ sang module_vmware nếu có gán VM
if ($service->vm_id) {
$vmData = array_filter([
@@ -196,7 +201,7 @@ class CustomerServiceProvisioningService
{
$customer = $this->resolveCustomer($uuid);
$product = Product::with('pricings')->find($data['product_id']);
$product = Product::find($data['product_id']);
abort_if(
$product->product_type === 'vps' && empty($data['virtual_machine_id']),
422,
@@ -206,9 +211,14 @@ class CustomerServiceProvisioningService
$billingCycle = $data['billing_cycle'] ?? 'monthly';
$months = OrderHelper::cycleToMonths($billingCycle);
$pricing = $product->pricings->where('billing_cycle', $billingCycle)->first();
// Số tiền đã thanh toán: admin nhập tay để khớp với thực tế (có thể lệch giá niêm yết do khuyến mãi/thoả thuận)
$basePrice = isset($data['amount_paid']) ? (int) $data['amount_paid'] : (int) ($pricing->price ?? 0);
$listPrice = $this->orderRecorder->listPrice($product, $billingCycle, $data['server_name'] ?? null);
abort_if(
!isset($data['amount_paid']) && $listPrice === null && $product->product_type === 'domain',
422,
'Tên miền không có giá trong bảng giá tên miền, vui lòng nhập giá.',
);
$basePrice = isset($data['amount_paid']) ? (int) $data['amount_paid'] : (int) $listPrice;
$paymentMethod = $data['payment_method'];
@@ -224,66 +234,25 @@ class CustomerServiceProvisioningService
);
return DB::transaction(function () use ($customer, $product, $data, $billingCycle, $months, $basePrice, $paymentMethod, $clientIp) {
// 1) Order + OrderItem (mô phỏng flow customer đặt hàng)
$order = Order::create([
'order_num' => OrderHelper::generateOrderNumber(),
'customer_id' => $customer->id,
'date' => now()->toDateString(),
'sub_total' => $basePrice,
'discount_amount' => 0,
'total' => $basePrice,
'status' => OrderStatus::ACTIVE->value,
'payment_method' => $paymentMethod,
'client_ip' => $clientIp,
]);
$orderItem = OrderItem::create([
'order_id' => $order->id,
'product_id' => $product->id,
'billing_cycle' => $billingCycle,
'months' => $months,
'server_name' => $data['server_name'] ?? null,
'quantity' => 1,
'price' => $basePrice,
]);
['order' => $order, 'orderItem' => $orderItem, 'invoice' => $invoice] = $this->orderRecorder->create(
$customer->id,
$product,
$billingCycle,
$basePrice,
$data['server_name'] ?? null,
paid: true,
paymentMethod: $paymentMethod,
clientIp: $clientIp,
);
// Ngày bắt đầu/hết hạn dùng chung cho cả module_vmware (next_due_date/next_invoice) và services.
$startedAt = $data['started_at'] ?? now();
$expiresAt = $data['expires_at'] ?? Carbon::parse($startedAt)->addMonths($months);
// 2) ModuleVmware: snapshot từ virtual_machines. `order_id` phải trỏ về
// order vừa tạo — client console-ticket/rebuild resolve VM qua
// service->orderItem->order->vmWare, không qua services.vm_id.
// module_vmware.order_id phải trỏ về order vừa tạo — client console-ticket/rebuild
// resolve VM qua service->orderItem->order->vmWare, không qua services.vm_id.
$vmId = $this->createModuleVmwareIfNeeded($data, $customer, $order, $basePrice, $expiresAt);
// 3) Invoice — đánh dấu PAID luôn vì admin đứng ra tạo & xác nhận thanh toán
$invoice = Invoice::create([
'invoice_number' => OrderHelper::generateOrderNumber('INV'),
'customer_id' => $customer->id,
'order_id' => $order->id,
'invoice_type' => 1,
'date' => now()->toDateString(),
'sub_total' => $basePrice,
'discount_amount' => 0,
'tax' => 0,
'total' => $basePrice,
'status' => InvoiceStatus::PAID->value,
'payment_method' => $paymentMethod,
'paid_at' => now(),
'due_date' => now()->addMonths($months)->toDateString(),
]);
InvoiceItem::create([
'invoice_id' => $invoice->id,
'product_id' => $product->id,
'type' => 'service',
'description' => $product->name ?? 'Dịch vụ',
'months' => $months,
'quantity' => 1,
'unit_price' => $basePrice,
'total_price' => $basePrice,
]);
// 4) Payment processing
if ($paymentMethod === 'balance') {
$customer->total = (int) $customer->total - $basePrice;
@@ -0,0 +1,116 @@
<?php
namespace App\Services\Domain;
use App\Models\DnsDomain;
use App\Models\Service;
use App\Services\Order\ServiceOrderRecorder;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
class CustomerDomainServiceSync
{
public const AUTO_CREATED_NOTE = 'Tự động tạo từ phân công DNS domain';
public function __construct(private readonly ServiceOrderRecorder $orderRecorder) {}
public function syncCustomers(DnsDomain $domain, array $customerIds, ?string $previousDomainName = null): void
{
$customerIds = collect($customerIds)->map(fn ($id) => (int) $id)->unique()->values();
$previousCustomerIds = $domain->customer()->pluck('customers.id');
$oldName = $previousDomainName ?? $domain->domain_name;
DB::transaction(function () use ($domain, $customerIds, $previousCustomerIds, $oldName): void {
$domain->customer()->sync($customerIds->all());
foreach ($customerIds as $customerId) {
$service = $this->findService($customerId, $oldName, $domain->domain_name);
if ($service?->trashed()) {
$service->restore();
}
$service ??= new Service([
'customer_id' => $customerId,
'type' => 'domain',
'note' => self::AUTO_CREATED_NOTE,
]);
$service->fill([
'server_name' => $domain->domain_name,
'status' => $this->serviceStatus($domain),
'started_at' => $service->started_at ?? $domain->created_at ?? Carbon::now(),
])->save();
if ($service->status !== 'cancelled') {
$this->orderRecorder->ensureForService($service);
}
}
$removedCustomerIds = $previousCustomerIds->diff($customerIds);
if ($removedCustomerIds->isNotEmpty()) {
Service::query()
->whereIn('customer_id', $removedCustomerIds)
->where('type', 'domain')
->whereIn(DB::raw('LOWER(TRIM(server_name))'), array_unique([
$this->domainKey($oldName),
$this->domainKey($domain->domain_name),
]))
->get()
->each(function (Service $service): void {
$this->orderRecorder->cancelUnpaidInvoice($service);
$service->delete();
});
}
});
}
public function updateCustomerDomain(
DnsDomain $domain,
int $customerId,
?string $expiresAt,
): Service {
$this->syncCustomers($domain, $domain->customer()->pluck('customers.id')->all());
$service = $this->findService($customerId, $domain->domain_name, $domain->domain_name);
abort_if(! $service, 404, 'Không tìm thấy dịch vụ tên miền');
$service->fill([
'status' => $this->serviceStatus($domain),
'started_at' => $domain->created_at,
'expires_at' => $expiresAt
? Carbon::parse($expiresAt)->setTimeFrom($service->expires_at ?? Carbon::today())
: null,
])->save();
return $service;
}
private function findService(int $customerId, string $oldName, string $currentName): ?Service
{
return Service::withTrashed()
->where('customer_id', $customerId)
->where('type', 'domain')
->whereIn(DB::raw('LOWER(TRIM(server_name))'), array_unique([
$this->domainKey($oldName),
$this->domainKey($currentName),
]))
->orderBy('id')
->first();
}
private function serviceStatus(DnsDomain $domain): string
{
return match ((int) $domain->is_created) {
DnsDomain::STATUS_CREATED => 'active',
DnsDomain::STATUS_INACTIVE => 'suspended',
DnsDomain::STATUS_CANCELLED => 'cancelled',
default => 'pending',
};
}
private function domainKey(?string $domainName): string
{
return mb_strtolower(trim((string) $domainName));
}
}
+171
View File
@@ -0,0 +1,171 @@
<?php
namespace App\Services\Order;
use App\Enums\Invoice\InvoiceStatus;
use App\Enums\Order\OrderStatus;
use App\Helpers\OrderHelper;
use App\Models\DomainPricing;
use App\Models\Invoice;
use App\Models\InvoiceItem;
use App\Models\Order;
use App\Models\OrderItem;
use App\Models\Product;
use App\Models\Service;
use Illuminate\Support\Facades\DB;
class ServiceOrderRecorder
{
/**
* @return array{order: Order, orderItem: OrderItem, invoice: Invoice}
*/
public function create(
int $customerId,
?Product $product,
string $billingCycle,
int $price,
?string $serverName,
bool $paid,
?string $paymentMethod = null,
?string $clientIp = null,
): array {
$months = OrderHelper::cycleToMonths($billingCycle);
$isDomain = $product?->product_type === 'domain';
$order = Order::create([
'order_num' => OrderHelper::generateOrderNumber(),
'customer_id' => $customerId,
'date' => now()->toDateString(),
'sub_total' => $price,
'discount_amount' => 0,
'total' => $price,
'status' => $paid ? OrderStatus::ACTIVE->value : OrderStatus::PENDING->value,
'payment_method' => $paymentMethod,
'client_ip' => $clientIp,
]);
$orderItem = OrderItem::create([
'order_id' => $order->id,
'product_id' => $product?->id,
'billing_cycle' => $billingCycle,
'months' => $months,
'server_name' => $serverName,
'domain_name' => $isDomain ? $serverName : null,
'quantity' => 1,
'price' => $price,
]);
$invoice = Invoice::create([
'invoice_number' => OrderHelper::generateOrderNumber('INV'),
'customer_id' => $customerId,
'order_id' => $order->id,
'invoice_type' => 1,
'date' => now()->toDateString(),
'sub_total' => $price,
'discount_amount' => 0,
'tax' => 0,
'total' => $price,
'status' => $paid ? InvoiceStatus::PAID->value : InvoiceStatus::UNPAID->value,
'payment_method' => $paymentMethod,
'paid_at' => $paid ? now() : null,
'due_date' => $paid ? now()->addMonths($months)->toDateString() : now()->toDateString(),
]);
InvoiceItem::create([
'invoice_id' => $invoice->id,
'product_id' => $product?->id,
'type' => 'service',
'description' => $this->describe($product, $serverName),
'months' => $months,
'quantity' => 1,
'unit_price' => $price,
'total_price' => $price,
]);
return compact('order', 'orderItem', 'invoice');
}
public function ensureForService(Service $service, ?int $price = null, ?string $billingCycle = null): void
{
if ($service->order_item_id || ! $service->customer_id) {
return;
}
DB::transaction(function () use ($service, $price, $billingCycle) {
$product = $service->product ?? $this->defaultProductFor($service->type);
$isDomain = ($product?->product_type ?? $service->type) === 'domain';
$billingCycle ??= $isDomain ? 'annually' : 'monthly';
$price ??= $this->listPrice($product, $billingCycle, $service->server_name) ?? 0;
$records = $this->create(
(int) $service->customer_id,
$product,
$billingCycle,
$price,
$service->server_name,
paid: false,
);
$service->forceFill([
'product_id' => $service->product_id ?? $product?->id,
'order_item_id' => $records['orderItem']->id,
'invoice_id' => $records['invoice']->id,
])->save();
});
}
public function syncUnpaidInvoice(Service $service, int $price): void
{
$invoice = $service->invoice;
if (! $invoice || (int) $invoice->status !== InvoiceStatus::UNPAID->value || (int) $invoice->invoice_type !== 1) {
return;
}
$months = (int) ($service->orderItem?->months ?: 1);
$invoice->update(['sub_total' => $price, 'total' => $price]);
$invoice->invoiceItems()->where('type', 'service')->update([
'months' => $months,
'unit_price' => $price,
'total_price' => $price,
]);
$invoice->order?->update(['sub_total' => $price, 'total' => $price]);
}
public function cancelUnpaidInvoice(Service $service): void
{
$invoice = $service->invoice;
if (! $invoice || (int) $invoice->status !== InvoiceStatus::UNPAID->value) {
return;
}
$invoice->update(['status' => InvoiceStatus::CANCELLED->value]);
$invoice->order?->update(['status' => OrderStatus::CANCELLED->value]);
}
public function listPrice(?Product $product, string $billingCycle, ?string $serverName): ?int
{
if ($product?->product_type === 'domain') {
return DomainPricing::matchDomain((string) $serverName)
?->orderPrice(DomainPricing::yearsForMonths(OrderHelper::cycleToMonths($billingCycle)));
}
$price = $product?->pricings()->where('billing_cycle', $billingCycle)->value('price');
return $price === null ? null : (int) $price;
}
private function defaultProductFor(?string $type): ?Product
{
return $type
? Product::where('product_type', $type)->where('is_active', true)->orderBy('id')->first()
: null;
}
private function describe(?Product $product, ?string $serverName): string
{
$name = $product->name ?? 'Dịch vụ';
return $serverName ? "{$name} - {$serverName}" : $name;
}
}
+25
View File
@@ -106,6 +106,31 @@ class PleskDomainService
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)) {
+46
View File
@@ -0,0 +1,46 @@
<?php
namespace App\Support;
use App\Enums\TransactionStatus;
use App\Models\Transaction;
use Carbon\CarbonInterface;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;
class ManualDepositDraft
{
public const TTL_MINUTES = 30;
public static function query(): Builder
{
return Transaction::query()
->where('type', 'manual')
->where('status', TransactionStatus::PENDING->value)
->whereNull('proof_image');
}
public static function expiresAt(Transaction $draft): CarbonInterface
{
return $draft->created_at->copy()->addMinutes(self::TTL_MINUTES);
}
public static function isExpired(Transaction $draft): bool
{
return self::expiresAt($draft)->isPast();
}
public static function expiredQuery(): Builder
{
return self::query()->where('created_at', '<=', now()->subMinutes(self::TTL_MINUTES));
}
public static function discard(Transaction $draft): bool
{
return DB::transaction(function () use ($draft) {
$locked = self::query()->whereKey($draft->getKey())->lockForUpdate()->first();
return $locked ? (bool) $locked->forceDelete() : false;
});
}
}