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->applied($lines); // Shipping is worked out on what the shopper pays for the products, after any discount. $afterDiscount = $subtotal - ($applied !== null ? $applied->amount : 0); $ways = $gateways->available($storefront->store); return $this->renderStorefront($request, $storefront, [ 'type' => 'checkout', 'states' => MalaysianStates::ALL, // False until the merchant connects their own Stripe account (and Stripe approves it) or HitPay. 'takingOrders' => $ways !== [], // The ways to pay, for the shopper to choose: Stripe (card, FPX, GrabPay) and HitPay (DuitNow QR, e-wallets). 'payWays' => collect($ways) ->map(fn (PaymentGateway $gateway, string $key) => ['key' => $key, 'methods' => $gateway->methods($storefront->store)]) ->values() ->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, PaymentGateways $gateways): HttpResponse { $storefront = $this->storefront($request); $store = $storefront->store; $ways = $gateways->available($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)], // Which way to pay. Optional: the first way is used when the shopper had no choice to make. 'pay_with' => ['nullable', 'string', 'max:20'], ], StorefrontText::checkoutMessages($store->language->value), StorefrontText::checkoutFields($store->language->value)); if ($ways === []) { throw ValidationException::withMessages(['cart' => StorefrontText::line($store->language->value, 'not_taking_orders')]); } // A way that is no longer offered (the merchant disconnected it meanwhile) falls back to the first one. $gateway = $ways[$details['pay_with'] ?? ''] ?? reset($ways); unset($details['pay_with']); $order = $checkout->placeOrder($store, $this->cart($request, $store), $details); // Set before the payment page is made, so the page and what the shopper is told end at the same time. $order->payment_expires_at = now()->addHours(Checkout::PAY_WITHIN_HOURS); 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->payment_url = $link->url; $order->payment_account = $link->account; $order->save(); // So the cart page can offer "Complete payment" if the shopper leaves the payment page. $this->cart($request, $store)->rememberUnpaidOrder($order); // 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, PaymentGateways $gateways): Response { $storefront = $this->storefront($request); $store = $storefront->store; $order = $this->findOrder($request, $checkout, $gateways); if ($order->isPaid()) { $this->cart($request, $store)->clear(); } return $this->renderStorefront($request, $storefront, [ 'type' => 'order', 'order' => $order->toDisplay(), // "Complete payment" while the payment page still takes payments; "closed" once it no longer does. 'payment' => $order->isPaid() || $order->payment_expires_at === null ? null : [ 'can_pay' => $order->canStillPay(), 'expires_at' => $order->payment_expires_at->toIso8601String(), ], ]); } /** * "Complete payment": back to the same payment page, for a shopper who left it. Used by the order page, the * cart page and the reminder email. Asks the provider first, so a shopper who already paid sees the thank-you. */ public function pay(Request $request, Checkout $checkout, PaymentGateways $gateways): HttpResponse { $storefront = $this->storefront($request); $order = $this->findOrder($request, $checkout, $gateways); if (! $order->canStillPay()) { return redirect($storefront->path('/orders/'.$order->public_id)); } // The payment page is on another website, so this is a full browser redirect. return Inertia::location((string) $order->payment_url); } /** * The order in the address, marked paid first if the provider says it was. Nothing in the web address is trusted. */ private function findOrder(Request $request, Checkout $checkout, PaymentGateways $gateways): Order { /** @var Order $order */ $order = $this->storefront($request)->store->orders()->with('items')->where('public_id', (string) $request->route('publicId'))->firstOrFail(); if (! $order->isPaid() && $order->payment_checkout_id !== null) { try { $checkout->markPaid($order, $gateways->for($order)->checkPayment($order)); } catch (PaymentException $e) { report($e); } } return $order; } }