| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- <?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\Http\Controllers\Concerns\RendersStorefront;
- use App\Models\Order;
- 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, string $slug): Response|RedirectResponse
- {
- $store = $this->ownedStore($request, $slug);
-
- if ($this->cart($request, $store)->lines()->isEmpty()) {
- return to_route('stores.preview.cart', $store->slug);
- }
-
- return $this->renderStorefront($request, $store, [
- 'type' => 'checkout',
- 'states' => MalaysianStates::ALL,
- ]);
- }
-
- /**
- * Save the order, then send the shopper to the payment page.
- */
- public function store(Request $request, Checkout $checkout, PaymentGateway $gateway, string $slug): HttpResponse
- {
- $store = $this->ownedStore($request, $slug);
-
- $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)],
- ], [
- 'phone.regex' => __('Please enter a valid phone number.'),
- 'postcode.regex' => __('The postcode must be 5 digits.'),
- ]);
-
- $order = $checkout->placeOrder($store, $this->cart($request, $store), $details);
-
- try {
- $link = $gateway->startCheckout(
- $order,
- successUrl: route('stores.preview.order', [$store->slug, $order->public_id]),
- cancelUrl: route('stores.preview.cart', $store->slug),
- );
- } catch (PaymentException $e) {
- report($e);
- $order->delete();
-
- throw ValidationException::withMessages([
- 'cart' => __('We could not start the payment. Please try again in a moment.'),
- ]);
- }
-
- $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, string $slug, string $publicId): Response
- {
- $store = $this->ownedStore($request, $slug);
-
- /** @var Order $order */
- $order = $store->orders()->with('items')->where('public_id', $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, $store, [
- 'type' => 'order',
- 'order' => $order->toDisplay(),
- ]);
- }
- }
|