| name | woo-payments |
| description | Build WooCommerce payment gateways — WC_Payment_Gateway, direct/redirect/hosted integrations, tokenization, subscriptions support, refunds, and PCI compliance. Use when creating custom payment method integrations.
|
WooCommerce Payment Gateway Development
Before writing code
Fetch live docs:
- Web-search
site:developer.woocommerce.com payment gateway api for payment gateway guide
- Web-search
woocommerce payment gateway tutorial for implementation patterns
- Fetch
https://woocommerce.github.io/code-reference/classes/WC-Payment-Gateway.html for class reference
Gateway Architecture
Base Class: WC_Payment_Gateway
All gateways extend WC_Payment_Gateway (which extends WC_Settings_API):
| Property | Description |
|---|
$id | Unique gateway identifier |
$method_title | Admin-facing title |
$method_description | Admin-facing description |
$title | Customer-facing title |
$description | Customer-facing description |
$icon | URL to gateway icon |
$has_fields | Whether gateway has payment fields on checkout |
$supports | Array of supported features |
Registration
Filter woocommerce_payment_gateways:
add_filter( 'woocommerce_payment_gateways', function( $gateways ) {
$gateways[] = 'My_Payment_Gateway';
return $gateways;
});
Implementation Methods
Required Methods
__construct() — set $id, $method_title, $supports, call init_form_fields(), init_settings(), load saved settings, register process_admin_options hook
init_form_fields() — define admin settings (API keys, sandbox mode, etc.)
process_payment( $order_id ) — core payment logic, returns result + redirect array
Optional Methods
payment_fields() — render custom payment form HTML on checkout
validate_fields() — validate payment form input
process_refund( $order_id, $amount, $reason ) — handle refunds
get_icon() — customize the gateway icon display
process_payment Return Format
return [
'result' => 'success',
'redirect' => $order->get_checkout_order_received_url(),
];
Integration Models
Direct Integration
Gateway processes payment inline:
- Collect card data via
payment_fields() (or tokenized via JS SDK)
- Call payment processor API in
process_payment()
- Mark order:
$order->payment_complete( $transaction_id )
- Return success with redirect to thank-you page
Redirect Integration
Customer redirected to external payment page:
process_payment() returns redirect URL to processor
- Processor handles payment and redirects back
- Handle return via a webhook or callback URL
- Process the response and update order status
Hosted/Iframe Integration
Payment form loaded in iframe on checkout page:
- Set
$has_fields = true
- Render iframe/JS SDK in
payment_fields()
- Handle tokenization callback
Supports Array
$this->supports = [
'products',
'refunds',
'tokenization',
'subscriptions',
'subscription_cancellation',
'subscription_reactivation',
'subscription_suspension',
'subscription_amount_changes',
'subscription_date_changes',
];
Tokenization (Saved Payment Methods)
WC_Payment_Token System
- Extend
WC_Payment_Token_CC (credit cards) or WC_Payment_Token (generic)
$this->supports[] = 'tokenization'
- Override
tokenization_script() to enqueue JS
- Save tokens via
WC_Payment_Tokens::set_users_default()
- Customer manages saved methods in My Account > Payment Methods
Webhook/IPN Handling
Registering Webhook Endpoints
Use WC_API to register callback URLs:
add_action( 'woocommerce_api_{$id}', [ $this, 'handle_webhook' ] )
- Callback URL:
home_url( '/wc-api/{$id}/' )
- Verify signatures, process payment notifications, update order status
Order Status Management
$order->update_status( 'processing', 'Payment received' ) — status + note
$order->payment_complete( $transaction_id ) — marks paid, triggers emails
$order->update_status( 'failed', 'Payment failed' ) — mark failed
$order->add_order_note( 'Note text' ) — add admin/customer note
Block Checkout Support
For block-based checkout, gateways need a JS integration:
- Register a payment method via
@woocommerce/blocks-registry → registerPaymentMethod()
- Provide React components for:
content, edit, label
- Define
canMakePayment callback
- Handle payment data submission via
onPaymentSetup event
Best Practices
- Never log or store raw credit card data
- Always use sandbox/test mode for development
- Verify webhook signatures to prevent spoofing
- Handle all error states gracefully with clear customer messages
- Support refunds if the processor allows them
- Declare
cart_checkout_blocks compatibility and provide block checkout integration
- Use
$order->payment_complete() — it handles status, stock reduction, and email triggers
- Store transaction IDs via
$order->set_transaction_id()
Fetch the WooCommerce payment gateway documentation and code reference for exact method signatures, supported features, and block checkout integration patterns before implementing.