diff --git a/src/Billable.php b/src/Billable.php index 7663185..0db1279 100644 --- a/src/Billable.php +++ b/src/Billable.php @@ -259,6 +259,18 @@ public function subscribed($subscription = 'default', $plan = null) $subscription->plan === $plan; } + /** + * Determine if the billable model has any active subscription. + * + * @return bool + */ + public function hasActiveSubscription(): bool + { + return $this + ->subscriptions() + ->whereActive() + ->exists(); + } /** * @param $plans * @param string $subscription diff --git a/tests/BillableTest.php b/tests/BillableTest.php index fd87070..54e3acf 100644 --- a/tests/BillableTest.php +++ b/tests/BillableTest.php @@ -4,6 +4,7 @@ use Illuminate\Support\Facades\Event; use Laravel\Cashier\Coupon\RedeemedCouponCollection; +use Laravel\Cashier\Subscription; use Laravel\Cashier\Tests\Database\Factories\OrderFactory; use Laravel\Cashier\Tests\Database\Factories\RedeemedCouponFactory; use Laravel\Cashier\Events\MandateClearedFromBillable; @@ -11,6 +12,7 @@ use Laravel\Cashier\Order\Invoice; use Laravel\Cashier\SubscriptionBuilder\FirstPaymentSubscriptionBuilder; use Laravel\Cashier\SubscriptionBuilder\MandatedSubscriptionBuilder; +use Laravel\Cashier\Tests\Database\Factories\SubscriptionFactory; use Laravel\Cashier\Tests\Fixtures\User; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; @@ -286,4 +288,29 @@ public function canFindInvoiceByOrderIdUsingFindInvoiceByOrderIdOrFail() $this->assertInstanceOf(Invoice::class, $invoice); $this->assertEquals('2018-0000-0002', $invoice->id()); } + + /** @test */ + public function testHasActiveSubscription() + { + $this->withConfiguredPlans(); + $this->withMockedCouponRepository(); // 'test-coupon' + $this->withMockedGetMollieCustomer(3); + $this->withMockedGetMollieMandateAccepted(3); + + $user = $this->getMandatedUser(true, [ + 'mollie_mandate_id' => 'mdt_unique_mandate_id', + 'mollie_customer_id' => 'cst_unique_customer_id', + ]); + $user->newSubscription('default', 'monthly-10-1')->create(); + + $this->assertTrue($user->hasActiveSubscription()); + } + + /** @test */ + public function testHasActiveSubscriptionsFalse() + { + $user = User::factory()->create(); + + $this->assertFalse($user->hasActiveSubscription()); + } }