From 17a0e4b5d5c7d85133a4d1f2c51f2319b5d570bf Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Fri, 19 Jul 2019 17:53:29 +0200 Subject: [PATCH] Refactor active subscription state --- src/Subscription.php | 28 +++++++++++++++++++++++++--- tests/Unit/SubscriptionTest.php | 6 +++--- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/Subscription.php b/src/Subscription.php index fc32910d..b8b64564 100644 --- a/src/Subscription.php +++ b/src/Subscription.php @@ -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'; } /** @@ -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(); } /** diff --git a/tests/Unit/SubscriptionTest.php b/tests/Unit/SubscriptionTest.php index 2b146fee..bcf66f16 100644 --- a/tests/Unit/SubscriptionTest.php +++ b/tests/Unit/SubscriptionTest.php @@ -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() @@ -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()