WC_Payment_Gateway class. This guide covers the essential methods, properties, and patterns for building payment gateways.
Gateway Types
Payment gateways in WooCommerce come in several varieties:- Form-based: User is redirected to the payment processor’s website (e.g., PayPal Standard)
- iFrame-based: Payment system loads in an iframe on your store
- Direct: Payment fields appear directly on checkout (e.g., credit card forms)
- Offline: No online payment is made (e.g., check, bank transfer)
Direct gateways require SSL certificates and may need PCI compliance. Form and iFrame-based gateways post data offsite, reducing security concerns.
The WC_Payment_Gateway Class
All payment gateways extend theWC_Payment_Gateway class, which provides the foundation for processing payments and managing settings.
Key Properties
string
required
Unique identifier for your gateway (e.g.,
'your_gateway')string
Title shown on the admin payment settings page
string
Description shown on the admin payment settings page
string
Payment method title displayed to customers during checkout
string
Description displayed to customers during checkout
string
Whether the gateway is enabled (
'yes' or 'no')boolean
Set to
true if the gateway displays payment fields on checkout (for direct integrations)array
Array of features this gateway supports (e.g.,
'products', 'refunds', 'tokenization')string
URL to an image displayed next to the gateway name on the frontend
Creating a Basic Payment Gateway
1
Create a plugin file
Payment gateways should be created as separate plugins that hook into WooCommerce.
2
Define your gateway class
Create a class that extends
WC_Payment_Gateway after plugins are loaded.3
Register the gateway with WooCommerce
Use the
woocommerce_payment_gateways filter to add your gateway to the list.Required Methods
init_form_fields()
Define the settings fields that appear on the gateway’s admin settings page./plugins/woocommerce/includes/gateways/cheque/class-wc-gateway-cheque.php:71-102 for a complete example.
process_payment( $order_id )
Handle payment processing and return the result. This is the core method of any payment gateway.Use
$order->payment_complete() instead of update_status() when payment is confirmed. This ensures stock reductions are made and the status changes to the correct value./plugins/woocommerce/includes/gateways/cheque/class-wc-gateway-cheque.php:144-170 for a real implementation.
Real-World Example: Cheque Gateway
Here’s a simplified version of WooCommerce’s built-in cheque gateway:Direct Payment Gateways
For gateways that collect payment information directly on checkout:1
Enable payment fields
2
Implement payment_fields()
Display your custom payment form.
3
Implement validate_fields()
Validate the submitted payment data.
4
Process payment in process_payment()
Supporting Refunds
To support refunds, declare support and implement the refund method:Payment Gateway Callbacks
For gateways that use callbacks (like PayPal IPN), use the WC-API hook:Order Status Best Practices
- On-Hold: Order completed but requires manual verification
- Failed: Payment attempt failed
- Processing/Completed: Use
$order->payment_complete()to let WooCommerce handle the status
Available Methods
TheWC_Payment_Gateway class provides these useful methods:
See
/plugins/woocommerce/includes/abstracts/abstract-wc-payment-gateway.php for the complete implementation.
Next Steps
Payment Token API
Add tokenization support to store payment methods
Settings API
Learn more about the WooCommerce Settings API