Files
saasshop/tests/Unit/PlatformOrderRefundInconsistentTest.php

149 lines
4.9 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace Tests\Unit;
use App\Models\Merchant;
use App\Models\Plan;
use App\Models\PlatformOrder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class PlatformOrderRefundInconsistentTest extends TestCase
{
use RefreshDatabase;
public function test_refund_total_can_fallback_to_refund_receipts_sum_when_refund_summary_missing(): void
{
$merchant = Merchant::query()->create([
'name' => '单测站点',
'slug' => 'unit-test-merchant',
'status' => 'active',
]);
$plan = Plan::query()->create([
'code' => 'unit_test_plan_refund_total',
'name' => '单测套餐',
'billing_cycle' => 'monthly',
'price' => 10,
'list_price' => 10,
'status' => 'active',
'sort' => 10,
'published_at' => now(),
]);
$order = PlatformOrder::query()->create([
'merchant_id' => $merchant->id,
'plan_id' => $plan->id,
'order_no' => 'PO_UNIT_REFUND_TOTAL_0001',
'order_type' => 'subscription',
'status' => 'activated',
'payment_status' => 'paid',
'plan_name' => 'Unit Test Plan',
'billing_cycle' => 'monthly',
'period_months' => 1,
'quantity' => 1,
'payable_amount' => 10,
'paid_amount' => 10,
'placed_at' => now(),
'meta' => [
'refund_receipts' => [
['amount' => 3.20],
['amount' => 1.30],
],
],
]);
$this->assertEquals(4.50, $order->refundTotal());
}
public function test_is_refund_inconsistent_when_status_refunded_but_refund_total_is_less_than_paid_amount(): void
{
$merchant = Merchant::query()->create([
'name' => '单测站点2',
'slug' => 'unit-test-merchant-2',
'status' => 'active',
]);
$plan = Plan::query()->create([
'code' => 'unit_test_plan_refund_inconsistent_1',
'name' => '单测套餐2',
'billing_cycle' => 'monthly',
'price' => 10,
'list_price' => 10,
'status' => 'active',
'sort' => 10,
'published_at' => now(),
]);
$order = PlatformOrder::query()->create([
'merchant_id' => $merchant->id,
'plan_id' => $plan->id,
'order_no' => 'PO_UNIT_REFUND_INCONSISTENT_0001',
'order_type' => 'subscription',
'status' => 'activated',
'payment_status' => 'refunded',
'plan_name' => 'Unit Test Plan',
'billing_cycle' => 'monthly',
'period_months' => 1,
'quantity' => 1,
'payable_amount' => 10,
'paid_amount' => 10,
'placed_at' => now(),
'meta' => [
'refund_summary' => [
'total_amount' => 9.98,
],
],
]);
// refunded 状态下:只有当退款总额 + 容差 < 已付金额 才算不一致。
// 这里 refund_total=9.98、paid=10.00,差额 0.02 > 默认容差 0.01,因此应判定为不一致。
$this->assertTrue($order->isRefundInconsistent());
}
public function test_is_refund_inconsistent_when_status_not_refunded_but_refund_total_reaches_paid_amount_plus_tolerance(): void
{
$merchant = Merchant::query()->create([
'name' => '单测站点3',
'slug' => 'unit-test-merchant-3',
'status' => 'active',
]);
$plan = Plan::query()->create([
'code' => 'unit_test_plan_refund_inconsistent_2',
'name' => '单测套餐3',
'billing_cycle' => 'monthly',
'price' => 10,
'list_price' => 10,
'status' => 'active',
'sort' => 10,
'published_at' => now(),
]);
$order = PlatformOrder::query()->create([
'merchant_id' => $merchant->id,
'plan_id' => $plan->id,
'order_no' => 'PO_UNIT_REFUND_INCONSISTENT_0002',
'order_type' => 'subscription',
'status' => 'activated',
'payment_status' => 'paid',
'plan_name' => 'Unit Test Plan',
'billing_cycle' => 'monthly',
'period_months' => 1,
'quantity' => 1,
'payable_amount' => 10,
'paid_amount' => 10,
'placed_at' => now(),
'meta' => [
'refund_summary' => [
'total_amount' => 10.00,
],
],
]);
// paid非 refunded状态下只有当退款总额 >= 已付金额 + 容差 才算不一致。
// 这里 refund_total=10.00 与 paid=10.00 相等,未达到 paid + 0.01,因此不应判定不一致。
$this->assertFalse($order->isRefundInconsistent());
}
}