-
Notifications
You must be signed in to change notification settings - Fork 1
feat(saas): free-plan lookup, expired-trial scan, and downgradeToFree() #141
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
414104c
feat(saas): add PlanRepository::findFree() to resolve the free plan b…
pierredup 633ca88
feat(saas): add SubscriptionRepository::findExpiredTrials()
pierredup 21ada70
feat(saas): add SubscriptionManager::downgradeToFree()
pierredup 29a8ae7
fix(saas): exclude externally-billed trials from findExpiredTrials scan
pierredup 32a9b1b
Add check for subscription status before activation
pierredup 24acaca
Fix CS
pierredup File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/Bundle/Saas/Exception/NoFreePlanConfiguredException.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /* | ||
| * This file is part of SolidWorx Platform project. | ||
| * | ||
| * (c) Pierre du Plessis <open-source@solidworx.co> | ||
| * | ||
| * This source file is subject to the MIT license that is bundled | ||
| * with this source code in the file LICENSE. | ||
| */ | ||
|
|
||
| namespace SolidWorx\Platform\SaasBundle\Exception; | ||
|
|
||
| use RuntimeException; | ||
| use SolidWorx\Platform\SaasBundle\Entity\Subscription; | ||
| use Throwable; | ||
| use function sprintf; | ||
|
|
||
| class NoFreePlanConfiguredException extends RuntimeException | ||
| { | ||
| public function __construct(?Subscription $subscription = null, int $code = 0, ?Throwable $previous = null) | ||
| { | ||
| $message = $subscription instanceof Subscription | ||
| ? sprintf('Cannot downgrade subscription "%s": no active free plan is configured.', $subscription->getId()->toBase58()) | ||
| : 'Cannot downgrade to the free plan: no active free plan is configured.'; | ||
|
|
||
| parent::__construct($message, $code, $previous); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
tests/Bundle/Saas/Subscription/SubscriptionManagerDowngradeTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /* | ||
| * This file is part of SolidWorx Platform project. | ||
| * | ||
| * (c) Pierre du Plessis <open-source@solidworx.co> | ||
| * | ||
| * This source file is subject to the MIT license that is bundled | ||
| * with this source code in the file LICENSE. | ||
| */ | ||
|
|
||
| namespace SolidWorx\Platform\Tests\Bundle\Saas\Subscription; | ||
|
|
||
| use PHPUnit\Framework\Attributes\CoversClass; | ||
| use PHPUnit\Framework\TestCase; | ||
| use SolidWorx\Platform\SaasBundle\Entity\Plan; | ||
| use SolidWorx\Platform\SaasBundle\Entity\Subscription; | ||
| use SolidWorx\Platform\SaasBundle\Enum\SubscriptionStatus; | ||
| use SolidWorx\Platform\SaasBundle\Exception\NoFreePlanConfiguredException; | ||
| use SolidWorx\Platform\SaasBundle\Integration\PaymentIntegrationInterface; | ||
| use SolidWorx\Platform\SaasBundle\Repository\PlanRepositoryInterface; | ||
| use SolidWorx\Platform\SaasBundle\Repository\SubscriptionRepositoryInterface; | ||
| use SolidWorx\Platform\SaasBundle\Subscription\SubscriptionManager; | ||
|
|
||
| #[CoversClass(SubscriptionManager::class)] | ||
| final class SubscriptionManagerDowngradeTest extends TestCase | ||
| { | ||
| public function testDowngradeChangesPlanAndActivatesWhenNotAlreadyFree(): void | ||
| { | ||
| $free = $this->freePlan(); | ||
| $plans = self::createStub(PlanRepositoryInterface::class); | ||
| $plans->method('findFree')->willReturn($free); | ||
|
|
||
| $subs = self::createMock(SubscriptionRepositoryInterface::class); | ||
| $subs->expects(self::atLeastOnce())->method('save'); | ||
|
|
||
| $subscription = (new Subscription())->setPlan($this->paidPlan())->setStatus(SubscriptionStatus::TRIAL); | ||
|
|
||
| $this->manager($plans, $subs)->downgradeToFree($subscription); | ||
|
|
||
| self::assertSame($free, $subscription->getPlan()); | ||
| self::assertSame(SubscriptionStatus::ACTIVE, $subscription->getStatus()); | ||
| } | ||
|
|
||
| public function testDowngradeActivatesWithoutChangingPlanWhenAlreadyFree(): void | ||
| { | ||
| $free = $this->freePlan(); | ||
| $plans = self::createStub(PlanRepositoryInterface::class); | ||
| $plans->method('findFree')->willReturn($free); | ||
|
|
||
| $subs = self::createStub(SubscriptionRepositoryInterface::class); | ||
|
|
||
| $subscription = (new Subscription())->setPlan($free)->setStatus(SubscriptionStatus::TRIAL); | ||
|
|
||
| $this->manager($plans, $subs)->downgradeToFree($subscription); | ||
|
|
||
| self::assertSame($free, $subscription->getPlan()); | ||
| self::assertSame(SubscriptionStatus::ACTIVE, $subscription->getStatus()); | ||
| } | ||
|
|
||
| public function testDowngradeThrowsWhenNoFreePlanConfigured(): void | ||
| { | ||
| $plans = self::createStub(PlanRepositoryInterface::class); | ||
| $plans->method('findFree')->willReturn(null); | ||
|
|
||
| $subscription = (new Subscription())->setPlan($this->paidPlan())->setStatus(SubscriptionStatus::TRIAL); | ||
|
|
||
| $this->expectException(NoFreePlanConfiguredException::class); | ||
|
|
||
| $this->manager($plans, self::createStub(SubscriptionRepositoryInterface::class))->downgradeToFree($subscription); | ||
| } | ||
|
|
||
| private function freePlan(): Plan | ||
| { | ||
| return (new Plan())->setName('Free')->setPlanId('0')->setPrice(0)->setActive(true); | ||
| } | ||
|
|
||
| private function paidPlan(): Plan | ||
| { | ||
| return (new Plan())->setName('Pro')->setPlanId('pro')->setPrice(1900)->setActive(true); | ||
| } | ||
|
|
||
| private function manager(PlanRepositoryInterface $plans, SubscriptionRepositoryInterface $subs): SubscriptionManager | ||
| { | ||
| return new SubscriptionManager($subs, $plans, self::createStub(PaymentIntegrationInterface::class)); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.