Forked from garygreen/2018_02_13_142413_add_renews_at_column_to_subscriptions.php
Last active
January 31, 2024 07:55
-
-
Save insign/832b56a3988b912ed809de816d2379c0 to your computer and use it in GitHub Desktop.
Revisions
-
insign revised this gist
Sep 23, 2019 . 1 changed file with 79 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,79 @@ <?php // Before use this file, read the steps about observers at https://laravel.com/docs/eloquent#observers // And remember, this file is just a helper, before the properly command run, to update the dates, which maybe should run every day at least. namespace App\Observers; use Carbon\Carbon; use Laravel\Cashier\Subscription; class SubscriptionsObserver { /** * Handle the laravel cashier subscription "created" event. * * @param Subscription $subscription * * @return void */ public function created(Subscription $subscription) { if ($subscription->stripe_plan == 'yearly') { $subscription->renews_at = Carbon::now()->addYear(); } elseif ($subscription->stripe_plan == 'monthly') { $subscription->renews_at = Carbon::now()->addMonth(); } $subscription->save(); } /** * Handle the laravel cashier subscription "updated" event. * * @param Subscription $subscription * * @return void */ public function updated(Subscription $subscription) { // } /** * Handle the laravel cashier subscription "deleted" event. * * @param Subscription $subscription * * @return void */ public function deleted(Subscription $subscription) { // } /** * Handle the laravel cashier subscription "restored" event. * * @param Subscription $subscription * * @return void */ public function restored(Subscription $subscription) { // } /** * Handle the laravel cashier subscription "force deleted" event. * * @param Subscription $subscription * * @return void */ public function forceDeleted(Subscription $subscription) { // } } -
garygreen revised this gist
Feb 13, 2018 . 1 changed file with 30 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,30 @@ <?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() { // } } -
garygreen renamed this gist
Feb 13, 2018 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
garygreen created this gist
Feb 13, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,78 @@ <?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++; } } }