| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin main class. |
| 4 |
*/ |
| 5 |
|
| 6 |
namespace WPDesk\GatewayWPPay; |
| 7 |
|
| 8 |
use WPDesk\GatewayWPPay\Blocks\AutopayBlock; |
| 9 |
use WPDesk\GatewayWPPay\Blocks\BlikBlock; |
| 10 |
use WPDesk\GatewayWPPay\Blocks\RecurringBlock; |
| 11 |
use WPDesk\GatewayWPPay\BlueMediaApi\BlueMediaClientFactory; |
| 12 |
use WPDesk\GatewayWPPay\BlueMediaApi\Handlers\APIHandler; |
| 13 |
use WPDesk\GatewayWPPay\WooCommerceGateway\BlikZeroEmbedGateway; |
| 14 |
use WPDesk\GatewayWPPay\WooCommerceGateway\CardEmbedGateway; |
| 15 |
use WPDesk\GatewayWPPay\WooCommerceGateway\GatewaysProxy; |
| 16 |
use WPDesk\GatewayWPPay\WooCommerceGateway\StandardPaymentGateway; |
| 17 |
use WPDesk\GatewayWPPay\WooCommerceGateway\SubscriptionGateway; |
| 18 |
use WPPayVendor\Psr\Log\LoggerAwareInterface; |
| 19 |
use WPPayVendor\Psr\Log\LoggerAwareTrait; |
| 20 |
use WPPayVendor\Psr\Log\LoggerInterface; |
| 21 |
use WPPayVendor\Psr\Log\NullLogger; |
| 22 |
use WPPayVendor\WPDesk\Dashboard\DashboardWidget; |
| 23 |
use WPPayVendor\WPDesk\Logger\Settings; |
| 24 |
use WPPayVendor\WPDesk\Logger\SimpleLoggerFactory; |
| 25 |
use WPPayVendor\WPDesk\View\Renderer\Renderer; |
| 26 |
use WPPayVendor\WPDesk\View\Renderer\SimplePhpRenderer; |
| 27 |
use WPPayVendor\WPDesk\View\Resolver\DirResolver; |
| 28 |
use WPPayVendor\WPDesk_Plugin_Info; |
| 29 |
use WPPayVendor\WPDesk\PluginBuilder\Plugin\AbstractPlugin; |
| 30 |
use WPPayVendor\WPDesk\PluginBuilder\Plugin\HookableCollection; |
| 31 |
use WPPayVendor\WPDesk\PluginBuilder\Plugin\HookableParent; |
| 32 |
|
| 33 |
/** |
| 34 |
* Main plugin class. The most important flow decisions are made here. |
| 35 |
*/ |
| 36 |
class Plugin extends AbstractPlugin implements LoggerAwareInterface, HookableCollection { |
| 37 |
|
| 38 |
use LoggerAwareTrait; |
| 39 |
use HookableParent; |
| 40 |
|
| 41 |
private $scripts_version = 3; |
| 42 |
|
| 43 |
const WPPAY_REGISTER_URL = 'https://wpdesk.link/wppay-plugin-rejestracja'; |
| 44 |
const NONCE_AJAX_ACTION_GET_SINGLE_CURRENCY = 'nonce_wppay_get_single_currency'; |
| 45 |
private const LOGGER_CHANNEL = 'wpdesk-autopay'; |
| 46 |
private GatewaysProxy $gateway_proxy; |
| 47 |
private BlueMediaClientFactory $client_factory; |
| 48 |
|
| 49 |
/** |
| 50 |
* Plugin constructor. |
| 51 |
* |
| 52 |
* @param WPDesk_Plugin_Info $plugin_info Plugin info. |
| 53 |
*/ |
| 54 |
public function __construct( WPDesk_Plugin_Info $plugin_info ) { |
| 55 |
parent::__construct( $plugin_info ); |
| 56 |
|
| 57 |
$settings = new Settings(); |
| 58 |
$settings->use_wp_log = false; |
| 59 |
|
| 60 |
$this->setLogger( ( new SimpleLoggerFactory( self::LOGGER_CHANNEL, $settings ) )->getLogger() ); |
| 61 |
|
| 62 |
$this->plugin_url = $this->plugin_info->get_plugin_url(); |
| 63 |
$this->plugin_namespace = $this->plugin_info->get_text_domain(); |
| 64 |
|
| 65 |
$this->client_factory = new BlueMediaClientFactory( $this->logger ); |
| 66 |
$this->gateway_proxy = new GatewaysProxy( $this->client_factory, $this->plugin_url, $this->get_renderer(), $this->logger ); |
| 67 |
} |
| 68 |
|
| 69 |
public function init(): void { |
| 70 |
$this->settings_url = admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=wppay' ); |
| 71 |
$this->support_url = 'https://www.wpdesk.pl/sk/pay-wp-support'; |
| 72 |
|
| 73 |
require_once __DIR__ . "/../vendor_prefixed/guzzlehttp/guzzle/src/functions_include.php"; |
| 74 |
|
| 75 |
add_action( 'init', function () { |
| 76 |
parent::init(); |
| 77 |
add_filter( 'woocommerce_payment_gateways', [ $this, 'register_payment_gateways' ] ); |
| 78 |
}, 0 ); |
| 79 |
add_action( 'woocommerce_blocks_loaded', [ $this, 'register_block_support' ] ); |
| 80 |
} |
| 81 |
|
| 82 |
public function hooks(): void { |
| 83 |
parent::hooks(); |
| 84 |
add_action( 'init', [ $this, 'init_gateways' ] ); |
| 85 |
( new DashboardWidget() )->hooks(); |
| 86 |
} |
| 87 |
|
| 88 |
public function init_gateways(){ |
| 89 |
$api_handler = new APIHandler( $this->gateway_proxy->get_autopay_gateway(), $this->gateway_proxy->get_subscription_gateway(), $this->logger ); |
| 90 |
|
| 91 |
$this->client_factory->set_settings( $this->gateway_proxy->get_autopay_gateway() ); |
| 92 |
|
| 93 |
$api_handler->hooks(); |
| 94 |
$this->gateway_proxy->get_autopay_gateway()->hooks(); |
| 95 |
$this->gateway_proxy->get_blik_gateway()->hooks(); |
| 96 |
$this->gateway_proxy->get_subscription_gateway()->hooks(); |
| 97 |
} |
| 98 |
|
| 99 |
public function register_payment_gateways( $methods ) { |
| 100 |
$methods[] = $this->gateway_proxy->get_autopay_gateway(); |
| 101 |
$methods[] = $this->gateway_proxy->get_blik_gateway(); |
| 102 |
if ( class_exists( 'WC_Subscriptions' ) ) { |
| 103 |
$methods[] = $this->gateway_proxy->get_subscription_gateway(); |
| 104 |
} |
| 105 |
|
| 106 |
return $methods; |
| 107 |
} |
| 108 |
|
| 109 |
private function get_renderer( string $folder = '' ): Renderer { |
| 110 |
return new SimplePhpRenderer( new DirResolver( $this->plugin_info->get_plugin_dir() . '/src/views/' . $folder ) ); |
| 111 |
} |
| 112 |
/** |
| 113 |
* For external use. |
| 114 |
* |
| 115 |
* @param string $currency |
| 116 |
* |
| 117 |
* @return bool |
| 118 |
*/ |
| 119 |
public static function has_credentials( string $currency = 'PLN' ): bool { |
| 120 |
$client_factory = new BlueMediaClientFactory(); |
| 121 |
$settings = new Settings(); |
| 122 |
$settings->use_wp_log = false; |
| 123 |
$client_factory->set_settings( new StandardPaymentGateway( $client_factory, '', ( new SimpleLoggerFactory( self::LOGGER_CHANNEL, $settings ) )->getLogger() ) ); |
| 124 |
|
| 125 |
return $client_factory->has_credentials( $currency ); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Admin enqueue scripts |
| 130 |
*/ |
| 131 |
public function admin_enqueue_scripts() { |
| 132 |
$current_screen = get_current_screen(); |
| 133 |
$suffix = ''; //defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 134 |
if ( in_array( $current_screen->id, [ 'woocommerce_page_wc-settings' ] ) |
| 135 |
&& isset( $_GET['tab'] ) && $_GET['tab'] === 'checkout' |
| 136 |
&& isset( $_GET['section'] ) && $_GET['section'] === 'wppay' |
| 137 |
) { |
| 138 |
wp_register_style( 'wppay_admin_css', $this->get_plugin_assets_url() . 'css/admin' . $suffix . '.css', [], |
| 139 |
$this->scripts_version ); |
| 140 |
wp_enqueue_style( 'wppay_admin_css' ); |
| 141 |
wp_enqueue_script( 'wppay_admin_js', $this->get_plugin_assets_url() . 'js/admin' . $suffix . '.js', |
| 142 |
[ 'jquery' ], $this->scripts_version, true ); |
| 143 |
$protocol = is_ssl() ? 'https://' : 'http://'; |
| 144 |
wp_localize_script( 'wppay_admin_js', 'wppay_admin_object', [ |
| 145 |
'site_url' => str_replace( $protocol, '', site_url() ), |
| 146 |
'protocol' => $protocol, |
| 147 |
'wppay_nonce' => wp_create_nonce( self::NONCE_AJAX_ACTION_GET_SINGLE_CURRENCY ), |
| 148 |
] ); |
| 149 |
|
| 150 |
wp_register_style( 'jquery-ui-style', WC()->plugin_url() . '/assets/css/jquery-ui/jquery-ui.min.css', |
| 151 |
[], $this->scripts_version ); |
| 152 |
wp_enqueue_script( 'jquery-ui-accordion' ); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
public function register_block_support() { |
| 157 |
if ( class_exists( 'Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType' ) ) { |
| 158 |
add_action( 'woocommerce_blocks_payment_method_type_registration', |
| 159 |
function ( \Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry $payment_method_registry ) { |
| 160 |
$payment_method_registry->register( new AutopayBlock( $this->gateway_proxy->get_autopay_gateway(), $this->get_plugin_assets_url() . 'js/checkout-blocks/autopay_block', $this->get_plugin_assets_url() . 'images/icon.svg' ) ); |
| 161 |
$payment_method_registry->register( new BlikBlock( $this->gateway_proxy->get_blik_gateway(), $this->get_plugin_assets_url() . 'js/checkout-blocks/blik_block', $this->get_plugin_assets_url() . 'images/icon-blik.png' ) ); |
| 162 |
$payment_method_registry->register( new RecurringBlock( $this->gateway_proxy->get_subscription_gateway(), $this->get_plugin_assets_url() . 'js/checkout-blocks/recurring_block', $this->get_plugin_assets_url() . 'images/icon.svg' ) ); |
| 163 |
} |
| 164 |
); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
public function get_logger(): ?LoggerInterface { |
| 169 |
$logger_enabled = apply_filters( 'autopay_logger_enabled', false); |
| 170 |
|
| 171 |
if(!$logger_enabled){ |
| 172 |
return new NullLogger(); |
| 173 |
} |
| 174 |
return $this->logger; |
| 175 |
} |
| 176 |
} |
| 177 |
|