add refund and event logic

This commit is contained in:
root
2026-03-10 23:12:49 -04:00
parent ba1206e314
commit f6be51576c
67 changed files with 4808 additions and 1000 deletions
@@ -0,0 +1,61 @@
<?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];
}
}