diff --git a/includes/api/api.php b/includes/api/api.php index 5901531a..782e87cb 100644 --- a/includes/api/api.php +++ b/includes/api/api.php @@ -179,7 +179,7 @@ function initCartCommon() } -add_action('setup_extra_setting_fields', 'addMagicCheckoutSettingFields'); +add_action('setup_extra_setting_fields_1cc', 'addMagicCheckoutSettingFields'); function addMagicCheckoutSettingFields(&$defaultFormFields) { diff --git a/includes/razorpay-affordability-widget.php b/includes/razorpay-affordability-widget.php index 2f8cbd94..bca72007 100644 --- a/includes/razorpay-affordability-widget.php +++ b/includes/razorpay-affordability-widget.php @@ -526,37 +526,41 @@ function updateAffordabilityWidgetSettings() woocommerce_update_options(getAffordabilityWidgetSettings()); try { - if (isset($_POST['woocommerce_razorpay_key_id']) and - empty($_POST['woocommerce_razorpay_key_id']) === false and - isset($_POST['woocommerce_razorpay_key_secret']) and - empty($_POST['woocommerce_razorpay_key_secret']) === false) - { - $api = new Api($_POST['woocommerce_razorpay_key_id'], $_POST['woocommerce_razorpay_key_secret']); - } - else + $checkout360status = (get_option('rzp_checkout360_status') === 'yes') ? true : false; + + if ($checkout360status === false) { - $api = new Api(get_option('woocommerce_razorpay_settings')['key_id'],get_option('woocommerce_razorpay_settings')['key_secret']); - } + if (isset($_POST['woocommerce_razorpay_key_id']) and + empty($_POST['woocommerce_razorpay_key_id']) === false and + isset($_POST['woocommerce_razorpay_key_secret']) and + empty($_POST['woocommerce_razorpay_key_secret']) === false) + { + $api = new Api($_POST['woocommerce_razorpay_key_id'], $_POST['woocommerce_razorpay_key_secret']); + } + else + { + $api = new Api(get_option('woocommerce_razorpay_settings')['key_id'],get_option('woocommerce_razorpay_settings')['key_secret']); + } - $merchantPreferences = $api->request->request('GET', 'accounts/me/features'); + $merchantPreferences = $api->request->request('GET', 'accounts/me/features'); - if (isset($merchantPreferences) === false or - isset($merchantPreferences['assigned_features']) === false) - { - throw new Exception("Error in Api call."); - } + if (isset($merchantPreferences) === false or + isset($merchantPreferences['assigned_features']) === false) + { + throw new Exception("Error in Api call."); + } - update_option('rzp_afd_enable', 'no'); - foreach ($merchantPreferences['assigned_features'] as $preference) - { - if ($preference['name'] === 'affordability_widget' or - $preference['name'] === 'affordability_widget_set') + update_option('rzp_afd_enable', 'no'); + foreach ($merchantPreferences['assigned_features'] as $preference) { - update_option('rzp_afd_enable', 'yes'); - break; + if ($preference['name'] === 'affordability_widget' or + $preference['name'] === 'affordability_widget_set') + { + update_option('rzp_afd_enable', 'yes'); + break; + } } } - } catch (\Exception $e) { diff --git a/woo-razorpay.php b/woo-razorpay.php index fe1e36db..d2620ce5 100644 --- a/woo-razorpay.php +++ b/woo-razorpay.php @@ -248,6 +248,193 @@ public function getCustomOrdercreationMessage($thank_you_title, $order) return $message; } + public function generate_multicheckbox_html($key, $data) + { + $c360FieldMapping = [ + 'enable_magic_checkout_cart_page' => 'enable_1cc', + 'enable_magic_checkout_test_mode' => 'enable_1cc_test_mode', + 'enable_magic_checkout_buy_now' => 'enable_1cc_pdp_checkout', + 'enable_magic_checkout_mini_cart' => 'enable_1cc_mini_cart_checkout', + 'enable_cod_intelligence' => 'rzp_cod_intelligence_enable', + 'enable_razorpay_trusted_business_widget' => 'rzp_rtb_enable', + 'enable_razorpay_affordability_widget' => 'rzp_afd_enable' + ]; + + $field = $this->get_field_key($key); + + $defaults = array( + 'title' => '', + 'type' => 'multicheckbox', + 'description' => '', + 'desc_tip' => false, + 'options' => array(), + ); + + $data = wp_parse_args( $data, $defaults ); + + ob_start(); + ?> +
We are sorry you are not Eligible to opt RTB.
+' . __('Advanced COD | Address Pre-Fills | Smart Coupons | COD Intelligence | Trust and Affordability').'
', + 'type' => 'title', + ), + 'configure_checkout360' => array( + 'title' => '' . __('Configure Checkout360 Settings').'', + 'type' => 'title', + ), + 'enable_magic_checkout_group' => array( + 'title' => __('Enable Magic Checkout'), + 'type' => 'multicheckbox', + 'label' => __('Enable Magic Checkout', $this->id), + 'options' => array( + 'enable_magic_checkout_cart_page' => array( + 'label' => __('Enable Magic Checkout', $this->id), + 'description' => __('Magic Checkout will launch when customer checkout with their cart', $this->id) + ), + 'enable_magic_checkout_buy_now' => array( + 'label' => __('Enable Magic Checkout On Buy Now Button', $this->id), + 'description' => __('Magic Checkout will launch when customer clicks on Buy Now on products page', $this->id) + ), + 'enable_magic_checkout_mini_cart' => array( + 'label' => __('Enable Magic Checkout On Mini Cart', $this->id), + 'description' => __('Magic Checkout will launch when checkout from mini cart', $this->id) + ), + 'enable_magic_checkout_test_mode' => array( + 'label' => __('Enable Magic Checkout On Test Mode', $this->id), + 'description' => __('Only logged in admin users will see Magic Checkout', $this->id) + ) + ) + ), + 'enable_cod_configurations_group' => array( + 'title' => __('Enable COD Intelligence'), + 'type' => 'multicheckbox', + 'options' => array( + 'enable_cod_intelligence' => array( + 'label' => __('Enable COD Intelligence', $this->id), + 'description' => __('Set up rules on Razorpay Dashboard to show/hide and charge for COD orders', $this->id) + ), + 'enable_cod_configuratiopn' => array( + 'label' => __('Set-up smart COD rules & RTO Prediction here', $this->id), + 'description' => __("Use Razorpay's RTO intelligence to block COD for high risk users", $this->id), + 'link' => 'http://dashboard.razorpay.com/app/magic' + ) + ) + ), + 'enhance_your_checkout_group' => array( + 'title' => __('Enhance Your Checkout'), + 'type' => 'multicheckbox', + 'options' => array( + 'enable_razorpay_trusted_business_widget' => array( + 'label' => __('Enable Razorpay Trusted Business Widget', $this->id), + 'description' => __('', $this->id) + ), + 'enable_razorpay_affordability_widget' => array( + 'label' => __('Enable Razorpay Affordability Widget', $this->id), + 'description' => __('', $this->id) + ), + 'enable_giftcards_coupons' => array( + 'label' => __('Set-up gift cards and coupons here', $this->id), + 'description' => __("", $this->id), + 'link' => 'https://dashboard.razorpay.com/app/magic/settings' + ) + ) + ) + ); + do_action_ref_array( 'setup_extra_setting_fields', array( &$defaultFormFields ) ); + $defaultFormFields = array_merge( $defaultFormFields, $checkout360Settings ); + + do_action_ref_array( 'setup_extra_setting_fields_1cc', array( &$defaultFormFields ) ); + foreach ($defaultFormFields as $key => $value) { if (in_array($key, $this->visibleSettings, true)) @@ -446,6 +743,8 @@ public function init_form_fields() { try { + $checkout360status = (get_option('rzp_checkout360_status') === 'yes') ? true : false; + if (isset($_POST['woocommerce_razorpay_key_id']) and empty($_POST['woocommerce_razorpay_key_id']) === false and isset($_POST['woocommerce_razorpay_key_secret']) and @@ -467,8 +766,11 @@ public function init_form_fields() throw new Exception("Error in Api call."); } - update_option('rzp_afd_enable', 'no'); - update_option('rzp_rtb_enable', 'no'); + if ($checkout360status === false) + { + update_option('rzp_afd_enable', 'no'); + update_option('rzp_rtb_enable', 'no'); + } foreach ($merchantPreferences['assigned_features'] as $preference) { @@ -478,33 +780,39 @@ public function init_form_fields() add_action('woocommerce_sections_checkout', 'addSubSection'); add_action('woocommerce_settings_tabs_checkout', 'displayAffordabilityWidgetSettings'); add_action('woocommerce_update_options_checkout', 'updateAffordabilityWidgetSettings'); - update_option('rzp_afd_enable', 'yes'); + if ($checkout360status === false) + { + update_option('rzp_afd_enable', 'yes'); + } break; } } - $rtbActivationStatus = $api->request->request('GET', 'rtb?key_id=' . $key_id); - - if (isset($rtbActivationStatus['widget_enabled']) and - $rtbActivationStatus['widget_enabled'] === true) + if ($checkout360status === false) { - $this->form_fields['rtb_widget_title'] = array( - 'title' => '' . __('Razorpay Trusted Business'). '', - 'type' => 'title' - ); - - $this->form_fields['enable_rtb_widget'] = array( - 'title' => __('RTB Widget Enable/Disable'), - 'type' => 'checkbox', - 'desc' => __('Enable RTB Widget?'), - 'default' => 'no', - 'id' => 'rzp_rtb_enable' - ); - update_option('rzp_rtb_enable', 'yes'); - } + $rtbActivationStatus = $api->request->request('GET', 'rtb?key_id=' . $key_id); - update_option('rzp_afd_feature_checked', 'yes'); - update_option('rzp_rtb_feature_checked', 'yes'); + if (isset($rtbActivationStatus['widget_enabled']) and + $rtbActivationStatus['widget_enabled'] === true) + { + $this->form_fields['rtb_widget_title'] = array( + 'title' => '' . __('Razorpay Trusted Business'). '', + 'type' => 'title' + ); + + $this->form_fields['enable_rtb_widget'] = array( + 'title' => __('RTB Widget Enable/Disable'), + 'type' => 'checkbox', + 'desc' => __('Enable RTB Widget?'), + 'default' => 'no', + 'id' => 'rzp_rtb_enable' + ); + update_option('rzp_rtb_enable', 'yes'); + } + + update_option('rzp_afd_feature_checked', 'yes'); + update_option('rzp_rtb_feature_checked', 'yes'); + } } catch (\Exception $e) { @@ -535,6 +843,52 @@ protected function triggerValidationInstrumentation($data) $trackObject->rzpTrackDataLake('formfield.validation.error', $properties); } + public function autoPostInstallationCheck() + { + try + { + $response = $this->rzpPostInstallationCall('evaluate'); + + $checkout360Available = ((isset($response['checkout360_status']) === true) and ($response['checkout360_status'] === true)) ? 'yes' : 'no'; + + update_option('rzp_checkout360_status', $checkout360Available); + update_option('rzp_post_installation_update_at', time()); + + // remove all checkout360 settings + if ((isset($response['merchant_status']) === true) and + ($response['merchant_status'] === 'existing_checkout360_merchant') and + ($checkout360Available === 'no')) + { + $checkout360Fields = [ + 'enable_1cc', + 'enable_1cc_test_mode', + 'enable_1cc_pdp_checkout', + 'enable_1cc_mini_cart_checkout', + 'rzp_cod_intelligence_enable' + ]; + $optionKey = parent::get_option_key(); + $savedData = get_option($optionKey); + + foreach ($checkout360Fields as $field) + { + if ($field === 'rzp_cod_intelligence_enable') + { + delete_option($field); + } + else + { + $savedData[$field] = 'no'; + } + } + update_option($optionKey, apply_filters('woocommerce_settings_api_sanitized_fields_' . $this->id, $savedData), 'yes'); + } + } + catch (\Exception $e) + { + rzpLogError($e->getMessage()); + } + } + public function autoEnableWebhook() { $webhookExist = false; @@ -1031,6 +1385,21 @@ public function createOrGetRazorpayOrderId($order, $orderId, $is1ccCheckout = 'n { try { + // Every hour post installation check + $getPostInstallationFlag = get_option('rzp_post_installation_update_at'); + if (empty($getPostInstallationFlag) == false) + { + if ($getPostInstallationFlag + 3600 < time()) + { + $this->autoPostInstallationCheck(); + } + } + else + { + update_option('rzp_post_installation_update_at', $time); + $this->autoPostInstallationCheck(); + } + return $this->createRazorpayOrderId($orderId, $sessionKey); } // For the bad request errors, it's safe to show the message to the customer. @@ -1131,6 +1500,32 @@ public function generate_razorpay_form($orderId) return $html; } + public function getDisplayConfig() + { + $config = [ + 'display' => [ + 'hide' => [ + [ + 'method' => 'cod' + ] + ] + ] + ]; + + $checkout360_status = get_option('rzp_checkout360_status'); + $rzp_cod_intelligence_enable = get_option('rzp_cod_intelligence_enable'); + + if ((isset($checkout360_status) === true) and + ($checkout360_status === 'yes') and + (empty($rzp_cod_intelligence_enable) === false) and + ($rzp_cod_intelligence_enable === 'yes')) + { + unset($config['display']['hide']); + } + + return $config; + } + /** * default parameters passed to checkout * @param WC_Order $order WC Order @@ -1152,6 +1547,8 @@ public function getDefaultCheckoutArguments($order) $productinfo = "Order $orderId"; + $config = $this->getDisplayConfig(); + return array( 'key' => $this->getSetting('key_id'), 'name' => html_entity_decode(get_bloginfo('name'), ENT_QUOTES), @@ -1163,7 +1560,8 @@ public function getDefaultCheckoutArguments($order) ), 'order_id' => $razorpayOrderId, 'callback_url' => $callbackUrl, - 'prefill' => $this->getCustomerInfo($order) + 'prefill' => $this->getCustomerInfo($order), + 'config' => $config ); } @@ -2935,11 +3333,13 @@ function razorpay_woo_plugin_links($links) function trigger_affordability_widget() { + $checkout360status = (get_option('rzp_checkout360_status') === 'yes') ? true : false; if (empty(get_option('rzp_afd_enable')) === false and get_option('rzp_afd_enable') === 'yes') { - if (empty(get_option('rzp_afd_feature_checked')) === true or - get_option('rzp_afd_feature_checked') === 'no') + if ((empty(get_option('rzp_afd_feature_checked')) === true or + get_option('rzp_afd_feature_checked') === 'no' ) and + $checkout360status === false) { try { @@ -2981,8 +3381,11 @@ function trigger_affordability_widget() function trigger_rtb_widget() { - if (empty(get_option('rzp_rtb_feature_checked')) === true or - get_option('rzp_rtb_feature_checked') === 'no') + $checkout360status = (get_option('rzp_checkout360_status') === 'yes') ? true : false; + + if ((empty(get_option('rzp_rtb_feature_checked')) === true or + get_option('rzp_rtb_feature_checked') === 'no') and + $checkout360status === false) { try { @@ -3007,11 +3410,15 @@ function trigger_rtb_widget() } } - if (empty(get_option('woocommerce_razorpay_settings')) === false and + if ((empty(get_option('woocommerce_razorpay_settings')) === false and isset(get_option('woocommerce_razorpay_settings')['enable_rtb_widget']) === true and get_option('woocommerce_razorpay_settings')['enable_rtb_widget'] === 'yes' and empty(get_option('rzp_rtb_enable')) === false and - get_option('rzp_rtb_enable') === 'yes') + get_option('rzp_rtb_enable') === 'yes') or + $checkout360status === true and + empty(get_option('rzp_rtb_enable')) === false and + get_option('rzp_rtb_enable') === 'yes' + ) { $key_id = get_option('woocommerce_razorpay_settings')['key_id']; echo '