Skip to content

Commit

Permalink
choose magic plan admin side
Browse files Browse the repository at this point in the history
  • Loading branch information
yashgit891 committed May 29, 2024
1 parent 6c5b543 commit d750a6a
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 2 deletions.
27 changes: 27 additions & 0 deletions includes/api/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 3 additions & 0 deletions public/css/1cc-plan-hide.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.hidden-field {
display: none;
}
14 changes: 13 additions & 1 deletion public/js/admin-rzp-settings.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand Down
101 changes: 100 additions & 1 deletion woo-razorpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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();

Expand Down Expand Up @@ -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();
?>
<tr valign="top" class="<?php echo esc_attr( $hidden_class ); ?>" id="woocommerce_razorpay_1cc_plan">
<th scope="row" class="titledesc">
<label><?php echo wp_kses_post($data['title']); ?></label>
<?php echo $this->get_tooltip_html($data); ?>
</th>
<td class="forminp">
<fieldset>
<?php foreach ($data['options'] as $option_key => $option_label) : ?>
<label for="<?php echo esc_attr($field . '_' . $option_key); ?>">
<input type="checkbox" name="<?php echo esc_attr($field . '[]'); ?>" id="<?php echo esc_attr($field . '_' . $option_key); ?>" value="<?php echo esc_attr($option_key); ?>" <?php checked(in_array($option_key, $value), true); ?> <?php disabled($this->check1ccFeatureFlag($option_key), true); ?>>
<?php echo esc_html($option_label); ?>
</label><br>
<?php endforeach; ?>
<?php echo $this->get_description_html($data); ?>
</fieldset>
</td>
</tr>
<?php
return ob_get_clean();
}

public function check1ccFeatureFlag($featureFlag)
{
if (!empty($this->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';
Expand Down

0 comments on commit d750a6a

Please sign in to comment.