Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.0] Refactor active subscription state #712

Merged
merged 1 commit into from
Jul 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions src/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ public function scopePastDue($query)
*/
public function active()
{
return (is_null($this->ends_at) || $this->onGracePeriod()) && ! $this->incomplete();
return (is_null($this->ends_at) || $this->onGracePeriod()) &&
$this->stripe_status !== 'incomplete' &&
$this->stripe_status !== 'incomplete_expired' &&
$this->stripe_status !== 'past_due' &&
$this->stripe_status !== 'unpaid';
}

/**
Expand All @@ -135,11 +139,29 @@ public function active()
*/
public function scopeActive($query)
{
$query->whereNull('ends_at')
->where('stripe_status', '!=', 'incomplete')
$query->where(function ($query) {
$query->whereNull('ends_at')
->orWhere(function ($query) {
$query->onGracePeriod();
});
})->where('stripe_status', '!=', 'incomplete')
->where('stripe_status', '!=', 'incomplete_expired')
->where('stripe_status', '!=', 'past_due')
->where('stripe_status', '!=', 'unpaid');
}

/**
* Sync the Stripe status of the subscription.
*
* @return void
*/
public function syncStripeStatus()
{
$subscription = $this->asStripeSubscription();

$this->status = $subscription->status;

$this->save();
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/SubscriptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function test_we_can_check_if_a_subscription_is_past_due()

$this->assertFalse($subscription->incomplete());
$this->assertTrue($subscription->pastDue());
$this->assertTrue($subscription->active());
$this->assertFalse($subscription->active());
}

public function test_we_can_check_if_a_subscription_is_active()
Expand All @@ -42,11 +42,11 @@ public function test_an_incomplete_subscription_is_not_valid()
$this->assertFalse($subscription->valid());
}

public function test_a_past_due_subscription_is_valid()
public function test_a_past_due_subscription_is_not_valid()
{
$subscription = new Subscription(['stripe_status' => 'past_due']);

$this->assertTrue($subscription->valid());
$this->assertFalse($subscription->valid());
}

public function test_an_active_subscription_is_valid()
Expand Down