diff --git a/includes/class-mmg-checkout-payment.php b/includes/class-mmg-checkout-payment.php index 97606513..e58fc61d 100644 --- a/includes/class-mmg-checkout-payment.php +++ b/includes/class-mmg-checkout-payment.php @@ -35,6 +35,14 @@ public function enqueue_scripts() { wp_localize_script('mmg-checkout', 'mmg_checkout_params', array( 'ajax_url' => admin_url('admin-ajax.php'), )); + + // For blocks support + $gateway_settings = get_option('woocommerce_mmg_checkout_settings', array()); + wp_localize_script('wc-mmg-payments-blocks', 'mmgCheckoutData', array( + 'title' => isset($gateway_settings['title']) ? $gateway_settings['title'] : 'MMG Checkout', + 'description' => isset($gateway_settings['description']) ? $gateway_settings['description'] : 'Pay with MMG Checkout', + 'supports' => array('products', 'refunds'), + )); } public function generate_checkout_url() { diff --git a/includes/class-wc-mmg-gateway.php b/includes/class-wc-mmg-gateway.php index 82a60d91..cdddf2f4 100644 --- a/includes/class-wc-mmg-gateway.php +++ b/includes/class-wc-mmg-gateway.php @@ -13,6 +13,12 @@ public function __construct() { $this->title = $this->get_option('title'); $this->description = $this->get_option('description'); + $this->supports = array( + 'products', + 'refunds', + 'checkout_block_support', + ); + add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options')); add_action('woocommerce_receipt_' . $this->id, array($this, 'receipt_page')); } @@ -71,4 +77,4 @@ public function receipt_page($order_id) { settings = get_option('woocommerce_mmg_checkout_settings', array()); + } + + public function is_active() { + return ! empty($this->settings['enabled']) && 'yes' === $this->settings['enabled']; + } + + public function get_payment_method_script_handles() { + wp_register_script( + 'wc-mmg-payments-blocks', + plugins_url('js/mmg-checkout-blocks.js', dirname(__FILE__)), + array('wc-blocks-registry', 'wc-settings', 'wp-element', 'wp-html-entities', 'wp-i18n'), + filemtime(plugin_dir_path(dirname(__FILE__)) . 'js/mmg-checkout-blocks.js'), + true + ); + return array('wc-mmg-payments-blocks'); + } + + public function get_payment_method_data() { + return array( + 'title' => $this->settings['title'], + 'description' => $this->settings['description'], + ); + } +} diff --git a/js/mmg-checkout-blocks.js b/js/mmg-checkout-blocks.js new file mode 100644 index 00000000..db5f07ee --- /dev/null +++ b/js/mmg-checkout-blocks.js @@ -0,0 +1,23 @@ +const { registerPaymentMethod } = window.wc.wcBlocksRegistry; +const { createElement } = window.wp.element; +const { decodeEntities } = window.wp.htmlEntities; + +const MMGCheckoutLabel = ({ title }) => { + return createElement('span', {}, decodeEntities(title)); +}; + +const MMGCheckoutContent = ({ description }) => { + return createElement('p', {}, decodeEntities(description)); +}; + +registerPaymentMethod({ + name: 'mmg_checkout', + label: createElement(MMGCheckoutLabel, { title: mmgCheckoutData.title }), + content: createElement(MMGCheckoutContent, { description: mmgCheckoutData.description }), + edit: createElement(MMGCheckoutContent, { description: mmgCheckoutData.description }), + canMakePayment: () => true, + ariaLabel: decodeEntities(mmgCheckoutData.title), + supports: { + features: mmgCheckoutData.supports, + }, +}); \ No newline at end of file diff --git a/main.php b/main.php index 59ad3e1a..01f7399b 100644 --- a/main.php +++ b/main.php @@ -21,4 +21,18 @@ function mmg_checkout_init() { new MMG_Checkout_Payment(); } add_action('plugins_loaded', 'mmg_checkout_init'); +} + +add_action('woocommerce_blocks_loaded', 'mmg_checkout_register_block_support'); + +function mmg_checkout_register_block_support() { + if (class_exists('Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType')) { + require_once plugin_dir_path(__FILE__) . 'includes/class-wc-mmg-payments-blocks.php'; + add_action( + 'woocommerce_blocks_payment_method_type_registration', + function(Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry $payment_method_registry) { + $payment_method_registry->register(new WC_MMG_Payments_Blocks()); + } + ); + } } \ No newline at end of file