Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"require": {
"predis/predis": "^3.0",
"whilesmart/entitlements-cashier": "^0.2.1"
"whilesmart/eloquent-admin": "^0.3",
"whilesmart/eloquent-entitlements": "^2.1",
"whilesmart/entitlements-cashier": "^0.4"
}
}
13 changes: 12 additions & 1 deletion src/CloudServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Trakli\Cloud\Console\SyncPlansCommand;
use Trakli\Cloud\Engagement\BillingMetricProvider;
use Trakli\Cloud\Support\FreePlanFallbackSource;
use Trakli\Cloud\Support\TokenMeterUsage;
use Whilesmart\Admin\Offers\FreePasses;
use Whilesmart\Admin\Offers\StripePromotionCodes;
use Whilesmart\Entitlements\Contracts\Entitlements;
use Whilesmart\Entitlements\Contracts\PlanSource;
use Whilesmart\Entitlements\Contracts\UsageMeter;
Expand All @@ -21,7 +24,7 @@ class CloudServiceProvider extends ServiceProvider
*/
public function register(): void
{
$this->mergeConfigFrom(__DIR__.'/../config/cloudplans.php', 'cloudplans');
$this->mergeConfigFrom(__DIR__ . '/../config/cloudplans.php', 'cloudplans');
}

/**
Expand All @@ -33,6 +36,14 @@ public function boot(): void
$this->registerRoutes();
$this->publishConfig();
$this->enforcePlans();
config(['engagement.providers' => array_values(array_unique([
BillingMetricProvider::class,
...(array) config('engagement.providers', []),
]))]);
config(['admin.offer_providers' => array_values(array_unique(array_merge(
(array) config('admin.offer_providers', []),
[StripePromotionCodes::class, FreePasses::class],
)))]);
app(\Illuminate\Routing\Router::class)->aliasMiddleware('feature', \App\Http\Middleware\RequireFeature::class);

if (config('entitlements-cashier.trial_days') === null) {
Expand Down
81 changes: 81 additions & 0 deletions src/Engagement/BillingMetricProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace Trakli\Cloud\Engagement;

use App\Models\User;
use Illuminate\Support\Collection;
use Whilesmart\Engagement\Contracts\MetricProvider;
use Whilesmart\Engagement\Support\Metric;
use Whilesmart\Engagement\Support\Period;
use Whilesmart\Entitlements\Enums\SubscriptionStatus;
use Whilesmart\Entitlements\Models\Subscription;

class BillingMetricProvider implements MetricProvider
{
public function key(): string
{
return 'billing';
}

public function label(): string
{
return 'Billing';
}

public function metrics(Period $period): array
{
$model = config('entitlements.models.subscription', Subscription::class);
$ownerType = (new User())->getMorphClass();
$latest = $model::query()->selectRaw('MAX(id)')->where('owner_type', $ownerType)->groupBy('owner_id');
$subscriptions = $model::query()->with('plan')->whereIn('id', $latest)->active()
->whereHasMorph('owner', [User::class])->get();
$paying = $subscriptions->where('status', SubscriptionStatus::Active);
$trialing = $subscriptions->where('status', SubscriptionStatus::Trialing);
$plans = $subscriptions->groupBy(fn ($subscription) => $subscription->plan?->name ?? 'Complimentary');

return [
Metric::count('active', 'Paying', $paying->count()),
Metric::count('trialing', 'On trial', $trialing->count()),
Metric::count(
'comped',
'Complimentary users',
$subscriptions->where('status', SubscriptionStatus::Comped)->count(),
),
Metric::count('trials_ending', 'Trials ending in this period', $trialing->filter(
fn ($subscription) => $subscription->current_period_end?->between($period->start, $period->end),
)->count()),
...$this->monthlyValues($paying, 'monthly_plan_value', 'Monthly plan value'),
...$this->monthlyValues($trialing, 'monthly_trial_value', 'Monthly trial plan value'),
Metric::ranking('by_plan', 'Current plans', $plans->map(
fn (Collection $subscribers, string $name) => ['label' => $name, 'value' => $subscribers->count()],
)->sortByDesc('value')->values()->all()),
];
}

private function monthlyValues(Collection $subscriptions, string $key, string $label): array
{
$totals = [];
foreach ($subscriptions as $subscription) {
$price = $subscription->plan?->metadata['price'] ?? null;
if ($price === null) {
continue;
}

$currency = strtoupper($price['currency']);
$amount = $price['amount_cents'] / (($price['interval'] === 'year') ? 12 : 1);
$totals[$currency] = ($totals[$currency] ?? 0) + $amount;
}

ksort($totals);

return array_map(
fn (string $currency) => Metric::sum(
sprintf('%s_%s', $key, strtolower($currency)),
$label,
round($totals[$currency] / 100, 2),
$currency,
),
array_keys($totals),
);
}
}
Loading