Creating Custom Online Payment Method in Magento 2

In this blog, we will learn to create online payment method. We will implement Stripe payment as a reference. You may change it as per your requirement. It will depend upon the library that the gateway provider gives.

Stripe Payment Gateway has several features, but we will be implementing the most basic ones. We will use the official PHP library of stripe i.e. this

It should be noticed that the library of Stripe is managed via Composer so it can be installed via that, alternatively, it can be downloaded and the files should be put in vendor/stripe. If we manage the whole process via composer, then it can be automatically included in the code through autoloader. But if we don’t, then we will have to include it in our code.

We will be creating several files and most of the files will be same as the previous blog. So, we will be dicussing about the important files only. Now, let’s get started:

The structure of the extension will look like this:

1. System.xml

This file is used to create the store configuration part for the extension so that the configuration for setting up the payment method can be done from admin panel easily. We will define several fields like API Key, Supported Credit Card Types etc.

2. Model

We will create a Model for handling the processing of payment and will define this model is the config.xml as explained in the previous blog:

In this file, there are two main methods:

  1. capture(): This method is handling the process of charging the credit card and saving the transaction. We are just fetching the billing address details from payment object, and the passing it to stripe API along with card details and order amount. The API will process this data and will return a charge object. We are just saving its id as transaction id for the order.
  2. refund(): This method will be used to process the refund. The previously saved transaction id will be sent to the API and then refund will be processed.

You can see the exact files and directory structure over here.

Creating Sandbox Account and Getting API Key:

To Create a sandbox account, go to and enter the required details and Sign Up. After creating the account, you will need to access the API key which needs to be entered in Payment Configuration (in Magento Admin Panel). Navigate to Developers -> API Keys. Here you will get two keys, Publishable key and Secret key.

You need to use the Secret key only. Copy it and save somewhere.

Now, we will see where to put the API key and how to do other configurations for the payment method, we have created. For this, navigate to Store -> Configuration -> Payment Methods -> Stripe

Here you can enter the API key and do other settings:

Now let’s see the use of all of the fields one by one:

Enabled: This is used to enable/disable the payment method

Title: The payment method title can be set here. It will be shown on frontend while checkout process on payment step.

API Key: The API key that we had got previously needs to be entered here

Debug: It is an optional field, this needs to be used if we want to make some entries in debug log whenever this payment method is used to complete the checkout process

Credit Card Types: Here we can select the  credit card types which needs to be supported

Payment from Applicable Countries/Payment from Specific Countries: Here we can select for which countries, the payment method should be used and shown on frontend while checkout.

Minimum Order Total/Maximum Order Total: Here we can define the rules based on order total. If the order total is less/more than the defined, then the payment method should be hidden/shown according to this only.

Sort Order: This field need to be manage the orde in which the payment method will be shown in respect of other available payment methods on frontend.