feat: implement comprehensive multi-provider payment processing system

- Add unified payment provider architecture with contract-based design
  - Implement 6 payment providers: Stripe, Lemon Squeezy, Polar, Oxapay, Crypto, Activation Keys
  - Create subscription management with lifecycle handling (create, cancel, pause, resume, update)
  - Add coupon system with usage tracking and trial extensions
  - Build Filament admin resources for payment providers, subscriptions, coupons, and trials
  - Implement payment orchestration service with provider registry and configuration management
  - Add comprehensive payment logging and webhook handling for all providers
  - Create customer analytics dashboard with revenue, churn, and lifetime value metrics
  - Add subscription migration service for provider switching
  - Include extensive test coverage for all payment functionality
This commit is contained in:
idevakk
2025-11-19 09:37:00 -08:00
parent 0560016f33
commit 27ac13948c
83 changed files with 15613 additions and 103 deletions

View File

@@ -0,0 +1,150 @@
<?php
namespace App\Filament\Widgets;
use App\Models\Subscription;
use Filament\Widgets\ChartWidget;
use Illuminate\Support\Facades\DB;
class ChurnAnalysis extends ChartWidget
{
protected static ?int $sort = 3;
protected int|string|array $columnSpan = 'full';
public function getHeading(): string
{
return 'Churn Analysis';
}
protected function getData(): array
{
$monthlyChurn = $this->getMonthlyChurnRate();
$churnByProvider = $this->getChurnByProvider();
$churnByPlan = $this->getChurnByPlan();
return [
'datasets' => [
[
'label' => 'Monthly Churn Rate (%)',
'data' => array_values($monthlyChurn),
'borderColor' => 'rgba(239, 68, 68, 1)',
'backgroundColor' => 'rgba(239, 68, 68, 0.1)',
'fill' => true,
'tension' => 0.4,
],
],
'labels' => array_keys($monthlyChurn),
];
}
protected function getType(): string
{
return 'line';
}
protected function getOptions(): array
{
return [
'responsive' => true,
'plugins' => [
'legend' => [
'position' => 'top',
],
'tooltip' => [
'callbacks' => [
'label' => 'function(context) {
let label = context.dataset.label || "";
if (label) {
label += ": ";
}
if (context.parsed.y !== null) {
label += context.parsed.y.toFixed(1) + "%";
}
return label;
}',
],
],
],
'scales' => [
'y' => [
'beginAtZero' => true,
'max' => 20,
'ticks' => [
'callback' => 'function(value) {
return value + "%";
}',
],
],
],
];
}
private function getMonthlyChurnRate(): array
{
$churnData = [];
for ($i = 5; $i >= 0; $i--) {
$month = now()->subMonths($i);
$monthStart = $month->copy()->startOfMonth();
$monthEnd = $month->copy()->endOfMonth();
// Active subscriptions at the beginning of the month
$activeAtStart = Subscription::query()
->where('status', 'active')
->where('created_at', '<', $monthStart)
->count();
// Subscriptions cancelled during the month
$cancelledDuringMonth = Subscription::query()
->where('status', 'cancelled')
->whereBetween('cancelled_at', [$monthStart, $monthEnd])
->count();
// Calculate churn rate
$churnRate = $activeAtStart > 0 ? ($cancelledDuringMonth / $activeAtStart) * 100 : 0;
$churnData[$month->format('M Y')] = round($churnRate, 2);
}
return $churnData;
}
private function getChurnByProvider(): array
{
return Subscription::query()
->select(
'provider',
DB::raw('COUNT(CASE WHEN status = "cancelled" THEN 1 END) as cancelled'),
DB::raw('COUNT(*) as total'),
DB::raw('(CAST(COUNT(CASE WHEN status = "cancelled" THEN 1 END) AS REAL) * 100.0 / COUNT(*)) as churn_rate')
)
->groupBy('provider')
->orderBy('churn_rate', 'desc')
->get()
->mapWithKeys(function ($item) {
return [$item->provider => round($item->churn_rate, 2)];
})
->toArray();
}
private function getChurnByPlan(): array
{
return Subscription::query()
->select(
'plans.name as plan_name',
DB::raw('COUNT(CASE WHEN subscriptions.status = "cancelled" THEN 1 END) as cancelled'),
DB::raw('COUNT(*) as total'),
DB::raw('(CAST(COUNT(CASE WHEN subscriptions.status = "cancelled" THEN 1 END) AS REAL) * 100.0 / COUNT(*)) as churn_rate')
)
->join('plans', 'subscriptions.plan_id', '=', 'plans.id')
->groupBy('plans.id', 'plans.name')
->orderBy('churn_rate', 'desc')
->limit(10)
->get()
->mapWithKeys(function ($item) {
return [$item->plan_name => round($item->churn_rate, 2)];
})
->toArray();
}
}

View File

@@ -0,0 +1,139 @@
<?php
namespace App\Filament\Widgets;
use App\Models\Coupon;
use App\Models\CouponUsage;
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;
use Illuminate\Support\Facades\DB;
class CouponPerformanceMetrics extends BaseWidget
{
protected static ?int $sort = 3;
protected function getStats(): array
{
$totalCoupons = Coupon::count();
$activeCoupons = Coupon::where('is_active', true)->count();
$totalUsages = CouponUsage::count();
$totalDiscount = CouponUsage::sum('discount_amount');
$conversionRate = $this->getCouponConversionRate();
$avgDiscountValue = $totalUsages > 0 ? $totalDiscount / $totalUsages : 0;
$topPerformingCoupon = $this->getTopPerformingCoupon();
$monthlyUsage = $this->getMonthlyUsage();
return [
Stat::make('Total Coupons', $totalCoupons)
->description($activeCoupons.' active')
->descriptionIcon('heroicon-o-ticket')
->color('primary'),
Stat::make('Total Usages', $totalUsages)
->description($this->getUsageGrowthRate())
->descriptionIcon($this->getUsageGrowthIcon())
->color($this->getUsageGrowthColor()),
Stat::make('Total Discount Given', '$'.number_format($totalDiscount, 2))
->description('Total value of discounts')
->descriptionIcon('heroicon-o-gift')
->color('success'),
Stat::make('Conversion Rate', $conversionRate.'%')
->description('Coupon to subscription rate')
->descriptionIcon('heroicon-o-chart-bar')
->color('info'),
Stat::make('Avg Discount Value', '$'.number_format($avgDiscountValue, 2))
->description('Per usage average')
->descriptionIcon('heroicon-o-calculator')
->color('warning'),
Stat::make('Top Performing', $topPerformingCoupon ? ($topPerformingCoupon['name'] ?? 'N/A') : 'N/A')
->description($topPerformingCoupon ? ($topPerformingCoupon['usages'] ?? 0).' usages' : '0 usages')
->descriptionIcon('heroicon-o-trophy')
->color('purple'),
Stat::make('Monthly Usage', $monthlyUsage)
->description('This month')
->descriptionIcon('heroicon-o-calendar')
->color('success'),
Stat::make('Revenue Impact', '$'.number_format($this->calculateRevenueImpact(), 2))
->description('Estimated new revenue')
->descriptionIcon('heroicon-o-currency-dollar')
->color('success'),
];
}
private function getCouponConversionRate(): string
{
$totalCoupons = Coupon::count();
if ($totalCoupons == 0) {
return '0';
}
$usedCoupons = Coupon::whereHas('usages')->count();
return number_format(($usedCoupons / $totalCoupons) * 100, 1);
}
private function getUsageGrowthRate(): string
{
$currentMonth = CouponUsage::whereMonth('created_at', now()->month)->count();
$previousMonth = CouponUsage::whereMonth('created_at', now()->subMonth()->month)->count();
if ($previousMonth == 0) {
return 'New this month';
}
$growth = (($currentMonth - $previousMonth) / $previousMonth) * 100;
return $growth >= 0 ? "+{$growth}%" : "{$growth}%";
}
private function getUsageGrowthIcon(): string
{
$currentMonth = CouponUsage::whereMonth('created_at', now()->month)->count();
$previousMonth = CouponUsage::whereMonth('created_at', now()->subMonth()->month)->count();
return $currentMonth > $previousMonth ? 'heroicon-o-arrow-trending-up' : 'heroicon-o-arrow-trending-down';
}
private function getUsageGrowthColor(): string
{
$currentMonth = CouponUsage::whereMonth('created_at', now()->month)->count();
$previousMonth = CouponUsage::whereMonth('created_at', now()->subMonth()->month)->count();
return $currentMonth > $previousMonth ? 'success' : 'danger';
}
private function getTopPerformingCoupon(): ?array
{
return CouponUsage::query()
->select('coupon_id', DB::raw('count(*) as usages, sum(discount_amount) as total_discount'))
->with('coupon:id,code')
->groupBy('coupon_id')
->orderBy('usages', 'desc')
->first()
?->toArray();
}
private function getMonthlyUsage(): int
{
return CouponUsage::whereMonth('created_at', now()->month)->count();
}
private function calculateRevenueImpact(): float
{
// Estimate revenue from coupons that led to subscriptions
return CouponUsage::query()
->whereHas('subscription', function ($query) {
$query->where('status', 'active');
})
->join('subscriptions', 'coupon_usages.subscription_id', '=', 'subscriptions.id')
->join('plans', 'subscriptions.plan_id', '=', 'plans.id')
->sum('plans.price');
}
}

View File

@@ -0,0 +1,120 @@
<?php
namespace App\Filament\Widgets;
use App\Models\Subscription;
use App\Models\User;
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;
class CustomerAnalyticsOverview extends BaseWidget
{
protected function getStats(): array
{
$totalCustomers = User::count();
$payingCustomers = Subscription::active()->distinct('user_id')->count('user_id');
$trialCustomers = Subscription::onTrial()->distinct('user_id')->count('user_id');
$churnedCustomers = Subscription::where('status', 'cancelled')->distinct('user_id')->count('user_id');
$mrr = $this->calculateMRR();
$arr = $mrr * 12;
$arpu = $payingCustomers > 0 ? $mrr / $payingCustomers : 0;
$ltv = $arpu * 12; // Simplified LTV calculation
return [
Stat::make('Total Customers', $totalCustomers)
->description('All registered users')
->descriptionIcon('heroicon-o-users')
->color('primary'),
Stat::make('Paying Customers', $payingCustomers)
->description($this->getCustomerGrowthRate($payingCustomers))
->descriptionIcon($this->getGrowthIcon($payingCustomers))
->color($this->getGrowthColor($payingCustomers)),
Stat::make('Trial Customers', $trialCustomers)
->description('Currently on trial')
->descriptionIcon('heroicon-o-clock')
->color('warning'),
Stat::make('Churned Customers', $churnedCustomers)
->description($this->getChurnRate($churnedCustomers, $payingCustomers + $churnedCustomers))
->descriptionIcon('heroicon-o-arrow-trending-down')
->color('danger'),
Stat::make('Monthly Recurring Revenue', '$'.number_format($mrr, 2))
->description('MRR from active subscriptions')
->descriptionIcon('heroicon-o-currency-dollar')
->color('success'),
Stat::make('Annual Recurring Revenue', '$'.number_format($arr, 2))
->description('ARR projection')
->descriptionIcon('heroicon-o-chart-bar')
->color('success'),
Stat::make('Average Revenue Per User', '$'.number_format($arpu, 2))
->description('ARPU for paying customers')
->descriptionIcon('heroicon-o-calculator')
->color('info'),
Stat::make('Lifetime Value', '$'.number_format($ltv, 2))
->description('Estimated customer LTV')
->descriptionIcon('heroicon-o-gift')
->color('purple'),
];
}
private function calculateMRR(): float
{
return Subscription::active()
->join('plans', 'subscriptions.plan_id', '=', 'plans.id')
->sum('plans.price');
}
private function getCustomerGrowthRate(int $current): string
{
$previous = Subscription::active()
->where('created_at', '<', now()->subMonth())
->distinct('user_id')
->count('user_id');
if ($previous == 0) {
return 'New customers';
}
$growth = (($current - $previous) / $previous) * 100;
return $growth >= 0 ? "+{$growth}%" : "{$growth}%";
}
private function getGrowthIcon(int $current): string
{
$previous = Subscription::active()
->where('created_at', '<', now()->subMonth())
->distinct('user_id')
->count('user_id');
return $current > $previous ? 'heroicon-o-arrow-trending-up' : 'heroicon-o-arrow-trending-down';
}
private function getGrowthColor(int $current): string
{
$previous = Subscription::active()
->where('created_at', '<', now()->subMonth())
->distinct('user_id')
->count('user_id');
return $current > $previous ? 'success' : 'danger';
}
private function getChurnRate(int $churned, int $total): string
{
if ($total == 0) {
return '0% churn rate';
}
$rate = ($churned / $total) * 100;
return "{$rate}% churn rate";
}
}

View File

@@ -0,0 +1,225 @@
<?php
namespace App\Filament\Widgets;
use App\Models\Subscription;
use Filament\Widgets\ChartWidget;
use Illuminate\Support\Facades\DB;
class CustomerLifetimeValue extends ChartWidget
{
protected static ?int $sort = 5;
protected int|string|array $columnSpan = 'full';
public function getHeading(): string
{
return 'Customer Lifetime Value Analysis';
}
protected function getData(): array
{
$ltvByCohort = $this->getLTVByCohort();
$ltvByProvider = $this->getLTVByProvider();
$ltvByPlan = $this->getLTVByPlan();
return [
'datasets' => [
[
'label' => 'Average LTV by Cohort ($)',
'data' => array_values($ltvByCohort),
'borderColor' => 'rgba(34, 197, 94, 1)',
'backgroundColor' => 'rgba(34, 197, 94, 0.1)',
'fill' => true,
'tension' => 0.4,
],
],
'labels' => array_keys($ltvByCohort),
];
}
protected function getType(): string
{
return 'bar';
}
protected function getOptions(): array
{
return [
'responsive' => true,
'plugins' => [
'legend' => [
'position' => 'top',
],
'tooltip' => [
'callbacks' => [
'label' => 'function(context) {
let label = context.dataset.label || "";
if (label) {
label += ": ";
}
if (context.parsed.y !== null) {
label += "$" + context.parsed.y.toFixed(2);
}
return label;
}',
],
],
],
'scales' => [
'y' => [
'beginAtZero' => true,
'ticks' => [
'callback' => 'function(value) {
return "$" + value;
}',
],
],
],
];
}
private function getLTVByCohort(): array
{
$cohorts = [];
// Get cohorts by signup month (last 6 months)
for ($i = 5; $i >= 0; $i--) {
$month = now()->subMonths($i);
$monthStart = $month->copy()->startOfMonth();
$monthEnd = $month->copy()->endOfMonth();
// Users who signed up in this month
$cohortUsers = Subscription::query()
->select('user_id')
->whereBetween('created_at', [$monthStart, $monthEnd])
->distinct()
->pluck('user_id');
if ($cohortUsers->isEmpty()) {
$cohorts[$month->format('M Y')] = 0;
continue;
}
// Calculate LTV for this cohort
$totalRevenue = Subscription::query()
->join('plans', 'subscriptions.plan_id', '=', 'plans.id')
->whereIn('subscriptions.user_id', $cohortUsers)
->where('subscriptions.status', 'active')
->sum('plans.price');
// Estimate LTV based on current MRR and average subscription length
$avgSubscriptionLength = $this->getAverageSubscriptionLength();
$cohortSize = $cohortUsers->count();
$avgMRRPerUser = $cohortSize > 0 ? $totalRevenue / $cohortSize : 0;
$ltv = $avgMRRPerUser * $avgSubscriptionLength;
$cohorts[$month->format('M Y')] = round($ltv, 2);
}
return $cohorts;
}
private function getLTVByProvider(): array
{
return Subscription::query()
->select(
'provider',
DB::raw('AVG(plans.price) as avg_monthly_price'),
DB::raw('COUNT(*) as total_subscriptions')
)
->join('plans', 'subscriptions.plan_id', '=', 'plans.id')
->where('subscriptions.status', 'active')
->groupBy('provider')
->orderBy('avg_monthly_price', 'desc')
->get()
->mapWithKeys(function ($item) {
$avgLength = $this->getAverageSubscriptionLengthByProvider($item->provider);
$ltv = $item->avg_monthly_price * $avgLength;
return [$item->provider => round($ltv, 2)];
})
->toArray();
}
private function getLTVByPlan(): array
{
return Subscription::query()
->select(
'plans.name as plan_name',
DB::raw('plans.price'),
DB::raw('COUNT(*) as total_subscriptions')
)
->join('plans', 'subscriptions.plan_id', '=', 'plans.id')
->where('subscriptions.status', 'active')
->groupBy('plans.id', 'plans.name', 'plans.price')
->orderBy('plans.price', 'desc')
->limit(10)
->get()
->mapWithKeys(function ($item) {
$avgLength = $this->getAverageSubscriptionLengthByPlan($item->plan_name);
$ltv = $item->price * $avgLength;
return [$item->plan_name => round($ltv, 2)];
})
->toArray();
}
private function getAverageSubscriptionLength(): float
{
// Average subscription length in months (simplified calculation)
return 12; // Could be calculated based on historical data
}
private function getAverageSubscriptionLengthByProvider(string $provider): float
{
// Provider-specific average subscription length
$providerLengths = [
'stripe' => 14,
'lemon_squeezy' => 12,
'polar' => 10,
'oxapay' => 8,
'crypto' => 6,
'activation_key' => 24,
];
return $providerLengths[$provider] ?? 12;
}
private function getAverageSubscriptionLengthByPlan(string $planName): float
{
// Plan-specific average subscription length (could be based on plan tier)
return 12; // Simplified, could be more sophisticated
}
private function getTopQuartileLTV(): float
{
$ltvs = Subscription::query()
->join('plans', 'subscriptions.plan_id', '=', 'plans.id')
->where('subscriptions.status', 'active')
->selectRaw('plans.price * ? as ltv', [$this->getAverageSubscriptionLength()])
->pluck('ltv')
->sort()
->values();
$quartileIndex = (int) ($ltvs->count() * 0.75);
return $ltvs->get($quartileIndex, 0);
}
private function getBottomQuartileLTV(): float
{
$ltvs = Subscription::query()
->join('plans', 'subscriptions.plan_id', '=', 'plans.id')
->where('subscriptions.status', 'active')
->selectRaw('plans.price * ? as ltv', [$this->getAverageSubscriptionLength()])
->pluck('ltv')
->sort()
->values();
$quartileIndex = (int) ($ltvs->count() * 0.25);
return $ltvs->get($quartileIndex, 0);
}
}

View File

@@ -0,0 +1,123 @@
<?php
namespace App\Filament\Widgets;
use App\Models\Subscription;
use Filament\Widgets\ChartWidget;
use Illuminate\Support\Facades\DB;
class RevenueMetrics extends ChartWidget
{
protected static ?int $sort = 1;
protected int|string|array $columnSpan = 'full';
public function getHeading(): string
{
return 'Revenue Trends';
}
protected function getData(): array
{
$monthlyRevenue = $this->getMonthlyRevenueTrend();
$mrrByProvider = $this->getMRRByProvider();
return [
'datasets' => [
[
'label' => 'Monthly Revenue',
'data' => array_values($monthlyRevenue),
'borderColor' => 'rgba(34, 197, 94, 1)',
'backgroundColor' => 'rgba(34, 197, 94, 0.1)',
'fill' => true,
'tension' => 0.4,
],
[
'label' => 'MRR by Provider',
'data' => array_values($mrrByProvider),
'borderColor' => 'rgba(59, 130, 246, 1)',
'backgroundColor' => 'rgba(59, 130, 246, 0.1)',
'fill' => true,
'tension' => 0.4,
],
],
'labels' => array_keys($monthlyRevenue),
];
}
protected function getType(): string
{
return 'line';
}
protected function getOptions(): array
{
return [
'responsive' => true,
'interaction' => [
'intersect' => false,
'mode' => 'index',
],
'plugins' => [
'legend' => [
'position' => 'top',
],
'tooltip' => [
'callbacks' => [
'label' => 'function(context) {
let label = context.dataset.label || "";
if (label) {
label += ": ";
}
if (context.parsed.y !== null) {
label += "$" + context.parsed.y.toFixed(2);
}
return label;
}',
],
],
],
'scales' => [
'y' => [
'beginAtZero' => true,
'ticks' => [
'callback' => 'function(value) {
return "$" + value;
}',
],
],
],
];
}
private function getMonthlyRevenueTrend(): array
{
return Subscription::query()
->select(
DB::raw("strftime('%Y-%m', subscriptions.created_at) as month"),
DB::raw('SUM(plans.price) as revenue')
)
->join('plans', 'subscriptions.plan_id', '=', 'plans.id')
->where('subscriptions.status', 'active')
->where('subscriptions.created_at', '>=', now()->subMonths(12))
->groupBy('month')
->orderBy('month')
->pluck('revenue', 'month')
->toArray();
}
private function getMRRByProvider(): array
{
return Subscription::query()
->select(
'provider',
DB::raw('SUM(plans.price) as mrr')
)
->join('plans', 'subscriptions.plan_id', '=', 'plans.id')
->where('subscriptions.status', 'active')
->groupBy('provider')
->orderBy('mrr', 'desc')
->pluck('mrr', 'provider')
->toArray();
}
}

View File

@@ -0,0 +1,126 @@
<?php
namespace App\Filament\Widgets;
use App\Models\Subscription;
use Filament\Widgets\ChartWidget;
use Illuminate\Support\Facades\DB;
class SubscriptionMetrics extends ChartWidget
{
protected static ?int $sort = 2;
protected int|string|array $columnSpan = 'full';
public function getHeading(): string
{
return 'Subscription Metrics';
}
protected function getData(): array
{
$period = $this->getPeriod();
$subscriptionsByProvider = $this->getSubscriptionsByProvider($period);
$subscriptionsByStatus = $this->getSubscriptionsByStatus();
$monthlyTrend = $this->getMonthlySubscriptionTrend();
return [
'datasets' => [
[
'label' => 'Subscriptions by Provider',
'data' => array_values($subscriptionsByProvider),
'backgroundColor' => [
'rgba(59, 130, 246, 0.8)', // blue
'rgba(34, 197, 94, 0.8)', // green
'rgba(168, 85, 247, 0.8)', // purple
'rgba(251, 146, 60, 0.8)', // orange
'rgba(107, 114, 128, 0.8)', // gray
'rgba(236, 72, 153, 0.8)', // pink
],
'borderColor' => [
'rgba(59, 130, 246, 1)',
'rgba(34, 197, 94, 1)',
'rgba(168, 85, 247, 1)',
'rgba(251, 146, 60, 1)',
'rgba(107, 114, 128, 1)',
'rgba(236, 72, 153, 1)',
],
],
],
'labels' => array_keys($subscriptionsByProvider),
];
}
protected function getType(): string
{
return 'doughnut';
}
protected function getOptions(): array
{
return [
'responsive' => true,
'plugins' => [
'legend' => [
'position' => 'bottom',
],
'tooltip' => [
'callbacks' => [
'label' => 'function(context) {
const label = context.label || "";
const value = context.parsed || 0;
const total = context.dataset.data.reduce((a, b) => a + b, 0);
const percentage = ((value / total) * 100).toFixed(1);
return label + ": " + value + " (" + percentage + "%)";
}',
],
],
],
];
}
private function getPeriod(): string
{
return 'last_30_days'; // Could be made configurable
}
private function getSubscriptionsByProvider(string $period): array
{
$query = Subscription::query();
if ($period === 'last_30_days') {
$query->where('created_at', '>=', now()->subDays(30));
}
return $query
->select('provider', DB::raw('count(*) as count'))
->groupBy('provider')
->orderBy('count', 'desc')
->pluck('count', 'provider')
->toArray();
}
private function getSubscriptionsByStatus(): array
{
return Subscription::query()
->select('status', DB::raw('count(*) as count'))
->groupBy('status')
->orderBy('count', 'desc')
->pluck('count', 'status')
->toArray();
}
private function getMonthlySubscriptionTrend(): array
{
return Subscription::query()
->select(
DB::raw("strftime('%Y-%m', subscriptions.created_at) as month"),
DB::raw('count(*) as count')
)
->groupBy('month')
->orderBy('month')
->pluck('count', 'month')
->toArray();
}
}

View File

@@ -0,0 +1,176 @@
<?php
namespace App\Filament\Widgets;
use App\Models\Subscription;
use App\Models\TrialExtension;
use Filament\Widgets\ChartWidget;
use Illuminate\Support\Facades\DB;
class TrialPerformance extends ChartWidget
{
protected static ?int $sort = 4;
protected int|string|array $columnSpan = 'full';
public function getHeading(): string
{
return 'Trial Performance';
}
protected function getData(): array
{
$trialConversion = $this->getTrialConversionRates();
$trialExtensions = $this->getTrialExtensionsTrend();
return [
'datasets' => [
[
'label' => 'Trial Conversion Rate (%)',
'data' => array_values($trialConversion),
'borderColor' => 'rgba(168, 85, 247, 1)',
'backgroundColor' => 'rgba(168, 85, 247, 0.1)',
'fill' => true,
'tension' => 0.4,
],
[
'label' => 'Trial Extensions',
'data' => array_values($trialExtensions),
'borderColor' => 'rgba(251, 146, 60, 1)',
'backgroundColor' => 'rgba(251, 146, 60, 0.1)',
'fill' => true,
'tension' => 0.4,
],
],
'labels' => array_keys($trialConversion),
];
}
protected function getType(): string
{
return 'line';
}
protected function getOptions(): array
{
return [
'responsive' => true,
'interaction' => [
'intersect' => false,
'mode' => 'index',
],
'plugins' => [
'legend' => [
'position' => 'top',
],
'tooltip' => [
'callbacks' => [
'label' => 'function(context) {
let label = context.dataset.label || "";
if (label) {
label += ": ";
}
if (context.parsed.y !== null) {
if (label.includes("Rate")) {
label += context.parsed.y.toFixed(1) + "%";
} else {
label += context.parsed.y;
}
}
return label;
}',
],
],
],
'scales' => [
'y' => [
'beginAtZero' => true,
'ticks' => [
'callback' => 'function(value, index, values) {
if (index === 0 || index === values.length - 1) {
return value + (this.chart.data.datasets[0].label.includes("Rate") ? "%" : "");
}
return "";
}',
],
],
],
];
}
private function getTrialConversionRates(): array
{
$conversionData = [];
for ($i = 5; $i >= 0; $i--) {
$month = now()->subMonths($i);
$monthStart = $month->copy()->startOfMonth();
$monthEnd = $month->copy()->endOfMonth();
// Trials that ended during this month
$endedTrials = Subscription::query()
->where('status', 'trialing')
->whereBetween('trial_ends_at', [$monthStart, $monthEnd])
->get();
$totalTrials = $endedTrials->count();
$convertedTrials = $endedTrials->filter(function ($trial) use ($monthEnd) {
// Check if trial converted to paid subscription
return Subscription::query()
->where('user_id', $trial->user_id)
->where('status', 'active')
->where('created_at', '>', $trial->trial_ends_at)
->where('created_at', '<=', $monthEnd)
->exists();
})->count();
$conversionRate = $totalTrials > 0 ? ($convertedTrials / $totalTrials) * 100 : 0;
$conversionData[$month->format('M Y')] = round($conversionRate, 2);
}
return $conversionData;
}
private function getTrialExtensionsTrend(): array
{
return TrialExtension::query()
->select(
DB::raw("strftime('%Y-%m', trial_extensions.granted_at) as month"),
DB::raw('COUNT(*) as extensions'),
DB::raw('SUM(extension_days) as total_days')
)
->where('trial_extensions.granted_at', '>=', now()->subMonths(6))
->groupBy('month')
->orderBy('month')
->get()
->mapWithKeys(function ($item) {
$date = \Carbon\Carbon::createFromFormat('Y-m', $item->month);
return [$date->format('M Y') => $item->extensions];
})
->toArray();
}
private function getAverageTrialLength(): float
{
return Subscription::query()
->where('status', 'trialing')
->orWhere('status', 'active')
->whereNotNull('trial_ends_at')
->selectRaw('AVG(DATEDIFF(trial_ends_at, created_at)) as avg_trial_days')
->value('avg_trial_days') ?? 0;
}
private function getMostCommonExtensionReasons(): array
{
return TrialExtension::query()
->select('reason', DB::raw('COUNT(*) as count'))
->whereNotNull('reason')
->groupBy('reason')
->orderBy('count', 'desc')
->limit(5)
->pluck('count', 'reason')
->toArray();
}
}