Show monthly price of subscription and date of next payment on billing page
ADD
app/Actions/FetchStripeSubscriptionAction.php
+<?php
+
+namespace App\Actions;
+
+use App\Exceptions\UserNotFoundException;
+use App\Exceptions\UserStripelessException;
+use App\Models\User;
+use Stripe\StripeClient;
+use Stripe\Subscription;
+
+class FetchStripeSubscriptionAction
+{
+ public function execute(int $userId): ?Subscription
+ {
+ $user = User::find($userId);
+
+ if (!$user) {
+ throw new UserNotFoundException();
+ }
+
+ if (!$user->stripe_customer_id) {
+ throw new UserStripelessException();
+ }
+
+ $stripe = new StripeClient(config('stripe.secret'));
+
+ $stripeCustomer = $stripe->customers->retrieve($user->stripe_customer_id);
+
+ return $stripeCustomer->subscriptions->first();
+ }
+}

MODIFY
app/Http/Controllers/SettingsController.php
app/Http/Controllers/SettingsController.php
@@ -4,6 +4,8 @@ namespace App\Http\Controllers;
use App\Actions\CreateStripeCheckoutAction;
use App\Actions\CreateStripeCustomerAction;
+use App\Actions\FetchStripeSubscriptionAction;
+use App\Exceptions\UserStripelessException;
use Illuminate\Http\Request;
use App\Mail\PasswordChanged;
use App\Models\Currency;
@@ -119,8 +121,18 @@ class SettingsController extends Controller
public function getBilling(Request $request)
{
$user = $request->user();
+ $stripeSubscription = null;
- return view('settings.billing', ['user' => $user]);
+ try {
+ $stripeSubscription = (new FetchStripeSubscriptionAction())->execute($user->id);
+ } catch (UserStripelessException $e) {
+ // Don't worry, $stripeSubscription will simply be `null`
+ }
+
+ return view('settings.billing', [
+ 'user' => $user,
+ 'stripeSubscription' => $stripeSubscription
+ ]);
}
public function postUpgrade(Request $request)

Show monthly price of subscription and date of next payment on billing page