storefront($request); $lines = $this->cart($request, $storefront->store)->lines(); if ($lines->isEmpty()) { return redirect($storefront->path('/cart')); } $cart = $this->cart($request, $storefront->store); $subtotal = (int) $lines->sum('line_total'); $applied = $cart->discount($subtotal); // Shipping is worked out on what the shopper pays for the products, after any discount. $afterDiscount = $subtotal - ($applied !== null ? $applied->amount : 0); return $this->renderStorefront($request, $storefront, [ 'type' => 'checkout', 'states' => MalaysianStates::ALL, // The fee for each state, in ringgit, so the page can show it as soon as a state is chosen. 'shippingByState' => collect(MalaysianStates::ALL) ->mapWithKeys(fn (string $state) => [$state => $shipping->quote($storefront->store, $afterDiscount, $state) / 100]) ->all(), ]); } /** * Save the order, then send the shopper to the payment page. */ public function store(Request $request, Checkout $checkout, PaymentGateway $gateway): HttpResponse { $storefront = $this->storefront($request); $store = $storefront->store; $details = $request->validate([ 'name' => ['required', 'string', 'min:2', 'max:120'], 'email' => ['required', 'email', 'max:160'], 'phone' => ['required', 'string', 'regex:/^[0-9+\-\s]{7,20}$/'], 'line1' => ['required', 'string', 'min:3', 'max:200'], 'line2' => ['nullable', 'string', 'max:200'], 'city' => ['required', 'string', 'min:2', 'max:100'], 'postcode' => ['required', 'string', 'regex:/^[0-9]{5}$/'], 'state' => ['required', Rule::in(MalaysianStates::ALL)], ], StorefrontText::checkoutMessages($store->language->value), StorefrontText::checkoutFields($store->language->value)); $order = $checkout->placeOrder($store, $this->cart($request, $store), $details); try { $link = $gateway->startCheckout( $order, successUrl: $storefront->url('/orders/'.$order->public_id), cancelUrl: $storefront->url('/cart'), ); } catch (PaymentException $e) { report($e); $order->delete(); throw ValidationException::withMessages([ 'cart' => StorefrontText::line($store->language->value, 'payment_unavailable'), ]); } $order->payment_provider = $gateway->name(); $order->payment_checkout_id = $link->id; $order->save(); // The payment page is on another website, so this is a full browser redirect. return Inertia::location($link->url); } /** * Where the shopper lands after paying. The long random id in the address is what protects the page. */ public function order(Request $request, Checkout $checkout, PaymentGateway $gateway): Response { $storefront = $this->storefront($request); $store = $storefront->store; /** @var Order $order */ $order = $store->orders()->with('items')->where('public_id', (string) $request->route('publicId'))->firstOrFail(); // Ask the payment provider directly. Nothing in the web address is trusted. if (! $order->isPaid() && $order->payment_checkout_id !== null) { try { $checkout->markPaid($order, $gateway->checkPayment($order->payment_checkout_id)); } catch (PaymentException $e) { report($e); } } if ($order->isPaid()) { $this->cart($request, $store)->clear(); } return $this->renderStorefront($request, $storefront, [ 'type' => 'order', 'order' => $order->toDisplay(), ]); } }