Create SyncStripeSubscriptions job and schedule it to run every minute
MODIFY
app/Console/Kernel.php
app/Console/Kernel.php
namespace App\Console;
+use App\Helper;
use App\Jobs\FetchConversionRates;
use App\Jobs\ProcessRecurrings;
use App\Jobs\SendWeeklyReports;
+use App\Jobs\SyncStripeSubscriptions;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
@@ -27,8 +29,9 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule)
{
- // $schedule->command('inspire')
- // ->hourly();
+ $schedule->job(new SyncStripeSubscriptions())->everyMinute()->when(function () {
+ return Helper::arePlansEnabled();
+ });
// Daily
$schedule->job(new ProcessRecurrings())->daily();

ADD
app/Jobs/SyncStripeSubscriptions.php
+<?php
+
+namespace App\Jobs;
+
+use App\Models\User;
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Bus\Dispatchable;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Queue\SerializesModels;
+use Stripe\StripeClient;
+
+class SyncStripeSubscriptions implements ShouldQueue
+{
+ use Dispatchable;
+ use InteractsWithQueue;
+ use Queueable;
+ use SerializesModels;
+
+ public function __construct()
+ {
+ //
+ }
+
+ public function handle()
+ {
+ $stripe = new StripeClient(config('stripe.secret'));
+
+ foreach ($stripe->customers->all() as $stripeCustomer) {
+ $user = User::where('stripe_customer_id', $stripeCustomer->id)->first();
+
+ if (!$user) {
+ continue;
+ }
+
+ $hasPremiumPlan = false;
+
+ foreach ($stripeCustomer->subscriptions as $stripeSubscription) {
+ if ($stripeSubscription->status !== 'active') {
+ continue;
+ }
+
+ $hasPremiumPlan = true;
+ }
+
+ if ($user->plan === 'standard' && $hasPremiumPlan) {
+ $user->update(['plan' => 'premium']);
+ }
+
+ if ($user->plan === 'premium' && !$hasPremiumPlan) {
+ $user->update(['plan' => 'standard']);
+ }
+ }
+ }
+}

Create SyncStripeSubscriptions job and schedule it to run every minute