Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save garygreen/fb4dc0288e2c57f9af015968ff7019bb to your computer and use it in GitHub Desktop.
Save garygreen/fb4dc0288e2c57f9af015968ff7019bb to your computer and use it in GitHub Desktop.
Sync Stripe Renewal Date for all subscriptions - Laravel Console Command
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddRenewsAtColumnToSubscriptions extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('subscriptions', function($table) {
$table->timestamp('renews_at')->nullable()->index()->after('trial_ends_at');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
<?php
namespace App\Console\Commands;
use DB;
use Exception;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Laravel\Cashier\Subscription;
class StripeSyncRenewals extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'stripe:sync-renewals';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Updates the renews_at column on stripe subscriptions to reflect the end of the current billing period.';
protected $updatedCount = 0;
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
\Stripe\Stripe::setApiKey(config('services.stripe.secret'));
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$apiOptions = ['limit' => 100];
while ($response = \Stripe\Subscription::all($apiOptions)) {
foreach ($response->data as $subscription) {
$this->updateSubscription($subscription);
}
if (! $response->has_more) {
break;
}
$apiOptions['starting_after'] = $subscription->id;
}
$this->info('Successfully updated ' . $this->updatedCount . ' subscriptions.');
}
protected function updateSubscription($subscription)
{
$updated = DB::table('subscriptions')
->where('stripe_id', $subscription->id)
->update([
'renews_at' => Carbon::createFromTimestamp($subscription->current_period_end),
]);
if ($updated) {
$this->updatedCount++;
}
}
}
@insign
Copy link

insign commented Sep 23, 2019

Thank you for this snippet. The problem is how to update this for every new costumer.

I added a SubscriptionObserver here

@sahilkashyap64
Copy link

sahilkashyap64 commented Jul 2, 2020

I added this in routes.php

Route::get('/stripesync', function (Request $request) {
    Artisan::call('stripe:sync-renewals');
    
    return redirect('admin/subscriptions')->with('info',Artisan::output());
 });

added this in my blade file

<div class="text-center">
           <a  class=" btn btn-xs btn-warning" href="{{ url('stripesync') }}">Stripe Expiry Date Sync</a>
</div>

@awesome1128
Copy link

$apiOptions = ['limit' => 100];
This means that you update only 100 subscribers, right?

@vldmitrofanov
Copy link

$apiOptions = ['limit' => 100]; This means that you update only 100 subscribers, right?

nope, it takes a batch of 100, then if there are no more (! $response->has_more) then it stops the loop, otherwise sets ID from which on it will be pulling out the next batch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment