Skip to content

Commit

Permalink
feat: improve setup
Browse files Browse the repository at this point in the history
  • Loading branch information
pushpak1300 committed Dec 22, 2024
1 parent ab6fd2c commit 458a838
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 114 deletions.
8 changes: 4 additions & 4 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ LOG_STACK=single
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

DB_CONNECTION=pgsql
DB_HOST=pgsql
DB_PORT=5432
DB_DATABASE=laravel
DB_CONNECTION=sqlite
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database/database.sqlite
DB_USERNAME=sail
DB_PASSWORD=password
FORWARD_DB_PORT="${DB_PORT}"
Expand Down
18 changes: 10 additions & 8 deletions app/Actions/Fortify/CreateNewUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
use App\Models\User;
use Stripe\Customer;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Str;
use Laravel\Jetstream\Jetstream;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Validator;
use Laravel\Fortify\Contracts\CreatesNewUsers;

Expand Down Expand Up @@ -61,13 +61,15 @@ private function createTeam(User $user): void
*/
private function createCustomer(User $user): void
{
if (Config::get('cashier.billing_enabled')) {
/** @var Customer $stripeCustomer */
$stripeCustomer = $user->createOrGetStripeCustomer();

$user->update([
'stripe_id' => $stripeCustomer?->id,
]);
if (! Config::get('cashier.billing_enabled')) {
return;
}

/** @var Customer $stripeCustomer */
$stripeCustomer = $user->createOrGetStripeCustomer();

$user->update([
'stripe_id' => $stripeCustomer->id,
]);
}
}
18 changes: 15 additions & 3 deletions app/Http/Controllers/SubscriptionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,24 @@ final class SubscriptionController extends Controller
/**
* Redirect authenticated user to Stripe billing portal.
*/
public function index(Request $request): RedirectResponse
public function index(): RedirectResponse
{
if (Config::get('cashier.billing_enabled')) {
return redirect()->route('dashboard');
}

return type(Auth::user())->as(User::class)->redirectToBillingPortal(route('subscriptions.create'));
}

/**
* Display subscription management page with active subscriptions and available plans.
*/
public function create(Request $request): Response
public function create(Request $request): Response|RedirectResponse
{
if (! Config::get('cashier.billing_enabled')) {
return redirect()->route('dashboard');
}

/** @var User $user */
$user = $request->user();

Expand All @@ -50,8 +58,12 @@ public function create(Request $request): Response
*
* @throws NotFoundHttpException If subscription plan not found
*/
public function show(string $subscription): Checkout
public function show(string $subscription): Checkout|RedirectResponse
{
if (! Config::get('cashier.billing_enabled')) {
return redirect()->route('dashboard');
}

/** @var array<int, array<string, mixed>> $subscriptionConfig */
$subscriptionConfig = Config::array('subscriptions.subscriptions');
$subscriptions = collect($subscriptionConfig);
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
],
"setup": [
"@php artisan key:generate",
"touch database/database.sqlite",
"php artisan migrate:fresh --seed --force --ansi",
"bun install",
"bun run build",
Expand Down
2 changes: 1 addition & 1 deletion config/cashier.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,6 @@

'logger' => env('CASHIER_LOGGER'),

'billing_enabled' => (bool) (env('STRIPE_KEY', false) && env('STRIPE_SECRET', false)),
'billing_enabled' => env('STRIPE_KEY', false) && env('STRIPE_SECRET', false),

];
29 changes: 0 additions & 29 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,7 @@ services:
networks:
- sail
depends_on:
- pgsql
- mailpit
pgsql:
image: 'postgres:17'
ports:
- '${FORWARD_DB_PORT:-5432}:5432'
environment:
PGPASSWORD: '${DB_PASSWORD:-secret}'
POSTGRES_DB: '${DB_DATABASE}'
POSTGRES_USER: '${DB_USERNAME}'
POSTGRES_PASSWORD: '${DB_PASSWORD:-secret}'
volumes:
- 'sail-pgsql:/var/lib/postgresql/data'
- './docker/pgsql/create-testing-database.sql:/docker-entrypoint-initdb.d/10-create-testing-database.sql'
networks:
- sail
healthcheck:
test:
- CMD
- pg_isready
- '-q'
- '-d'
- '${DB_DATABASE}'
- '-U'
- '${DB_USERNAME}'
retries: 3
timeout: 5s
mailpit:
image: 'axllent/mailpit:latest'
ports:
Expand All @@ -62,6 +36,3 @@ services:
networks:
sail:
driver: bridge
volumes:
sail-pgsql:
driver: local
72 changes: 9 additions & 63 deletions resources/views/scribe/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
</span>
</a>
<div class="tocify-wrapper">
<img src="img/rocker-dark.png" alt="logo" class="logo" style="padding-top: 10px;" width="100%"/>

<div class="lang-selector">
<button type="button" class="lang-button" data-language-name="bash">bash</button>
Expand Down Expand Up @@ -97,7 +98,7 @@
</ul>

<ul class="toc-footer" id="last-updated">
<li>Last updated: December 8, 2024</li>
<li>Last updated: December 21, 2024</li>
</ul>
</div>

Expand Down Expand Up @@ -162,7 +163,7 @@

<span id="example-responses-GETapi-user">
<blockquote>
<p>Example response (200):</p>
<p>Example response (500):</p>
</blockquote>
<details class="annotation">
<summary style="cursor: pointer;">
Expand All @@ -174,48 +175,7 @@
</code></pre></details> <pre>

<code class="language-json" style="max-height: 300px;">{
&quot;current_page&quot;: 1,
&quot;data&quot;: [
{
&quot;id&quot;: 1,
&quot;name&quot;: &quot;Test User&quot;,
&quot;email&quot;: &quot;test@example.com&quot;,
&quot;email_verified_at&quot;: &quot;2024-12-07T23:39:16.000000Z&quot;,
&quot;current_team_id&quot;: 1,
&quot;profile_photo_path&quot;: null,
&quot;created_at&quot;: &quot;2024-12-07T23:39:16.000000Z&quot;,
&quot;updated_at&quot;: &quot;2024-12-07T23:39:39.000000Z&quot;,
&quot;deleted_at&quot;: null,
&quot;two_factor_confirmed_at&quot;: null
}
],
&quot;first_page_url&quot;: &quot;http://localhost:8010/api/user?page=1&quot;,
&quot;from&quot;: 1,
&quot;last_page&quot;: 1,
&quot;last_page_url&quot;: &quot;http://localhost:8010/api/user?page=1&quot;,
&quot;links&quot;: [
{
&quot;url&quot;: null,
&quot;label&quot;: &quot;&amp;laquo; Previous&quot;,
&quot;active&quot;: false
},
{
&quot;url&quot;: &quot;http://localhost:8010/api/user?page=1&quot;,
&quot;label&quot;: &quot;1&quot;,
&quot;active&quot;: true
},
{
&quot;url&quot;: null,
&quot;label&quot;: &quot;Next &amp;raquo;&quot;,
&quot;active&quot;: false
}
],
&quot;next_page_url&quot;: null,
&quot;path&quot;: &quot;http://localhost:8010/api/user&quot;,
&quot;per_page&quot;: 15,
&quot;prev_page_url&quot;: null,
&quot;to&quot;: 1,
&quot;total&quot;: 1
&quot;message&quot;: &quot;Server Error&quot;
}</code>
</pre>
</span>
Expand Down Expand Up @@ -426,7 +386,6 @@
<h2 id="user-management-GETapi-user--id-">Get a specific user by ID.</h2>

<p>
<small class="badge badge-darkred">requires authentication</small>
</p>


Expand All @@ -438,7 +397,6 @@
<div class="bash-example">
<pre><code class="language-bash">curl --request GET \
--get "http://localhost:8010/api/user/1" \
--header "Authorization: Bearer {YOUR_AUTH_KEY}" \
--header "Content-Type: application/json" \
--header "Accept: application/json"</code></pre></div>

Expand All @@ -449,7 +407,6 @@
);

const headers = {
"Authorization": "Bearer {YOUR_AUTH_KEY}",
"Content-Type": "application/json",
"Accept": "application/json",
};
Expand Down Expand Up @@ -478,11 +435,11 @@
&quot;id&quot;: 1,
&quot;name&quot;: &quot;Test User&quot;,
&quot;email&quot;: &quot;test@example.com&quot;,
&quot;email_verified_at&quot;: &quot;2024-12-07T23:39:16.000000Z&quot;,
&quot;current_team_id&quot;: 1,
&quot;email_verified_at&quot;: &quot;2024-12-21T12:58:50.000000Z&quot;,
&quot;current_team_id&quot;: null,
&quot;profile_photo_path&quot;: null,
&quot;created_at&quot;: &quot;2024-12-07T23:39:16.000000Z&quot;,
&quot;updated_at&quot;: &quot;2024-12-07T23:39:39.000000Z&quot;,
&quot;created_at&quot;: &quot;2024-12-21T12:58:51.000000Z&quot;,
&quot;updated_at&quot;: &quot;2024-12-21T12:58:51.000000Z&quot;,
&quot;deleted_at&quot;: null,
&quot;two_factor_confirmed_at&quot;: null
}</code>
Expand All @@ -505,7 +462,7 @@
</span>
<form id="form-GETapi-user--id-" data-method="GET"
data-path="api/user/{id}"
data-authed="1"
data-authed="0"
data-hasfiles="0"
data-isarraybody="0"
autocomplete="off"
Expand Down Expand Up @@ -536,17 +493,6 @@
</p>
<h4 class="fancy-heading-panel"><b>Headers</b></h4>
<div style="padding-left: 28px; clear: unset;">
<b style="line-height: 2;"><code>Authorization</code></b>&nbsp;&nbsp;
&nbsp;
&nbsp;
<input type="text" style="display: none"
name="Authorization" class="auth-value" data-endpoint="GETapi-user--id-"
value="Bearer {YOUR_AUTH_KEY}"
data-component="header">
<br>
<p>Example: <code>Bearer {YOUR_AUTH_KEY}</code></p>
</div>
<div style="padding-left: 28px; clear: unset;">
<b style="line-height: 2;"><code>Content-Type</code></b>&nbsp;&nbsp;
&nbsp;
&nbsp;
Expand Down
9 changes: 3 additions & 6 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\User\OauthController;
use App\Http\Controllers\SubscriptionController;
use Illuminate\Support\Facades\Config;

Route::get('/', [WelcomeController::class, 'home'])->name('home');

Expand All @@ -23,9 +22,7 @@

Route::get('/chat', [ChatController::class, 'index'])->name('chat.index');

if (Config::get('cashier.billing_enabled')) {
Route::resource('/subscriptions', SubscriptionController::class)
->names('subscriptions')
->only(['index', 'create', 'store', 'show']);
}
Route::resource('/subscriptions', SubscriptionController::class)
->names('subscriptions')
->only(['index', 'create', 'store', 'show']);
});

0 comments on commit 458a838

Please sign in to comment.