'on-hold', 'global_after_canceled_payment_status' => 'failed', 'global_retrieve_payment_status' => 'yes', 'credit_widget_on_listings' => 'yes', 'credit_widget_on_product_page' => 'yes', 'credit_widget_on_cart_page' => 'yes', 'credit_widget_on_checkout_page' => 'yes' ]; } function on_woocommerce_blocks_loaded() { init_payu_blocks(); init_credit_widget_blocks(); } function init_payu_blocks() { if ( class_exists( 'Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType' ) ) { add_action( 'woocommerce_blocks_payment_method_type_registration', function ( PaymentMethodRegistry $payment_method_registry ) { $payment_method_registry->register( new PayuStandardBlock() ); $payment_method_registry->register( new PayuListBanksBlock() ); $payment_method_registry->register( new PayuCreditCardBlock() ); $payment_method_registry->register( new PayuSecureFormBlock() ); $payment_method_registry->register( new PayuPaypoBlock() ); $payment_method_registry->register( new PayuKlarnaBlock() ); $payment_method_registry->register( new PayuTwistoPlBlock() ); $payment_method_registry->register( new PayuTwistoSliceBlock() ); $payment_method_registry->register( new PayuInstallmentsBlock() ); $payment_method_registry->register( new PayuBlikBlock() ); $payment_method_registry->register( new PayuPragmaBlock() ); $payment_method_registry->register( new PayuGooglePayBlock() ); } ); } } function init_credit_widget_blocks() { if ( interface_exists( 'Automattic\WooCommerce\Blocks\Integrations\IntegrationInterface' ) ) { add_action( 'woocommerce_blocks_cart_block_registration', function ( IntegrationRegistry $integration_registry ) { $integration_registry->register( new CartCreditWidgetBlock() ); } ); add_action( 'woocommerce_blocks_checkout_block_registration', function ( IntegrationRegistry $integration_registry ) { $integration_registry->register( new CheckoutCreditWidgetBlock() ); } ); } } function init_gateway_payu() { if ( ! class_exists( 'WC_Payment_Gateway' ) ) { return; } load_plugin_textdomain( 'woo-payu-payment-gateway', false, dirname( plugin_basename( __FILE__ ) ) . '/lang' ); add_filter( 'woocommerce_payment_gateways', 'add_payu_gateways' ); add_filter( 'plugin_row_meta', 'plugin_row_meta', 10, 2 ); } //enable pbl and standard if first install register_activation_hook( __FILE__, 'payu_plugin_on_activate' ); function payu_plugin_on_activate(): void { if ( ! get_option( '_payu_plugin_version' ) ) { add_option( '_payu_plugin_version', PAYU_PLUGIN_VERSION ); add_option( 'woocommerce_payulistbanks_settings', [ 'enabled' => 'yes' ] ); add_option( 'woocommerce_payucreditcard_settings', [ 'enabled' => 'yes' ] ); add_option( 'payu_settings_option_name', payu_get_default_settings() ); } } function upgrade_gateway_payu(): void { $stored_version = get_option( '_payu_plugin_version', '' ); if ( PAYU_PLUGIN_VERSION !== $stored_version ) { $payu_settings = get_option( 'payu_settings_option_name' ); if ( empty( $payu_settings ) ) { add_option( 'payu_settings_option_name', payu_get_default_settings() ); } else { update_option( 'payu_settings_option_name', array_merge( payu_get_default_settings(), $payu_settings ) ); } disable_widget_once_if_installments_disabled( $stored_version ); update_option( '_payu_plugin_version', PAYU_PLUGIN_VERSION ); } } function disable_widget_once_if_installments_disabled( string $old_version ): void { if ( version_compare( $old_version, '2.6.2', '<=' ) ) { $installments_settings = get_option( 'woocommerce_payuinstallments_settings' ); if ( isset( $installments_settings['enabled'] ) && $installments_settings['enabled'] === 'no' ) { $disabled_widget_settings = [ 'credit_widget_on_listings' => 'no', 'credit_widget_on_product_page' => 'no', 'credit_widget_on_cart_page' => 'no', 'credit_widget_on_checkout_page' => 'no' ]; update_option( 'woocommerce_payuinstallments_settings', array_merge( $installments_settings, $disabled_widget_settings ) ); } } } function on_admin_init() { move_old_payu_installments_settings(); } function move_old_payu_installments_settings() { if ( $installments_settings = get_option( 'woocommerce_payuinstallments_settings' ) ) { $old_widget_settings = array_filter( $installments_settings, function ( $key ) { return strpos( $key, 'credit_widget' ) === 0; }, ARRAY_FILTER_USE_KEY ); if ( ! empty( $old_widget_settings ) ) { update_option( 'payu_settings_option_name', array_merge( get_option( 'payu_settings_option_name' ), $old_widget_settings ) ); foreach ( $old_widget_settings as $key => $value ) { unset( $installments_settings[ $key ] ); } update_option( 'woocommerce_payuinstallments_settings', $installments_settings ); } } } function add_payu_gateways( array $gateways ): array { foreach ( WC_Payu_Gateways::gateways_list() as $gateway ) { $gateways[] = $gateway['class']; } return $gateways; } function plugin_row_meta( $links, $plugin_file ) { if ( strpos( $plugin_file, 'woo-payu-payment-gateway' ) === false ) { return $links; } $row_meta = array( 'docs' => '' . esc_html__( 'Docs', 'woo-payu-payment-gateway' ) . '' ); return array_merge( $links, $row_meta ); } //enqueue assets function enqueue_payu_admin_assets() { wp_enqueue_script( 'payu-admin', plugins_url( '/assets/js/payu-admin.js', PAYU_PLUGIN_FILE ), [ 'jquery' ], PAYU_PLUGIN_VERSION ); wp_enqueue_style( 'payu-admin', plugins_url( '/assets/css/payu-admin.css', PAYU_PLUGIN_FILE ), [], PAYU_PLUGIN_VERSION ); } if ( is_admin() ) { add_action( 'admin_enqueue_scripts', 'enqueue_payu_admin_assets' ); } function get_site_language() { return substr( get_locale(), 0, 2 ); } function get_installment_option( $option ) { $paymentGateways = WC()->payment_gateways()->payment_gateways(); $result = null; if ( array_key_exists( 'payuinstallments', $paymentGateways ) ) { /** * @var $installmentsGateway WC_Gateway_PayuInstallments */ $installmentsGateway = $paymentGateways['payuinstallments']; switch ( $option ) { case 'pos_id': $result = $installmentsGateway->pos_id; break; case 'widget_key': $result = $installmentsGateway->pos_widget_key; break; case 'enable_for_shipping': $result = $installmentsGateway->enable_for_shipping; break; case 'enable_for_virtual': $result = $installmentsGateway->enable_for_virtual; break; case 'paymethods': $result = $installmentsGateway->get_available_paymethods(); } } return $result; } function is_credit_widget_available_for_feature( $feature_name ): bool { $payu_settings = get_option( 'payu_settings_option_name', [] ); return isset( $payu_settings[ $feature_name ] ) && $payu_settings[ $feature_name ] === 'yes'; } if ( is_credit_widget_available_for_feature( 'credit_widget_on_listings' ) ) { add_action( 'woocommerce_after_shop_loop_item', 'installments_mini_product' ); add_filter( 'woocommerce_blocks_product_grid_item_html', 'installments_mini_aware_product_block', 10, 3 ); } if ( is_credit_widget_available_for_feature( 'credit_widget_on_product_page' ) ) { add_action( 'woocommerce_before_add_to_cart_form', 'installments_mini_product' ); } if ( is_credit_widget_available_for_feature( 'credit_widget_on_cart_page' ) ) { add_action( 'woocommerce_cart_totals_after_order_total', 'installments_mini_total' ); } if ( is_credit_widget_available_for_feature( 'credit_widget_on_checkout_page' ) ) { add_action( 'woocommerce_review_order_after_order_total', 'installments_mini_total' ); } function get_credit_widget_excluded_paytypes(): array { $payu_settings = get_option( 'payu_settings_option_name', [] ); return $payu_settings['credit_widget_excluded_paytypes'] ?? []; } function is_any_credit_paymethod_available(): bool { $available_paymethods = get_installment_option( 'paymethods' ); if ( ! empty( $available_paymethods ) ) { $credit_paymethods = []; $payment_gateways = WC()->payment_gateways()->payment_gateways(); foreach ( $payment_gateways as $gateway ) { if ( $gateway instanceof WC_PayuCreditGateway ) { $credit_paymethods = array_merge( $credit_paymethods, $gateway->get_related_paytypes() ); } } foreach ( $credit_paymethods as $paymethod ) { if ( in_array( $paymethod, $available_paymethods ) ) { return true; } } } return false; } function installments_mini_product() { if ( ! is_any_credit_paymethod_available() ) { return; } $product = wc_get_product(); if ( ! $product ) { return; } $price = wc_get_price_including_tax( $product ); $productId = $product->get_id(); $posId = get_installment_option( 'pos_id' ); $widgetKey = get_installment_option( 'widget_key' ); $excludedPaytypes = get_credit_widget_excluded_paytypes(); $lang = get_site_language(); $currency = get_woocommerce_currency(); wp_enqueue_script( 'payu-installments-widget', 'https://static.payu.com/res/v2/widget-mini-installments.js' ); ?>