AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

CheckoutController.php 5.2KB

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