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
+46 -294
View File
@@ -5,8 +5,6 @@ declare(strict_types=1);
namespace Tests\Feature\Api\Service;
use App\Enums\Invoice\InvoiceStatus;
use App\Models\Addon;
use App\Enums\Order\OrderStatus;
use App\Models\Customer;
use App\Models\Invoice;
use App\Models\Order;
@@ -14,7 +12,6 @@ use App\Models\OrderItem;
use App\Models\Product;
use App\Models\ProductPricings;
use App\Models\Service;
use App\Models\ServiceAddon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
@@ -23,330 +20,85 @@ class RenewServiceTest extends TestCase
use RefreshDatabase;
private Customer $customer;
private Product $product;
private Service $service;
private Order $order;
protected function setUp(): void
{
parent::setUp();
$this->customer = Customer::factory()->create();
$this->product = Product::factory()->create([
'product_type' => 'hosting',
'is_active' => true,
]);
}
$this->customer = Customer::factory()->create(['total' => 100_000]);
$product = Product::factory()->create(['product_type' => 'hosting', 'is_active' => true]);
ProductPricings::create(['product_id' => $product->id, 'billing_cycle' => 'monthly', 'price' => 15_000, 'is_active' => true]);
private function actingAsCustomer(): static
{
return $this->actingAs($this->customer, 'client')
->withHeaders(['x-access-token' => $this->buildClientAccessToken()]);
}
/** Encrypts the shared secret the same way ClientAccessTokenCheck expects, for test requests. */
private function createServiceWithOldPrice(int $oldPrice): Service
{
$order = Order::create([
'order_num' => 'ORD-' . uniqid(),
$this->order = Order::create([
'order_num' => 'ORD-RENEW-TEST',
'customer_id' => $this->customer->id,
'sub_total' => $oldPrice,
'total' => $oldPrice,
'status' => OrderStatus::ACTIVE->value,
'total' => 15_000,
'status' => 1,
]);
$orderItem = OrderItem::create([
'order_id' => $order->id,
'product_id' => $this->product->id,
'order_id' => $this->order->id,
'product_id' => $product->id,
'billing_cycle' => 'monthly',
'months' => 1,
'quantity' => 1,
'price' => $oldPrice,
'price' => 15_000,
'addons_meta' => [],
]);
return Service::create([
$this->service = Service::create([
'customer_id' => $this->customer->id,
'product_id' => $this->product->id,
'product_id' => $product->id,
'order_item_id' => $orderItem->id,
'type' => 'hosting',
'status' => 'active',
'server_name' => 'test-server',
'started_at' => now()->subMonth(),
'expires_at' => now()->addDays(5),
]);
}
private function createServiceWithCycle(string $billingCycle, int $cycleMonths, int $oldPrice): Service
private function renew(int $months)
{
$order = Order::create([
'order_num' => 'ORD-' . uniqid(),
'customer_id' => $this->customer->id,
'sub_total' => $oldPrice,
'total' => $oldPrice,
'status' => OrderStatus::ACTIVE->value,
]);
$orderItem = OrderItem::create([
'order_id' => $order->id,
'product_id' => $this->product->id,
'billing_cycle' => $billingCycle,
'months' => $cycleMonths,
'quantity' => 1,
'price' => $oldPrice,
]);
return Service::create([
'customer_id' => $this->customer->id,
'product_id' => $this->product->id,
'order_item_id' => $orderItem->id,
'status' => 'active',
'server_name' => 'test-server',
]);
return $this->actingAs($this->customer, 'client')
->withHeaders(['x-access-token' => $this->buildClientAccessToken()])
->postJson("/v1/client/dich-vu/{$this->service->id}/renew", ['months' => $months]);
}
public function test_renew_annual_service_for_exactly_one_cycle_charges_the_annual_price(): void
public function test_renew_charges_balance_and_extends_expiry_on_the_same_order(): void
{
$service = $this->createServiceWithCycle('annually', 12, 1_800_000);
$expiresAt = $this->service->expires_at->copy();
ProductPricings::create([
'product_id' => $this->product->id,
'billing_cycle' => 'annually',
'price' => 1_800_000,
'is_active' => true,
]);
$this->renew(3)->assertOk()->assertJsonPath('success', true);
$response = $this->actingAsCustomer()->postJson(
"/v1/client/dich-vu/{$service->id}/renew",
['months' => 12],
);
$response->assertOk();
$response->assertJsonPath('data.total', 1_800_000);
$this->assertDatabaseHas('invoices', [
'id' => $response->json('data.invoice_id'),
'total' => 1_800_000,
]);
$invoice = Invoice::where('invoice_type', 2)->sole();
$this->assertSame($this->order->id, (int) $invoice->order_id);
$this->assertSame(InvoiceStatus::PAID->value, (int) $invoice->status);
$this->assertSame(45_000, (int) $invoice->total);
$this->assertSame(55_000, (int) $this->customer->fresh()->total);
$this->assertSame(1, Order::count());
$this->assertTrue($this->service->fresh()->expires_at->isSameDay($expiresAt->addMonths(3)));
$this->assertDatabaseHas('transactions', ['customer_id' => $this->customer->id, 'amount' => 45_000]);
}
public function test_renew_annual_service_with_months_not_matching_the_cycle_is_rejected(): void
public function test_insufficient_balance_keeps_unpaid_invoice_and_does_not_extend(): void
{
$service = $this->createServiceWithCycle('annually', 12, 1_800_000);
$this->customer->update(['total' => 10_000]);
$expiresAt = $this->service->expires_at->copy();
ProductPricings::create([
'product_id' => $this->product->id,
'billing_cycle' => 'annually',
'price' => 1_800_000,
'is_active' => true,
]);
$this->renew(1)->assertStatus(422)->assertJsonPath('data.status', InvoiceStatus::UNPAID->value);
$response = $this->actingAsCustomer()->postJson(
"/v1/client/dich-vu/{$service->id}/renew",
['months' => 1],
);
$this->assertSame(10_000, (int) $this->customer->fresh()->total);
$this->assertTrue($this->service->fresh()->expires_at->equalTo($expiresAt));
$response->assertStatus(422);
$this->assertSame(0, Invoice::where('customer_id', $this->customer->id)->count());
}
$this->customer->update(['total' => 100_000]);
$this->renew(2)->assertOk();
public function test_renew_charges_the_recurring_addon_price_on_top_of_the_service_price(): void
{
$service = $this->createServiceWithOldPrice(90_000);
$addon = Addon::create([
'type' => $this->product->product_type,
'code' => 'ram',
'name' => 'RAM',
'price' => 50_000,
'min' => 1,
'max' => 64,
'step' => 1,
'status' => true,
]);
$service->orderItem->update(['addons_meta' => [[
'addon_id' => $addon->id,
'code' => 'ram',
'name' => 'RAM',
'qty' => 2,
'price' => 40_000,
'subtotal' => 80_000,
]]]);
ServiceAddon::create([
'service_id' => $service->id,
'addon_type' => 'ram',
'addon_value' => '6144',
'price' => 50_000,
]);
ProductPricings::create([
'product_id' => $this->product->id,
'billing_cycle' => 'monthly',
'price' => 100_000,
'is_active' => true,
]);
$response = $this->actingAsCustomer()->postJson(
"/v1/client/dich-vu/{$service->id}/renew",
['months' => 1],
);
$response->assertOk();
$response->assertJsonPath('data.total', 200_000);
$invoiceId = $response->json('data.invoice_id');
$this->assertDatabaseHas('invoice_items', [
'invoice_id' => $invoiceId,
'type' => 'renewal',
'unit_price' => 100_000,
]);
$this->assertDatabaseHas('invoice_items', [
'invoice_id' => $invoiceId,
'type' => 'addon',
'quantity' => 2,
'total_price' => 100_000,
]);
}
public function test_renew_uses_current_catalog_price_not_the_original_order_price(): void
{
$service = $this->createServiceWithOldPrice(90_000);
// Giá sản phẩm hiện tại đã tăng lên 100.000 kể từ lúc khách đặt mua.
ProductPricings::create([
'product_id' => $this->product->id,
'billing_cycle' => 'monthly',
'price' => 100_000,
'is_active' => true,
]);
$response = $this->actingAsCustomer()->postJson(
"/v1/client/dich-vu/{$service->id}/renew",
['months' => 2],
);
$response->assertOk();
$response->assertJsonPath('data.total', 200_000);
$this->assertDatabaseHas('invoices', [
'id' => $response->json('data.invoice_id'),
'total' => 200_000,
]);
}
public function test_renew_fails_when_product_pricing_is_no_longer_active(): void
{
$service = $this->createServiceWithOldPrice(90_000);
ProductPricings::create([
'product_id' => $this->product->id,
'billing_cycle' => 'monthly',
'price' => 100_000,
'is_active' => false,
]);
$response = $this->actingAsCustomer()->postJson(
"/v1/client/dich-vu/{$service->id}/renew",
['months' => 1],
);
$response->assertStatus(400);
$this->assertSame(0, Invoice::where('customer_id', $this->customer->id)->count());
}
public function test_renew_reuses_original_order_instead_of_creating_a_new_one(): void
{
$service = $this->createServiceWithOldPrice(90_000);
$originalOrderId = $service->orderItem->order_id;
ProductPricings::create([
'product_id' => $this->product->id,
'billing_cycle' => 'monthly',
'price' => 100_000,
'is_active' => true,
]);
$response = $this->actingAsCustomer()->postJson(
"/v1/client/dich-vu/{$service->id}/renew",
['months' => 1],
);
$response->assertOk();
$response->assertJsonPath('data.order_id', $originalOrderId);
// Order 1:N Invoice — no second Order row is ever created for a renewal.
$this->assertSame(1, Order::where('customer_id', $this->customer->id)->count());
$this->assertDatabaseHas('invoices', [
'id' => $response->json('data.invoice_id'),
'order_id' => $originalOrderId,
'invoice_type' => 2,
]);
}
public function test_renewal_invoice_has_a_billing_period_and_is_not_duplicated_for_the_same_period(): void
{
$service = $this->createServiceWithOldPrice(90_000);
ProductPricings::create([
'product_id' => $this->product->id,
'billing_cycle' => 'monthly',
'price' => 100_000,
'is_active' => true,
]);
$response = $this->actingAsCustomer()->postJson(
"/v1/client/dich-vu/{$service->id}/renew",
['months' => 1],
);
$response->assertOk();
$invoiceId = $response->json('data.invoice_id');
$this->assertDatabaseHas('invoices', ['id' => $invoiceId]);
$invoice = Invoice::find($invoiceId);
$this->assertNotNull($invoice->billing_period_start);
$this->assertNotNull($invoice->billing_period_end);
// Calling renew again while the invoice is still open must return the SAME
// invoice, not create a duplicate for the same billing period.
$second = $this->actingAsCustomer()->postJson(
"/v1/client/dich-vu/{$service->id}/renew",
['months' => 1],
);
$second->assertOk();
$second->assertJsonPath('data.invoice_id', $invoiceId);
$this->assertSame(1, Invoice::where('customer_id', $this->customer->id)->where('invoice_type', 2)->count());
}
/**
* Đúng flow đề bài mục 6: invoice gia hạn quá due_date -> OVERDUE, KHÔNG tạo invoice mới.
* Verify khách bấm "Gia hạn" lại LÚC invoice đang OVERDUE (chưa qua billing cycle tiếp theo)
* vẫn nhận về đúng invoice cũ, không bị tạo trùng.
*/
public function test_renew_while_existing_invoice_is_overdue_returns_the_same_invoice(): void
{
$service = $this->createServiceWithOldPrice(90_000);
ProductPricings::create([
'product_id' => $this->product->id,
'billing_cycle' => 'monthly',
'price' => 100_000,
'is_active' => true,
]);
$first = $this->actingAsCustomer()->postJson(
"/v1/client/dich-vu/{$service->id}/renew",
['months' => 1],
);
$first->assertOk();
$invoiceId = $first->json('data.invoice_id');
// Invoice quá due_date -> cron invoices:check-overdue chuyển sang OVERDUE (flow thật).
Invoice::whereKey($invoiceId)->update(['due_date' => now()->subDay()->toDateString()]);
$this->artisan('invoices:check-overdue')->assertExitCode(0);
$this->assertSame(InvoiceStatus::OVERDUE->value, (int) Invoice::find($invoiceId)->status);
// Khách bấm "Gia hạn" lại trong khi invoice đang OVERDUE — không được tạo invoice #2.
$second = $this->actingAsCustomer()->postJson(
"/v1/client/dich-vu/{$service->id}/renew",
['months' => 1],
);
$second->assertOk();
$second->assertJsonPath('data.invoice_id', $invoiceId);
$this->assertSame(1, Invoice::where('customer_id', $this->customer->id)->where('invoice_type', 2)->count());
$invoice = Invoice::where('invoice_type', 2)->sole();
$this->assertSame(InvoiceStatus::PAID->value, (int) $invoice->status);
$this->assertSame(30_000, (int) $invoice->total);
$this->assertSame(1, $invoice->invoiceItems()->count());
$this->assertSame(70_000, (int) $this->customer->fresh()->total);
}
}