AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

CheckoutController.php 4.6KB

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