first(); if ($plan) { $user_plan = UserPlan::where('user_id', $user->id)->first(); if ($user_plan) { $user_plan->update([ 'plan_id' => $plan->id, 'current_period_end' => $current_period_end, 'cancel_at' => $cancel_at, 'canceled_at' => $canceled_at, ]); } else { $user_plan = UserPlan::create([ 'user_id' => $user->id, 'plan_id' => $plan->id, 'current_period_end' => $current_period_end, 'cancel_at' => $cancel_at, 'canceled_at' => $canceled_at, ]); } } } } } private static function handleSubscriptionDelete(WebhookReceived $event) { $object = StripeHelper::getEventObject($event); $user = StripeHelper::getUserByStripeID($object['customer']); if ($user) { foreach ($object['items']['data'] as $line_item) { $stripe_price_id = $line_item['plan']['id']; $subscription_config = PurchaseHelper::getSubscriptionPlanByStripePriceID($stripe_price_id); if ($subscription_config['type'] == 'subscription_plans') { $free_plan = Plan::where('tier', 'free')->first(); $user_plan = UserPlan::where('user_id', $user->id)->first(); if ($user_plan) { $user_plan->update([ 'plan_id' => $free_plan->id, 'current_period_end' => null, 'cancel_at' => null, 'canceled_at' => null, ]); } else { $user_plan = UserPlan::create([ 'user_id' => $user->id, 'plan_id' => $free_plan->id, 'current_period_end' => null, 'cancel_at' => null, 'canceled_at' => null, ]); } } } } } }