diff --git a/src/Concerns/ManagesSubscriptions.php b/src/Concerns/ManagesSubscriptions.php index d3e8af63..b61e1ad9 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; } /** diff --git a/tests/Feature/SubscriptionsTest.php b/tests/Feature/SubscriptionsTest.php index f20a9037..c8c10067 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');