Back to Top

How to create a payment gateway extension in WooCommerce

Updated 18 July 2024

In this article, we will take a look at how to create a custom payment gateway in WooCommerce payment.

Are you looking to add a custom payment gateway to your WooCommerce online store?

WooCommerce, being one of the most popular e-commerce platforms, offers flexibility and customization options for integrating various payment gateways to cater to your specific business needs.

In this blog post, we will provide you with a step-by-step guide on how to create a custom payment gateway in WooCommerce.

Why Create a Custom Payment Gateway?

A payment gateway is a critical component of any online store as it facilitates the secure processing of online payments from customers.

Start your headless eCommerce
now.
Find out More

While WooCommerce offers several built-in payment gateways, there may be instances where you need to add a custom payment gateway to meet unique requirements.

Creating a custom payment gateway allows you to integrate third-party payment processors, payment gateways from local banks, or custom payment methods that are specific to your business.

This enables you to offer a seamless checkout experience to your customers and accept payments in the way that works best for your business.

If you require expert assistance or want to develop custom unique functionality, hire WooCommerce Developers for your project.

Creating a payment gateway extention

init_form_fields(): This function defines the settings fields that will be displayed in the WooCommerce admin settings page for your custom payment gateway.

You can specify the type of fields, their labels, and their default values.

init_settings(): This function initializes the settings for your custom payment gateway. You can set default values for your settings here.

process_payment($order_id): This function is called when a customer submits an order with your custom payment gateway selected.

It handles the processing of the payment and returns the result, which can be either success, failure, or an error message.

payment_fields(): This function displays the fields that the customer needs to fill in during the checkout process, such as credit card information or any other required information for your custom payment gateway.

validate_fields(): This function validates the fields submitted by the customer during the checkout process.

You can perform any necessary validation, such as checking for valid credit card information or verifying other required fields.

process_refund($order_id, $amount = null, $reason = ”): This function handles the refund process for your custom payment gateway. It can process partial or full refunds and returns the result.

admin_options(): This function displays the settings fields in the WooCommerce admin settings page for your custom payment gateway, allowing the admin to configure the settings.

we offer WooCommerce Payment Method Development services to help businesses integrate custom payment gateways seamlessly.

Step 1: Define the Custom Payment Gateway Class

To create a custom payment gateway, you need to define a class that extends the WooCommerce Payment Gateway API. Here’s an example:

class Custom_Payment_Gateway extends WC_Payment_Gateway {
// Constructor for initializing the payment gateway
public function __construct() {
$this->id = 'custom_payment_gateway';
$this->method_title = 'Custom Payment Gateway';
$this->method_description = 'Add a custom description here.';
$this->has_fields = true;
$this->init_form_fields();
$this->init_settings();
$this->title = $this->get_option('title');
$this->description = $this->get_option('description');
// Add more settings here
// ...
add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
}

// Initialize settings fields
public function init_form_fields() {
$this->form_fields = array(
'title' => array(
'title' => __('Title', 'woocommerce'),
'type' => 'text',
'description' => __('This controls the title displayed during checkout.', 'woocommerce'),
'default' => __('Custom Payment Gateway', 'woocommerce'),
'desc_tip' => true,
),
'description' => array(
'title' => __('Description', 'woocommerce'),
'type' => 'textarea',
'description' => __('This controls the description displayed during checkout.', 'woocommerce'),
'default' => __('Pay using Custom Payment Gateway', 'woocommerce'),
'desc_tip' => true,
),
// Add more fields here
// ...
);
}

// Process payment
public function process_payment($order_id) {
// Handle payment processing logic here
// ...
}

// Display payment fields during checkout
public function payment_fields() {
// Display payment fields such as credit card info or other required info
// ...
}
// Validate payment fields
public function validate_fields() {
// Validate payment fields submitted by the customer
// ...
}
}

Step 2: Add Hooks and Filters

To integrate your custom payment gateway into the WooCommerce checkout process, you’ll need to add appropriate hooks and filters. Here’s an example:

// Add custom payment gateway to available payment gateways in checkout
add_filter('woocommerce_payment_gateways', 'add_custom_payment_gateway');
function add_custom_payment_gateway($gateways) {
$gateways[] = 'Custom_Payment_Gateway';
return $gateways;
}

View of payment gateway

After completed these two steps custom payment gateway listed in Woocommerce->settings->payments section.

create-a-payment-gateway-extension

Step 3: Configure your WooCommerce payment gateway.

Now it’s time to configure your WooCommerce payment gateway according to your website and needs. The plugin you choose will offer additional details about how to configure it to your specific needs.

Take another look at the screenshot above. You’ll click the Set up button if this is the first time you’re configuring the payment gateway.

To make any additional changes after set-up, you’ll click the Manage button.

Step 4: Checkout process of payment gateway

After configuration the payment gateway.Test on the checkout page after added the item into the cart.

On checkout page, payment option will be show in payment list.

payment-gateway-extension

Step 5: Test Your Custom Payment Gateway

Thoroughly test your custom payment gateway to ensure it’s working correctly. Test scenarios such as successful payments, failed payments, refunds, and error handling.

Step 6: Deploy Your Custom Payment Gateway

Once you’ve tested and verified that your custom payment gateway is working correctly.

You can deploy it on your live WooCommerce store by uploading the plugin to your WordPress plugins directory and activating it in your admin dashboard.

By following these steps and using the example provided.

You can create a custom payment gateway in WooCommerce to offer unique payment options to your customers.

Remember to thoroughly test your custom payment gateway before deploying it to your live store.

To ensure a seamless and secure checkout experience for your customers.

We have multiple popular payment gateway addon like: Woocommerce Marketplace Paypal Commerce and other payment gateway.

Support

For any technical assistance kindly raise a ticket or reach us by email at [email protected]. Thanks for Your Time! Have a Good Day!

Also, discover various solutions to add more features and enhance your online store by visiting the WooCommerce plugins.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home