Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
  1. Code
  2. OpenCart

Create a Custom Payment Method in OpenCart: Part 2

Read Time:6 minsLanguages:
This post is part of a series called Create a Custom Payment Method in OpenCart.
Create a Custom Payment Method in OpenCart: Part 1
Create a Custom Payment Method in OpenCart: Part 3

There are many payment methods available in the OpenCart core itself and plenty of others are available at the OpenCart store in the form of third party extensions. Although sometimes you'll find yourself in the situation where you need something different, either there is no method available for your choice of payment gateway or you want some different logic. In either case, you're left with the only option: To create a new payment method module in OpenCart.

In this tutorial, we'll see the back-end part of the custom method. We'll see how to list your custom payment method along with the other payment methods. Further, we'll check how you can create the configuration form for your custom payment method.

We'll assume that our custom payment method name is "custom". There are at least three files you need to create in order to set up the things. Let's check the same in detail.

Setting Up the Controller

Go ahead and create the controller file at admin/controller/payment/custom.php. Paste the following contents in the newly created controller file custom.php. This file will be responsible for displaying our back-end configuration form by calling the appropriate view file and at the same time saving the post values of the form to the database.

As you can see, the class name follows the standard OpenCart naming conventions. Let's understand each section.

In the beginning of the index method we load the language file and set the value of the <title> tag.

In the next section, we load the model file "setting.php which will provide us the methods to save post values to the database. We also check if post values are available we'll save it to the database.

Further, next couple of lines of code is just used to set up the static labels which will be used in the template file.

Next, we've set up the "action" variable to make sure form is submitted to our "index" method when submitted. And in the same way, user is taken back to list of payment methods if she clicks on "Cancel" button.

Furthermore, there's code to populate the default values of the configuration form fields either in add or edit mode.

In the next section, we load the different order status values that area available, which will be used for the drop-down in the configuration form for the Order Status field.

Finally, we assign our custom template file custom.tpl and render the view.

That's the set up for the controller file.

Language and Template Files

So now that we're done with the controller file, let's create the language and template files. Go ahead and create the language file at admin/language/english/payment/custom.php. Make sure that your language file looks like code that we've provided below in our example custom.php.

As you can see we've just set up the language variables earlier used in the controller file. This is pretty simple and straightforward.

Let's create the view file. Go ahead and create the template file at admin/view/template/payment/custom.tpl. Paste the following contents in the newly created template file custom.tpl.

Again, this should be pretty easy to understand: The purpose of this template file is to provide the configuration form for our custom payment method. It uses the variables we set up earlier in the controller file.

How It Looks

We're done with the files set up. Now it's time to move on and see everything things in action. Head over to the admin section of the OpenCart and Go to Extensions > Payment. You should see that our custom method is listed just below the Cash On Delivery.

Custom Payment Method Listed in the Back-EndCustom Payment Method Listed in the Back-EndCustom Payment Method Listed in the Back-End

Click on the install link to install our custom payment method. After installing, you should be able to see the Edit link to open the configuration form. Click on the Edit link.

Custom Payment Method Configuration FormCustom Payment Method Configuration FormCustom Payment Method Configuration Form

So this is the configuration form for which we've done all the hard work so far! As you can see we've provided the generic Order Status and "Status fields. In addition to that, we've just demonstrated how you could provide the custom parameters as well by setting up Parameter One and Parameter Two.

You can fill up the form and save it by clicking on the Save button. We've set up the code for this in our index method of controller so it should work out of the box! And yes, don't forget to set the status to Enabled if you want your method available in the front-end!

In the next and final part we'll see the front-end counterpart of the same. Don't forget to share your thoughts using the feed below!

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.