Building a Custom Payment Gateway in Magento: A Step-by-Step Guide

Table of Contents


As an online retailer, you understand the importance of having a seamless and secure payment process for your customers. Magento, an open-source e-commerce platform, offers a variety of payment gateways out-of-the-box, including PayPal and Stripe. However, if you’re looking to offer a custom payment solution to your customers, building a custom payment gateway in Magento may be the best option for you.

Building a custom payment gateway in Magento may sound intimidating, but with the right knowledge and tools, it can be a straightforward process. In this article, we’ll walk you through the step-by-step process of building a custom payment gateway in Magento, so you can offer your customers a seamless and secure checkout experience.

Preparing Your Environment

Before we dive into the technical steps of building a custom payment gateway in Magento, it’s important to ensure that your environment is set up correctly. Here are a few things you’ll need to have in place before you get started:

  1. A development environment: Building a custom payment gateway in Magento requires a development environment, separate from your live production site, where you can make changes and test them without affecting your customers. You can set up a development environment on your local machine using software like XAMPP or MAMP, or on a remote server.
  2. A Magento installation: You’ll need to have a working installation of Magento on your development environment. If you haven’t already done so, you can download the latest version of Magento from the official website and install it on your development environment.
  3. A merchant account: To process payments through your custom payment gateway, you’ll need to have a merchant account with a payment processor. Some popular payment processors include, Braintree, and PayPal.

Once you have these three things in place, you’re ready to start building your custom payment gateway in Magento.

Building a Custom Payment Gateway in Magento_ A Step-by-Step GuideCreating a Payment Gateway Module

The first step in building a custom payment gateway in Magento is to create a payment gateway module. A payment gateway module is a Magento extension that adds a new payment method to the checkout process. Here’s how to create a payment gateway module in Magento:

  1. Create a new directory: In your Magento installation, navigate to the app/code directory and create a new directory with the name of your payment gateway. For example, if you’re building a payment gateway for ABC Payments, you would create a directory called ABCPayments.
  2. Create a module file: Inside your new directory, create a module file called registration.php. This file will tell Magento about your payment gateway module and its location. Here’s an example of what the file should look like:

<?php MagentoFrameworkComponentComponentRegistrar::register( MagentoFrameworkComponentComponentRegistrar::MODULE, ‘ABCPayments_PaymentGateway’, __DIR__ );

Want to offer your customers a seamless payment experience on your Magento store? Contact us to create a custom payment gateway!

In this example, we’re registering a new Magento module called ABCPayments_PaymentGateway, and telling Magento that the module is located in the current directory (DIR).

  1. Create a module configuration file: In the same directory, create a new file called module.xml. This file will contain information about your payment gateway module, such as its name and version number. Here’s an example of what the file should look like:

<?xml version=”1.0″?> <config xmlns_xsi=”” xsi_noNamespaceSchemaLocation=”urn:magento:framework:Module/etc/module.xsd”> <module name=”ABCPay

  1. Create a payment gateway model: Inside your new directory, create a subdirectory called Model and create a new file called Payment.php. This file will contain the logic for your payment gateway. Here’s an example of what the file should look like:
<?php namespace ABCPaymentsPaymentGatewayModel; class Payment extends MagentoPaymentModelMethodAbstractMethod { protected $_code = ‘abcpayments_paymentgateway’; protected $_isGateway = true; protected $_canAuthorize = true; public function authorize(MagentoPaymentModelInfoInterface $payment, $amount) { // Payment authorization logic here } }

A custom payment gateway can help streamline the checkout process on your Magento store. Let us build one for you! Contact us today.

In this example, we’re creating a new payment gateway model called ABCPaymentsPaymentGatewayModelPayment, which extends Magento’s AbstractMethod class. We’re setting the payment gateway code to ‘abcpayments_paymentgateway’, indicating that this is the code that will be used to identify the payment method. We’re also setting the payment gateway to be a gateway method (as opposed to an offline method) and indicating that it can authorize payments. Finally, we’re implementing the authorize method, which contains the logic for authorizing payments.

  1. Enable your payment gateway module: To enable your payment gateway module, navigate to your Magento admin panel and go to Stores > Configuration > Sales > Payment Methods. You should see your new payment method listed under the “Other Payment Methods” section. Click on it to expand the settings, then set “Enabled” to “Yes” and fill in the rest of the required fields.

Congratulations! You’ve now created a custom payment gateway module in Magento. However, your payment gateway won’t work yet, because you haven’t connected it to a payment processor. Let’s fix that next.

Connecting Your Payment Gateway to a Payment Processor

To process payments through your custom payment gateway, you’ll need to connect it to a payment processor. There are many payment processors to choose from, but for the purposes of this article, we’ll use as an example.

Don’t settle for a generic payment gateway on your Magento store. Work with us to create a custom solution that fits your business needs.

  1. Sign up for an account: If you don’t already have an account, you’ll need to sign up for one. Go to the website and follow the instructions to create an account.
  2. Obtain your API login ID and transaction key: Once you have an account, log in to the Merchant Interface and navigate to Account > Settings > Security Settings > API Credentials & Keys. Here, you’ll find your API login ID and transaction key, which you’ll need to connect your payment gateway to
  3. Configure your payment gateway: In your payment gateway model (app/code/ABCPayments/PaymentGateway/Model/Payment.php), add the following code to the authorize method:
$api_login_id = ‘YOUR_API_LOGIN_ID_HERE’; $transaction_key = ‘YOUR_TRANSACTION_KEY_HERE’; $transaction_type = ‘AUTH_CAPTURE’; $amount = $payment->getOrder()->getGrandTotal(); $currency_code = $payment->getOrder()->getBaseCurrencyCode(); $post_url = ‘’; $post_values = array( ‘x_login’ => $api_login_id, ‘x_tran_key’ => $transaction_key, ‘x_version’ => ‘3.1’, ‘x_delim_data’ => ‘TRUE’, ‘x_delim_char’ => ‘|’, ‘x_relay_response’ => ‘FALSE’, ‘x_type’ => $transaction_type, ‘x_method’ => ‘CC’, ‘x_card_num’ => $payment->getCcNumber(), ‘x_exp_date’ => $payment->getCcExpMonth() . $payment->getCcExpYear(), ‘x_amount’ => $amount, ‘x_currency_code’ => $currency_code, ); $post_string = http_build_query($post_values); $curl = curl_init($post_url); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $post_string); $response = curl_exec($curl); curl_close($curl); $response_array = explode(‘|’, $response); if ($response_array[0] == ‘1’) { $payment->setTransactionId($response_array[6]); $payment->setIsTransactionClosed(0); } else { $error_message = $response_array[3]; throw new MagentoFrameworkExceptionLocalizedException(__($error_message)); }

In this code, we’re setting the API login ID and transaction key to the values you obtained from We’re also setting the transaction type to “AUTH_CAPTURE”, indicating that we want to authorize and capture the payment. We’re getting the order amount and currency code from the payment object, and setting the post URL to the test server URL.

Next, we’re setting up an array of post values to send to, including the API login ID, transaction key, transaction type, payment method, credit card number and expiration date, order amount and currency code. We’re then building a query string from this array and sending it to using cURL. Finally, we’re parsing the response from and either setting the transaction ID and closing the transaction if the payment was successful, or throwing a localized exception if there was an error.

  1. Test your payment gateway: To test your payment gateway, place an order on your Magento website using the payment method you just created. Make sure to use a test credit card number (such as 4111 1111 1111 1111) and a future expiration date. If everything is set up correctly, your payment should be processed successfully.


Q: Can I use a different payment processor besides

A: Yes, you can use any payment processor that provides an API for processing payments. You’ll need to modify the code in your payment gateway model accordingly.

Q: Is it difficult to create a custom payment gateway in Magento?

A: It can be challenging, especially if you’re not familiar with Magento development. However, with the right resources and some patience, it’s definitely doable


Creating a custom payment gateway in Magento can seem daunting at first, but with the right resources and a little bit of know-how, it’s definitely achievable. By following the steps outlined in this guide, you can create a payment gateway that integrates seamlessly with your Magento website and provides your customers with a secure and convenient way to pay.

Remember to test your payment gateway thoroughly before deploying it to your live website, and to adhere to all relevant security and compliance standards to protect your customers’ sensitive information.

With a custom payment gateway in place, you can streamline your checkout process, reduce cart abandonment, and provide your customers with a positive shopping experience that keeps them coming back for more.

Now that you know how to build a custom payment gateway in Magento, it’s time to put your skills to the test and start creating your own. Happy coding!

Ready to take your Magento store’s payment options to the next level? Our team can help you build a custom payment gateway. Reach out to us now!

Additional Resources

If you’re looking for more information on Magento development, payment gateway integration, or e-commerce in general, check out these helpful resources:

  • Magento DevDocs: The official documentation for Magento development, including tutorials, guides, and reference materials.
  • Magento Stack Exchange: A community-driven Q&A site where you can ask and answer questions about Magento development.
  • Developer Center: A resource for developers looking to integrate into their e-commerce websites.
  • Stripe Developer Docs: Documentation for Stripe, another popular payment gateway that integrates with Magento.
  • PayPal Developer Docs: Documentation for PayPal, another popular payment gateway that integrates with Magento.

By leveraging these resources and staying up to date on the latest trends and best practices in e-commerce development, you can create a custom payment gateway that meets your business’s unique needs and helps you achieve your goals. Good luck!

Read our expert written article on How to Customize Your Magento Theme

Frequently Asked Questions (FAQ)

Found this article interesting? Share it on

Contact us today