Skip to content

Commit acbc818

Browse files
committed
Adicionando Stripe via Cashier
1 parent 3c4f3bd commit acbc818

File tree

14 files changed

+741
-9
lines changed

14 files changed

+741
-9
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
7+
class CheckoutController extends Controller
8+
{
9+
private $user;
10+
11+
public function __construct()
12+
{
13+
$this->user = auth()->user();
14+
}
15+
16+
public function checkout()
17+
{
18+
return inertia('Checkout', [
19+
'stripe_intent' => $this->user->createSetupIntent(),
20+
'cashier_key' => config('cashier.key'),
21+
]);
22+
}
23+
24+
public function charge(Request $request)
25+
{
26+
if (!$this->user->subscribed('default')) {
27+
$this->user->newSubscription('default', 'price_1R9wUuLVUytZ8PTCEJl3PkVS')->create($request->paymentMethod);
28+
}
29+
30+
return redirect()->route('my-content');
31+
}
32+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace App\Http\Middleware;
4+
5+
use Closure;
6+
use Illuminate\Http\Request;
7+
use Symfony\Component\HttpFoundation\Response;
8+
9+
class CheckUserSubscription
10+
{
11+
/**
12+
* Handle an incoming request.
13+
*
14+
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
15+
*/
16+
public function handle(Request $request, Closure $next): Response
17+
{
18+
if ($request->user() && ! $request->user()->subscribed('default')) {
19+
return redirect()->route('checkout');
20+
}
21+
22+
return $next($request);
23+
}
24+
}

app/Models/User.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
use Illuminate\Database\Eloquent\Factories\HasFactory;
77
use Illuminate\Foundation\Auth\User as Authenticatable;
88
use Illuminate\Notifications\Notifiable;
9+
use Laravel\Cashier\Billable;
910

1011
class User extends Authenticatable
1112
{
1213
/** @use HasFactory<\Database\Factories\UserFactory> */
13-
use HasFactory, Notifiable;
14+
use HasFactory, Notifiable, Billable;
1415

1516
/**
1617
* The attributes that are mass assignable.

bootstrap/app.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
return Application::configure(basePath: dirname(__DIR__))
1111
->withRouting(
12-
web: __DIR__.'/../routes/web.php',
13-
commands: __DIR__.'/../routes/console.php',
14-
channels: __DIR__.'/../routes/channels.php',
12+
web: __DIR__ . '/../routes/web.php',
13+
commands: __DIR__ . '/../routes/console.php',
14+
channels: __DIR__ . '/../routes/channels.php',
1515
health: '/up',
1616
)
1717
->withMiddleware(function (Middleware $middleware) {
@@ -22,6 +22,10 @@
2222
HandleInertiaRequests::class,
2323
AddLinkHeadersForPreloadedAssets::class,
2424
]);
25+
26+
$middleware->alias([
27+
'check.user.subscribed' => \App\Http\Middleware\CheckUserSubscription::class
28+
]);
2529
})
2630
->withExceptions(function (Exceptions $exceptions) {
2731
//

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"require": {
1212
"php": "^8.2",
1313
"inertiajs/inertia-laravel": "^2.0",
14+
"laravel/cashier": "^15.6",
1415
"laravel/framework": "^12.0",
1516
"laravel/reverb": "^1.0",
1617
"laravel/tinker": "^2.10.1",

0 commit comments

Comments
 (0)