Intro
In the following example, the My_Payment_Gateway
class extends the WC_Payment_Gateway
(see class contents) class, which is the base class for payment gateways in WooCommerce. The class defines the necessary properties and methods for a custom payment gateway, including the init_form_fields
method for defining the settings that are displayed in the WooCommerce settings page, and the process_payment
method for processing the payment.
By using OOP principles, you can create a modular and reusable payment gateway that can be easily customized and extended. For example, you could create a subclass of My_Payment_Gateway
that adds additional functionality, or create a separate payment gateway class for a different payment processor.”
<?php
class My_Payment_Gateway extends WC_Payment_Gateway {
public function __construct() {
$this->id = 'my_payment_gateway';
$this->has_fields = true;
$this->method_title = 'My Payment Gateway';
$this->method_description = 'Pay with My Payment Gateway';
$this->supports = array('products');
$this->init_form_fields();
$this->init_settings();
$this->title = $this->get_option('title');
$this->description = $this->get_option('description');
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 My Payment Gateway', 'woocommerce'),
'default' => 'yes',
),
'title' => array(
'title' => __('Title', 'woocommerce'),
'type' => 'text',
'description' => __('This controls the title which the user sees during checkout.', 'woocommerce'),
'default' => __('My Payment Gateway', 'woocommerce'),
'desc_tip' => true,
),
'description' => array(
'title' => __('Description', 'woocommerce'),
'type' => 'textarea',
'description' => __('This controls the description which the user sees during checkout.', 'woocommerce'),
'default' => __('Pay with My Payment Gateway', 'woocommerce'),
),
);
}
public function process_payment($order_id) {
$order = wc_get_order($order_id);
$order->update_status('processing', __('Payment has been received.', 'woocommerce'));
WC()->cart->empty_cart();
return array(
'result' => 'success',
'redirect' => $this->get_return_url($order),
);
}
}
function add_my_payment_gateway($gateways) {
$gateways[] = 'My_Payment_Gateway';
return $gateways;
}
add_filter('woocommerce_payment_gateways', 'add_my_payment_gateway');
Extend, exteeend, exteeeeeeeend
For the next example, the My_Payment_Gateway_Pro
class extends the My_Payment_Gateway
class and overrides the __construct
, init_form_fields
, and process_payment
methods to add additional functionality. The __construct
method changes the id, method_title, and method_description properties to differentiate it from the base class. The init_form_fields method calls the parent method to add the existing form fields, and then adds a new pro_setting field to the form. The process_payment method calls the parent method to process the payment with the base class functionality, and then adds additional functionality specific to the My_Payment_Gateway_Pro
class.
By using inheritance, you can create a modular and extensible system of payment gateways that can be easily customized and extended for different payment processors or additional functionality. You could create additional subclasses that add support for different payment processors, or create a separate class hierarchy for different types of payment gateways (such as credit card vs. PayPal).
Let’s see that in action! For our example, we will create two new gateways: My_Credit_Card_Gateway
and My_PayPal_Gateway
. The My_Credit_Card_Gateway and My_PayPal_Gateway classes both extend the My_Payment_Gateway class to inherit the base functionality of a payment gateway. They override the __construct method to change the id, method_title, and method_description properties to differentiate them from the base class. They also override the process_payment method to add the specific functionality for each payment processor.”
<?php
class My_Credit_Card_Gateway extends My_Payment_Gateway {
public function __construct() {
parent::__construct();
$this->id = 'my_credit_card_gateway';
$this->method_title = 'My Credit Card Gateway';
$this->method_description = 'Pay with My Credit Card Gateway';
}
public function process_payment($order_id) {
// process payment for My Credit Card Gateway
parent::process_payment($order_id);
}
}
class My_PayPal_Gateway extends My_Payment_Gateway {
public function __construct() {
parent::__construct();
$this->id = 'my_paypal_gateway';
$this->method_title = 'My PayPal Gateway';
$this->method_description = 'Pay with My PayPal Gateway';
}
public function process_payment($order_id) {
// process payment for My PayPal Gateway
parent::process_payment($order_id);
}
}
By using inheritance, you can create a modular and extensible system of payment gateways that can be easily customized and extended for different payment processors. For example, you could create a My_Bitcoin_Gateway
class that extends the My_Payment_Gateway
class to add support for bitcoin payments, or a My_Subscription_Gateway
class that extends the My_Payment_Gateway
class to add support for subscription payments. See how limitless the possibilities are?