| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
-
- namespace App\Http\Controllers;
-
- use App\Commerce\Payments\PaymentException;
- use App\Commerce\Payments\StripeConnect;
- use App\Models\Store;
- use Illuminate\Http\RedirectResponse;
- use Illuminate\Http\Request;
- use Inertia\Inertia;
- use Symfony\Component\HttpFoundation\Response;
-
- /**
- * Getting paid: the merchant fills in Stripe's own sign-up form for their store's Stripe account, then comes back.
- * Stripe checks their identity, business and bank. We never see those details.
- */
- class StripeConnectController extends Controller
- {
- public function connect(Request $request, string $slug, StripeConnect $connect): Response
- {
- $store = $this->ownedStore($request, $slug);
-
- if (! $connect->isConfigured()) {
- Inertia::flash('toast', ['type' => 'error', 'message' => __('Stripe is not set up on this app yet.')]);
-
- return to_route('stores.settings', $store->slug);
- }
-
- try {
- $url = $this->setupUrl($request, $store, $connect);
- } catch (PaymentException $e) {
- report($e);
- Inertia::flash('toast', ['type' => 'error', 'message' => __('Stripe could not be reached. Please try again in a moment.')]);
-
- return to_route('stores.settings', $store->slug);
- }
-
- // A full page visit to Stripe, not an Inertia one.
- return Inertia::location($url);
- }
-
- /**
- * Stripe sends the merchant back here when they finish or leave the form.
- */
- public function return(Request $request, string $slug, StripeConnect $connect): RedirectResponse
- {
- $store = $this->ownedStore($request, $slug);
-
- try {
- $account = $connect->refresh($store);
- } catch (PaymentException $e) {
- report($e);
- $account = $store->stripeAccount;
- }
-
- $message = match (true) {
- $account === null => null,
- $account->charges_enabled => ['type' => 'success', 'message' => __('Stripe is connected. Your store can take payments.')],
- $account->details_submitted => ['type' => 'success', 'message' => __('Thank you. Stripe is checking your details, which usually takes a few minutes.')],
- default => ['type' => 'error', 'message' => __('Stripe sign-up is not finished yet. Click Finish setup to carry on.')],
- };
-
- if ($message !== null) {
- Inertia::flash('toast', $message);
- }
-
- return to_route('stores.settings', $store->slug);
- }
-
- /**
- * Stripe's sign-up link only works once and for a short time. When it expired, Stripe sends the
- * merchant here, and we open a fresh one.
- */
- public function resume(Request $request, string $slug, StripeConnect $connect): RedirectResponse
- {
- $store = $this->ownedStore($request, $slug);
-
- if ($store->stripeAccount === null) {
- return to_route('stores.settings', $store->slug);
- }
-
- try {
- return redirect()->away($this->setupUrl($request, $store, $connect));
- } catch (PaymentException $e) {
- report($e);
- Inertia::flash('toast', ['type' => 'error', 'message' => __('Stripe could not be reached. Please try again in a moment.')]);
-
- return to_route('stores.settings', $store->slug);
- }
- }
-
- /**
- * @throws PaymentException
- */
- private function setupUrl(Request $request, Store $store, StripeConnect $connect): string
- {
- return $connect->setupUrl(
- $store,
- (string) $request->user()->email,
- returnUrl: route('stores.payments.return', $store->slug),
- refreshUrl: route('stores.payments.resume', $store->slug),
- );
- }
-
- private function ownedStore(Request $request, string $slug): Store
- {
- return $request->user()->stores()->where('slug', $slug)->firstOrFail();
- }
- }
|