@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class EnsureSubscribed
|
||||
{
|
||||
/**
|
||||
* Send users without an active subscription to the pricing page, where
|
||||
* they can subscribe. Reads the local subscription rows only, so it does
|
||||
* not call Stripe on every request.
|
||||
*
|
||||
* @param Closure(Request): Response $next
|
||||
*/
|
||||
public function handle(Request $request, Closure $next): Response
|
||||
{
|
||||
if ($request->user()?->hasActiveSubscription() === true) {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
return redirect()->route('pricing');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Responses;
|
||||
|
||||
use App\Models\Organization;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Laravel\Fortify\Contracts\RegisterResponse as RegisterResponseContract;
|
||||
use Laravel\Fortify\Fortify;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Sends a new account straight into checkout for the plan it signed up with.
|
||||
*
|
||||
* Fortify's default response redirects to the home route, which the
|
||||
* "subscribed" middleware would then bounce to the pricing page. Reading the
|
||||
* plan off the registration request keeps the account details and the plan
|
||||
* choice in a single submission, so the user never sees an interstitial.
|
||||
*/
|
||||
class RegisterResponse implements RegisterResponseContract
|
||||
{
|
||||
/**
|
||||
* Create an HTTP response that represents the object.
|
||||
*
|
||||
* The signature is left untyped to stay compatible with Responsable, which
|
||||
* declares the parameter without a type.
|
||||
*
|
||||
* @param Request $request
|
||||
*/
|
||||
public function toResponse($request): Response
|
||||
{
|
||||
if ($request->wantsJson()) {
|
||||
return new JsonResponse('', 201);
|
||||
}
|
||||
|
||||
$plan = $request->input('plan');
|
||||
|
||||
// A plan is only required when at least one is configured. Without a
|
||||
// Stripe price there is nothing to sell, so registration still succeeds
|
||||
// and the pricing page explains that no plans are available rather than
|
||||
// making the application impossible to sign up for.
|
||||
if (! is_string($plan) || ! Organization::isAvailablePlan($plan)) {
|
||||
return $this->defaultRedirect($request);
|
||||
}
|
||||
|
||||
$organization = $request->user()?->currentOrganization()->first();
|
||||
|
||||
if ($organization === null) {
|
||||
return $this->defaultRedirect($request);
|
||||
}
|
||||
|
||||
try {
|
||||
return $organization->checkoutForPlan($plan)->redirect();
|
||||
} catch (Throwable $e) {
|
||||
// The account already exists by this point, so a Stripe outage must
|
||||
// not turn a completed signup into an error page. Report it and fall
|
||||
// back to the pricing page, where checkout can be retried.
|
||||
report($e);
|
||||
|
||||
$request->session()->flash(
|
||||
'billing.checkout_failed',
|
||||
__('Your account was created, but we could not start checkout. Please try again to finish subscribing.'),
|
||||
);
|
||||
|
||||
return redirect()->route('pricing');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the new account to the home route, as Fortify does by default.
|
||||
*/
|
||||
protected function defaultRedirect(Request $request): RedirectResponse
|
||||
{
|
||||
return redirect()->intended(Fortify::redirects('register'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user