From 934533e2eb32da9b13b47647e1e0aa8c4e589380 Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Thu, 2 Nov 2023 08:33:59 +0100 Subject: [PATCH 1/2] Better cleanup for subscription items --- src/Http/Controllers/WebhookController.php | 6 +++--- src/Subscription.php | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Http/Controllers/WebhookController.php b/src/Http/Controllers/WebhookController.php index 5224569b..6b936cca 100644 --- a/src/Http/Controllers/WebhookController.php +++ b/src/Http/Controllers/WebhookController.php @@ -180,10 +180,10 @@ protected function handleCustomerSubscriptionUpdated(array $payload) // Update subscription items... if (isset($data['items'])) { - $prices = []; + $cleanup = []; foreach ($data['items']['data'] as $item) { - $prices[] = $item['price']['id']; + $cleanup[] = $item['id']; $subscription->items()->updateOrCreate([ 'stripe_id' => $item['id'], @@ -195,7 +195,7 @@ protected function handleCustomerSubscriptionUpdated(array $payload) } // Delete items that aren't attached to the subscription anymore... - $subscription->items()->whereNotIn('stripe_price', $prices)->delete(); + $subscription->items()->whereNotIn('stripe_id', $cleanup)->delete(); } } diff --git a/src/Subscription.php b/src/Subscription.php index 222f8486..17bc8c89 100644 --- a/src/Subscription.php +++ b/src/Subscription.php @@ -699,10 +699,10 @@ public function swap($prices, array $options = []) 'ends_at' => null, ])->save(); - $stripePrices = []; + $cleanup = []; foreach ($stripeSubscription->items as $item) { - $stripePrices[] = $item->price->id; + $cleanup[] = $item->id; $this->items()->updateOrCreate([ 'stripe_id' => $item->id, @@ -714,7 +714,7 @@ public function swap($prices, array $options = []) } // Delete items that aren't attached to the subscription anymore... - $this->items()->whereNotIn('stripe_price', $stripePrices)->delete(); + $this->items()->whereNotIn('stripe_id', $cleanup)->delete(); $this->unsetRelation('items'); From 6bfaac368ef845b5c723e88dfe3ba583ddc0119b Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 2 Nov 2023 08:34:07 -0500 Subject: [PATCH 2/2] formatting --- src/Http/Controllers/WebhookController.php | 6 +++--- src/Subscription.php | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Http/Controllers/WebhookController.php b/src/Http/Controllers/WebhookController.php index 6b936cca..7a52dd67 100644 --- a/src/Http/Controllers/WebhookController.php +++ b/src/Http/Controllers/WebhookController.php @@ -180,10 +180,10 @@ protected function handleCustomerSubscriptionUpdated(array $payload) // Update subscription items... if (isset($data['items'])) { - $cleanup = []; + $subscriptionItemIds = []; foreach ($data['items']['data'] as $item) { - $cleanup[] = $item['id']; + $subscriptionItemIds[] = $item['id']; $subscription->items()->updateOrCreate([ 'stripe_id' => $item['id'], @@ -195,7 +195,7 @@ protected function handleCustomerSubscriptionUpdated(array $payload) } // Delete items that aren't attached to the subscription anymore... - $subscription->items()->whereNotIn('stripe_id', $cleanup)->delete(); + $subscription->items()->whereNotIn('stripe_id', $subscriptionItemIds)->delete(); } } diff --git a/src/Subscription.php b/src/Subscription.php index 17bc8c89..8a548637 100644 --- a/src/Subscription.php +++ b/src/Subscription.php @@ -699,10 +699,10 @@ public function swap($prices, array $options = []) 'ends_at' => null, ])->save(); - $cleanup = []; + $subscriptionItemIds = []; foreach ($stripeSubscription->items as $item) { - $cleanup[] = $item->id; + $subscriptionItemIds[] = $item->id; $this->items()->updateOrCreate([ 'stripe_id' => $item->id, @@ -714,7 +714,7 @@ public function swap($prices, array $options = []) } // Delete items that aren't attached to the subscription anymore... - $this->items()->whereNotIn('stripe_id', $cleanup)->delete(); + $this->items()->whereNotIn('stripe_id', $subscriptionItemIds)->delete(); $this->unsetRelation('items');