Files
portal-api/tests/Feature/Payment/InvoicePaymentServiceTest.php
T
2026-09-23 15:17:18 +07:00

190 lines
7.8 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\Payment;
use App\Enums\Invoice\InvoiceStatus;
use App\Enums\Order\OrderStatus;
use App\Models\Customer;
use App\Models\Invoice;
use App\Models\Order;
use App\Models\Transaction;
use App\Services\InvoicePaymentService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class InvoicePaymentServiceTest extends TestCase
{
use RefreshDatabase;
public function test_paying_an_already_paid_invoice_does_not_double_deduct_balance(): void
{
$customer = Customer::factory()->create(['total' => 200_000]);
$order = Order::create([
'order_num' => 'ORD-' . uniqid(),
'customer_id' => $customer->id,
'sub_total' => 100_000,
'total' => 100_000,
'status' => OrderStatus::PENDING->value,
]);
$invoice = Invoice::create([
'invoice_number' => 'INV-' . uniqid(),
'customer_id' => $customer->id,
'order_id' => $order->id,
'invoice_type' => 1,
'sub_total' => 100_000,
'tax' => 0,
'total' => 100_000,
'status' => InvoiceStatus::UNPAID->value,
]);
$service = app(InvoicePaymentService::class);
$service->payWithBalance($invoice, $customer, 'balance');
// Simulate a retried request (double-click / network retry) for the same invoice.
$service->payWithBalance($invoice->fresh(), $customer->fresh(), 'balance');
$this->assertSame(100_000, (int) $customer->fresh()->total);
$this->assertSame(1, Transaction::where('customer_id', $customer->id)->where('type', 'invoice')->count());
}
public function test_a_cancelled_invoice_cannot_be_paid_and_is_never_counted_as_paid(): void
{
$customer = Customer::factory()->create(['total' => 200_000]);
$order = Order::create([
'order_num' => 'ORD-' . uniqid(),
'customer_id' => $customer->id,
'sub_total' => 100_000,
'total' => 100_000,
'status' => OrderStatus::CANCELLED->value,
]);
$invoice = Invoice::create([
'invoice_number' => 'INV-' . uniqid(),
'customer_id' => $customer->id,
'order_id' => $order->id,
'invoice_type' => 1,
'sub_total' => 100_000,
'tax' => 0,
'total' => 100_000,
'status' => InvoiceStatus::CANCELLED->value,
]);
$service = app(InvoicePaymentService::class);
$this->expectException(\InvalidArgumentException::class);
try {
$service->payWithBalance($invoice, $customer, 'balance');
} finally {
$this->assertSame(200_000, (int) $customer->fresh()->total);
$this->assertSame(InvoiceStatus::CANCELLED->value, (int) $invoice->fresh()->status);
$this->assertNull($invoice->fresh()->paid_at);
}
}
/** Guard mới: admin không được bật hoá đơn đã huỷ sang đã thanh toán. */
public function test_admin_cannot_mark_a_cancelled_invoice_as_paid(): void
{
$customer = Customer::factory()->create(['total' => 200_000]);
$invoice = $this->makeInvoice($customer, 100_000, InvoiceStatus::CANCELLED->value);
$this->expectException(\InvalidArgumentException::class);
try {
app(InvoicePaymentService::class)->markPaidByAdmin($invoice, null);
} finally {
$this->assertSame(InvoiceStatus::CANCELLED->value, (int) $invoice->fresh()->status);
$this->assertNull($invoice->fresh()->paid_at);
}
}
/** Trả thiếu: cộng dồn amount_paid, hoá đơn vẫn chưa thanh toán, dịch vụ chưa kích hoạt. */
public function test_partial_payment_records_amount_paid_and_keeps_invoice_unpaid(): void
{
$customer = Customer::factory()->create(['total' => 200_000]);
$invoice = $this->makeInvoice($customer, 100_000);
$service = app(InvoicePaymentService::class);
$invoice = $service->payWithBalance($invoice, $customer, 'balance', 40_000);
$this->assertSame(40_000, (int) $invoice->amount_paid);
$this->assertSame(InvoiceStatus::UNPAID->value, (int) $invoice->status);
$this->assertNull($invoice->paid_at);
$this->assertSame(60_000, InvoicePaymentService::outstanding($invoice));
$this->assertSame(160_000, (int) $customer->fresh()->total);
$this->assertSame(OrderStatus::PENDING->value, (int) $invoice->order->fresh()->status);
}
/** Trả nốt phần còn thiếu thì hoá đơn mới chuyển PAID và đơn hàng mới được kích hoạt. */
public function test_paying_the_remainder_settles_the_invoice(): void
{
$customer = Customer::factory()->create(['total' => 200_000]);
$invoice = $this->makeInvoice($customer, 100_000);
$service = app(InvoicePaymentService::class);
$service->payWithBalance($invoice, $customer, 'balance', 40_000);
$invoice = $service->payWithBalance($invoice->fresh(), $customer->fresh(), 'balance');
$this->assertSame(100_000, (int) $invoice->amount_paid);
$this->assertSame(InvoiceStatus::PAID->value, (int) $invoice->status);
$this->assertNotNull($invoice->paid_at);
$this->assertSame(100_000, (int) $customer->fresh()->total);
$this->assertSame(OrderStatus::ACTIVE->value, (int) $invoice->order->fresh()->status);
// Mỗi lần trả là một dòng sổ riêng, không gộp cũng không ghi đè.
$this->assertSame(2, Transaction::where('customer_id', $customer->id)->where('type', 'invoice')->count());
}
/** Trả quá số còn thiếu thì chỉ trừ đúng phần còn thiếu, không thu dư. */
public function test_overpaying_only_charges_the_outstanding_amount(): void
{
$customer = Customer::factory()->create(['total' => 500_000]);
$invoice = $this->makeInvoice($customer, 100_000);
$invoice = app(InvoicePaymentService::class)->payWithBalance($invoice, $customer, 'balance', 300_000);
$this->assertSame(100_000, (int) $invoice->amount_paid);
$this->assertSame(400_000, (int) $customer->fresh()->total);
}
/** Hoàn tiền: cộng lại số dư, ghi Transaction âm, hoá đơn sang REFUNDED. */
public function test_refund_returns_balance_and_records_a_negative_transaction(): void
{
$customer = Customer::factory()->create(['total' => 200_000]);
$invoice = $this->makeInvoice($customer, 100_000);
$service = app(InvoicePaymentService::class);
$invoice = $service->payWithBalance($invoice, $customer, 'balance');
$this->assertSame(100_000, (int) $customer->fresh()->total);
$invoice = $service->refund($invoice, null, null, 'Khách huỷ dịch vụ');
$this->assertSame(InvoiceStatus::REFUNDED->value, (int) $invoice->status);
$this->assertSame(200_000, (int) $customer->fresh()->total);
$this->assertSame(
-100_000,
(int) Transaction::where('customer_id', $customer->id)->where('type', 'refund')->value('amount'),
);
}
private function makeInvoice(Customer $customer, int $total, int $status = InvoiceStatus::UNPAID->value): Invoice
{
$order = Order::create([
'order_num' => 'ORD-' . uniqid(),
'customer_id' => $customer->id,
'sub_total' => $total,
'total' => $total,
'status' => OrderStatus::PENDING->value,
]);
return Invoice::create([
'invoice_number' => 'INV-' . uniqid(),
'customer_id' => $customer->id,
'order_id' => $order->id,
'invoice_type' => 1,
'sub_total' => $total,
'tax' => 0,
'total' => $total,
'status' => $status,
]);
}
}