feat: migrate legacy subscription checks to unified payment system
- Replace Laravel Cashier methods with new subscription system - Remove session-based subscription checking in bulk components - Update Dashboard.php to use PaymentOrchestrator for provider-agnostic sync - Maintain backward compatibility with existing Stripe subscriptions - Improve performance by eliminating session overhead - Add automatic migration of legacy subscriptions to new system BREAKING CHANGE: Subscription checking now uses unified payment system instead of Laravel Cashier methods
This commit is contained in:
@@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
namespace App\Livewire\Dashboard;
|
namespace App\Livewire\Dashboard;
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Session;
|
|
||||||
use Livewire\Component;
|
use Livewire\Component;
|
||||||
|
|
||||||
class Bulk extends Component
|
class Bulk extends Component
|
||||||
@@ -13,8 +12,7 @@ class Bulk extends Component
|
|||||||
|
|
||||||
public function mount(): void
|
public function mount(): void
|
||||||
{
|
{
|
||||||
$subscriptionCheck = auth()->user()->subscribedToProduct(config('app.plans')[0]['product_id']);
|
// Subscription check is now handled directly in render method
|
||||||
Session::put('isSubscribed', $subscriptionCheck);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function generateBulk(): void
|
public function generateBulk(): void
|
||||||
@@ -50,20 +48,20 @@ class Bulk extends Component
|
|||||||
private function randomEmail(): string
|
private function randomEmail(): string
|
||||||
{
|
{
|
||||||
$domain = $this->getRandomDomain();
|
$domain = $this->getRandomDomain();
|
||||||
if ($domain == 'gmail.com' || $domain == 'googlemail.com') {
|
if ($domain === 'gmail.com' || $domain === 'googlemail.com') {
|
||||||
$uname = $this->getRandomGmailUser();
|
$uname = $this->getRandomGmailUser();
|
||||||
$uname_len = strlen((string) $uname);
|
$uname_len = strlen((string) $uname);
|
||||||
$len_power = $uname_len - 1;
|
$len_power = $uname_len - 1;
|
||||||
$combination = 2 ** $len_power;
|
$combination = 2 ** $len_power;
|
||||||
$rand_comb = mt_rand(1, $combination);
|
$rand_comb = random_int(1, $combination);
|
||||||
$formatted = implode(' ', str_split((string) $uname));
|
$formatted = implode(' ', str_split((string) $uname));
|
||||||
$uname_exp = explode(' ', $formatted);
|
$uname_exp = explode(' ', $formatted);
|
||||||
|
|
||||||
$bin = intval('');
|
$bin = 0;
|
||||||
for ($i = 0; $i < $len_power; $i++) {
|
for ($i = 0; $i < $len_power; $i++) {
|
||||||
$bin .= mt_rand(0, 1);
|
$bin .= random_int(0, 1);
|
||||||
}
|
}
|
||||||
$bin = explode(' ', implode(' ', str_split(strval($bin))));
|
$bin = explode(' ', implode(' ', str_split((string) $bin)));
|
||||||
|
|
||||||
$email = '';
|
$email = '';
|
||||||
for ($i = 0; $i < $len_power; $i++) {
|
for ($i = 0; $i < $len_power; $i++) {
|
||||||
@@ -73,7 +71,7 @@ class Bulk extends Component
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
$email .= $uname_exp[$i];
|
$email .= $uname_exp[$i];
|
||||||
$gmail_rand = mt_rand(1, 10);
|
$gmail_rand = random_int(1, 10);
|
||||||
if ($gmail_rand > 5) {
|
if ($gmail_rand > 5) {
|
||||||
$email .= '@gmail.com';
|
$email .= '@gmail.com';
|
||||||
} else {
|
} else {
|
||||||
@@ -82,6 +80,7 @@ class Bulk extends Component
|
|||||||
|
|
||||||
return $email;
|
return $email;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->generateRandomUsername().'@'.$domain;
|
return $this->generateRandomUsername().'@'.$domain;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -148,9 +147,10 @@ class Bulk extends Component
|
|||||||
|
|
||||||
public function render()
|
public function render()
|
||||||
{
|
{
|
||||||
if (Session::get('isSubscribed')) {
|
if (auth()->user()->hasActiveSubscription()) {
|
||||||
return view('livewire.dashboard.bulk')->layout('components.layouts.dashboard');
|
return view('livewire.dashboard.bulk')->layout('components.layouts.dashboard');
|
||||||
}
|
}
|
||||||
|
|
||||||
return view('livewire.dashboard.not-subscribed')->layout('components.layouts.dashboard');
|
return view('livewire.dashboard.not-subscribed')->layout('components.layouts.dashboard');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
namespace App\Livewire\Dashboard;
|
namespace App\Livewire\Dashboard;
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Session;
|
|
||||||
use Livewire\Component;
|
use Livewire\Component;
|
||||||
|
|
||||||
class BulkGmail extends Component
|
class BulkGmail extends Component
|
||||||
@@ -15,8 +14,7 @@ class BulkGmail extends Component
|
|||||||
|
|
||||||
public function mount(): void
|
public function mount(): void
|
||||||
{
|
{
|
||||||
$subscriptionCheck = auth()->user()->subscribedToProduct(config('app.plans')[0]['product_id']);
|
// Subscription check is now handled directly in render method
|
||||||
Session::put('isSubscribed', $subscriptionCheck);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function generateBulk(): void
|
public function generateBulk(): void
|
||||||
@@ -110,9 +108,10 @@ class BulkGmail extends Component
|
|||||||
|
|
||||||
public function render()
|
public function render()
|
||||||
{
|
{
|
||||||
if (Session::get('isSubscribed')) {
|
if (auth()->user()->hasActiveSubscription()) {
|
||||||
return view('livewire.dashboard.bulk-gmail')->layout('components.layouts.dashboard');
|
return view('livewire.dashboard.bulk-gmail')->layout('components.layouts.dashboard');
|
||||||
}
|
}
|
||||||
|
|
||||||
return view('livewire.dashboard.not-subscribed')->layout('components.layouts.dashboard');
|
return view('livewire.dashboard.not-subscribed')->layout('components.layouts.dashboard');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,15 +2,15 @@
|
|||||||
|
|
||||||
namespace App\Livewire\Dashboard;
|
namespace App\Livewire\Dashboard;
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Cache;
|
|
||||||
use Illuminate\Support\Facades\Date;
|
|
||||||
use Illuminate\Support\Facades\DB;
|
|
||||||
use Illuminate\Support\Facades\Log;
|
|
||||||
use App\Models\UsageLog;
|
use App\Models\UsageLog;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
use Illuminate\Support\Facades\Date;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
use Livewire\Component;
|
use Livewire\Component;
|
||||||
use Stripe\StripeClient;
|
use Stripe\StripeClient;
|
||||||
|
use App\Models\Subscription;
|
||||||
|
|
||||||
class Dashboard extends Component
|
class Dashboard extends Component
|
||||||
{
|
{
|
||||||
@@ -28,11 +28,12 @@ class Dashboard extends Component
|
|||||||
{
|
{
|
||||||
$status = $request->route('status');
|
$status = $request->route('status');
|
||||||
$request->fullUrl();
|
$request->fullUrl();
|
||||||
if ($status == 'success') {
|
if ($status === 'success') {
|
||||||
$this->syncSubscription();
|
$this->syncSubscription();
|
||||||
|
|
||||||
return to_route('dashboard')->with('status', 'success');
|
return to_route('dashboard')->with('status', 'success');
|
||||||
}
|
}
|
||||||
if ($status == 'cancel') {
|
if ($status === 'cancel') {
|
||||||
return to_route('dashboard')->with('status', 'cancel');
|
return to_route('dashboard')->with('status', 'cancel');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -41,19 +42,20 @@ class Dashboard extends Component
|
|||||||
{
|
{
|
||||||
$redirect = false;
|
$redirect = false;
|
||||||
$user = auth()->user();
|
$user = auth()->user();
|
||||||
$userId = $user->id;
|
|
||||||
if ($user->subscribed()) {
|
// Use the new subscription system
|
||||||
$subscription = $user->subscriptions()
|
if ($user->hasActiveSubscription()) {
|
||||||
// ->where(['stripe_status' => 'active'])
|
$currentSubscription = $user->currentSubscription;
|
||||||
->orderByDesc('updated_at')
|
|
||||||
->first();
|
if ($currentSubscription && $currentSubscription->provider === 'stripe') {
|
||||||
if ($subscription !== null) {
|
$subscriptionId = $currentSubscription->provider_subscription_id;
|
||||||
$subscriptionId = $subscription->stripe_id;
|
$cacheKey = "stripe_check_executed_user_{$user->id}_{$subscriptionId}";
|
||||||
$cacheKey = "stripe_check_executed_user_{$userId}_{$subscriptionId}";
|
|
||||||
if (! Cache::has($cacheKey)) {
|
if (! Cache::has($cacheKey)) {
|
||||||
try {
|
try {
|
||||||
$stripe = new StripeClient(config('cashier.secret'));
|
$stripe = new StripeClient(config('cashier.secret'));
|
||||||
$subscriptionData = $stripe->subscriptions->retrieve($subscriptionId, []);
|
$subscriptionData = $stripe->subscriptions->retrieve($subscriptionId, []);
|
||||||
|
|
||||||
if ($subscriptionData !== null) {
|
if ($subscriptionData !== null) {
|
||||||
$items = $subscriptionData->items->data[0];
|
$items = $subscriptionData->items->data[0];
|
||||||
if ($items !== null) {
|
if ($items !== null) {
|
||||||
@@ -62,10 +64,10 @@ class Dashboard extends Component
|
|||||||
$cancel_at = $subscriptionData->cancel_at;
|
$cancel_at = $subscriptionData->cancel_at;
|
||||||
$canceled_at = $subscriptionData->canceled_at;
|
$canceled_at = $subscriptionData->canceled_at;
|
||||||
$status = $subscriptionData->status;
|
$status = $subscriptionData->status;
|
||||||
|
|
||||||
if ($cancel_at_period_end) {
|
if ($cancel_at_period_end) {
|
||||||
$final_ends_at = Date::createFromTimestamp($cancel_at)->toDateTimeString();
|
$final_ends_at = Date::createFromTimestamp($cancel_at)->toDateTimeString();
|
||||||
} elseif ($cancel_at === null && $canceled_at !== null && $status === 'canceled' && $cancel_at_period_end === false) {
|
} elseif ($cancel_at === null && $canceled_at !== null && $status === 'canceled' && $cancel_at_period_end === false) {
|
||||||
// $final_ends_at = Carbon::createFromTimestamp($canceled_at)->toDateTimeString();
|
|
||||||
$final_ends_at = Date::now()->subDays(2)->toDateTimeString();
|
$final_ends_at = Date::now()->subDays(2)->toDateTimeString();
|
||||||
$redirect = true;
|
$redirect = true;
|
||||||
} elseif ($status === 'active' && $cancel_at !== null) {
|
} elseif ($status === 'active' && $cancel_at !== null) {
|
||||||
@@ -74,12 +76,11 @@ class Dashboard extends Component
|
|||||||
$final_ends_at = null;
|
$final_ends_at = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
DB::table('subscriptions')
|
// Update using the new subscription model
|
||||||
->where('stripe_id', $subscriptionId)
|
$currentSubscription->update([
|
||||||
->update([
|
'status' => $status,
|
||||||
'stripe_status' => $status,
|
'ends_at' => $final_ends_at ? Date::createFromTimeString($final_ends_at) : null,
|
||||||
'ends_at' => $final_ends_at,
|
'updated_at' => now(),
|
||||||
'updated_at' => Date::now()->toDateTimeString(),
|
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -88,7 +89,6 @@ class Dashboard extends Component
|
|||||||
Log::error($exception->getMessage());
|
Log::error($exception->getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,10 +98,21 @@ class Dashboard extends Component
|
|||||||
private function syncSubscription(): void
|
private function syncSubscription(): void
|
||||||
{
|
{
|
||||||
$user = auth()->user();
|
$user = auth()->user();
|
||||||
$userId = $user->id;
|
|
||||||
|
// Use the new payment orchestrator to sync subscription
|
||||||
|
try {
|
||||||
|
$currentSubscription = $user->currentSubscription;
|
||||||
|
|
||||||
|
if ($currentSubscription && $currentSubscription->provider === 'stripe') {
|
||||||
|
// Sync with the new payment system
|
||||||
|
$currentSubscription->syncWithProvider();
|
||||||
|
}
|
||||||
|
|
||||||
|
// For backward compatibility, also check legacy Stripe data
|
||||||
if ($user->hasStripeId()) {
|
if ($user->hasStripeId()) {
|
||||||
$stripe = new StripeClient(config('cashier.secret'));
|
$stripe = new StripeClient(config('cashier.secret'));
|
||||||
$subscriptions = $stripe->subscriptions->all(['limit' => 1]);
|
$subscriptions = $stripe->subscriptions->all(['limit' => 1]);
|
||||||
|
|
||||||
if (! $subscriptions->isEmpty()) {
|
if (! $subscriptions->isEmpty()) {
|
||||||
$data = $subscriptions->data[0];
|
$data = $subscriptions->data[0];
|
||||||
$items = $subscriptions->data[0]->items->data[0];
|
$items = $subscriptions->data[0]->items->data[0];
|
||||||
@@ -117,35 +128,36 @@ class Dashboard extends Component
|
|||||||
$subscriptionItemId = $items->id;
|
$subscriptionItemId = $items->id;
|
||||||
$final_ends_at = $cancel_at_period_end ? Date::createFromTimestamp($ends_at)->toDateTimeString() : null;
|
$final_ends_at = $cancel_at_period_end ? Date::createFromTimestamp($ends_at)->toDateTimeString() : null;
|
||||||
|
|
||||||
try {
|
|
||||||
if ($status === 'active') {
|
if ($status === 'active') {
|
||||||
$subscriptionsTable = DB::table('subscriptions')->where(['stripe_id' => $subscriptionId])->first();
|
// Check if subscription exists in new system
|
||||||
if ($subscriptionsTable == null) {
|
$existingSubscription = Subscription::where('provider', 'stripe')
|
||||||
$subscriptionsTable = DB::table('subscriptions')->insert([
|
->where('provider_subscription_id', $subscriptionId)
|
||||||
'user_id' => $userId,
|
->where('user_id', $user->id)
|
||||||
|
->first();
|
||||||
|
|
||||||
|
if (! $existingSubscription) {
|
||||||
|
// Create subscription in new system
|
||||||
|
$newSubscription = Subscription::create([
|
||||||
|
'user_id' => $user->id,
|
||||||
'type' => $type,
|
'type' => $type,
|
||||||
'stripe_id' => $subscriptionId,
|
'provider' => 'stripe',
|
||||||
'stripe_status' => $status,
|
'provider_subscription_id' => $subscriptionId,
|
||||||
|
'status' => $status,
|
||||||
'stripe_price' => $stripe_price,
|
'stripe_price' => $stripe_price,
|
||||||
'quantity' => $quantity,
|
'quantity' => $quantity,
|
||||||
'ends_at' => $final_ends_at,
|
'ends_at' => $final_ends_at ? Date::createFromTimeString($final_ends_at) : null,
|
||||||
'created_at' => Date::now()->toDateTimeString(),
|
'stripe_id' => $subscriptionId, // Keep for backward compatibility
|
||||||
'updated_at' => Date::now()->toDateTimeString(),
|
'stripe_status' => $status, // Keep for backward compatibility
|
||||||
|
'created_at' => now(),
|
||||||
|
'updated_at' => now(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$subscriptionsTable = DB::table('subscriptions')->where(['stripe_id' => $subscriptionId])->first();
|
// Try to find the associated plan
|
||||||
$subID = $subscriptionsTable->id;
|
$plan = \App\Models\Plan::where('stripe_price_id', $stripe_price)->first();
|
||||||
$subscriptionItemsTable = DB::table('subscription_items')->where(['stripe_id' => $subscriptionItemId])->first();
|
if ($plan) {
|
||||||
if ($subscriptionItemsTable == null) {
|
$newSubscription->update(['plan_id' => $plan->id]);
|
||||||
$subscriptionItemsTable = DB::table('subscription_items')->insert([
|
}
|
||||||
'subscription_id' => $subID,
|
}
|
||||||
'stripe_id' => $subscriptionItemId,
|
|
||||||
'stripe_product' => $stripe_product,
|
|
||||||
'stripe_price' => $stripe_price,
|
|
||||||
'quantity' => $quantity,
|
|
||||||
'created_at' => Date::now()->toDateTimeString(),
|
|
||||||
'updated_at' => Date::now()->toDateTimeString(),
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -153,8 +165,6 @@ class Dashboard extends Component
|
|||||||
Log::error($exception->getMessage());
|
Log::error($exception->getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function mount(Request $request)
|
public function mount(Request $request)
|
||||||
{
|
{
|
||||||
@@ -164,7 +174,7 @@ class Dashboard extends Component
|
|||||||
try {
|
try {
|
||||||
$status = $request->session()->get('status');
|
$status = $request->session()->get('status');
|
||||||
if (isset($status)) {
|
if (isset($status)) {
|
||||||
if ($status == 'success') {
|
if ($status === 'success') {
|
||||||
$this->message = ['type' => 'success', 'message' => 'Order completed successfully.'];
|
$this->message = ['type' => 'success', 'message' => 'Order completed successfully.'];
|
||||||
} else {
|
} else {
|
||||||
$this->message = ['type' => 'error', 'message' => 'Order cancelled.'];
|
$this->message = ['type' => 'error', 'message' => 'Order cancelled.'];
|
||||||
@@ -175,23 +185,18 @@ class Dashboard extends Component
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$plans = config('app.plans', []);
|
// Use the new subscription system
|
||||||
if (! empty($plans) && isset($plans[0]) && is_array($plans[0]) && isset($plans[0]['product_id']) && auth()->user()->subscribedToProduct($plans[0]['product_id'])) {
|
if (auth()->user()->hasActiveSubscription()) {
|
||||||
try {
|
try {
|
||||||
$result = auth()->user()->subscriptions()->where(['stripe_status' => 'active'])->orderByDesc('updated_at')->first();
|
$currentSubscription = auth()->user()->currentSubscription;
|
||||||
if ($result != null) {
|
|
||||||
$userPriceID = $result['items'][0]['stripe_price'];
|
|
||||||
$subscriptionEnd = $result['ends_at'];
|
|
||||||
|
|
||||||
$planName = null;
|
if ($currentSubscription && $currentSubscription->plan) {
|
||||||
|
$planName = $currentSubscription->plan->name;
|
||||||
|
$subscriptionEnd = $currentSubscription->ends_at;
|
||||||
|
|
||||||
|
// Check if plan accepts Stripe (for backward compatibility)
|
||||||
|
$this->showStripeBilling = $currentSubscription->provider === 'stripe';
|
||||||
|
|
||||||
foreach (config('app.plans') as $plan) {
|
|
||||||
if ($plan['pricing_id'] === $userPriceID) {
|
|
||||||
$planName = $plan['name'];
|
|
||||||
$this->showStripeBilling = $plan['accept_stripe'];
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$this->subscription['name'] = $planName;
|
$this->subscription['name'] = $planName;
|
||||||
$this->subscription['ends_at'] = $subscriptionEnd;
|
$this->subscription['ends_at'] = $subscriptionEnd;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ class Subscription extends Model
|
|||||||
'migration_source',
|
'migration_source',
|
||||||
'migration_date',
|
'migration_date',
|
||||||
'migration_reason',
|
'migration_reason',
|
||||||
|
'created_at',
|
||||||
];
|
];
|
||||||
|
|
||||||
protected $casts = [
|
protected $casts = [
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ class User extends Authenticatable implements FilamentUser, MustVerifyEmail
|
|||||||
protected $hidden = [
|
protected $hidden = [
|
||||||
'password',
|
'password',
|
||||||
'remember_token',
|
'remember_token',
|
||||||
|
'level',
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user