Allowing Users to Cancel Their Own Commerce Licenses

UPDATE: I’ve written a contrib module called Commerce License Cancel to get the ball rolling to make this a community effort. Read on if you’d like to see how I got there.

There’s been some talk in the issue queue for Commerce License Billing about needing a way for users to cancel their own licenses. Here are some pieces and parts that I’m using to achieve this. This is a work in progress and I’m really just posting here so I can provide a clean link in the issue queue. Sorry for the lack of detailed explanations; hopefully you still find this helpful.

There’s already a method to “revoke” a user license. When you revoke a license, the recurring billing should close out automatically and the role granted (if it’s a licensed role) will be taken away from the user.

The site I’m working on has just a few “membership plans” or levels that a user can purchase. For this reason, I could hardcode a few things. It’d take a LOT more work to make this stuff ready for the masses.

Providing the cancellation confirmation form

I chose to create a menu callback that renders a simple form. The URL ([productid_here]) allows for a product ID because I want the user to cancel a license that’s associated with a specific product.

Here’s what that looks like (work in progress, of course):


First, I created a permission to make sure only specific roles can cancel their own memberships:

Here’s the hook_menu implementation:

Here’s the form definition and its validation/submission callbacks:

Rendering the “Cancel membership” link

To get the user to that cancellation form I actually override the Add to Cart form for the membership products so that if the user already has the license for the product, they can cancel it instead of add it to their cart. Two birds with one stone, right?

I have two membership products (monthly recurring and yearly recurring) shown under a single product display. As I said, this site really only has a few products, so some things are hardcoded (like MEMBERSHIP_PREMIUM_MONTHLY, which is just an integer of “7” (the product ID for that product)).

I’m probably going to have to clean this up a bit, but it works for now.


One Comment

Leave a Reply

Your email address will not be published.