AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Commerce\Checkout;
  4. use App\Commerce\MalaysianStates;
  5. use App\Commerce\Payments\PaymentException;
  6. use App\Commerce\Payments\PaymentGateway;
  7. use App\Http\Controllers\Concerns\RendersStorefront;
  8. use App\Models\Order;
  9. use Illuminate\Http\RedirectResponse;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Validation\Rule;
  12. use Illuminate\Validation\ValidationException;
  13. use Inertia\Inertia;
  14. use Inertia\Response;
  15. use Symfony\Component\HttpFoundation\Response as HttpResponse;
  16. /**
  17. * Checkout: the shopper's details, the trip to the payment page, and the page they come back to.
  18. */
  19. class CheckoutController extends Controller
  20. {
  21. use RendersStorefront;
  22. /**
  23. * The form for the shopper's contact details and delivery address.
  24. */
  25. public function show(Request $request): Response|RedirectResponse
  26. {
  27. $storefront = $this->storefront($request);
  28. if ($this->cart($request, $storefront->store)->lines()->isEmpty()) {
  29. return redirect($storefront->path('/cart'));
  30. }
  31. return $this->renderStorefront($request, $storefront, [
  32. 'type' => 'checkout',
  33. 'states' => MalaysianStates::ALL,
  34. ]);
  35. }
  36. /**
  37. * Save the order, then send the shopper to the payment page.
  38. */
  39. public function store(Request $request, Checkout $checkout, PaymentGateway $gateway): HttpResponse
  40. {
  41. $storefront = $this->storefront($request);
  42. $store = $storefront->store;
  43. $details = $request->validate([
  44. 'name' => ['required', 'string', 'min:2', 'max:120'],
  45. 'email' => ['required', 'email', 'max:160'],
  46. 'phone' => ['required', 'string', 'regex:/^[0-9+\-\s]{7,20}$/'],
  47. 'line1' => ['required', 'string', 'min:3', 'max:200'],
  48. 'line2' => ['nullable', 'string', 'max:200'],
  49. 'city' => ['required', 'string', 'min:2', 'max:100'],
  50. 'postcode' => ['required', 'string', 'regex:/^[0-9]{5}$/'],
  51. 'state' => ['required', Rule::in(MalaysianStates::ALL)],
  52. ], [
  53. 'phone.regex' => __('Please enter a valid phone number.'),
  54. 'postcode.regex' => __('The postcode must be 5 digits.'),
  55. ]);
  56. $order = $checkout->placeOrder($store, $this->cart($request, $store), $details);
  57. try {
  58. $link = $gateway->startCheckout(
  59. $order,
  60. successUrl: $storefront->url('/orders/'.$order->public_id),
  61. cancelUrl: $storefront->url('/cart'),
  62. );
  63. } catch (PaymentException $e) {
  64. report($e);
  65. $order->delete();
  66. throw ValidationException::withMessages([
  67. 'cart' => __('We could not start the payment. Please try again in a moment.'),
  68. ]);
  69. }
  70. $order->payment_provider = $gateway->name();
  71. $order->payment_checkout_id = $link->id;
  72. $order->save();
  73. // The payment page is on another website, so this is a full browser redirect.
  74. return Inertia::location($link->url);
  75. }
  76. /**
  77. * Where the shopper lands after paying. The long random id in the address is what protects the page.
  78. */
  79. public function order(Request $request, Checkout $checkout, PaymentGateway $gateway): Response
  80. {
  81. $storefront = $this->storefront($request);
  82. $store = $storefront->store;
  83. /** @var Order $order */
  84. $order = $store->orders()->with('items')->where('public_id', (string) $request->route('publicId'))->firstOrFail();
  85. // Ask the payment provider directly. Nothing in the web address is trusted.
  86. if (! $order->isPaid() && $order->payment_checkout_id !== null) {
  87. try {
  88. $checkout->markPaid($order, $gateway->checkPayment($order->payment_checkout_id));
  89. } catch (PaymentException $e) {
  90. report($e);
  91. }
  92. }
  93. if ($order->isPaid()) {
  94. $this->cart($request, $store)->clear();
  95. }
  96. return $this->renderStorefront($request, $storefront, [
  97. 'type' => 'order',
  98. 'order' => $order->toDisplay(),
  99. ]);
  100. }
  101. }