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.