I am in the process of creating a plugin to integrate a custom WooCommerce payment gateway. Although I have successfully activated it in the admin panel but the payment option does not yet appear on the checkout page.
WooCommerce version: 8.4.0
WordPress version: 6.4.2
Here is the complete code of my plugin
if (!defined("ABSPATH")) {
exit(); // Exit if accessed directly.
}
// Hook to plugins_loaded to ensure WooCommerce is loaded
add_action("plugins_loaded", "init_woocommerce_to_stripe", 0);
function init_woocommerce_to_stripe()
{
if (!class_exists("WC_Payment_Gateway")) {
return;
}
// Define the custom payment gateway class
class WC_Gateway_WooCommerce_To_Stripe extends WC_Payment_Gateway
{
// Declare properties
public $redirect_url;
public function __construct()
{
// Basic setup for the gateway
$this->id = "woocommerce_to_stripe";
$this->has_fields = false;
$this->icon = "";
$this->method_title = "WooCommerce to Stripe";
$this->method_description =
"Redirects users to a custom URL with order ID and total amount for Stripe payments.";
$this->supports = [];
// Initialize settings
$this->init_form_fields();
$this->init_settings();
// Get settings values
$this->title = $this->get_option("title");
$this->description = $this->get_option("description");
$this->enabled = $this->get_option("enabled");
// Use the constructor to set dynamic properties
$this->redirect_url = $this->get_option(
"redirect_url",
"https://www.example.com/"
); // Default redirect URL
// Save settings on WooCommerce admin options update
add_action(
"woocommerce_update_options_payment_gateways_" . $this->id,
[$this, "process_admin_options"]
);
}
// Define form fields for WooCommerce settings
public function init_form_fields()
{
$this->form_fields = [
"enabled" => [
"title" => "Enable/Disable",
"type" => "checkbox",
"label" => "Enable WooCommerce to Stripe",
"default" => "yes",
],
"title" => [
"title" => "Title",
"type" => "text",
"description" =>
"This controls the title which the user sees during checkout.",
"default" => "WooCommerce to Stripe",
"desc_tip" => true,
],
"description" => [
"title" => "Description",
"type" => "textarea",
"description" =>
"This controls the description which the user sees during checkout.",
"default" =>
"Redirects users to a custom URL with order ID and total amount for Stripe payments.",
],
"redirect_url" => [
"title" => "Redirect URL",
"type" => "text",
"description" =>
"Enter the custom URL where users will be redirected with order ID and total amount.",
"default" => "https://www.example.com/",
],
];
}
// Process payment and redirect to the custom URL
public function process_payment($order_id)
{
$order = wc_get_order($order_id);
// Redirect URL with order ID and total amount as query parameters
$redirect_url =
$this->redirect_url .
"?order_id=" .
$order_id .
"&total_amount=" .
$order->get_total();
return [
"result" => "success",
"redirect" => $redirect_url,
];
}
}
// Register the custom payment gateway class
function add_woocommerce_to_stripe_gateway($gateways)
{
$gateways[] = "WC_Gateway_WooCommerce_To_Stripe";
return $gateways;
}
// Filter to add the custom payment gateway class
add_filter(
"woocommerce_payment_gateways",
"add_woocommerce_to_stripe_gateway",
999
);
}