- Gửi email theo template cho 1 khách, tab lịch sử email, nhắc gia hạn dịch vụ hàng loạt - Mail lưu from/bcc/sent_by; mẫu MARKETING và mẫu cũ trong TemplateSeeder - Gói hosting: giới hạn, trạng thái, lấy plan Plesk qua CLI gateway - Backfill payment_method và ngày đơn hàng còn thiếu - Bổ sung field CPU/RAM/Disk/Băng thông cho loại sản phẩm vps và cloud Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
219 lines
8.9 KiB
PHP
219 lines
8.9 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);
|
|
}
|
|
|
|
public function test_admin_payment_records_source_and_note(): void
|
|
{
|
|
$customer = Customer::factory()->create(['total' => 0]);
|
|
$invoice = $this->makeInvoice($customer, 100_000);
|
|
|
|
$invoice = app(InvoicePaymentService::class)->markPaidByAdmin(
|
|
$invoice,
|
|
null,
|
|
method: 'manual',
|
|
receivedAmount: 40_000,
|
|
note: 'Khách chuyển khoản đợt 1',
|
|
);
|
|
|
|
$this->assertSame(40_000, (int) $invoice->amount_paid);
|
|
$this->assertSame('Khách chuyển khoản đợt 1', $invoice->note);
|
|
$this->assertDatabaseHas('transactions', [
|
|
'customer_id' => $customer->id,
|
|
'received_amount' => 40_000,
|
|
]);
|
|
$this->assertStringContainsString(
|
|
'Khách chuyển khoản đợt 1',
|
|
(string) Transaction::where('customer_id', $customer->id)->value('note'),
|
|
);
|
|
$this->assertStringContainsString(
|
|
'(manual)',
|
|
(string) Transaction::where('customer_id', $customer->id)->value('note'),
|
|
);
|
|
}
|
|
|
|
/** 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,
|
|
]);
|
|
}
|
|
}
|