Skip to main content

Payment Gateway API

The WooCommerce Payment Gateway API allows you to create custom payment methods that integrate seamlessly with your WooCommerce store. This guide covers everything from basic gateway creation to advanced payment flows.

Overview

Payment gateways in WooCommerce are class-based and can be added through plugins. They extend the WC_Payment_Gateway class to handle payment processing, settings management, and order handling.

Types of Payment Gateways

WooCommerce supports four main types of payment gateways:
Redirects users to the payment processor’s website to complete payment. The user clicks a button that takes them offsite.Examples: PayPal Standard, Authorize.net DPMAdvantages:
  • Lower PCI compliance requirements
  • Reduced security burden on your store
  • Easier to implement
Loads the payment system inside an iframe on your store, keeping users on your site while processing payment externally.Examples: SagePay Form, PayPal AdvancedAdvantages:
  • Users stay on your domain
  • Moderate security requirements
  • Better user experience than full redirects
Payment fields appear directly on the checkout page. Payment is processed when the user clicks “Place Order.”Examples: PayPal Pro, Authorize.net AIM, StripeRequirements:
  • SSL certificate required
  • PCI compliance necessary
  • Higher security standards
Advantages:
  • Seamless checkout experience
  • Full control over the payment flow
  • No redirects
No online payment is processed. Used for cash, checks, bank transfers, or other manual payment methods.Examples: Check, Bank Transfer, Cash on DeliveryAdvantages:
  • Simple to implement
  • No technical requirements
  • Useful for B2B or local businesses
Form and iFrame-based gateways post data offsite, reducing security concerns. Direct gateways require SSL certificates and may require PCI compliance.

Creating a Basic Payment Gateway

Step 1: Set Up the Plugin

Create a new plugin file that hooks into WooCommerce:

Step 2: Create the Gateway Class

Your gateway must extend WC_Payment_Gateway:

Gateway Properties

The WC_Payment_Gateway class provides several important properties you can configure:
string
required
Unique identifier for your gateway (e.g., ‘custom_gateway’)
string
URL to an image shown next to the gateway name on checkout
boolean
default:"false"
Set to true if payment fields should appear on checkout (for direct gateways)
string
required
Title shown on the admin payment settings page
string
Description shown on the admin payment settings page
array
Array of features the gateway supports:
  • products - Standard product checkout
  • refunds - Refund processing via API
  • subscriptions - WooCommerce Subscriptions
  • pre-orders - WooCommerce Pre-Orders
  • tokenization - Saved payment methods
  • add_payment_method - Add payment method from My Account
  • default_credit_card_form - Use WooCommerce’s default credit card form

Direct Payment Gateways

For gateways that collect payment information directly on the checkout page:

Enable Payment Fields

Add Custom Payment Fields

Validate Payment Fields

Process Direct Payment

Direct payment gateways handling credit card data must use SSL/TLS encryption and comply with PCI DSS standards. Consider using tokenization or hosted payment fields to reduce your PCI compliance scope.

Payment Gateway Callbacks

Many payment processors use webhooks or callbacks to notify your store about payment status changes.

Setting Up the Callback URL

Use WooCommerce’s WC-API endpoint:

Handle Webhook Notifications

Processing Refunds

If your gateway supports refunds via API, add refund processing:

Order Status Best Practices

1

Payment Pending

Use On-Hold status when payment is submitted but needs manual verification or is awaiting confirmation from the payment processor.
2

Payment Failed

Use Failed status when payment processing fails or is declined.
3

Payment Complete

Use payment_complete() method when payment is successful. WooCommerce will automatically set the correct status (Processing or Completed) and handle stock reduction.
The payment_complete() method handles stock reduction and sets the appropriate order status automatically. Don’t manually call stock reduction functions when using this method.

Tokenization and Saved Payment Methods

Allow customers to save payment methods for future use:

Testing Your Payment Gateway

1

Enable Test Mode

Implement a test mode in your gateway settings to use sandbox credentials.
2

Test Order Flow

  • Create test products
  • Add to cart and proceed to checkout
  • Select your payment gateway
  • Complete payment with test credentials
  • Verify order status updates correctly
3

Test Refunds

Process test refunds from the WooCommerce order screen.
4

Test Webhooks

Use tools like ngrok to test webhook callbacks locally.