Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to switch or cancel a subscription on the front-end #1650

Closed
jameslindeman opened this issue Aug 15, 2020 · 7 comments
Closed

Unable to switch or cancel a subscription on the front-end #1650

jameslindeman opened this issue Aug 15, 2020 · 7 comments
Labels
ℹ️ status: need more info When waiting for user to supply database or more information.

Comments

@jameslindeman
Copy link

Description
Unable to switch a Stripe subscription plan or cancel a plan on the front-end. This could be a problem with https://github.com/craftcms/commerce-stripe but starting here as it seems to be a problem since the recent Commerce 3.2 update.

To Reproduce

  1. Setup at least two Stripe subscriptions in the CP and enable them for customers to select.
  2. Using the example templates, subscribe to a plan on the front-end
  3. Then go to /shop/plans and click ‘manage’
  4. Try to switch the plan by clicking ‘Switch’ next to the alternative plan
    • I’m then seeing a flash error after this: Unable to modify subscription at this time.
  5. Try to cancel the plan by clicking ‘Unsubscribe’
    • I’m then seeing a flash error after this: Unable to cancel subscription at this time.

I only get the flash errors on the front-end. I can switch and cancel a membership from the control panel.

Additional info

  • Craft version: 3.5.4
  • Commerce version: 3.2.2.1
  • Commerce Stripe: 2.3.1.1
  • PHP version: 7.2.24
@nfourtythree nfourtythree added ℹ️ status: need more info When waiting for user to supply database or more information. and removed New labels Aug 17, 2020
@nfourtythree
Copy link
Contributor

Hi @jameslindeman

I have just tried to replicate this locally and I was able to unsubscribe as expected.

The error you are seeing is triggered when trying to save the element. This could suggest there is an issue on saving the element. Is there any custom logic or fields in your project that could be causing an issue?

Thanks.

@jameslindeman
Copy link
Author

Hi @nfourtythree, thanks for taking a look. Guess it must be my end but struggling to find it.

There was some custom logic to send a tax code to Stripe, and a couple of custom Subscription fields to save country and state, although I’ve disabled all plugins and removed those fields. Still not able to unsubscribe on front-end but control panel works.

@lukeholder
Copy link
Member

@jameslindeman

The error is triggered in here:

$subscription = Subscription::find()->anyStatus()->uid($subscriptionUid)->one();
$plan = Commerce::getInstance()->getPlans()->getPlanByUid($planUid);
$userSession = Craft::$app->getUser();
$validData = $planUid && $plan && $subscriptionUid && $subscription;
$validAction = $plan->canSwitchFrom($subscription->getPlan());
$canModifySubscription = ($subscription->userId === $userSession->getId()) || $userSession->checkPermission('commerce-manageSubscriptions');
if ($validData && $validAction && $canModifySubscription) {
/** @var SubscriptionGateway $gateway */
$gateway = $subscription->getGateway();
$parameters = $gateway->getSwitchPlansFormModel();
foreach ($parameters->attributes() as $attributeName) {
$value = $request->getValidatedBodyParam($attributeName);
if (is_string($value) && StringHelper::countSubstrings($value, ':') > 0) {
list($hashedPlanUid, $parameterValue) = explode(':', $value);
if ($hashedPlanUid == $planUid) {
$parameters->{$attributeName} = $parameterValue;
}
}
}
if (!$plugin->getSubscriptions()->switchSubscriptionPlan($subscription, $plan, $parameters)) {
$error = Plugin::t('Unable to modify subscription at this time.');
}
} else {
$error = Plugin::t('Unable to modify subscription at this time.');
}

Since you mention it working on the backed, are you possibly trying to switch the subscription of the non-current user on the front end? See lines:

$canModifySubscription = ($subscription->userId === $userSession->getId()) || $userSession->checkPermission('commerce-manageSubscriptions');

@jameslindeman
Copy link
Author

Thanks @lukeholder

I’m logged in as the user I’m trying to unsubscribe/switch. Doing a dump and die for the subscription I’m trying to manage looks right. I’m also testing with the example templates, and have disabled plugins. If I unsubscribe/switch in the CP, those changes are reflected in the front-end template I’m looking at.

@jameslindeman
Copy link
Author

jameslindeman commented Aug 21, 2020

Found it. The user has to have permissions set to manage subscriptions. (CP > Users > User > Permissions > Craft Commerce > Manage Subscriptions).

Can you confirm? Is that expected behaviour? If not, I can’t yet see how it’s part of my setup with disabled plugins and using the example templates.

@kevadamson
Copy link

image

Had the same issue. Told it's because userId is string, but getId is integer. == would work but not ===?

@lukeholder
Copy link
Member

Thanks, this is fixed in 3.2.6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ℹ️ status: need more info When waiting for user to supply database or more information.
Projects
None yet
Development

No branches or pull requests

4 participants