Skip to content
Sassy Infotech Pvt. Ltd.
Development

How to Build a WooCommerce Payment Gateway Plugin

How to build a custom WooCommerce payment gateway plugin with WC_Payment_Gateway, plus HPOS, block checkout support, security and testing.

Sassy Infotech Team · Updated 27 Nov 2026 · 6 min read

When should you build a custom gateway?

Before writing code, check whether your payment provider already offers an official WooCommerce plugin. Many do, including widely used Indian providers such as Razorpay, Paytm and CCAvenue, and an officially maintained plugin is usually the safer choice.

A custom gateway makes sense when:

  • Your provider has no plugin, or its plugin does not support your flow
  • You need a custom checkout flow, such as part payment, deposits or B2B invoicing
  • You are integrating an in-house or regional payment system
  • You need tighter control over order statuses, webhooks or reconciliation

Note that WooCommerce core only ships with offline methods (direct bank transfer, cheque and cash on delivery). Online gateways such as PayPal and Stripe are separate plugins built on the same API described here.

Key components of a payment gateway plugin

Component What it does
Gateway class Extends WC_Payment_Gateway and defines the gateway's ID, title and features
Settings form Lets the store admin enter API keys, merchant IDs and options
Payment processing process_payment() sends the request to the provider and updates the order
Webhook or callback handler Confirms payment status from the provider's server
Refunds (optional) process_refund() sends refunds through the provider's API
Block checkout integration Makes the gateway available on the Cart and Checkout blocks

Step-by-step guide

Step 1: Set up the plugin

Install WordPress and WooCommerce locally, then create a folder under /wp-content/plugins/ with a main PHP file. Load your class only after WooCommerce is available and register it with WooCommerce:

<?php
/**
 * Plugin Name: Example Gateway for WooCommerce
 * Requires Plugins: woocommerce
 */

add_action( 'plugins_loaded', function () {
    if ( class_exists( 'WC_Payment_Gateway' ) ) {
        require_once __DIR__ . '/class-wc-gateway-example.php';
    }
} );

add_filter( 'woocommerce_payment_gateways', function ( $gateways ) {
    $gateways[] = 'WC_Gateway_Example';
    return $gateways;
} );

The Requires Plugins header (WordPress 6.5 and later) tells WordPress that WooCommerce must be active.

Step 2: Create the gateway class and settings

<?php
class WC_Gateway_Example extends WC_Payment_Gateway {

    public function __construct() {
        $this->id                 = 'example';
        $this->method_title       = 'Example Gateway';
        $this->method_description = 'Accept payments through Example.';
        $this->has_fields         = false;
        $this->supports           = array( 'products' );

        $this->init_form_fields();
        $this->init_settings();
        $this->title = $this->get_option( 'title' );

        add_action(
            'woocommerce_update_options_payment_gateways_' . $this->id,
            array( $this, 'process_admin_options' )
        );
    }

    public function init_form_fields() {
        $this->form_fields = array(
            'enabled' => array( 'title' => 'Enable', 'type' => 'checkbox', 'default' => 'no' ),
            'title'   => array( 'title' => 'Title', 'type' => 'text', 'default' => 'Example Pay' ),
            'api_key' => array( 'title' => 'API key', 'type' => 'password' ),
        );
    }
}

Step 3: Handle payment processing

Add process_payment() to the class. It receives the order ID and must return a result and a redirect URL:

public function process_payment( $order_id ) {
    $order = wc_get_order( $order_id );

    // Call your provider's API here using wp_remote_post().

    // On confirmed success:
    $order->payment_complete( $transaction_id );
    WC()->cart->empty_cart();

    return array(
        'result'   => 'success',
        'redirect' => $this->get_return_url( $order ),
    );
}

If the payment fails, show the customer a clear message with wc_add_notice( $message, 'error' ) and return without completing the order.

For redirect-based providers (common with UPI and net banking), return the provider's payment page URL as redirect, leave the order as pending and mark it paid only after verifying the provider's webhook or callback. WooCommerce exposes an endpoint for this through the woocommerce_api_{gateway_id} action. Always verify the webhook signature.

Step 4: Add refunds if your provider supports them

Add 'refunds' to $this->supports and implement process_refund( $order_id, $amount, $reason ). Only declare refund support once the method works, because WooCommerce shows a refund button based on it.

Step 5: Support HPOS and block checkout

Two current WooCommerce requirements are easy to miss:

  • High-Performance Order Storage (HPOS). Use wc_get_order() and order methods such as $order->update_meta_data() rather than reading post meta directly, then declare compatibility with FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true ) on the before_woocommerce_init hook.
  • Cart and Checkout blocks. A classic WC_Payment_Gateway class on its own does not appear in the block-based checkout, which is the default for new stores. You need a separate integration: a PHP class extending AbstractPaymentMethodType, registered on the woocommerce_blocks_payment_method_type_registration hook, plus a small JavaScript file that calls registerPaymentMethod(). See the WooCommerce developer documentation for the current details.

Step 6: Test thoroughly

Use your provider's sandbox to test successful payments, failures, cancellations, refunds, duplicate webhooks and timeouts. Test on mobile, on both classic and block checkout, and with any caching or security plugins the store uses.

Security and compliance

  • Never store card numbers or CVV codes. Use the provider's hosted page, hosted fields or tokenisation so card data never touches your server.
  • Serve the whole store over HTTPS.
  • Keep API keys in settings or environment configuration, never in code repositories.
  • Verify every webhook signature and check the amount and currency against the order.
  • Follow PCI DSS guidance from your provider for the integration type you use.

Best practices

  • Keep checkout simple: fewer steps and clear instructions reduce abandoned orders.
  • Write clear error messages: tell customers what went wrong and what to do next.
  • Log carefully: use wc_get_logger() for debugging, without logging sensitive data.
  • Support localisation: make strings translatable and handle the currencies your provider supports.
  • Keep it updated: test against new WooCommerce and WordPress releases before your clients update.
  • Document it: give store admins setup steps and give developers notes on hooks and webhooks.

Common mistakes to avoid

  • Marking orders paid on the browser redirect alone, without server-side confirmation
  • Storing sensitive card data
  • Ignoring block checkout, so the gateway disappears on new stores
  • Reading and writing order data as post meta, which breaks with HPOS
  • Testing only the happy path

Payment gateways in India

Indian shoppers commonly pay by UPI, cards, net banking and wallets, usually through providers such as Razorpay, Paytm or CCAvenue. UPI is normally offered through one of these providers rather than integrated directly. If your provider has an official WooCommerce plugin, start there; build custom only for what the official plugin cannot do.

Need help?

Sassy Infotech builds and maintains WooCommerce stores and custom integrations from Surat. If you need a gateway built, reviewed or updated for HPOS and block checkout, see our WooCommerce development services or our wider e-commerce development work.

Frequently asked questions

Is WC_Payment_Gateway still the right way to build a WooCommerce gateway?

Yes. Extending WC_Payment_Gateway is still the standard approach for the gateway's settings and payment logic. For the Cart and Checkout blocks you add a separate block integration on top of that class.

Why does my custom gateway not show on the checkout page?

The most common reason is that the store uses the Checkout block and your gateway has no block integration. Other causes are the gateway being disabled in settings, or is_available() returning false because of currency or order total rules.

Can I accept UPI payments in WooCommerce?

Yes, usually through a payment provider that supports UPI and offers a WooCommerce plugin or API. The provider handles the UPI flow, and your plugin confirms payment through a webhook before marking the order paid.

Do I need PCI DSS compliance for a WooCommerce gateway?

Any store taking card payments has PCI DSS responsibilities. Using your provider's hosted payment page or hosted fields keeps card data off your server, which greatly reduces the scope. Check your provider's guidance for your integration type.

Should I build a custom gateway or use an existing plugin?

Use an official, maintained plugin when one meets your needs. Build custom when no plugin exists for your provider or you need a flow the plugin does not support.

Tell us what you are building.

A senior engineer replies within one business day, or message us on WhatsApp. NDA on request.