Repository navigation
feat(billing): Add shared space subscriptions #17
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
8 commits
Select commit
Hold shift + click to select a range
5c70eb3
feat: Configure cloud access for shared workspaces
nfebe e7314ee
feat: Sync plan features without changing prices
nfebe 56738bf
feat(billing): Add shared workspace subscriptions
nfebe 49f1f7f
fix(billing): Use the premium checkout adapter
nfebe 5accd39
refactor(billing): Centralize checkout redirects
nfebe bd400b8
test(ui): Check shared billing against the matching dashboard
nfebe f4d808e
fix(billing): Show cancelled workspace checkout returns
nfebe c0f79b5
feat(billing): Refine shared space plans and notices
nfebe 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <?php | ||
|
|
||
| return [ | ||
| 'pricing' => Trakli\Cloud\Billing\BasePlusMemberPricing::class, | ||
| 'currency' => 'usd', | ||
| 'return_url' => rtrim(env('FRONTEND_URL', 'http://localhost:3001'), '/'), | ||
| 'base_amount_cents' => (int) env('CLOUD_GROUP_BASE_AMOUNT_CENTS', 1200), | ||
| 'included_members' => (int) env('CLOUD_GROUP_INCLUDED_MEMBERS', 2), | ||
| 'additional_member_amount_cents' => (int) env('CLOUD_GROUP_ADDITIONAL_MEMBER_AMOUNT_CENTS', 500), | ||
| 'max_members' => (int) env('CLOUD_GROUP_MAX_MEMBERS', 100), | ||
| 'feature_keys' => ['shared_workspaces', 'plaid', 'bank_connections'], | ||
| 'member_feature_keys' => ['plaid', 'bank_connections'], | ||
| 'limits' => ['max_wallets' => null, 'max_categories' => null], | ||
| ]; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| <?php | ||
|
|
||
| namespace Trakli\Cloud\Billing; | ||
|
|
||
| use InvalidArgumentException; | ||
| use Trakli\Cloud\Contracts\WorkspacePricing; | ||
|
|
||
| class BasePlusMemberPricing implements WorkspacePricing | ||
| { | ||
| public function quote(int $members): array | ||
| { | ||
| $config = config('workspace-billing'); | ||
| $included = (int) $config['included_members']; | ||
| $base = (int) $config['base_amount_cents']; | ||
| $additional = (int) $config['additional_member_amount_cents']; | ||
| $max = (int) $config['max_members']; | ||
| if ($included < 1 || $base < 1 || $additional < 0 || $max < $included) { | ||
| throw new InvalidArgumentException('Invalid workspace pricing configuration.'); | ||
| } | ||
| if ($members < $included || $members > $max) { | ||
| throw new InvalidArgumentException("Choose between {$included} and {$max} members, including the owner."); | ||
| } | ||
|
|
||
| return [ | ||
| 'currency' => $config['currency'], | ||
| 'interval' => 'month', | ||
| 'members' => $members, | ||
| 'included_members' => $included, | ||
| 'max_members' => $max, | ||
| 'base_amount_cents' => $base, | ||
| 'additional_member_amount_cents' => $additional, | ||
| 'amount_cents' => $base + max(0, $members - $included) * $additional, | ||
| ]; | ||
| } | ||
| } |
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,29 @@ | ||
| <?php | ||
|
|
||
| namespace Trakli\Cloud\Billing; | ||
|
|
||
| use Illuminate\Database\Eloquent\Model; | ||
| use Whilesmart\EntitlementsCashier\CashierBillingProvider; | ||
| use Whilesmart\Entitlements\Models\Plan; | ||
|
|
||
| class WorkspaceCheckoutProvider extends CashierBillingProvider | ||
| { | ||
| public function createCheckout(Model $owner, Plan $plan): string | ||
| { | ||
| $previous = ['entitlements-cashier.success_url' => config('entitlements-cashier.success_url'), | ||
| 'entitlements-cashier.cancel_url' => config('entitlements-cashier.cancel_url')]; | ||
| $url = rtrim(config('workspace-billing.return_url'), '/').'/workspaces/'.$owner->slug; | ||
| config(['entitlements-cashier.success_url' => $url.'?checkout=success', | ||
| 'entitlements-cashier.cancel_url' => $url.'?checkout=cancelled']); | ||
| try { | ||
| return parent::createCheckout($owner, $plan); | ||
| } finally { | ||
| config($previous); | ||
| } | ||
| } | ||
|
|
||
| protected function trialDays(): ?int | ||
| { | ||
| return null; | ||
| } | ||
| } | ||
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,16 @@ | ||
| <?php | ||
|
|
||
| namespace Trakli\Cloud\Billing; | ||
|
|
||
| use App\Models\Workspace; | ||
| use App\Services\WorkspaceFinance; | ||
|
|
||
| class WorkspaceDeletionGuard | ||
| { | ||
| public function deleting(Workspace $workspace): void | ||
| { | ||
| if (! app(WorkspaceSubscription::class)->canDelete($workspace)) { | ||
| app(WorkspaceFinance::class)->deny('End the workspace subscription and cancel any pending checkout before deleting this space.', 409); | ||
| } | ||
| } | ||
| } |
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,29 @@ | ||
| <?php | ||
|
|
||
| namespace Trakli\Cloud\Billing; | ||
|
|
||
| use Illuminate\Contracts\Auth\Authenticatable; | ||
| use Whilesmart\Entitlements\Contracts\Entitlements; | ||
| use Whilesmart\Workspaces\Contracts\InvitationGuard; | ||
| use Whilesmart\Workspaces\Models\Workspace; | ||
| use Whilesmart\Workspaces\Models\WorkspaceInvitation; | ||
|
|
||
| class WorkspaceInvitationGuard implements InvitationGuard | ||
| { | ||
| public function beforeInvite(Workspace $workspace, string $email, string $role, ?Authenticatable $inviter): void | ||
| { | ||
| abort_unless(app(Entitlements::class)->allows($workspace, 'shared_workspaces'), 402, 'Activate the workspace subscription before inviting members.'); | ||
| } | ||
|
|
||
| public function beforeAccept(WorkspaceInvitation $invitation, Authenticatable $user): void | ||
| { | ||
| $model = config('workspaces.workspace_model'); | ||
| $workspace = $model::whereKey($invitation->workspace_id)->lockForUpdate()->firstOrFail(); | ||
| abort_unless(app(Entitlements::class)->allows($workspace, 'shared_workspaces'), 402, 'The workspace subscription is inactive.'); | ||
| if ($user->belongsToWorkspace($workspace)) { | ||
| return; | ||
| } | ||
| $capacity = app(Entitlements::class)->limit($workspace, 'workspace_members'); | ||
| abort_if($workspace->members()->distinct()->count('users.id') >= $capacity, 402, 'The owner must add a member to the subscription before you can join.'); | ||
| } | ||
| } |
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,60 @@ | ||
| <?php | ||
|
|
||
| namespace Trakli\Cloud\Billing; | ||
|
|
||
| use App\Models\User; | ||
| use App\Models\Workspace; | ||
| use Illuminate\Database\Eloquent\Model; | ||
| use Whilesmart\Entitlements\Support\AccessResult; | ||
| use Whilesmart\Entitlements\Support\PlanEntitlements; | ||
|
|
||
| class WorkspaceMemberEntitlements extends PlanEntitlements | ||
| { | ||
| public function check(?Model $owner, string $feature): AccessResult | ||
| { | ||
| if ($feature === 'shared_workspaces') { | ||
| if ($owner instanceof User) { | ||
| return AccessResult::allow($feature); | ||
| } | ||
| if ($owner instanceof Workspace) { | ||
| $capacity = app(WorkspaceSubscription::class)->capacity($owner); | ||
|
|
||
| return $capacity > 0 && $owner->members()->distinct()->count('users.id') <= $capacity | ||
| ? parent::check($owner, $feature) | ||
| : AccessResult::deny($feature, 'workspace_subscription_inactive'); | ||
| } | ||
| } | ||
| $result = parent::check($owner, $feature); | ||
| if ($result->allowed || ! in_array($feature, config('workspace-billing.member_feature_keys', []), true)) { | ||
| return $result; | ||
| } | ||
|
|
||
| return $this->hasWorkspaceAccess($owner, $feature) ? AccessResult::allow($feature) : $result; | ||
| } | ||
|
|
||
| public function limit(?Model $owner, string $key): ?int | ||
| { | ||
| if ($owner instanceof Workspace && $key === 'workspace_members') { | ||
| return app(WorkspaceSubscription::class)->capacity($owner); | ||
| } | ||
| if (array_key_exists($key, config('workspace-billing.limits', [])) && $this->hasWorkspaceAccess($owner)) { | ||
| return config('workspace-billing.limits.'.$key); | ||
| } | ||
|
|
||
| return parent::limit($owner, $key); | ||
| } | ||
|
|
||
| private function hasWorkspaceAccess(?Model $owner, ?string $feature = null): bool | ||
| { | ||
| if (! $owner instanceof User) { | ||
| return false; | ||
| } | ||
| foreach ($owner->workspaces()->where('is_active', true)->where('is_personal', false)->distinct()->get() as $workspace) { | ||
| if ($this->allows($workspace, 'shared_workspaces') && ($feature === null || parent::check($workspace, $feature)->allowed)) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } |
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,42 @@ | ||
| <?php | ||
|
|
||
| namespace Trakli\Cloud\Billing; | ||
|
|
||
| use App\Models\Workspace; | ||
| use Whilesmart\Entitlements\Support\PlanEntitlements; | ||
| use Whilesmart\Entitlements\Models\Plan; | ||
| use Whilesmart\EntitlementsCashier\Models\BillingProfile; | ||
|
|
||
| class WorkspaceSubscription | ||
| { | ||
| public function canDelete(Workspace $workspace): bool | ||
| { | ||
| $model = config('entitlements-cashier.billing_profile_model', BillingProfile::class); | ||
| $profile = $model::where('owner_type', $workspace->getMorphClass())->where('owner_id', $workspace->id)->first(); | ||
| $subscription = $profile?->subscription('default'); | ||
|
|
||
| return (! $subscription || $subscription->ended()) | ||
| && ! ($profile?->checkout_expires_at?->isFuture() && filled($profile->checkout_parameters)); | ||
| } | ||
|
|
||
| public function capacity(Workspace $workspace): int | ||
| { | ||
| $entitlements = app(PlanEntitlements::class); | ||
| if (! $workspace->is_active || ! $entitlements->allows($workspace, 'shared_workspaces')) { | ||
| return 0; | ||
| } | ||
| $model = config('entitlements-cashier.billing_profile_model', BillingProfile::class); | ||
| $profile = $model::where('owner_type', $workspace->getMorphClass())->where('owner_id', $workspace->id)->first(); | ||
| $subscription = $profile?->subscription('default'); | ||
| if (! $subscription || ! in_array($subscription->stripe_status, ['active', 'trialing'], true) || $subscription->ended()) { | ||
| return 0; | ||
| } | ||
| $planModel = config('entitlements.models.plan', Plan::class); | ||
| $paidPlan = $planModel::where('provider_price_id', $subscription->stripe_price)->first(); | ||
| if (! ($paidPlan?->features['shared_workspaces'] ?? false)) { | ||
| return 0; | ||
| } | ||
|
|
||
| return min((int) ($paidPlan->limits['workspace_members'] ?? 0), (int) $entitlements->limit($workspace, 'workspace_members')); | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <?php | ||
|
|
||
| namespace Trakli\Cloud\Contracts; | ||
|
|
||
| interface WorkspacePricing | ||
| { | ||
| public function quote(int $members): array; | ||
| } |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The cancel return emits
?checkout=cancel, but the sharedcheckoutNoticehelper andBillingSettingsonly recognisesuccess/cancelled, andWorkspaceBilling.vuehandles onlysuccess. A user who abandons workspace checkout therefore gets no confirmation that nothing changed. Emitcancelledhere and surface it viacheckoutNoticeinWorkspaceBilling.vue.