AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

CheckoutController.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. $cart = $this->cart($request, $storefront->store);
  35. $subtotal = (int) $lines->sum('line_total');
  36. $applied = $cart->applied($lines);
  37. // Shipping is worked out on what the shopper pays for the products, after any discount.
  38. $afterDiscount = $subtotal - ($applied !== null ? $applied->amount : 0);
  39. return $this->renderStorefront($request, $storefront, [
  40. 'type' => 'checkout',
  41. 'states' => MalaysianStates::ALL,
  42. // The fee for each state, in ringgit, so the page can show it as soon as a state is chosen.
  43. 'shippingByState' => collect(MalaysianStates::ALL)
  44. ->mapWithKeys(fn (string $state) => [$state => $shipping->quote($storefront->store, $afterDiscount, $state) / 100])
  45. ->all(),
  46. ]);
  47. }
  48. /**
  49. * Save the order, then send the shopper to the payment page.
  50. */
  51. public function store(Request $request, Checkout $checkout, PaymentGateway $gateway): HttpResponse
  52. {
  53. $storefront = $this->storefront($request);
  54. $store = $storefront->store;
  55. $details = $request->validate([
  56. 'name' => ['required', 'string', 'min:2', 'max:120'],
  57. 'email' => ['required', 'email', 'max:160'],
  58. 'phone' => ['required', 'string', 'regex:/^[0-9+\-\s]{7,20}$/'],
  59. 'line1' => ['required', 'string', 'min:3', 'max:200'],
  60. 'line2' => ['nullable', 'string', 'max:200'],
  61. 'city' => ['required', 'string', 'min:2', 'max:100'],
  62. 'postcode' => ['required', 'string', 'regex:/^[0-9]{5}$/'],
  63. 'state' => ['required', Rule::in(MalaysianStates::ALL)],
  64. ], StorefrontText::checkoutMessages($store->language->value), StorefrontText::checkoutFields($store->language->value));
  65. $order = $checkout->placeOrder($store, $this->cart($request, $store), $details);
  66. try {
  67. $link = $gateway->startCheckout(
  68. $order,
  69. successUrl: $storefront->url('/orders/'.$order->public_id),
  70. cancelUrl: $storefront->url('/cart'),
  71. );
  72. } catch (PaymentException $e) {
  73. report($e);
  74. $order->delete();
  75. throw ValidationException::withMessages([
  76. 'cart' => StorefrontText::line($store->language->value, 'payment_unavailable'),
  77. ]);
  78. }
  79. $order->payment_provider = $gateway->name();
  80. $order->payment_checkout_id = $link->id;
  81. $order->save();
  82. // The payment page is on another website, so this is a full browser redirect.
  83. return Inertia::location($link->url);
  84. }
  85. /**
  86. * Where the shopper lands after paying. The long random id in the address is what protects the page.
  87. */
  88. public function order(Request $request, Checkout $checkout, PaymentGateway $gateway): Response
  89. {
  90. $storefront = $this->storefront($request);
  91. $store = $storefront->store;
  92. /** @var Order $order */
  93. $order = $store->orders()->with('items')->where('public_id', (string) $request->route('publicId'))->firstOrFail();
  94. // Ask the payment provider directly. Nothing in the web address is trusted.
  95. if (! $order->isPaid() && $order->payment_checkout_id !== null) {
  96. try {
  97. $checkout->markPaid($order, $gateway->checkPayment($order->payment_checkout_id));
  98. } catch (PaymentException $e) {
  99. report($e);
  100. }
  101. }
  102. if ($order->isPaid()) {
  103. $this->cart($request, $store)->clear();
  104. }
  105. return $this->renderStorefront($request, $storefront, [
  106. 'type' => 'order',
  107. 'order' => $order->toDisplay(),
  108. ]);
  109. }
  110. }