added stripe sync in dashboard to ensure proper sync even in absence of webhook
This commit is contained in:
@@ -3,6 +3,10 @@
|
||||
namespace App\Livewire\Dashboard;
|
||||
|
||||
use App\Models\UsageLog;
|
||||
use Cache;
|
||||
use Carbon\Carbon;
|
||||
use DB;
|
||||
use Exception;
|
||||
use Illuminate\Http\Request;
|
||||
use Livewire\Component;
|
||||
|
||||
@@ -18,14 +22,137 @@ class Dashboard extends Component
|
||||
$status = $request->route('status');
|
||||
$currentUrl = $request->fullUrl();
|
||||
if ($status == 'success') {
|
||||
$this->syncSubscription();
|
||||
return redirect()->route('dashboard')->with('status', 'success');
|
||||
} elseif ($status == 'cancel') {
|
||||
return redirect()->route('dashboard')->with('status', 'cancel');
|
||||
}
|
||||
}
|
||||
|
||||
private function checkForSubscriptionStatus(): bool
|
||||
{
|
||||
$redirect = false;
|
||||
$user = auth()->user();
|
||||
$userId = $user->id;
|
||||
if ($user->subscribed()) {
|
||||
$subscription = $user->subscriptions()->where(['stripe_status' => 'active'])->orderByDesc('updated_at')->first();
|
||||
if ($subscription !== null) {
|
||||
$subscriptionId = $subscription->stripe_id;
|
||||
$cacheKey = "stripe_check_executed_user_{$userId}_{$subscriptionId}";
|
||||
if (!Cache::has($cacheKey)) {
|
||||
try {
|
||||
$stripe = new \Stripe\StripeClient(config('cashier.secret'));
|
||||
$subscriptionData = $stripe->subscriptions->retrieve($subscriptionId, []);
|
||||
if ($subscriptionData !== null) {
|
||||
$items = $subscriptionData->items->data[0];
|
||||
if ($items !== null) {
|
||||
$cancel_at_period_end = $subscriptionData->cancel_at_period_end;
|
||||
$ends_at = $items->current_period_end;
|
||||
$cancel_at = $subscriptionData->cancel_at;
|
||||
$canceled_at = $subscriptionData->canceled_at;
|
||||
$status = $subscriptionData->status;
|
||||
if ($cancel_at_period_end) {
|
||||
$final_ends_at = Carbon::createFromTimestamp($cancel_at)->toDateTimeString();
|
||||
} else {
|
||||
if ($cancel_at === null && $canceled_at !== null && $status === "canceled" && $cancel_at_period_end === false) {
|
||||
//$final_ends_at = Carbon::createFromTimestamp($canceled_at)->toDateTimeString();
|
||||
$final_ends_at = Carbon::now()->subDays(2)->toDateTimeString();
|
||||
$redirect = true;
|
||||
} elseif($status === "active" && $cancel_at !== null) {
|
||||
$final_ends_at = Carbon::createFromTimestamp($cancel_at)->toDateTimeString();
|
||||
}
|
||||
}
|
||||
|
||||
DB::table('subscriptions')
|
||||
->where('stripe_id', $subscriptionId)
|
||||
->update([
|
||||
'stripe_status' => $status,
|
||||
'ends_at' => $final_ends_at,
|
||||
'updated_at' => Carbon::now()->toDateTimeString(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Cache::put($cacheKey, true, now()->addHour());
|
||||
} catch (Exception $exception) {
|
||||
\Log::error($exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return $redirect;
|
||||
}
|
||||
|
||||
private function syncSubscription(): void
|
||||
{
|
||||
$user = auth()->user();
|
||||
$userId = $user->id;
|
||||
if ($user->hasStripeId()) {
|
||||
$stripe = new \Stripe\StripeClient(config('cashier.secret'));
|
||||
$subscriptions = $stripe->subscriptions->all(['limit' => 1]);
|
||||
if (!$subscriptions->isEmpty()) {
|
||||
$data = $subscriptions->data[0];
|
||||
$items = $subscriptions->data[0]->items->data[0];
|
||||
|
||||
$type = 'default';
|
||||
$subscriptionId = $items->subscription;
|
||||
$status = $data->status;
|
||||
$cancel_at_period_end = $data->cancel_at_period_end;
|
||||
$quantity = $items->quantity;
|
||||
$stripe_price = $items->price->id;
|
||||
$stripe_product = $items->price->product;
|
||||
$ends_at = $items->current_period_end;
|
||||
$subscriptionItemId = $items->id;
|
||||
if ($cancel_at_period_end) {
|
||||
$final_ends_at = Carbon::createFromTimestamp($ends_at)->toDateTimeString();
|
||||
} else {
|
||||
$final_ends_at = null;
|
||||
}
|
||||
|
||||
try {
|
||||
if ($status === "active") {
|
||||
$subscriptionsTable = DB::table('subscriptions')->where(['stripe_id' => $subscriptionId])->first();
|
||||
if ($subscriptionsTable == null) {
|
||||
$subscriptionsTable = DB::table('subscriptions')->insert([
|
||||
'user_id' => $userId,
|
||||
'type' => $type,
|
||||
'stripe_id' => $subscriptionId,
|
||||
'stripe_status' => $status,
|
||||
'stripe_price' => $stripe_price,
|
||||
'quantity' => $quantity,
|
||||
'ends_at' => $final_ends_at,
|
||||
'created_at' => Carbon::now()->toDateTimeString(),
|
||||
'updated_at' => Carbon::now()->toDateTimeString(),
|
||||
]);
|
||||
|
||||
$subscriptionsTable = DB::table('subscriptions')->where(['stripe_id' => $subscriptionId])->first();
|
||||
$subID = $subscriptionsTable->id;
|
||||
$subscriptionItemsTable = DB::table('subscription_items')->where(['stripe_id' => $subscriptionItemId])->first();
|
||||
if ($subscriptionItemsTable == null) {
|
||||
$subscriptionItemsTable = DB::table('subscription_items')->insert([
|
||||
'subscription_id' => $subID,
|
||||
'stripe_id' => $subscriptionItemId,
|
||||
'stripe_product' => $stripe_product,
|
||||
'stripe_price' => $stripe_price,
|
||||
'quantity' => $quantity,
|
||||
'created_at' => Carbon::now()->toDateTimeString(),
|
||||
'updated_at' => Carbon::now()->toDateTimeString(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception $exception) {
|
||||
\Log::error($exception->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function mount(Request $request)
|
||||
{
|
||||
if($this->checkForSubscriptionStatus()) {
|
||||
return redirect()->route('dashboard');
|
||||
};
|
||||
try {
|
||||
$status = $request->session()->get('status');
|
||||
if (isset($status)) {
|
||||
@@ -43,6 +170,7 @@ class Dashboard extends Component
|
||||
if (auth()->user()->subscribedToProduct(config('app.plans')[0]['product_id'])) {
|
||||
try {
|
||||
$result = auth()->user()->subscriptions()->where(['stripe_status' => 'active'])->orderByDesc('updated_at')->first();
|
||||
if ($result != null) {
|
||||
$userPriceID = $result['items'][0]['stripe_price'];
|
||||
$subscriptionEnd = $result['ends_at'];
|
||||
|
||||
@@ -56,9 +184,9 @@ class Dashboard extends Component
|
||||
}
|
||||
$this->subscription['name'] = $planName;
|
||||
$this->subscription['ends_at'] = $subscriptionEnd;
|
||||
}
|
||||
|
||||
} catch (\Exception $e) {
|
||||
|
||||
\Log::error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,8 +27,8 @@
|
||||
|
||||
<article class="flex items-center gap-4 rounded-lg border border-gray-100 bg-white p-6 dark:border-gray-800 dark:bg-white/[0.03]">
|
||||
<div>
|
||||
<p class="text-sm text-gray-500 dark:text-gray-400">Your <span class="text-accent-content font-bold">{{ $subscription['name'] }}</span> subscription is active and will be
|
||||
@if($subscription['ends_at']) end at<span class="app-primary"> {{ \Carbon\Carbon::make($subscription['ends_at'])->toFormattedDayDateString() }}.</span>
|
||||
<p class="text-sm text-gray-500 dark:text-gray-400">Your <span class="text-accent-content font-bold">{{ $subscription['name'] ?? "" }}</span> subscription is active and will be
|
||||
@if($subscription['ends_at'] ?? "") end at<span class="app-primary"> {{ \Carbon\Carbon::make($subscription['ends_at'])->toFormattedDayDateString() ?? "" }}.</span>
|
||||
@else auto-renew as per the plan you chose.
|
||||
@endif
|
||||
To manage you subscription <a href="{{ route('billing') }}" target="_blank" class="text-blue-500">Click here</a></p>
|
||||
|
||||
Reference in New Issue
Block a user