Skip to content

Commit 1ac6ea0

Browse files
authored
Merge pull request #379 from enupal/develop
v5.5.1
2 parents df7de24 + 42ade50 commit 1ac6ea0

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Stripe Payments Changelog
22

3+
## 5.5.1 - 2024.02.26
4+
### Fixed
5+
- Fixed issue where Subscription Grant user group was not updated after updating plan. ([#374])
6+
7+
[#374]: https://github.com/enupal/stripe/issues/374
8+
39
## 5.5.0 - 2023.10.30
410
### Added
511
- Added Craft CMS requirement `^4.5.0`

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "enupal/stripe",
33
"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",
44
"type": "craft-plugin",
5-
"version": "5.5.0",
5+
"version": "5.5.1",
66
"keywords": [
77
"craft",
88
"cms",

src/services/Subscriptions.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,26 @@ public function processSubscriptionGrantEvent(WebhookEvent $event)
257257

258258
break;
259259

260+
case 'customer.subscription.updated':
261+
// remove current user groups and add new one
262+
$userGroups = $user->getGroups();
263+
$currentUserGroupIds = [];
264+
265+
if ($userGroups){
266+
foreach ($userGroups as $userGroup) {
267+
$currentUserGroupIds[] = $userGroup->id;
268+
}
269+
}
270+
271+
$grantUserGroupIds = $this->getCurrentUserGroups();
272+
$removedUserGroupIds = array_diff($currentUserGroupIds, $grantUserGroupIds);
273+
Craft::$app->getUsers()->assignUserToGroups($user->id, $removedUserGroupIds);
274+
Craft::info("Groups (".json_encode($grantUserGroupIds).") removed to user: ".$user->username, __METHOD__);
275+
276+
// add new plan
277+
$this->handleSubscriptionGrants($planId, $user);
278+
break;
279+
260280
case 'customer.subscription.deleted':
261281
$userGroups = $user->getGroups();
262282
$currentUserGroupIds = [];
@@ -334,6 +354,25 @@ public function getUserGroupsByPlanId($planId, $checkRemoveWhenCanceled = false)
334354
return $newGroups;
335355
}
336356

357+
/**
358+
* @return array
359+
*/
360+
public function getCurrentUserGroups()
361+
{
362+
$subscriptionGrants = (new Query())
363+
->select(['*'])
364+
->from(["{{%enupalstripe_subscriptiongrants}}"])
365+
->all();
366+
367+
$currentGroups = [];
368+
369+
foreach ($subscriptionGrants as $subscriptionGrant) {
370+
$currentGroups[] = $subscriptionGrant['userGroupId'];
371+
}
372+
373+
return $currentGroups;
374+
}
375+
337376
/**
338377
* @param $planId
339378
* @return array

src/templates/settings/subscription-grants/_edit.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878

7979
{{ forms.lightswitchField({
8080
label: "Remove when canceled?"|t('enupal-stripe'),
81-
instructions: "Remove this user group if the subscription is canceled (at the end of the billing period)"|t('enupal-stripe'),
81+
instructions: "Remove the user from this user group if the subscription is canceled (at the end of the billing period)"|t('enupal-stripe'),
8282
name: 'removeWhenCanceled',
8383
on: subscriptionGrant.removeWhenCanceled
8484
}) }}

0 commit comments

Comments
 (0)