How to add payment gateway in Woocommerce if it has a namespace?

Good day, colleagues! We have a namespace, let's call it Site, inside it we have a namespace Gateways.
The class with the gateway podtseplena through the function:
use \Site\Gateways\initGateways as gates;
add_action('plugins_loaded', function () {
 if (!class_exists('\WC_Payment_Gateway'))
return;
 if (!class_exists('\Site\Gateways\Gateway'))
return;
 $gates = new gates();
 $available = new \WC_Payment_Gateways();
 $array = $available->get_available_payment_gateways();
 $gate = $gates -> initialize($array);
 foreach ($gate as $g):
if(!class_exists($g)):
return;
endif;
endforeach;
 add_filter('woocommerce_payment_gateways', array($gate, 'initialize'));
});

The class itself looks like this:
namespace Site\Gateways;
defined('ABSPATH') || die('Cheating, ya? Die.');

/**
 * Class initGateways
 * @package Site\Gateways
*/
class initGateways
{
/**
 * @var array
*
 * Contains class names that should interpretirovat as payment gateways.
*/
 protected $gates = array();

/**
 * initGateways constructor.
*/
 public function __construct()
{
 $this -> gates[] = 'GatewayProd';
 $this -> gates[] = 'GatewayTest';
}

/**
 * @param $methods
 * @return array
*
 * Returned payment gateways
*/
 public function initialize($methods)
{
 $gates = $this -> gates;
 foreach ($gates as $gate) {
 $methods[] = $gate;
}
 return $methods;
}
}

Well, the test payment gateway (default gateway code I think it is pointless to attach):
namespace Site\Gateways;
defined('ABSPATH') || die('Cheating, ya? Die.');

/**
 * Class GatewayTest
 * @package Site\Gateways
*/
class Gateway extends GatewayTest
{
 public function __construct()
{
 $this->id = 'test';
 $this->method_title = "Test gateway";
 $this->title = 'Test';
 $this->method_description = __('qwerty123', 'woocommerce');
parent::__construct();
}
}


As the class is in a namespace, it must be uploaded to Woocommerce, otherwise the class will not exist, which is logical.
If you pass in Woocommerce namespace - get an error : call_user_func_array() expects parameter 1 to be a valid callback, first array member is not a valid class name or object, and if not pass - the gateway podtseplena as class gateway does not exist. Without a namespace the gateway works without any problem, to abandon it would not be desirable
April 4th 20 at 12:57
1 answer
April 4th 20 at 12:59
Solution

Find more questions by tags PHP