62 lines
1.8 KiB
PHP
62 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Refunds;
|
|
|
|
use App\Models\Refund;
|
|
|
|
class RefundDecisionService
|
|
{
|
|
public function updateDecision(int $refundId, string $status, string $reason, int $actorId): array
|
|
{
|
|
$refund = Refund::query()->find($refundId);
|
|
if (! $refund) {
|
|
return ['ok' => false, 'message' => 'Refund not found.'];
|
|
}
|
|
|
|
if (! in_array($status, [Refund::STATUS_APPROVED, Refund::STATUS_REJECTED], true)) {
|
|
return ['ok' => false, 'message' => 'Invalid refund status.'];
|
|
}
|
|
|
|
if ($status === Refund::STATUS_APPROVED && (float) ($refund->refund_amount ?? 0) <= 0) {
|
|
return ['ok' => false, 'message' => 'Cannot approve a zero refund.'];
|
|
}
|
|
|
|
$refund->update([
|
|
'status' => $status,
|
|
'reason' => $reason,
|
|
'approved_at' => utc_now(),
|
|
'approved_by' => $actorId,
|
|
'updated_at' => utc_now(),
|
|
'updated_by' => $actorId,
|
|
]);
|
|
|
|
return ['ok' => true];
|
|
}
|
|
|
|
public function approve(int $refundId, int $actorId): array
|
|
{
|
|
return $this->updateDecision($refundId, Refund::STATUS_APPROVED, 'Approved', $actorId);
|
|
}
|
|
|
|
public function reject(int $refundId, string $reason, int $actorId): array
|
|
{
|
|
return $this->updateDecision($refundId, Refund::STATUS_REJECTED, $reason, $actorId);
|
|
}
|
|
|
|
public function cancel(int $refundId, int $actorId): array
|
|
{
|
|
$refund = Refund::query()->find($refundId);
|
|
if (! $refund) {
|
|
return ['ok' => false, 'message' => 'Refund not found.'];
|
|
}
|
|
|
|
$refund->update([
|
|
'status' => Refund::STATUS_CANCELED,
|
|
'updated_at' => utc_now(),
|
|
'updated_by' => $actorId,
|
|
]);
|
|
|
|
return ['ok' => true];
|
|
}
|
|
}
|