Add refund action to booking list/detail with full-refund toggle

- New RefundBookingTableAction on the booking list row and detail page,
  refunding a Confirmed booking directly via RefundBookingAction — no need
  to hunt up its Payment on the Refunds resource first.
- Payment::refundableBalance() extracted from RefundBookingAction's private
  balance check so both refund forms can display and cap against it.
- RefundBookingAction::resolveRefundablePayment() made public for the same
  reason (round-trip leg resolution reused by the UI).
- Both refund forms (ProcessRefundAction and the new booking action) gain a
  "Full refund" toggle, on by default, which refunds the payment's whole
  remaining balance without requiring a manually typed amount. Turning it
  off reveals an amount field capped at the refundable balance.
This commit is contained in:
Nyan Lin Paing
2026-08-30 14:46:26 +07:00
parent a905320d50
commit 231f5679ef
9 changed files with 368 additions and 15 deletions
@@ -7,12 +7,46 @@ use Modules\Booking\Filament\Resources\Bookings\Pages\ListBookings;
use Modules\Booking\Filament\Resources\Bookings\Pages\ViewBooking;
use Modules\Booking\Models\Booking;
use Modules\Booking\Models\BookingVehicleOption;
use Modules\Payment\Contracts\PaymentGatewayInterface;
use Modules\Payment\Data\PaymentRequestData;
use Modules\Payment\Data\PaymentResultData;
use Modules\Payment\Data\RefundResultData;
use Modules\Payment\Enums\PaymentMethod;
use Modules\Payment\Enums\PaymentStatus;
use Modules\Payment\Enums\RefundStatus;
use Modules\Payment\Factories\PaymentGatewayFactory;
use Modules\Payment\Models\Payment;
use Modules\Payment\Models\Refund;
use Modules\Shared\Enums\VehicleOption;
use Spatie\Permission\Models\Permission;
/**
* Never calls the real KBZ refund API in tests (mirrors RefundResourceTest's
* fake for the Refunds resource's own process action).
*/
class FakeBookingResourceRefundGateway implements PaymentGatewayInterface
{
public function initiate(PaymentRequestData $data): PaymentResultData
{
throw new RuntimeException('not needed for this test');
}
public function verify(string $gatewayTransactionId): PaymentResultData
{
throw new RuntimeException('not needed for this test');
}
public function refund(string $gatewayTransactionId, string $amount, string $reason): RefundResultData
{
return new RefundResultData(status: RefundStatus::Completed, gatewayRefundId: 'REFUND123', gatewayPayload: []);
}
public function handleWebhook(array $payload): PaymentResultData
{
throw new RuntimeException('not needed for this test');
}
}
beforeEach(function () {
foreach (['view_bookings', 'manage_bookings', 'process_refunds'] as $permission) {
Permission::findOrCreate($permission, 'web');
@@ -384,3 +418,100 @@ test('the restore action is hidden from a user without manage_bookings', functio
->filterTable('trashed', true)
->assertTableActionHidden('restore', $booking);
});
test('the refund action is visible and enabled for a confirmed booking with process_refunds', function () {
$booking = Booking::factory()->create(['status' => BookingStatus::Confirmed]);
Livewire::test(ListBookings::class)
->assertTableActionVisible('refund', $booking)
->assertTableActionEnabled('refund', $booking);
});
test('the refund action is visible but disabled for a pending_payment booking', function () {
$booking = Booking::factory()->create(['status' => BookingStatus::PendingPayment]);
Livewire::test(ListBookings::class)
->assertTableActionVisible('refund', $booking)
->assertTableActionDisabled('refund', $booking);
});
test('the refund action is hidden from a user without process_refunds', function () {
$viewer = User::factory()->create()->givePermissionTo('view_bookings');
$this->actingAs($viewer);
$booking = Booking::factory()->create(['status' => BookingStatus::Confirmed]);
Livewire::test(ListBookings::class)
->assertTableActionHidden('refund', $booking);
});
test('calling the refund action from the bookings list with full refund toggled on refunds the whole balance', function () {
app(PaymentGatewayFactory::class)->register(PaymentMethod::KbzMiniApp, FakeBookingResourceRefundGateway::class);
$booking = Booking::factory()->create(['status' => BookingStatus::Confirmed, 'price' => 15000]);
$payment = Payment::factory()->completed()->create([
'booking_id' => $booking->id,
'gateway' => PaymentMethod::KbzMiniApp,
'amount' => 15000,
'gateway_transaction_id' => 'EVB-BOOKING-REFUND-1',
]);
Livewire::test(ListBookings::class)
->callTableAction('refund', $booking, data: [
'full_refund' => true,
'reason' => 'customer requested cancellation',
])
->assertNotified();
expect(Refund::where('payment_id', $payment->id)->where('status', RefundStatus::Completed)->where('amount', 15000)->exists())->toBeTrue();
});
test('calling the refund action with full refund toggled off refunds only the given amount', function () {
app(PaymentGatewayFactory::class)->register(PaymentMethod::KbzMiniApp, FakeBookingResourceRefundGateway::class);
$booking = Booking::factory()->create(['status' => BookingStatus::Confirmed, 'price' => 15000]);
$payment = Payment::factory()->completed()->create([
'booking_id' => $booking->id,
'gateway' => PaymentMethod::KbzMiniApp,
'amount' => 15000,
'gateway_transaction_id' => 'EVB-BOOKING-PARTIAL-1',
]);
Livewire::test(ListBookings::class)
->callTableAction('refund', $booking, data: [
'full_refund' => false,
'amount' => 5000,
'reason' => 'customer requested cancellation',
])
->assertNotified();
expect(Refund::where('payment_id', $payment->id)->where('status', RefundStatus::Completed)->where('amount', 5000)->exists())->toBeTrue();
});
test('the refund action\'s amount field is capped at the booking\'s payment\'s refundable balance', function () {
$booking = Booking::factory()->create(['status' => BookingStatus::Confirmed, 'price' => 15000]);
Payment::factory()->completed()->create([
'booking_id' => $booking->id,
'gateway' => PaymentMethod::KbzMiniApp,
'amount' => 15000,
'gateway_transaction_id' => 'EVB-BOOKING-MAX-1',
]);
Livewire::test(ListBookings::class)
->callTableAction('refund', $booking, data: [
'full_refund' => false,
'amount' => 15000.01,
'reason' => 'reason',
])
->assertHasTableActionErrors(['amount' => 'max']);
expect(Refund::where('booking_id', $booking->id)->exists())->toBeFalse();
});
test('the detail page also has a refund action, shared with the table', function () {
$confirmed = Booking::factory()->create(['status' => BookingStatus::Confirmed]);
Livewire::test(ViewBooking::class, ['record' => $confirmed->getRouteKey()])
->assertActionVisible('refund')
->assertActionEnabled('refund');
});