Skip to content

Commit

Permalink
Merge pull request #379 from enupal/develop
Browse files Browse the repository at this point in the history
v5.5.1
  • Loading branch information
andrelopez authored Feb 26, 2024
2 parents df7de24 + 42ade50 commit 1ac6ea0
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
39 changes: 39 additions & 0 deletions src/services/Subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/templates/settings/subscription-grants/_edit.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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
}) }}
Expand Down

0 comments on commit 1ac6ea0

Please sign in to comment.