84 lines
2.3 KiB
PHP
84 lines
2.3 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace Modules\Core\Payment\Services;
|
||
|
|
|
||
|
|
use Illuminate\Support\Collection;
|
||
|
|
use Illuminate\Support\Facades\Event;
|
||
|
|
use Modules\Core\Payment\Events\PaymentMethodCreated;
|
||
|
|
use Modules\Core\Payment\Events\PaymentMethodDeleted;
|
||
|
|
use Modules\Core\Payment\Events\PaymentMethodUpdated;
|
||
|
|
use Modules\Core\Payment\Models\PaymentMethod;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The single write (AND read) gateway for PaymentMethod — every Filament
|
||
|
|
* resource/action calls this, not PaymentMethod::create()/update()/delete()
|
||
|
|
* directly, so cache invalidation is one explicit step colocated with the
|
||
|
|
* mutation (not hidden in a model observer) and every admin change to a
|
||
|
|
* payment method dispatches a matching event, the same convention
|
||
|
|
* Modules\Core\Cart\Services\CartService already established for its own
|
||
|
|
* mutating methods.
|
||
|
|
*
|
||
|
|
* list() is what PaymentMethodCache actually reads through — see that
|
||
|
|
* class for why this needs caching at all (Modules\Core\Checkout\
|
||
|
|
* Services\CheckoutService and PaymentServiceProvider's Lunar\Facades\
|
||
|
|
* Payments shim both read the full payment-method list on the hot path).
|
||
|
|
*/
|
||
|
|
class PaymentMethodService
|
||
|
|
{
|
||
|
|
public function __construct(
|
||
|
|
private readonly PaymentMethodCache $cache,
|
||
|
|
) {}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @return Collection<int, PaymentMethod>
|
||
|
|
*/
|
||
|
|
public function list(): Collection
|
||
|
|
{
|
||
|
|
return $this->cache->all();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param array<string, mixed> $data
|
||
|
|
*/
|
||
|
|
public function create(array $data): PaymentMethod
|
||
|
|
{
|
||
|
|
$method = PaymentMethod::create($data);
|
||
|
|
|
||
|
|
$this->cache->forget();
|
||
|
|
|
||
|
|
Event::dispatch(new PaymentMethodCreated($method));
|
||
|
|
|
||
|
|
return $method;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param array<string, mixed> $data
|
||
|
|
*/
|
||
|
|
public function update(PaymentMethod $method, array $data): PaymentMethod
|
||
|
|
{
|
||
|
|
$old = $method->only(array_keys($data));
|
||
|
|
|
||
|
|
$method->update($data);
|
||
|
|
|
||
|
|
$this->cache->forget();
|
||
|
|
|
||
|
|
Event::dispatch(new PaymentMethodUpdated($method, $old));
|
||
|
|
|
||
|
|
return $method;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function delete(PaymentMethod $method): void
|
||
|
|
{
|
||
|
|
$snapshot = $method->only([
|
||
|
|
'id', 'type', 'name', 'driver', 'capture_mode',
|
||
|
|
'captured_status', 'authorized_status', 'position', 'enabled',
|
||
|
|
]);
|
||
|
|
|
||
|
|
$method->delete();
|
||
|
|
|
||
|
|
$this->cache->forget();
|
||
|
|
|
||
|
|
Event::dispatch(new PaymentMethodDeleted($snapshot));
|
||
|
|
}
|
||
|
|
}
|