2026-08-29 01:14:58 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Modules\Core\Checkout\Services;
|
|
|
|
|
|
2026-08-31 13:16:13 +03:00
|
|
|
use Lunar\Exceptions\FingerprintMismatchException;
|
|
|
|
|
use Lunar\Exceptions\Carts\CartException;
|
2026-08-29 01:14:58 +03:00
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
|
use Illuminate\Support\Facades\Event;
|
|
|
|
|
use Lunar\Base\Addressable;
|
|
|
|
|
use Lunar\DataTypes\ShippingOption;
|
|
|
|
|
use Lunar\Facades\ShippingManifest;
|
|
|
|
|
use Lunar\Models\Cart;
|
|
|
|
|
use Modules\Core\Cart\Services\CartService;
|
|
|
|
|
use Modules\Core\Checkout\Events\BillingAddressSet;
|
2026-08-31 13:54:20 +03:00
|
|
|
use Modules\Core\Checkout\Events\PaymentMethodSelected;
|
2026-08-29 01:14:58 +03:00
|
|
|
use Modules\Core\Checkout\Events\ShippingAddressSet;
|
|
|
|
|
use Modules\Core\Checkout\Events\ShippingOptionSelected;
|
|
|
|
|
use Modules\Core\Checkout\Exceptions\InvalidShippingOptionException;
|
2026-08-31 13:54:20 +03:00
|
|
|
use Modules\Core\Checkout\Exceptions\UnknownPaymentTypeException;
|
2026-09-03 17:27:34 +03:00
|
|
|
use Modules\Core\Payment\DTOs\PaymentResult;
|
2026-08-31 14:12:21 +03:00
|
|
|
use Modules\Core\Payment\Models\PaymentMethod;
|
2026-09-02 16:14:52 +03:00
|
|
|
use Modules\Core\Payment\Services\PaymentDriverResolver;
|
2026-08-29 01:14:58 +03:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Storefront-facing checkout operations, mirroring
|
|
|
|
|
* Modules\Core\Cart\Services\CartService's shape — one boboko-owned API a
|
|
|
|
|
* storefront calls, keeping Lunar's own Cart/ShippingManifest primitives an
|
|
|
|
|
* implementation detail. See docs/checkout.md for the full design —
|
|
|
|
|
* Checkout is the middle of a three-stage lifecycle (Cart → Checkout →
|
|
|
|
|
* Order): it owns the placement moment itself (address, shipping selection,
|
2026-09-03 17:27:34 +03:00
|
|
|
* ensuring a draft Order exists) and hands off to Payment the instant that
|
|
|
|
|
* draft exists — see initiatePayment(). What happens to that Order
|
|
|
|
|
* afterward (status transitions, fulfillment) is deliberately out of
|
|
|
|
|
* scope here — see docs/payments.md and Checkout\Events\OrderPlaced's
|
|
|
|
|
* docblock for where that now lives.
|
2026-08-29 01:14:58 +03:00
|
|
|
*
|
|
|
|
|
* Depends on CartService for cart access rather than reaching into
|
|
|
|
|
* Lunar\Facades\CartSession directly a second time, so Checkout stays
|
|
|
|
|
* layered on top of Cart's own service boundary instead of duplicating it.
|
|
|
|
|
*/
|
|
|
|
|
class CheckoutService
|
|
|
|
|
{
|
|
|
|
|
public function __construct(
|
|
|
|
|
private readonly CartService $cart,
|
2026-09-02 16:14:52 +03:00
|
|
|
private readonly PaymentDriverResolver $paymentDrivers,
|
2026-08-29 01:14:58 +03:00
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
public function setShippingAddress(array|Addressable $address): Cart
|
|
|
|
|
{
|
|
|
|
|
$cart = $this->cart->currentOrCreate()->setShippingAddress($address);
|
|
|
|
|
|
|
|
|
|
Event::dispatch(new ShippingAddressSet($cart, $address));
|
|
|
|
|
|
|
|
|
|
return $cart;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function setBillingAddress(array|Addressable $address): Cart
|
|
|
|
|
{
|
|
|
|
|
$cart = $this->cart->currentOrCreate()->setBillingAddress($address);
|
|
|
|
|
|
|
|
|
|
Event::dispatch(new BillingAddressSet($cart, $address));
|
|
|
|
|
|
|
|
|
|
return $cart;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Every shipping option currently available for the cart — already
|
|
|
|
|
* fully backed by the merged Shipping-Carriers work: this runs every
|
|
|
|
|
* registered Lunar\Shipping\Interfaces\ShippingRateInterface driver
|
|
|
|
|
* (ACS/Box Now live-rate quoting alongside table-rate-shipping's own
|
|
|
|
|
* flat-rate/free-shipping/collection drivers) through
|
|
|
|
|
* ShippingManifest's pipeline. No rate-resolution logic lives here —
|
|
|
|
|
* this is a thin pass-through.
|
|
|
|
|
*
|
|
|
|
|
* @return Collection<int, ShippingOption>
|
|
|
|
|
*/
|
|
|
|
|
public function getShippingOptions(): Collection
|
|
|
|
|
{
|
|
|
|
|
return ShippingManifest::getOptions($this->cart->currentOrCreate());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @throws InvalidShippingOptionException if $identifier doesn't resolve
|
|
|
|
|
* to a real, currently-available option for the cart
|
|
|
|
|
*/
|
|
|
|
|
public function selectShippingOption(string $identifier): Cart
|
|
|
|
|
{
|
|
|
|
|
$cartBefore = $this->cart->currentOrCreate();
|
|
|
|
|
$option = ShippingManifest::getOption($cartBefore, $identifier);
|
|
|
|
|
|
|
|
|
|
if ($option === null) {
|
|
|
|
|
throw new InvalidShippingOptionException($identifier);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$cart = $cartBefore->setShippingOption($option);
|
|
|
|
|
|
|
|
|
|
Event::dispatch(new ShippingOptionSelected($cart, $option));
|
|
|
|
|
|
|
|
|
|
return $cart;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-31 14:12:21 +03:00
|
|
|
/**
|
|
|
|
|
* Every payment type currently offered to the storefront — every key
|
|
|
|
|
* in config('lunar.payments.types') that is BOTH administratively
|
|
|
|
|
* enabled (Modules\Core\Payment\Models\PaymentMethod::enabled) AND
|
2026-09-03 17:27:34 +03:00
|
|
|
* whose registered driver reports itself usable right now
|
|
|
|
|
* (Configurable::isConfigured() — e.g. Stripe with no API key set is
|
2026-08-31 14:12:21 +03:00
|
|
|
* never offered, regardless of the enabled toggle). A type with no
|
|
|
|
|
* PaymentMethod row at all (never seeded) is treated as not offered,
|
|
|
|
|
* same as disabled — nothing here creates one; see
|
|
|
|
|
* InstallLunarCommand::seedPaymentMethods().
|
|
|
|
|
*
|
|
|
|
|
* @return array<string>
|
|
|
|
|
*/
|
|
|
|
|
public function getPaymentMethods(): array
|
|
|
|
|
{
|
|
|
|
|
return PaymentMethod::where('enabled', true)
|
|
|
|
|
->pluck('type')
|
2026-09-02 16:14:52 +03:00
|
|
|
->filter(fn (string $type) => $this->paymentDrivers->resolve($type)?->isConfigured() ?? false)
|
2026-08-31 14:12:21 +03:00
|
|
|
->values()
|
|
|
|
|
->all();
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-31 13:54:20 +03:00
|
|
|
/**
|
|
|
|
|
* Records which payment type the shopper picked (Cart::meta
|
|
|
|
|
* ['payment_method']) — read by e.g. Modules\Core\Payment\Pipelines\
|
|
|
|
|
* Cart\ApplyCashOnDeliveryFee to add that type's own cart-total
|
2026-08-31 14:12:21 +03:00
|
|
|
* adjustments before recalculation.
|
|
|
|
|
*
|
|
|
|
|
* Also snapshots Cart::fingerprint() into meta, *after* saving the
|
|
|
|
|
* chosen type — the fingerprint has to reflect the final total
|
|
|
|
|
* including any payment-type-specific adjustment (e.g. a COD
|
|
|
|
|
* surcharge), which only exists once payment_method is set and the
|
|
|
|
|
* cart recalculates. Captured here, server-side, rather than asked of
|
2026-09-03 17:27:34 +03:00
|
|
|
* the storefront: this is the last moment before initiatePayment() that
|
|
|
|
|
* the shopper's reviewed total is known, and initiatePayment() reads it
|
2026-08-31 14:12:21 +03:00
|
|
|
* back internally instead of taking a fingerprint parameter — a
|
|
|
|
|
* storefront should never need to know Cart::fingerprint() exists.
|
|
|
|
|
*
|
2026-09-03 17:27:34 +03:00
|
|
|
* Does not itself call a payment driver — selecting a method and
|
|
|
|
|
* initiating payment against it are deliberately separate steps, same
|
2026-08-31 13:54:20 +03:00
|
|
|
* as selecting a shipping option happens before placing the order.
|
|
|
|
|
*
|
2026-08-31 14:12:21 +03:00
|
|
|
* @throws UnknownPaymentTypeException if $type isn't currently offered
|
|
|
|
|
* — see getPaymentMethods() for what that means (registered,
|
|
|
|
|
* administratively enabled, and its driver reports itself usable)
|
2026-08-31 13:54:20 +03:00
|
|
|
*/
|
|
|
|
|
public function selectPaymentMethod(string $type): Cart
|
|
|
|
|
{
|
2026-08-31 14:12:21 +03:00
|
|
|
if (! in_array($type, $this->getPaymentMethods(), true)) {
|
|
|
|
|
throw new UnknownPaymentTypeException($type);
|
|
|
|
|
}
|
2026-08-31 13:54:20 +03:00
|
|
|
|
|
|
|
|
$cart = $this->cart->currentOrCreate();
|
2026-09-03 17:42:30 +03:00
|
|
|
$cart->meta = [...($cart->meta?->toArray() ?? []), 'payment_method' => $type];
|
2026-08-31 13:54:20 +03:00
|
|
|
$cart->save();
|
|
|
|
|
|
2026-08-31 14:12:21 +03:00
|
|
|
$cart = $cart->calculate();
|
2026-09-03 17:42:30 +03:00
|
|
|
$cart->meta = [...($cart->meta?->toArray() ?? []), 'checkout_fingerprint' => $cart->fingerprint()];
|
2026-08-31 14:12:21 +03:00
|
|
|
$cart->save();
|
|
|
|
|
|
2026-08-31 13:54:20 +03:00
|
|
|
Event::dispatch(new PaymentMethodSelected($cart, $type));
|
|
|
|
|
|
|
|
|
|
return $cart;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-09-03 17:27:34 +03:00
|
|
|
* The one storefront-facing "place this order and pay for it" call —
|
|
|
|
|
* the point where Checkout hands off to Payment. Ensures a draft
|
|
|
|
|
* Order exists (Cart::createOrder() — confirmed idempotent against a
|
|
|
|
|
* cart's own pre-existing, not-yet-placed-at draft; see
|
|
|
|
|
* vendor/lunarphp/core/src/Actions/Carts/CreateOrder.php), then
|
|
|
|
|
* resolves the payment type selected by selectPaymentMethod() and
|
|
|
|
|
* calls pay() or authorize() on its driver, per that type's
|
|
|
|
|
* config('lunar.payments.types.{type}.capture_mode') — boboko-core's
|
|
|
|
|
* own types (config/payment.php) are merged into that same Lunar
|
|
|
|
|
* config key by PaymentServiceProvider::boot().
|
2026-09-02 16:14:52 +03:00
|
|
|
*
|
2026-09-03 17:27:34 +03:00
|
|
|
* Returns the driver's own PaymentResult UNCHANGED — this method does
|
|
|
|
|
* not wait for or resolve anything past what pay()/authorize() itself
|
|
|
|
|
* returns synchronously. A Pending result (an async gateway like
|
|
|
|
|
* Stripe requiring 3-D Secure/a redirect) is a normal, expected
|
|
|
|
|
* outcome, not an error — the caller (a storefront controller) is
|
|
|
|
|
* responsible for whatever the gateway needs next.
|
2026-08-31 13:54:20 +03:00
|
|
|
*
|
2026-09-03 17:27:34 +03:00
|
|
|
* KNOWN GAP, explicitly out of scope for now: PaymentResult alone does
|
|
|
|
|
* not carry gateway-specific continuation data (e.g. Stripe's
|
|
|
|
|
* PaymentIntent client_secret for a Pending result needing frontend
|
|
|
|
|
* confirmation) — that concept existed on the deleted PaymentInitiation
|
|
|
|
|
* DTO and was intentionally removed from Payment's abstraction layer.
|
|
|
|
|
* Nothing here re-introduces it; only OfflinePaymentDriver's
|
|
|
|
|
* always-Immediate-Succeeded path is fully wired end-to-end today.
|
2026-08-31 14:12:21 +03:00
|
|
|
*
|
2026-09-03 17:27:34 +03:00
|
|
|
* The draft order's own $order->total (not the Cart's) is what gets
|
|
|
|
|
* passed as $amount — Order::$total is Lunar's own Price-cast
|
|
|
|
|
* attribute, already resolving the correct Currency via the order's
|
|
|
|
|
* own currency_code, and is the authoritative total once the draft
|
|
|
|
|
* row exists.
|
2026-08-31 13:54:20 +03:00
|
|
|
*
|
2026-09-03 17:27:34 +03:00
|
|
|
* $context passed to the driver is {cart_id, order_id} — the exact
|
|
|
|
|
* keys Modules\Core\Payment\Drivers\StripePaymentDriver::
|
|
|
|
|
* rememberIntent() already reads.
|
|
|
|
|
*
|
|
|
|
|
* Same fingerprint precondition the old placeOrder() had: mandatory,
|
|
|
|
|
* not optional, checked before the draft is created.
|
|
|
|
|
*
|
|
|
|
|
* @param array<string, mixed> $data passed through untouched to
|
|
|
|
|
* the driver's pay()/authorize() — e.g. Stripe's payment_method
|
|
|
|
|
* token.
|
|
|
|
|
*
|
|
|
|
|
* @throws UnknownPaymentTypeException if the cart's selected
|
|
|
|
|
* payment_method (from selectPaymentMethod()) is no longer offered
|
|
|
|
|
* — re-checked here, not just at selection time, since a type could
|
|
|
|
|
* be disabled in between
|
|
|
|
|
* @throws FingerprintMismatchException
|
|
|
|
|
* @throws CartException
|
2026-08-31 13:54:20 +03:00
|
|
|
*/
|
2026-09-03 17:27:34 +03:00
|
|
|
public function initiatePayment(string $fingerprint, array $data = []): PaymentResult
|
2026-08-31 13:54:20 +03:00
|
|
|
{
|
2026-09-03 17:27:34 +03:00
|
|
|
$cart = $this->cart->currentOrCreate();
|
|
|
|
|
$cart->checkFingerprint($fingerprint);
|
|
|
|
|
|
|
|
|
|
$type = $cart->meta['payment_method'] ?? null;
|
|
|
|
|
|
|
|
|
|
if ($type === null || ! in_array($type, $this->getPaymentMethods(), true)) {
|
|
|
|
|
throw new UnknownPaymentTypeException((string) $type);
|
2026-08-31 14:12:21 +03:00
|
|
|
}
|
2026-08-31 13:54:20 +03:00
|
|
|
|
2026-09-03 17:27:34 +03:00
|
|
|
$order = $cart->createOrder();
|
|
|
|
|
|
|
|
|
|
$driver = $this->paymentDrivers->resolve($type);
|
|
|
|
|
$captureMode = config("lunar.payments.types.{$type}.capture_mode", 'pay');
|
|
|
|
|
|
|
|
|
|
$context = ['cart_id' => $cart->id, 'order_id' => $order->id];
|
2026-08-31 14:12:21 +03:00
|
|
|
|
2026-09-03 17:27:34 +03:00
|
|
|
return $captureMode === 'authorize'
|
|
|
|
|
? $driver->authorize($type, $order->total, $data, $context)
|
|
|
|
|
: $driver->pay($type, $order->total, $data, $context);
|
2026-08-31 13:54:20 +03:00
|
|
|
}
|
2026-08-29 01:14:58 +03:00
|
|
|
}
|