This commit is contained in:
ct
2025-07-03 16:34:21 +08:00
parent 0dd7d82502
commit f183d1ff13
10 changed files with 246 additions and 39 deletions

View File

@@ -89,26 +89,33 @@ private static function handleSubscriptionDelete(WebhookReceived $event)
if ($user) {
$plan = Plan::where('tier', 'free')->first();
foreach ($object['items']['data'] as $line_item) {
if ($plan) {
$user_plan = UserPlan::where('user_id', $user->id)->first();
$stripe_price_id = $line_item['plan']['id'];
if ($user_plan) {
$user_plan->update([
'plan_id' => $plan->id,
'current_period_end' => null,
'cancel_at' => null,
'canceled_at' => null,
]);
} else {
$user_plan = UserPlan::create([
'user_id' => $user->id,
'plan_id' => $plan->id,
'current_period_end' => null,
'cancel_at' => null,
'canceled_at' => null,
]);
$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,
]);
}
}
}
}