πŸ’‚β€β™‚οΈ Authorization
Cashierstream lets you define a custom closure to check whether the billable is allowed to perform a request on the billing portal.
The first thing you need to make sure of is to have the RenokiCo\BillingPortal\Http\Middleware\Authorize middleware in config/billing-portal.php:
1
return [
2
​
3
'middleware' => [
4
// ...
5
​
6
\RenokiCo\BillingPortal\Http\Middleware\Authorize::class,
7
],
8
​
9
// ...
10
11
];
Copied!
To customize the authorization response, you can define the closure resolver in your BillingPortalServiceProvider file:
1
use Illuminate\Http\Request;
2
use RenokiCo\BillingPortal\BillingPortal;
3
​
4
class BillingPortalServiceProvider extends BaseServiceProvider
5
{
6
/**
7
* Boot the service provider.
8
*
9
* @return void
10
*/
11
public function boot()
12
{
13
parent::boot();
14
​
15
BillingPortal::resolveAuthorization(function ($billable, Request $request) {
16
return $billable && $billable->id == $request->user()->id;
17
});
18
}
19
}
Copied!

Redirecting instead of throwing error

Based on the true/false return value, it will either allow the request to pass through or throw a 403 error response. However, you can redirect your users instead of letting the package throw the error. The package middleware will check for a RedirectResponse value and in case it is met, it will redirect the user instead of throwing the error:
1
use Illuminate\Http\Request;
2
use RenokiCo\BillingPortal\BillingPortal;
3
​
4
class BillingPortalServiceProvider extends BaseServiceProvider
5
{
6
/**
7
* Boot the service provider.
8
*
9
* @return void
10
*/
11
public function boot()
12
{
13
parent::boot();
14
​
15
BillingPortal::resolveAuthorization(function ($billable, Request $request) {
16
$isAuthorized = $billable && $billable->id == $request->user()->id;
17
18
if (! $isAuthorized) {
19
return Redirect::route('home')
20
->with('flash.banner', 'You are not allowed to manage the subscriptions.')
21
->with('flash.bannerStyle', 'danger');
22
}
23
24
return true;
25
});
26
}
27
}
Copied!
Copy link
Edit on GitHub