Files
core/src/Payment/Drivers/OfflinePaymentDriver.php
T

62 lines
2.2 KiB
PHP
Raw Normal View History

<?php
namespace Modules\Core\Payment\Drivers;
use Lunar\Models\Cart;
use Modules\Core\Checkout\Events\OrderPlaced;
use Modules\Core\Checkout\Events\PaymentConfirmed;
use Modules\Core\Payment\Contracts\PaymentDriver;
/**
* Shared by every payment type with no real gateway to confirm against —
* cash-in-hand, cash-on-delivery — where the shopper pays at pickup/on
* delivery, not at checkout. confirm() has nothing to wait on, so it
* dispatches PaymentConfirmed immediately, same moment Lunar's own
* OfflinePayment would place the order — but the actual placement now
* happens in CheckoutService::onPaymentConfirmed(), not here. $data is
* unused: nothing about this confirmation depends on gateway-specific
* payload.
*
* The status-mapping step this driver used to do inline right after
* placeOrder() returned now happens in onOrderPlaced() below instead —
* see PaymentDriver's docblock for why a driver can no longer rely on
* placeOrder()'s return value.
*/
class OfflinePaymentDriver implements PaymentDriver
{
/**
* Always true — no external dependency to be missing.
*/
public function isConfigured(): bool
{
return true;
}
public function confirm(Cart $cart, string $type, string $fingerprint, array $data): void
{
PaymentConfirmed::dispatch($cart, $type, $fingerprint, $data);
}
/**
* Registered in PaymentServiceProvider. Every offline-style type
* shares this one driver, so $order->meta['payment_method'] is checked
* against config('lunar.payments.types') to confirm the placed order
* actually belongs to one of them, rather than assuming every
* OrderPlaced is this driver's to act on — a Stripe order placed via
* StripePaymentDriver fires the same event.
*/
public function onOrderPlaced(OrderPlaced $event): void
{
$order = $event->order;
$type = $order->meta['payment_method'] ?? null;
if (! $type || config("lunar.payments.types.{$type}.payment_driver") !== self::class) {
return;
}
$order->update([
'status' => config("lunar.payments.types.{$type}.authorized", $order->status),
]);
}
}