How to Set up a Custom Drupal Commerce Payment Method and Restrict it for a Certain User Role
Hello there! Sorry for not writing for so long, had been quite busy for the last couple of weeks. Yesterday I had a task to set up a sort of cashless payment method for a certain users and generate monthly reports for orders made with this payment method. Firstly, it seemed like a very specific task and I started thinking about writing a custom payment method from sketch, but fortunately, after quick googling of this matter, I achieved this goal not writing a single line of custom code at all. Here's a step-by-step guide.
Setting up Commerce Commerce Custom Offline Payments
1. Download and install the Commerce Custom Offline Payments module
There’s actually a few similar modules around but this one seems to be an aggregator of all custom payments related functionality. Commerce COP is meant for offline payments (say, payments on product delivery) and triggers the checkout completion event (and all the custom logic attached to it–we have it a lot). Checkout completion happens right after entering billing information without asking for credit card or anything else.
Download and install it manually or by using the following drush command:
drush en commerce_cop –y
2. Create a new custom payment method
Go to admin/commerce/config/custom-offline-payments/add
Here we’ve got a few fields to fill in:
- Title: the name (and machine name) of the method (for instance, Cashless Payment)
- Description: this text is meant to be shown on the checkout page but I couldn’t find it there even on a clear Drupal installation (at the moment of writing). It doesn’t matter though, because there’s the next information field.
- Information: additional text to be shown on checkout page when this payment method is selected. This value actually shows up.
- Status checkbox: check if you want to activate this payment method immediately.
3. Restrict usage of this payment method to certain user roles
This part is also quite simple. Go to the payment method rules page admin/commerce/config/payment-methods
and edit the created rule. Add the condition User->User has role(s), set the data selector to site:current-user and select to user role(s) you want this payment method to be available to. Save the rule. If you did not check the Status checkbox in the previous step, this payment method rule is still disabled. You can enable it right here by clicking on the enable link.
Now the new cashless payment method is ready to use.
Setting up a Views page with orders of a certain payment method
In addition, I want to explain how to create a view that will be displaying only the orders made with this new cashless payment method.
1. Create a new view
Go to admin/structure/views/add
. Select show Commerce Order entity, write view name, page title (say, “Cashless Orders List”) and path (the URL this page will be available at). Click Save & continue.
2. Set up display and add necessary fields.
I wanted to see the results as a table, so I set format to table (on the left column). Further, I needed to add some fields that are not available by default, like order owner or payment method. To expose them, two additional relationships are needed. They can be added in the third Advanced settings column. Press the Add button within the Relationship section and add Commerce Order: Owner and Commerce Order: Payment Transaction. Leave the rest of the further options with their defaults.
Now in the field section you should have Commerce Payment Transaction: Payment method and User fields available. The bunch of user fields refers to the User object of the order owner. Now you can sort and filter the records as you like.
For my custom report, I added the Order ID field, updated date and grouping by month. The latter can be achieved with the Date module and custom date format that uses month and year only.
Obviously, this article covers the very basics of what can be done with Commerce Custom Offline Payments. There’s a bunch of sub-modules like Pay in person payment or Bank payment and other that add preconfigured payment methods for common use cases. Just enable and check them out.
The module description also says that there’s a built-in features support so you can set up a payment method in your development environment and then quickly move it to production with a feature transfer. I didn’t try it, but if set up a few methods and don’t want to go through this process over again, you should definitely take a look at this functionality.