Payment gateway configuration lets your WordPress site accept online payments safely and easily.
Payment gateway configuration in Wordpress
<?php // Example: Adding a payment gateway in WooCommerce add_filter('woocommerce_payment_gateways', 'add_custom_gateway'); function add_custom_gateway($gateways) { $gateways[] = 'WC_Gateway_Custom'; return $gateways; } class WC_Gateway_Custom extends WC_Payment_Gateway { public function __construct() { $this->id = 'custom_gateway'; $this->method_title = __('Custom Gateway', 'woocommerce'); $this->has_fields = false; $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/Disable', 'woocommerce'), 'type' => 'checkbox', 'label' => __('Enable Custom Payment Gateway', 'woocommerce'), 'default' => 'yes' ), 'title' => array( 'title' => __('Title', 'woocommerce'), 'type' => 'text', 'description' => __('This controls the title shown during checkout.', 'woocommerce'), 'default' => __('Custom Payment', 'woocommerce'), 'desc_tip' => true ) ); } public function process_payment($order_id) { $order = wc_get_order($order_id); $order->payment_complete(); $order->reduce_order_stock(); wc_add_notice(__('Payment received, thank you!', 'woocommerce'), 'success'); return array( 'result' => 'success', 'redirect' => $this->get_return_url($order) ); } }
This example shows how to add a custom payment gateway in WooCommerce, a popular WordPress plugin.
You register the gateway, define settings, and handle payment processing.
<?php // Enable PayPal gateway in WooCommerce add_filter('woocommerce_payment_gateways', function($gateways) { $gateways[] = 'WC_Gateway_Paypal'; return $gateways; });
<?php
// Set payment gateway title
$this->form_fields = array(
'title' => array(
'title' => __('Title', 'woocommerce'),
'type' => 'text',
'default' => __('Credit Card', 'woocommerce')
)
);This plugin adds a simple custom payment gateway to WooCommerce. It shows a new payment option during checkout. When selected, it marks the order as paid immediately.
<?php /** * Plugin Name: Simple Custom Payment Gateway * Description: Adds a simple custom payment gateway to WooCommerce. * Version: 1.0 * Author: Your Name */ add_filter('woocommerce_payment_gateways', 'add_simple_custom_gateway'); function add_simple_custom_gateway($gateways) { $gateways[] = 'WC_Gateway_Simple_Custom'; return $gateways; } class WC_Gateway_Simple_Custom extends WC_Payment_Gateway { public function __construct() { $this->id = 'simple_custom_gateway'; $this->method_title = __('Simple Custom Gateway', 'woocommerce'); $this->has_fields = false; $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/Disable', 'woocommerce'), 'type' => 'checkbox', 'label' => __('Enable Simple Custom Gateway', 'woocommerce'), 'default' => 'yes' ), 'title' => array( 'title' => __('Title', 'woocommerce'), 'type' => 'text', 'description' => __('Title shown during checkout.', 'woocommerce'), 'default' => __('Simple Custom Payment', 'woocommerce'), 'desc_tip' => true ) ); } public function process_payment($order_id) { $order = wc_get_order($order_id); $order->payment_complete(); $order->reduce_order_stock(); wc_add_notice(__('Payment received, thank you!', 'woocommerce'), 'success'); return array( 'result' => 'success', 'redirect' => $this->get_return_url($order) ); } }
Always test payment gateways in a safe test mode before going live.
Use HTTPS on your site to keep payment data secure.
Check WooCommerce documentation for updates on payment gateway development.
Payment gateway configuration connects your WordPress site to payment services.
It involves adding, enabling, and setting up gateways like PayPal or custom ones.
Proper setup ensures smooth and secure online payments for your customers.
Practice
Solution
Step 1: Understand what a payment gateway does
A payment gateway connects the website to payment services to process payments.Step 2: Identify the correct purpose in the options
Only To allow customers to make payments securely on the website mentions secure payments, which matches the gateway's role.Final Answer:
To allow customers to make payments securely on the website -> Option AQuick Check:
Payment gateway = secure payments [OK]
- Confusing payment gateway with website design features
- Thinking it manages blog posts
- Assuming it speeds up the site
Solution
Step 1: Identify correct WordPress hook type
To modify gateways list, use add_filter, not add_action.Step 2: Check function signature and return
The function must accept $gateways array, add new gateway, and return the updated array.Final Answer:
add_filter with function adding to $gateways and returning it -> Option AQuick Check:
Filter modifies list and returns it [OK]
- Using add_action instead of add_filter
- Not returning the updated gateways array
- Missing $gateways parameter in function
add_filter('woocommerce_payment_gateways', function($gateways) {
$gateways[] = 'WC_Gateway_Custom';
return $gateways;
});What will happen after this code runs?
Solution
Step 1: Understand the add_filter usage
The anonymous function receives $gateways array, adds a new gateway, and returns the updated array.Step 2: Predict the effect on WooCommerce gateways
The new gateway 'WC_Gateway_Custom' is appended, so it becomes available alongside existing gateways.Final Answer:
The custom payment gateway 'WC_Gateway_Custom' will be added to the available gateways list -> Option DQuick Check:
Appending gateway and returning list adds it [OK]
- Thinking the list is replaced instead of appended
- Assuming no return means error
- Confusing add_filter with add_action
add_filter('woocommerce_payment_gateways', 'add_gateway');
function add_gateway() {
$gateways[] = 'WC_Gateway_New';
return $gateways;
}What is the main problem?
Solution
Step 1: Check function parameters for filter hook
Filter functions must accept the value they modify, here $gateways array.Step 2: Identify missing parameter causing error
Function add_gateway() has no parameters, so $gateways is undefined inside, causing error.Final Answer:
The function add_gateway does not accept the $gateways parameter -> Option BQuick Check:
Filter function needs $gateways parameter [OK]
- Omitting the parameter in the filter function
- Using add_action instead of add_filter
- Not returning the updated array
Solution
Step 1: Understand how to conditionally show gateways
WooCommerce gateways can override the is_available() method to control when they appear.Step 2: Apply country check in is_available()
Checking the customer's billing country inside is_available() and returning true only for 'US' ensures correct display.Final Answer:
Override is_available() to check billing country and return true only for 'US' -> Option CQuick Check:
Use is_available() for conditional gateway display [OK]
- Trying to hide gateway with CSS (not secure or reliable)
- Filtering gateways after checkout (too late)
- Only enabling gateway in settings without conditions
Ready to prove it?
Pass the test and this pattern moves unknown
