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]); ProductPricings::create(['product_id' => $product->id, 'billing_cycle' => 'quarterly', 'price' => 40_000, 'is_active' => true]); $this->order = Order::create([ 'order_num' => 'ORD-RENEW-TEST', 'customer_id' => $this->customer->id, 'total' => 15_000, 'status' => 1, ]); $orderItem = OrderItem::create([ 'order_id' => $this->order->id, 'product_id' => $product->id, 'billing_cycle' => 'monthly', 'months' => 1, 'quantity' => 1, 'price' => 15_000, 'addons_meta' => [], ]); $this->service = Service::create([ 'customer_id' => $this->customer->id, 'product_id' => $product->id, 'order_item_id' => $orderItem->id, 'type' => 'hosting', 'status' => 'active', 'started_at' => now()->subMonth(), 'expires_at' => now()->addDays(5), ]); } private function renew(int $months) { 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_charges_balance_and_extends_expiry_on_the_same_order(): void { $expiresAt = $this->service->expires_at->copy(); $this->renew(3)->assertOk()->assertJsonPath('success', true); $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(40_000, (int) $invoice->total); $this->assertSame(60_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' => 40_000]); } public function test_insufficient_balance_keeps_unpaid_invoice_and_does_not_extend(): void { $this->customer->update(['total' => 10_000]); $expiresAt = $this->service->expires_at->copy(); $this->renew(1)->assertStatus(422)->assertJsonPath('data.status', InvoiceStatus::UNPAID->value); $this->assertSame(10_000, (int) $this->customer->fresh()->total); $this->assertTrue($this->service->fresh()->expires_at->equalTo($expiresAt)); $this->customer->update(['total' => 100_000]); $this->renew(3)->assertOk(); $invoice = Invoice::where('invoice_type', 2)->sole(); $this->assertSame(InvoiceStatus::PAID->value, (int) $invoice->status); $this->assertSame(40_000, (int) $invoice->total); $this->assertSame(1, $invoice->invoiceItems()->count()); $this->assertSame(60_000, (int) $this->customer->fresh()->total); } public function test_months_without_a_priced_cycle_are_rejected(): void { $this->renew(2)->assertStatus(422); $this->assertSame(0, Invoice::where('invoice_type', 2)->count()); } }