commit1dc4e29c69Author: ncoronel <ncoronel@quo.ar> Date: Wed Aug 19 13:58:03 2026 -0300 refactor(reservations): unify expiration command commit093e894cc3Author: ncoronel <ncoronel@quo.ar> Date: Wed Aug 19 13:48:09 2026 -0300 feat(cart): expire abandoned stock reservations commitfdf0f3328fAuthor: ncoronel <ncoronel@quo.ar> Date: Wed Aug 19 12:53:12 2026 -0300 refactor(stock): implement expiration for stock reservations and add configuration commit8d6bcdcc43Author: ncoronel <ncoronel@quo.ar> Date: Wed Aug 19 12:38:21 2026 -0300 refactor(cart): invalidate payment on actual changes commit3206e293ebAuthor: ncoronel <ncoronel@quo.ar> Date: Wed Aug 19 12:24:48 2026 -0300 refactor(cart): own checkout item editing commitaed99bd05eAuthor: ncoronel <ncoronel@quo.ar> Date: Wed Aug 19 12:14:57 2026 -0300 refactor(checkout): remove legacy purchase item reservations commitf1649e0e4bAuthor: ncoronel <ncoronel@quo.ar> Date: Wed Aug 19 12:06:29 2026 -0300 refactor(checkout): materialize purchase items on confirmation commite6c4b40a37Author: ncoronel <ncoronel@quo.ar> Date: Wed Aug 19 12:06:19 2026 -0300 feat(inventory): add traceable cart stock reservations
64 lines
1.9 KiB
PHP
64 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Purchase\Services\Checkout;
|
|
|
|
use App\Domains\Purchase\Models\Purchase;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class EditCheckoutService
|
|
{
|
|
/** @param array<string, string> $customerData */
|
|
public function updateCustomer(Purchase $purchase, array $customerData): Purchase
|
|
{
|
|
return DB::transaction(function () use ($purchase, $customerData): Purchase {
|
|
$purchase = $this->lockPurchase($purchase);
|
|
$this->assertEditable($purchase);
|
|
|
|
$purchase->update($customerData);
|
|
|
|
return $this->loadPurchase($purchase);
|
|
});
|
|
}
|
|
|
|
private function assertEditable(Purchase $purchase): void
|
|
{
|
|
if (
|
|
! in_array($purchase->status, [
|
|
Purchase::STATUS_CREATED,
|
|
Purchase::STATUS_PENDING_PAYMENT,
|
|
], true)
|
|
|| $this->hasExpired($purchase)
|
|
) {
|
|
throw ValidationException::withMessages([
|
|
'purchase' => __('api.purchase.not_editable'),
|
|
]);
|
|
}
|
|
}
|
|
|
|
private function hasExpired(Purchase $purchase): bool
|
|
{
|
|
return $purchase->expires_at !== null && $purchase->expires_at->isPast();
|
|
}
|
|
|
|
private function lockPurchase(Purchase $purchase): Purchase
|
|
{
|
|
/** @var Purchase */
|
|
return Purchase::query()->lockForUpdate()->findOrFail($purchase->getKey());
|
|
}
|
|
|
|
private function loadPurchase(Purchase $purchase): Purchase
|
|
{
|
|
return $purchase->load([
|
|
'items.imageAttachment',
|
|
'cart.items.catalogItem.inventory',
|
|
'cart.items.catalogItem.attachments',
|
|
'cart.items.variant.inventory',
|
|
'cart.items.variant.attachments',
|
|
'cart.items.variant.definitions.itemAttribute.attribute',
|
|
'cart.items.variant.eventDates',
|
|
'cart.items.variant.eventDate',
|
|
]);
|
|
}
|
|
}
|