| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
-
- namespace App\Http\Controllers;
-
- use App\Commerce\Checkout;
- use App\Commerce\MalaysianStates;
- use App\Commerce\Payments\PaymentException;
- use App\Commerce\Payments\PaymentGateway;
- use App\Commerce\Shipping\ShippingProvider;
- use App\Http\Controllers\Concerns\RendersStorefront;
- use App\Models\Order;
- use App\Storefront\StorefrontText;
- use Illuminate\Http\RedirectResponse;
- use Illuminate\Http\Request;
- use Illuminate\Validation\Rule;
- use Illuminate\Validation\ValidationException;
- use Inertia\Inertia;
- use Inertia\Response;
- use Symfony\Component\HttpFoundation\Response as HttpResponse;
-
- /**
- * Checkout: the shopper's details, the trip to the payment page, and the page they come back to.
- */
- class CheckoutController extends Controller
- {
- use RendersStorefront;
-
- /**
- * The form for the shopper's contact details and delivery address.
- */
- public function show(Request $request, ShippingProvider $shipping): Response|RedirectResponse
- {
- $storefront = $this->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);
-
- 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(),
- ]);
- }
- }
|