From 2a421e399e51f1c4e1399c062ea54307b6eb5c33 Mon Sep 17 00:00:00 2001 From: Ivan Petak Date: Tue, 16 Jul 2024 19:06:13 +0200 Subject: [PATCH] Add checkout block support --- assets/src/js/kekspay-blocks.js | 20 ++++++++ assets/src/scss/kekspay.scss | 1 + .../core/class-kekspay-block-checkout.php | 50 +++++++++++++++++++ .../core/class-kekspay-payment-gateway.php | 25 +++++++++- includes/utilities/class-kekspay-data.php | 2 +- webpack.mix.js | 1 + woocommerce-gateway-kekspay.php | 29 +++++++++-- 7 files changed, 122 insertions(+), 6 deletions(-) create mode 100644 assets/src/js/kekspay-blocks.js create mode 100644 includes/core/class-kekspay-block-checkout.php diff --git a/assets/src/js/kekspay-blocks.js b/assets/src/js/kekspay-blocks.js new file mode 100644 index 0000000..51f1e13 --- /dev/null +++ b/assets/src/js/kekspay-blocks.js @@ -0,0 +1,20 @@ +const settings = window.wc.wcSettings.getSetting( kekspayBlocksData.kekspayID + '_data', {} ); +const label = window.wp.htmlEntities.decodeEntities( settings.title ); + +const Content = () => { + return window.wp.htmlEntities.decodeEntities( settings.description ); +}; + +const Kekspay_Gateway = { + name: kekspayBlocksData.kekspayID, + label: label, + content: Object( window.wp.element.createElement )( Content, null ), + edit: Object( window.wp.element.createElement )( Content, null ), + canMakePayment: () => true, + ariaLabel: label, + supports: { + features: settings.supports, + }, +}; + +window.wc.wcBlocksRegistry.registerPaymentMethod( Kekspay_Gateway ); diff --git a/assets/src/scss/kekspay.scss b/assets/src/scss/kekspay.scss index 28ff13c..2a125a3 100644 --- a/assets/src/scss/kekspay.scss +++ b/assets/src/scss/kekspay.scss @@ -51,6 +51,7 @@ } .kekspay-logo { + display: block; width: 300px; height: 129px; margin: 0 auto; diff --git a/includes/core/class-kekspay-block-checkout.php b/includes/core/class-kekspay-block-checkout.php new file mode 100644 index 0000000..f11b246 --- /dev/null +++ b/includes/core/class-kekspay-block-checkout.php @@ -0,0 +1,50 @@ +settings = Kekspay_Data::get_settings(); + $this->gateway = Kekspay_Payment_Gateway::get_instance(); + } + + public function is_active() { + return $this->gateway->is_available(); + } + + public function get_payment_method_script_handles() { + wp_register_script( + 'kekspay-blocks-script', + KEKSPAY_DIR_URL . 'assets/dist/js/kekspay-blocks.js', + [ + 'wc-blocks-registry', + 'wc-settings', + 'wp-element', + 'wp-html-entities', + 'wp-i18n', + ], + KEKSPAY_PLUGIN_VERSION, + true + ); + + wp_localize_script( + 'kekspay-blocks-script', + 'kekspayBlocksData', + [ + 'kekspayID' => KEKSPAY_PLUGIN_ID, + ] + ); + + return [ 'kekspay-blocks-script' ]; + } + + public function get_payment_method_data() { + return [ + 'title' => $this->gateway->method_title, + 'description' => $this->gateway->method_description, + ]; + } +} diff --git a/includes/core/class-kekspay-payment-gateway.php b/includes/core/class-kekspay-payment-gateway.php index 6b3ab65..238944b 100644 --- a/includes/core/class-kekspay-payment-gateway.php +++ b/includes/core/class-kekspay-payment-gateway.php @@ -12,6 +12,12 @@ * Kekspay_Payment_Gateway class */ class Kekspay_Payment_Gateway extends WC_Payment_Gateway { + /** + * Instance of the current class, null before first usage. + * + * @var WC_Kekspay + */ + protected static $instance = null; /** * App data handler. @@ -42,6 +48,8 @@ public function __construct() { $this->title = esc_attr( Kekspay_Data::get_settings( 'title' ) ); $this->add_hooks(); + + self::$instance = $this; } /** @@ -49,7 +57,10 @@ public function __construct() { */ private function add_hooks() { add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, [ $this, 'process_admin_options' ] ); - add_action( 'woocommerce_receipt_' . $this->id, [ $this, 'do_receipt_page' ] ); + + if ( ! self::$instance ) { + add_action( 'woocommerce_receipt_' . $this->id, [ $this, 'do_receipt_page' ] ); + } } /** @@ -217,5 +228,17 @@ public function process_refund( $order_id, $amount = null, $reason = '' ) { return Kekspay_Connector::refund( $order, $amount ); } + /** + * Return class instance. + * + * @static + * @return Kekspay_Payment_Gateway + */ + public static function get_instance() { + if ( is_null( self::$instance ) ) { + self::$instance = new self(); + } + return self::$instance; + } } } diff --git a/includes/utilities/class-kekspay-data.php b/includes/utilities/class-kekspay-data.php index e23d939..2ff9353 100644 --- a/includes/utilities/class-kekspay-data.php +++ b/includes/utilities/class-kekspay-data.php @@ -105,7 +105,7 @@ public static function test_mode() { self::load_settings(); } - return 'yes' === self::$settings['in-test-mode']; + return isset( self::$settings['in-test-mode'] ) && 'yes' === self::$settings['in-test-mode']; } /** diff --git a/webpack.mix.js b/webpack.mix.js index 8ecbad3..421617c 100644 --- a/webpack.mix.js +++ b/webpack.mix.js @@ -16,6 +16,7 @@ if (!mix.inProduction()) { } mix.setPublicPath("./"); // set because of this issue: https://github.com/JeffreyWay/laravel-mix/issues/1126 +mix.js("assets/src/js/kekspay-blocks.js", "assets/dist/js/kekspay-blocks.js"); mix.js("assets/src/js/kekspay-admin.js", "assets/dist/js/kekspay-admin.js"); mix.js("assets/src/js/kekspay.js", "assets/dist/js/kekspay.js"); diff --git a/woocommerce-gateway-kekspay.php b/woocommerce-gateway-kekspay.php index 9dd704c..f675f7e 100644 --- a/woocommerce-gateway-kekspay.php +++ b/woocommerce-gateway-kekspay.php @@ -51,17 +51,19 @@ function kekspay_admin_notice_missing_woocommerce() { } /** - * Declare Kekspay plugin compatible with HPOS. + * Declare Kekspay plugin compatibility for certain WooCommerce features: + * - HPOS + * - Checkout Blocks * * @return void */ -function kekspay_hpos_compatible() { +function kekspay_wc_features_compatibility() { if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) { \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true ); + \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', __FILE__, true ); } } -add_action( 'before_woocommerce_init', 'kekspay_hpos_compatible' ); - +add_action( 'before_woocommerce_init', 'kekspay_wc_features_compatibility' ); if ( ! class_exists( 'WC_Kekspay' ) ) { /** @@ -106,6 +108,7 @@ protected function __construct() { add_action( 'admin_init', [ $this, 'check_for_other_kekspay_gateways' ], 1 ); add_action( 'activated_plugin', [ $this, 'set_kekspay_plugins_check_required' ] ); add_action( 'woocommerce_admin_field_payment_gateways', [ $this, 'set_kekspay_plugins_check_required' ] ); + add_action( 'woocommerce_blocks_loaded', [ $this, 'register_checkout_block_gateway' ] ); } /** @@ -243,6 +246,24 @@ public static function set_kekspay_plugins_check_required() { update_option( 'kekspay_plugins_check_required', 'yes' ); } + /** + * Register Kekspay method for block checkout. + */ + public function register_checkout_block_gateway() { + if ( ! class_exists( 'Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType' ) ) { + return; + } + + require_once KEKSPAY_DIR_PATH . 'includes/core/class-kekspay-block-checkout.php'; + + add_action( + 'woocommerce_blocks_payment_method_type_registration', + function( Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry $payment_method_registry ) { + $payment_method_registry->register( new Kekspay_Block_Checkout() ); + } + ); + } + /** * Deactivate plugin. */