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
@@ -35,14 +35,7 @@ class RefundBookingAction
throw RefundNotAllowedException::notConfirmed($booking);
}
// Round trip: payment is combined on the outbound leg, so a return
// leg has no Payment of its own — refund against its linked leg's
// Payment instead (domain.md §2b). The Confirmed check above still
// applies to $booking itself, not the payment holder, so each leg
// remains independently cancellable/refundable.
$paymentBooking = $booking->is_return_leg ? ($booking->linkedBooking ?? $booking) : $booking;
$payment = $paymentBooking->payments()->where('status', PaymentStatus::Completed->value)->latest()->first();
$payment = $this->resolveRefundablePayment($booking);
if ($payment === null) {
throw RefundNotAllowedException::noCompletedPayment($booking);
@@ -80,10 +73,27 @@ class RefundBookingAction
return $refund;
}
/**
* The Completed Payment a refund against $booking would apply to.
* Public so the Filament refund forms can look up the same Payment to
* surface its refundable balance before staff submit an amount.
*
* Round trip: payment is combined on the outbound leg, so a return leg
* has no Payment of its own resolve against its linked leg's Payment
* instead (domain.md §2b). The Confirmed check in handle() still applies
* to $booking itself, not the payment holder, so each leg remains
* independently cancellable/refundable.
*/
public function resolveRefundablePayment(Booking $booking): ?Payment
{
$paymentBooking = $booking->is_return_leg ? ($booking->linkedBooking ?? $booking) : $booking;
return $paymentBooking->payments()->where('status', PaymentStatus::Completed->value)->latest()->first();
}
private function assertWithinRefundableBalance(Payment $payment, string $amount): void
{
$alreadyRefunded = (string) $payment->refunds()->where('status', RefundStatus::Completed->value)->sum('amount');
$remaining = bcsub((string) $payment->amount, $alreadyRefunded, 2);
$remaining = $payment->refundableBalance();
if (bccomp($amount, $remaining, 2) === 1) {
throw RefundNotAllowedException::exceedsRefundableBalance($payment, $amount, $remaining);
@@ -0,0 +1,77 @@
<?php
namespace Modules\Payment\Filament\Actions;
use Filament\Actions\Action;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Notifications\Notification;
use Filament\Schemas\Components\Utilities\Get;
use Filament\Support\Icons\Heroicon;
use Modules\Booking\Enums\BookingStatus;
use Modules\Booking\Models\Booking;
use Modules\Payment\Actions\RefundBookingAction;
use Modules\Payment\Exceptions\RefundFailedException;
use Modules\Payment\Exceptions\RefundNotAllowedException;
/**
* Shared between BookingsTable (row action) and ViewBooking (header action)
* in the Booking module lets staff refund a booking directly instead of
* hunting up its Payment on the Refunds resource (ProcessRefundAction). Both
* surfaces call the same RefundBookingAction used by the API.
*/
class RefundBookingTableAction
{
public static function make(): Action
{
return Action::make('refund')
->label('Refund')
->icon(Heroicon::OutlinedReceiptRefund)
->color('danger')
->visible(fn (): bool => auth()->user()?->can('process_refunds') ?? false)
->disabled(fn (Booking $record): bool => $record->status !== BookingStatus::Confirmed)
->schema([
Toggle::make('full_refund')
->label('Full refund')
->live()
->default(true)
->helperText(fn (Booking $record): string => 'Refundable balance: '.(app(RefundBookingAction::class)
->resolveRefundablePayment($record)?->refundableBalance() ?? '0.00')),
TextInput::make('amount')
->numeric()
->minValue(0.01)
->visible(fn (Get $get): bool => ! $get('full_refund'))
->required(fn (Get $get): bool => ! $get('full_refund'))
->maxValue(fn (Booking $record): ?string => app(RefundBookingAction::class)
->resolveRefundablePayment($record)?->refundableBalance()),
Textarea::make('reason')
->required(),
])
->action(function (Booking $record, array $data): void {
$amount = $data['full_refund']
? app(RefundBookingAction::class)->resolveRefundablePayment($record)?->refundableBalance() ?? '0.00'
: (string) $data['amount'];
try {
app(RefundBookingAction::class)->handle(
$record,
$amount,
$data['reason'],
auth()->id(),
);
Notification::make()
->title('Refund processed')
->success()
->send();
} catch (RefundNotAllowedException|RefundFailedException $exception) {
Notification::make()
->title('Refund failed')
->body($exception->getMessage())
->danger()
->send();
}
});
}
}
@@ -6,7 +6,9 @@ use Filament\Actions\Action;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Notifications\Notification;
use Filament\Schemas\Components\Utilities\Get;
use Filament\Support\Icons\Heroicon;
use Modules\Payment\Actions\RefundBookingAction;
use Modules\Payment\Enums\PaymentStatus;
@@ -43,11 +45,21 @@ class ProcessRefundAction
$payment->id => "{$payment->booking?->booking_ref}{$payment->amount} {$payment->currency} (#{$payment->id})",
]))
->searchable()
->live()
->required(),
Toggle::make('full_refund')
->label('Full refund')
->live()
->default(true)
->helperText(fn (Get $get): string => $get('payment_id')
? 'Refundable balance: '.(Payment::find($get('payment_id'))?->refundableBalance() ?? '0.00')
: 'Select a payment to see its refundable balance.'),
TextInput::make('amount')
->numeric()
->minValue(0.01)
->required(),
->visible(fn (Get $get): bool => ! $get('full_refund'))
->required(fn (Get $get): bool => ! $get('full_refund'))
->maxValue(fn (Get $get): ?string => Payment::find($get('payment_id'))?->refundableBalance()),
Textarea::make('reason')
->required(),
])
@@ -68,10 +80,12 @@ class ProcessRefundAction
return;
}
$amount = $data['full_refund'] ? $payment->refundableBalance() : (string) $data['amount'];
try {
app(RefundBookingAction::class)->handle(
$payment->booking,
(string) $data['amount'],
$amount,
$data['reason'],
auth()->id(),
);
@@ -10,6 +10,7 @@ use Modules\Booking\Models\Booking;
use Modules\Payment\Database\Factories\PaymentFactory;
use Modules\Payment\Enums\PaymentMethod;
use Modules\Payment\Enums\PaymentStatus;
use Modules\Payment\Enums\RefundStatus;
use Spatie\Activitylog\Models\Concerns\LogsActivity;
use Spatie\Activitylog\Support\LogOptions;
@@ -74,4 +75,17 @@ class Payment extends Model
{
return $this->hasMany(Refund::class);
}
/**
* What's left to refund on this Payment its total minus whatever has
* already been completed-refunded (partial refunds supported, domain.md
* §6). Shared by RefundBookingAction's own guard and the Filament refund
* forms, which surface it to staff before they submit.
*/
public function refundableBalance(): string
{
$alreadyRefunded = (string) $this->refunds()->where('status', RefundStatus::Completed->value)->sum('amount');
return bcsub((string) $this->amount, $alreadyRefunded, 2);
}
}