From d750a6ade53842be4764fbf0489438345dd71357 Mon Sep 17 00:00:00 2001 From: Yash Tripathi Date: Wed, 29 May 2024 15:51:52 +0530 Subject: [PATCH] choose magic plan admin side --- includes/api/api.php | 27 +++++++++ public/css/1cc-plan-hide.css | 3 + public/js/admin-rzp-settings.js | 14 ++++- woo-razorpay.php | 101 +++++++++++++++++++++++++++++++- 4 files changed, 143 insertions(+), 2 deletions(-) create mode 100644 public/css/1cc-plan-hide.css diff --git a/includes/api/api.php b/includes/api/api.php index 5901531a..d252acd9 100644 --- a/includes/api/api.php +++ b/includes/api/api.php @@ -192,6 +192,33 @@ function addMagicCheckoutSettingFields(&$defaultFormFields) 'label' => __('Activate Magic Checkout'), 'default' => 'no', ), + // '1cc_plan' => array( + // 'title' => __('Choose Magic Plan'), + // 'type' => 'multiselect', + // 'select_buttons'=> true, + // 'options' => [ + // 'magic_lite' => 'Magic Lite', + // 'magic_premium' => 'Magic Premium' + // ], + // ), + // 'custom_field' => array( + // 'title' => __( 'Custom Field', 'woocommerce' ), + // 'type' => 'custom_type', // Custom field type + // 'description' => __( 'This is a custom field for your gateway.', 'woocommerce' ), + // 'default' => '', + // 'desc_tip' => true, + // ), + '1cc_plan' => array( + 'title' => __( 'Choose Magic Plan', 'woocommerce' ), + 'type' => 'custom_checkboxes', // Custom field type + 'description' => __( 'Choose the Magic plan according to your tier.', 'woocommerce' ), + 'default' => array(), // Default value should be an array + 'desc_tip' => true, + 'options' => array( + 'one_click_lite' => 'Magic Lite', + 'one_click_premium' => 'Magic Premium', + ), + ), 'enable_1cc_test_mode' => array( 'title' => __('Activate test mode'), 'type' => 'checkbox', diff --git a/public/css/1cc-plan-hide.css b/public/css/1cc-plan-hide.css new file mode 100644 index 00000000..4aeff22a --- /dev/null +++ b/public/css/1cc-plan-hide.css @@ -0,0 +1,3 @@ +.hidden-field { + display: none; +} \ No newline at end of file diff --git a/public/js/admin-rzp-settings.js b/public/js/admin-rzp-settings.js index 56b9ac8f..3f40f91e 100644 --- a/public/js/admin-rzp-settings.js +++ b/public/js/admin-rzp-settings.js @@ -1,6 +1,8 @@ window.onload = function() { // for confirmation of toggling 1cc var enableRzpCheckout = document.getElementById('woocommerce_razorpay_enable_1cc'); + var oneCCPlan = document.getElementById('woocommerce_razorpay_1cc_plan'); + if (enableRzpCheckout) { enableRzpCheckout.onclick = function(e) { var current_val = enableRzpCheckout.checked; @@ -9,7 +11,17 @@ window.onload = function() { } else { var message = 'Are you sure you want to deactivate Magic Checkout?' } - if (!confirm(message)) { + if (confirm(message)) { + if (current_val == false) { + oneCCPlan.classList.add('hidden-field'); + // console.log('print in false', oneCCPlan); + } else { + oneCCPlan.classList.remove('hidden-field'); + // console.log('print in true', oneCCPlan); + } + } + else + { if (current_val) { enableRzpCheckout.checked = false; } else { diff --git a/woo-razorpay.php b/woo-razorpay.php index 0bfcff4c..0aa3b541 100644 --- a/woo-razorpay.php +++ b/woo-razorpay.php @@ -299,6 +299,7 @@ public function __construct($hooks = true) if ($is1ccAvailable) { $this->visibleSettings = array_merge($this->visibleSettings, array( 'enable_1cc', + '1cc_plan', 'enable_1cc_mandatory_login', 'enable_1cc_test_mode', 'enable_1cc_pdp_checkout', @@ -315,9 +316,24 @@ public function __construct($hooks = true) '1cc_account_creation', )); } - + + if ($this->getSetting('enable_1cc') === 'yes') + { + if (get_option('razorpay_magicx_first_check') === false) + { + $this->update_option('1cc_plan', [ + 'one_click_lite', + 'one_click_premium' + ]); + } + } } + update_option('razorpay_magicx_first_check', true); + + + wp_enqueue_style('custom-gateway-admin-css', plugin_dir_url(__FILE__) .'public/css/1cc-plan-hide.css'); + $this->init_form_fields(); $this->init_settings(); @@ -361,6 +377,89 @@ protected function initHooks() add_filter( 'woocommerce_thankyou_order_received_text', array($this, 'getCustomOrdercreationMessage'), 20, 2 ); } + public function generate_custom_checkboxes_html( $key, $data ) + { + $field = $this->get_field_key( $key ); + $defaults = array( + 'title' => '', + 'type' => 'custom_checkboxes', + 'description' => '', + 'desc_tip' => false, + 'options' => array(), + ); + + $hidden_class = ''; + if ($this->getSetting('enable_1cc') === 'no') + { + $hidden_class = 'hidden-field'; + } + + + $data = wp_parse_args( $data, $defaults ); + + + $value = $this->get_option($key, array()); + + ob_start(); + ?> + + + + get_tooltip_html($data); ?> + + +
+ $option_label) : ?> +
+ + get_description_html($data); ?> +
+ + + getSetting('key_id')) && !empty($this->getSetting('key_secret'))) + { + return false; + try + { + $api = $this->getRazorpayApiInstance(); + $merchantPreferences = $api->request->request('GET', 'merchant/1cc_preferences'); + + if (!empty($merchantPreferences['features'][$featureFlag])) { + return false; + } + + } catch (\Exception $e) { + rzpLogError($e->getMessage()); + } + + return true; + } + } + // public function process_admin_options() { + // $saved = parent::process_admin_options(); + // // Ensure the custom checkboxes field is properly saved as an array + // $custom_checkboxes = isset( $_POST[ $this->plugin_id . $this->id . '_custom_checkboxes' ] ) ? (array) $_POST[ $this->plugin_id . $this->id . '_custom_checkboxes' ] : array(); + // update_option( $this->plugin_id . $this->id . '_custom_checkboxes', $custom_checkboxes ); + // return $saved; + // } + + public function validate_custom_checkboxes_field( $key, $value ) { + // Ensure the value is an array + if ( ! is_array( $value ) ) { + $value = array(); + } + return $value; + } + public function init_form_fields() { $webhookUrl = esc_url(admin_url('admin-post.php')) . '?action=rzp_wc_webhook';