From e89ac32aef0bad436ea8169318bbbd6aab016d9d Mon Sep 17 00:00:00 2001 From: Andre Date: Mon, 26 Feb 2024 09:18:51 +0000 Subject: [PATCH 1/2] Fixes issue on updating plan #374 --- src/services/Subscriptions.php | 39 +++++++++++++++++++ .../settings/subscription-grants/_edit.twig | 2 +- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/services/Subscriptions.php b/src/services/Subscriptions.php index deccab21..f7e5d1c6 100644 --- a/src/services/Subscriptions.php +++ b/src/services/Subscriptions.php @@ -257,6 +257,26 @@ public function processSubscriptionGrantEvent(WebhookEvent $event) break; + case 'customer.subscription.updated': + // remove current user groups and add new one + $userGroups = $user->getGroups(); + $currentUserGroupIds = []; + + if ($userGroups){ + foreach ($userGroups as $userGroup) { + $currentUserGroupIds[] = $userGroup->id; + } + } + + $grantUserGroupIds = $this->getCurrentUserGroups(); + $removedUserGroupIds = array_diff($currentUserGroupIds, $grantUserGroupIds); + Craft::$app->getUsers()->assignUserToGroups($user->id, $removedUserGroupIds); + Craft::info("Groups (".json_encode($grantUserGroupIds).") removed to user: ".$user->username, __METHOD__); + + // add new plan + $this->handleSubscriptionGrants($planId, $user); + break; + case 'customer.subscription.deleted': $userGroups = $user->getGroups(); $currentUserGroupIds = []; @@ -334,6 +354,25 @@ public function getUserGroupsByPlanId($planId, $checkRemoveWhenCanceled = false) return $newGroups; } + /** + * @return array + */ + public function getCurrentUserGroups() + { + $subscriptionGrants = (new Query()) + ->select(['*']) + ->from(["{{%enupalstripe_subscriptiongrants}}"]) + ->all(); + + $currentGroups = []; + + foreach ($subscriptionGrants as $subscriptionGrant) { + $currentGroups[] = $subscriptionGrant['userGroupId']; + } + + return $currentGroups; + } + /** * @param $planId * @return array diff --git a/src/templates/settings/subscription-grants/_edit.twig b/src/templates/settings/subscription-grants/_edit.twig index a7ede620..a7195dbf 100644 --- a/src/templates/settings/subscription-grants/_edit.twig +++ b/src/templates/settings/subscription-grants/_edit.twig @@ -78,7 +78,7 @@ {{ forms.lightswitchField({ label: "Remove when canceled?"|t('enupal-stripe'), - instructions: "Remove this user group if the subscription is canceled (at the end of the billing period)"|t('enupal-stripe'), + instructions: "Remove the user from this user group if the subscription is canceled (at the end of the billing period)"|t('enupal-stripe'), name: 'removeWhenCanceled', on: subscriptionGrant.removeWhenCanceled }) }} From 42ade506718357d4ef6a9359373ffd1a36eb887a Mon Sep 17 00:00:00 2001 From: Andre Date: Mon, 26 Feb 2024 09:22:00 +0000 Subject: [PATCH 2/2] Adds v5.5.1 --- CHANGELOG.md | 6 ++++++ composer.json | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f8c2f21..f81c3c0b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Stripe Payments Changelog +## 5.5.1 - 2024.02.26 +### Fixed +- Fixed issue where Subscription Grant user group was not updated after updating plan. ([#374]) + +[#374]: https://github.com/enupal/stripe/issues/374 + ## 5.5.0 - 2023.10.30 ### Added - Added Craft CMS requirement `^4.5.0` diff --git a/composer.json b/composer.json index aa510a9e..b0a2ffca 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "enupal/stripe", "description": "Allows customers sign up for recurring and one-time payments with Stripe, perfect for orders, donations, subscriptions, and events. Create simple payment forms in seconds easily without coding. For Craft CMS 3.x", "type": "craft-plugin", - "version": "5.5.0", + "version": "5.5.1", "keywords": [ "craft", "cms",