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.

StripeConnectController.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Commerce\Payments\PaymentException;
  4. use App\Commerce\Payments\StripeConnect;
  5. use App\Models\Store;
  6. use Illuminate\Http\RedirectResponse;
  7. use Illuminate\Http\Request;
  8. use Inertia\Inertia;
  9. use Symfony\Component\HttpFoundation\Response;
  10. /**
  11. * Getting paid: the merchant fills in Stripe's own sign-up form for their store's Stripe account, then comes back.
  12. * Stripe checks their identity, business and bank. We never see those details.
  13. */
  14. class StripeConnectController extends Controller
  15. {
  16. public function connect(Request $request, string $slug, StripeConnect $connect): Response
  17. {
  18. $store = $this->ownedStore($request, $slug);
  19. if (! $connect->isConfigured()) {
  20. Inertia::flash('toast', ['type' => 'error', 'message' => __('Stripe is not set up on this app yet.')]);
  21. return to_route('stores.settings', $store->slug);
  22. }
  23. try {
  24. $url = $this->setupUrl($request, $store, $connect);
  25. } catch (PaymentException $e) {
  26. report($e);
  27. Inertia::flash('toast', ['type' => 'error', 'message' => __('Stripe could not be reached. Please try again in a moment.')]);
  28. return to_route('stores.settings', $store->slug);
  29. }
  30. // A full page visit to Stripe, not an Inertia one.
  31. return Inertia::location($url);
  32. }
  33. /**
  34. * Stripe sends the merchant back here when they finish or leave the form.
  35. */
  36. public function return(Request $request, string $slug, StripeConnect $connect): RedirectResponse
  37. {
  38. $store = $this->ownedStore($request, $slug);
  39. try {
  40. $account = $connect->refresh($store);
  41. } catch (PaymentException $e) {
  42. report($e);
  43. $account = $store->stripeAccount;
  44. }
  45. $message = match (true) {
  46. $account === null => null,
  47. $account->charges_enabled => ['type' => 'success', 'message' => __('Stripe is connected. Your store can take payments.')],
  48. $account->details_submitted => ['type' => 'success', 'message' => __('Thank you. Stripe is checking your details, which usually takes a few minutes.')],
  49. default => ['type' => 'error', 'message' => __('Stripe sign-up is not finished yet. Click Finish setup to carry on.')],
  50. };
  51. if ($message !== null) {
  52. Inertia::flash('toast', $message);
  53. }
  54. return to_route('stores.settings', $store->slug);
  55. }
  56. /**
  57. * Stripe's sign-up link only works once and for a short time. When it expired, Stripe sends the
  58. * merchant here, and we open a fresh one.
  59. */
  60. public function resume(Request $request, string $slug, StripeConnect $connect): RedirectResponse
  61. {
  62. $store = $this->ownedStore($request, $slug);
  63. if ($store->stripeAccount === null) {
  64. return to_route('stores.settings', $store->slug);
  65. }
  66. try {
  67. return redirect()->away($this->setupUrl($request, $store, $connect));
  68. } catch (PaymentException $e) {
  69. report($e);
  70. Inertia::flash('toast', ['type' => 'error', 'message' => __('Stripe could not be reached. Please try again in a moment.')]);
  71. return to_route('stores.settings', $store->slug);
  72. }
  73. }
  74. /**
  75. * @throws PaymentException
  76. */
  77. private function setupUrl(Request $request, Store $store, StripeConnect $connect): string
  78. {
  79. return $connect->setupUrl(
  80. $store,
  81. (string) $request->user()->email,
  82. returnUrl: route('stores.payments.return', $store->slug),
  83. refreshUrl: route('stores.payments.resume', $store->slug),
  84. );
  85. }
  86. private function ownedStore(Request $request, string $slug): Store
  87. {
  88. return $request->user()->stores()->where('slug', $slug)->firstOrFail();
  89. }
  90. }