From 9c60467cd0a94fff23a181afbb5b4b626c2b0f4c Mon Sep 17 00:00:00 2001 From: Max Heckel Date: Tue, 26 Sep 2023 07:14:34 -0400 Subject: [PATCH 1/4] Check all subscriptions with a given name when verifying if a user is subscribed --- src/Concerns/ManagesSubscriptions.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Concerns/ManagesSubscriptions.php b/src/Concerns/ManagesSubscriptions.php index d3e8af63..0c34289b 100644 --- a/src/Concerns/ManagesSubscriptions.php +++ b/src/Concerns/ManagesSubscriptions.php @@ -135,13 +135,14 @@ public function trialEndsAt($name = 'default') */ public function subscribed($name = 'default', $price = null) { - $subscription = $this->subscription($name); - - if (! $subscription || ! $subscription->valid()) { - return false; + $subscriptions = $this->subscriptions()->where('name', $name)->get(); + foreach ($subscriptions as $subscription){ + if ($subscription->valid() && (!$price || $subscription->hasPrice($price))) { + return true; + } } - return ! $price || $subscription->hasPrice($price); + return false; } /** From a78751d784f60c74f0f1d70bb00432ae49d33b26 Mon Sep 17 00:00:00 2001 From: Max Heckel Date: Tue, 26 Sep 2023 07:25:03 -0400 Subject: [PATCH 2/4] Add test --- tests/Feature/SubscriptionsTest.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/Feature/SubscriptionsTest.php b/tests/Feature/SubscriptionsTest.php index f20a9037..6f85b062 100644 --- a/tests/Feature/SubscriptionsTest.php +++ b/tests/Feature/SubscriptionsTest.php @@ -107,6 +107,24 @@ public static function setUpBeforeClass(): void ])->id; } + + public function test_subscribed_when_multiple_subscriptions_of_same_name_exist(){ + $user = $this->createCustomer('subscriptions_can_be_created'); + + // Create Subscription + $user->newSubscription('main', static::$priceId) + ->withMetadata($metadata = ['order_id' => '8']) + ->create('pm_card_visa'); + + // Create Canceled Subscription + $user->newSubscription('main', static::$priceId) + ->withMetadata($metadata = ['order_id' => '8']) + ->create('pm_card_visa'); + $user->subscription('main')->cancelNow(); + + + $this->assertTrue($user->subscribed('main')); + } public function test_subscriptions_can_be_created() { $user = $this->createCustomer('subscriptions_can_be_created'); From c8bb7cbf5a584e9494e366440bc5a826bcdbca81 Mon Sep 17 00:00:00 2001 From: Max Heckel Date: Tue, 26 Sep 2023 07:30:00 -0400 Subject: [PATCH 3/4] Linting --- src/Concerns/ManagesSubscriptions.php | 4 ++-- tests/Feature/SubscriptionsTest.php | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Concerns/ManagesSubscriptions.php b/src/Concerns/ManagesSubscriptions.php index 0c34289b..b61e1ad9 100644 --- a/src/Concerns/ManagesSubscriptions.php +++ b/src/Concerns/ManagesSubscriptions.php @@ -136,8 +136,8 @@ public function trialEndsAt($name = 'default') public function subscribed($name = 'default', $price = null) { $subscriptions = $this->subscriptions()->where('name', $name)->get(); - foreach ($subscriptions as $subscription){ - if ($subscription->valid() && (!$price || $subscription->hasPrice($price))) { + foreach ($subscriptions as $subscription) { + if ($subscription->valid() && (! $price || $subscription->hasPrice($price))) { return true; } } diff --git a/tests/Feature/SubscriptionsTest.php b/tests/Feature/SubscriptionsTest.php index 6f85b062..90ded0d3 100644 --- a/tests/Feature/SubscriptionsTest.php +++ b/tests/Feature/SubscriptionsTest.php @@ -108,7 +108,8 @@ public static function setUpBeforeClass(): void } - public function test_subscribed_when_multiple_subscriptions_of_same_name_exist(){ + public function test_subscribed_when_multiple_subscriptions_of_same_name_exist() + { $user = $this->createCustomer('subscriptions_can_be_created'); // Create Subscription From cb511e3f66dff0c9a58dc347786fbffd64375d2e Mon Sep 17 00:00:00 2001 From: Max Heckel Date: Tue, 26 Sep 2023 07:32:44 -0400 Subject: [PATCH 4/4] Linting --- tests/Feature/SubscriptionsTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/Feature/SubscriptionsTest.php b/tests/Feature/SubscriptionsTest.php index 90ded0d3..c8c10067 100644 --- a/tests/Feature/SubscriptionsTest.php +++ b/tests/Feature/SubscriptionsTest.php @@ -107,7 +107,6 @@ public static function setUpBeforeClass(): void ])->id; } - public function test_subscribed_when_multiple_subscriptions_of_same_name_exist() { $user = $this->createCustomer('subscriptions_can_be_created'); @@ -123,9 +122,9 @@ public function test_subscribed_when_multiple_subscriptions_of_same_name_exist() ->create('pm_card_visa'); $user->subscription('main')->cancelNow(); - $this->assertTrue($user->subscribed('main')); } + public function test_subscriptions_can_be_created() { $user = $this->createCustomer('subscriptions_can_be_created');