lunarphp / livewire-starter-kit

Demonstration store for Lunar
https://docs.lunarphp.io/core/starter-kits.html
161 stars 52 forks source link

CheckoutSuccessPage retrieves wrong cart #92

Open russdot opened 1 week ago

russdot commented 1 week ago

I noticed that when submitting an order, there is no confirmation and it redirects to the main storefront (/).

After some debugging and tracing, I believe I've tracked down what's happening but I'm not sure why.

  1. CheckoutPage->checkout() redirects on successful offline payment.
  2. However, when CheckoutSuccessPage->mount() runs, the CartSession::current() retrieves an apparently new session with an ID=N+1. For example, when the payment succeeds the cart ID might be 8, but in CheckoutSuccessPage the ID will be 9.
  3. The results in the check for completedOrder to fail, and redirect to the main storefront without displaying the checkout-success.view
russdot commented 1 week ago

Continuing to investigate this for my own learning and to help out...

I've been stepping through the code to determine where the session changes and I think I've found it.

In my debug console I was running Lunar\Facades\CartSession::current()->id to check the output. I noticed it increments by +1 after updating the order within OfflinePayment->authorize()

To illustrate using excerpt from OfflinePayment, where I've added comments to indicate breakpoint locations and values for the cart ID.

public function authorize(): ?PaymentAuthorize
    {
        if (! $this->order) {
            if (! $this->order = $this->cart->draftOrder()->first()) {
                $this->order = $this->cart->createOrder();
            }
        }
        $orderMeta = array_merge(
            (array) $this->order->meta,
            $this->data['meta'] ?? []
        );

        $status = $this->data['authorized'] ?? null;

//
// (after executing line above) Lunar\Facades\CartSession::current()->id = N
//

        $this->order->update([
            'status' => $status ?? ($this->config['authorized'] ?? null),
            'meta' => $orderMeta,
            'placed_at' => now(),
        ]);

//
// (after executing line above)  Lunar\Facades\CartSession::current()->id = N + 1
// The CartSession ID increments by 1 after updating the order
//
        $response = new PaymentAuthorize(
            success: true,
            orderId: $this->order->id,
            paymentType: 'offline',
        );

        PaymentAttemptEvent::dispatch($response);

        return $response;
    }

Not sure yet why this is occurring - but wanted to capture my findings. Perhaps this is a bug with Lunar core?

alecritson commented 4 days ago

I think the issue here is the Starter kit is quite outdated at this point, we're working on new starter kits for v1 and to bring this back up to speed with the latest beta.

There was a change to the way carts are retrieved from the session after a cart completed an order, so because you're trying to fetch the cart afterwards, it has a completed order and therefore returns a new cart. So, in the case of the offline payment, you would need to pass the cart id to the success page to fetch it that way instead of going through the session manager.

alecritson commented 4 days ago

You can see this on this PR which is awaiting review by @glennjacobs https://github.com/lunarphp/livewire-starter-kit/pull/89