0
0
Wordpressframework~5 mins

Payment gateway configuration in Wordpress

Choose your learning style9 modes available
Introduction

Payment gateway configuration lets your WordPress site accept online payments safely and easily.

You want to sell products or services on your WordPress website.
You need to accept credit card or digital wallet payments from customers.
You want to connect your site to popular payment providers like PayPal or Stripe.
You want to manage payment settings like currency, test mode, and transaction options.
You want to provide a smooth checkout experience for your visitors.
Syntax
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.

Examples
Adds PayPal as a payment option in WooCommerce checkout.
Wordpress
<?php
// Enable PayPal gateway in WooCommerce
add_filter('woocommerce_payment_gateways', function($gateways) {
    $gateways[] = 'WC_Gateway_Paypal';
    return $gateways;
});
Defines the title shown to customers during checkout.
Wordpress
<?php
// Set payment gateway title
$this->form_fields = array(
    'title' => array(
        'title' => __('Title', 'woocommerce'),
        'type' => 'text',
        'default' => __('Credit Card', 'woocommerce')
    )
);
Sample Program

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.

Wordpress
<?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)
        );
    }
}
OutputSuccess
Important Notes

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.

Summary

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.