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,
]);
}
}
}
}

View File

@@ -14,6 +14,44 @@ public static function handleWatermarkUsageWebhookEvents(WebhookReceived $event)
case 'invoice.paid':
self::handleInvoicePaid($event);
break;
case 'customer.subscription.deleted':
self::handleClearWatermarkCredits($event);
break;
}
}
private static function handleClearWatermarkCredits(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') {
// revert the watermark credits back to 0
$user_usage = UserUsage::where('user_id', $user->id)->first();
if ($user_usage) {
$user_usage->update([
'non_watermark_videos_left' => 0,
]);
} else {
$user_usage = UserUsage::create([
'user_id' => $user->id,
'non_watermark_videos_left' => 0,
]);
}
}
}
}
}