| @@ -1,61 +1,273 @@ | ||
| 1 | -document.addEventListener('DOMContentLoaded', function() { | |
| 2 | - var form = document.getElementById('mxchat-activation-form'); | |
| 3 | - var spinner = document.getElementById('mxchat-activation-spinner'); | |
| 4 | - var submitButton = document.getElementById('activate_license_button'); | |
| 5 | - var licenseStatus = document.getElementById('mxchat-license-status'); | |
| 1 | +/** | |
| 2 | + * Enhanced MxChat License Activation JS | |
| 3 | + * This handles BOTH local activation AND domain tracking | |
| 4 | + * Plus deactivation functionality | |
| 5 | + * VERSION: Increased timeouts for better user experience | |
| 6 | + */ | |
| 7 | +jQuery(document).ready(function($) { | |
| 8 | + // Get configuration from hidden fields | |
| 9 | + const AJAX_URL = $('#mxchat-ajax-url').val() || ajaxurl; | |
| 10 | + const NONCE = $('#mxchat-nonce').val(); | |
| 11 | + const API_URL = $('#mxchat-api-url').val() || 'https://mxchat.ai'; | |
| 12 | + | |
| 13 | + // Elements | |
| 14 | + const form = $('#mxchat-activation-form'); | |
| 15 | + const spinner = $('#mxchat-activation-spinner'); | |
| 16 | + const submitButton = $('#activate_license_button'); | |
| 17 | + const licenseStatus = $('#mxchat-license-status'); | |
| 18 | + | |
| 19 | + // Activation handling | |
| 20 | + if (form.length && licenseStatus.length && submitButton.length) { | |
| 21 | + function handleActivationResponse(response) { | |
| 22 | + if (response.success) { | |
| 23 | + licenseStatus.text('Active'); | |
| 24 | + licenseStatus.removeClass('inactive').addClass('active'); | |
| 25 | + form.hide(); | |
| 26 | + | |
| 27 | + // Track domain BEFORE showing success | |
| 28 | + trackDomainActivation() | |
| 29 | + .done(function(trackResponse) { | |
| 30 | + console.log('Domain tracking successful:', trackResponse); | |
| 31 | + spinner.hide(); | |
| 32 | + alert('License activated and domain linked successfully!'); | |
| 33 | + | |
| 34 | + // Wait longer before reload to ensure all processes complete | |
| 35 | + setTimeout(function() { | |
| 36 | + location.reload(); | |
| 37 | + }, 3000); | |
| 38 | + }) | |
| 39 | + .fail(function(jqXHR, textStatus, errorThrown) { | |
| 40 | + console.error('Domain tracking failed:', textStatus, errorThrown); | |
| 41 | + spinner.hide(); | |
| 42 | + | |
| 43 | + // Still show success but warn about domain linking | |
| 44 | + alert('License activated successfully!\n\nNote: Domain linking failed. You may need to use the "Link Domain" button after the page reloads.'); | |
| 45 | + | |
| 46 | + // Wait longer before reload to ensure all cleanup is done | |
| 47 | + setTimeout(function() { | |
| 48 | + location.reload(); | |
| 49 | + }, 4000); | |
| 50 | + }); | |
| 51 | + } else { | |
| 52 | + spinner.hide(); | |
| 53 | + licenseStatus.text('Inactive'); | |
| 54 | + alert(response.data || 'Activation failed. Please check your input.'); | |
| 55 | + submitButton.prop('disabled', false); | |
| 56 | + } | |
| 57 | + } | |
| 6 | 58 | |
| 7 | - form.addEventListener('submit', function(e) { | |
| 8 | - e.preventDefault(); | |
| 59 | + function checkActivationStatus() { | |
| 60 | + const email = $('#mxchat_pro_email').val(); | |
| 61 | + const key = $('#mxchat_activation_key').val(); | |
| 62 | + | |
| 63 | + $.ajax({ | |
| 64 | + type: 'POST', | |
| 65 | + url: AJAX_URL, | |
| 66 | + data: { | |
| 67 | + action: 'mxchat_check_license_status', | |
| 68 | + email: email, | |
| 69 | + key: key, | |
| 70 | + security: NONCE | |
| 71 | + }, | |
| 72 | + timeout: 45000, // Increased from default to 45 seconds | |
| 73 | + success: function(response) { | |
| 74 | + if (response.is_active) { | |
| 75 | + licenseStatus.text('Active'); | |
| 76 | + licenseStatus.removeClass('inactive').addClass('active'); | |
| 77 | + form.hide(); | |
| 78 | + | |
| 79 | + // Try to track domain for timeout scenario | |
| 80 | + trackDomainActivation() | |
| 81 | + .done(function(trackResponse) { | |
| 82 | + console.log('Domain tracking completed after timeout check:', trackResponse); | |
| 83 | + alert('Your license has been activated and domain linked successfully!'); | |
| 84 | + setTimeout(function() { | |
| 85 | + location.reload(); | |
| 86 | + }, 3000); | |
| 87 | + }) | |
| 88 | + .fail(function() { | |
| 89 | + console.log('Domain tracking failed after timeout check'); | |
| 90 | + alert('Your license has been activated successfully!\n\nNote: Domain linking may have failed. Check the "Link Domain" button after reload if needed.'); | |
| 91 | + setTimeout(function() { | |
| 92 | + location.reload(); | |
| 93 | + }, 3000); | |
| 94 | + }); | |
| 95 | + } else { | |
| 96 | + alert('License activation timed out. Please try again or contact support if the problem persists.'); | |
| 97 | + submitButton.prop('disabled', false); | |
| 98 | + } | |
| 99 | + }, | |
| 100 | + error: function() { | |
| 101 | + alert('Unable to verify license status. Please refresh the page to check if activation was successful.'); | |
| 102 | + submitButton.prop('disabled', false); | |
| 103 | + } | |
| 104 | + }); | |
| 105 | + } | |
| 9 | 106 | |
| 10 | - // Show spinner and disable the button to prevent multiple submissions | |
| 11 | - spinner.style.display = 'inline-block'; | |
| 12 | - submitButton.disabled = true; | |
| 107 | + function trackDomainActivation() { | |
| 108 | + const email = $('#mxchat_pro_email').val(); | |
| 109 | + const key = $('#mxchat_activation_key').val(); | |
| 110 | + const domain = $('#mxchat_domain').val(); | |
| 111 | + | |
| 112 | + console.log('Attempting to track domain:', domain); | |
| 113 | + | |
| 114 | + return $.ajax({ | |
| 115 | + type: 'POST', | |
| 116 | + url: API_URL + '/mxchat-api/link-domain', // Changed to use same endpoint as manual linking | |
| 117 | + data: { | |
| 118 | + email: email, // Changed parameter name to match manual linking | |
| 119 | + license_key: key, // Changed parameter name to match manual linking | |
| 120 | + domain: domain | |
| 121 | + }, | |
| 122 | + timeout: 30000, // Increased from 10 seconds to 30 seconds | |
| 123 | + xhrFields: { | |
| 124 | + withCredentials: false // Ensure CORS compatibility | |
| 125 | + }, | |
| 126 | + crossDomain: true | |
| 127 | + }); | |
| 128 | + } | |
| 13 | 129 | |
| 14 | - // Gather form data | |
| 15 | - var formData = new FormData(form); | |
| 16 | - formData.append('action', 'mxchat_activate_license'); | |
| 17 | - formData.append('security', mxchatData.nonce); | |
| 18 | - | |
| 19 | - // Send the AJAX request | |
| 20 | - var xhr = new XMLHttpRequest(); | |
| 21 | - xhr.open('POST', mxchatData.ajax_url, true); | |
| 22 | - xhr.onload = function() { | |
| 23 | - // Hide the spinner | |
| 24 | - spinner.style.display = 'none'; | |
| 25 | - | |
| 26 | - if (xhr.status >= 200 && xhr.status < 400) { | |
| 27 | - var response = JSON.parse(xhr.responseText); | |
| 130 | + form.on('submit', function(event) { | |
| 131 | + event.preventDefault(); | |
| 132 | + spinner.show(); | |
| 133 | + submitButton.prop('disabled', true); | |
| 134 | + | |
| 135 | + var formData = { | |
| 136 | + action: 'mxchat_handle_activate_license', | |
| 137 | + mxchat_pro_email: $('#mxchat_pro_email').val(), | |
| 138 | + mxchat_activation_key: $('#mxchat_activation_key').val(), | |
| 139 | + security: NONCE | |
| 140 | + }; | |
| 141 | + | |
| 142 | + $.ajax({ | |
| 143 | + type: 'POST', | |
| 144 | + url: AJAX_URL, | |
| 145 | + data: formData, | |
| 146 | + timeout: 120000, // Increased from 70 seconds to 2 minutes (120 seconds) | |
| 147 | + success: function(response) { | |
| 148 | + handleActivationResponse(response); | |
| 149 | + }, | |
| 150 | + error: function(jqXHR, textStatus) { | |
| 151 | + if (textStatus === 'timeout') { | |
| 152 | + spinner.hide(); | |
| 153 | + alert('Activation is taking longer than expected. Checking status...'); | |
| 154 | + checkActivationStatus(); | |
| 155 | + } else { | |
| 156 | + alert('Activation failed due to a server error: ' + (jqXHR.responseText || textStatus)); | |
| 157 | + spinner.hide(); | |
| 158 | + submitButton.prop('disabled', false); | |
| 159 | + } | |
| 160 | + } | |
| 161 | + }); | |
| 162 | + }); | |
| 163 | + } | |
| 164 | + | |
| 165 | + // Domain linking for existing activations | |
| 166 | + $('#link-domain-button').on('click', function() { | |
| 167 | + const button = $(this); | |
| 168 | + const status = $('#domain-link-status'); | |
| 169 | + const originalText = button.text(); | |
| 170 | + | |
| 171 | + // Get stored values from the page | |
| 172 | + const email = $('input#mxchat_pro_email').val() || | |
| 173 | + $('p:contains("Email:")').text().split(':')[1]?.trim() || | |
| 174 | + ''; | |
| 175 | + const license_key = $('input#mxchat_activation_key').val() || | |
| 176 | + $('code:contains("-")').text().trim() || | |
| 177 | + ''; | |
| 178 | + const domain = $('#mxchat_domain').val(); | |
| 179 | + | |
| 180 | + if (!email || !license_key) { | |
| 181 | + status.html('<span style="color: red;">✗ Missing email or license key</span>'); | |
| 182 | + return; | |
| 183 | + } | |
| 184 | + | |
| 185 | + button.prop('disabled', true).text('Linking...'); | |
| 186 | + status.html('<span style="color: #666;">Processing...</span>'); | |
| 187 | + | |
| 188 | + $.ajax({ | |
| 189 | + type: 'POST', | |
| 190 | + url: API_URL + '/mxchat-api/link-domain', | |
| 191 | + data: { | |
| 192 | + email: email, | |
| 193 | + license_key: license_key, | |
| 194 | + domain: domain | |
| 195 | + }, | |
| 196 | + timeout: 30000, // Increased from 10 seconds to 30 seconds | |
| 197 | + success: function(response) { | |
| 28 | 198 | if (response.success) { |
| 29 | - // Update the license status to active | |
| 30 | - licenseStatus.textContent = 'Active'; | |
| 31 | - licenseStatus.classList.remove('inactive'); | |
| 32 | - licenseStatus.classList.add('active'); | |
| 33 | - | |
| 34 | - // Hide the activation form | |
| 35 | - form.style.display = 'none'; | |
| 199 | + status.html('<span style="color: green;">✓ Domain linked successfully!</span>'); | |
| 200 | + button.hide(); | |
| 201 | + | |
| 202 | + setTimeout(function() { | |
| 203 | + location.reload(); | |
| 204 | + }, 2000); | |
| 36 | 205 | } else { |
| 37 | - // Re-enable the button | |
| 38 | - submitButton.disabled = false; | |
| 39 | - | |
| 40 | - // Handle the error case | |
| 41 | - alert(response.data || 'There was an error activating the license.'); | |
| 206 | + status.html('<span style="color: red;">✗ ' + (response.data || 'Failed to link domain') + '</span>'); | |
| 207 | + button.prop('disabled', false).text(originalText); | |
| 42 | 208 | } |
| 43 | - } else { | |
| 44 | - // Re-enable the button | |
| 45 | - submitButton.disabled = false; | |
| 46 | - | |
| 47 | - // Handle server error | |
| 48 | - alert('Server error. Please try again.'); | |
| 209 | + }, | |
| 210 | + error: function(jqXHR, textStatus, errorThrown) { | |
| 211 | + console.error('Domain linking error:', textStatus, errorThrown); | |
| 212 | + status.html('<span style="color: red;">✗ Connection error. Please try again.</span>'); | |
| 213 | + button.prop('disabled', false).text(originalText); | |
| 49 | 214 | } |
| 50 | - }; | |
| 51 | - | |
| 52 | - xhr.onerror = function() { | |
| 53 | - // Hide the spinner and re-enable the button on error | |
| 54 | - spinner.style.display = 'none'; | |
| 55 | - submitButton.disabled = false; | |
| 56 | - alert('Request failed. Please check your internet connection.'); | |
| 57 | - }; | |
| 58 | - | |
| 59 | - xhr.send(formData); // Send the form data | |
| 215 | + }); | |
| 60 | 216 | }); |
| 61 | -}); | |
| 217 | + | |
| 218 | + // License deactivation | |
| 219 | + $('#deactivate-license-button').on('click', function() { | |
| 220 | + const button = $(this); | |
| 221 | + const status = $('#deactivate-status'); | |
| 222 | + const originalText = button.text(); | |
| 223 | + | |
| 224 | + // Confirmation dialog | |
| 225 | + const confirmMessage = 'Are you sure you want to deactivate this license?\n\n' + | |
| 226 | + 'This will:\n' + | |
| 227 | + '• Disable pro features on this site\n' + | |
| 228 | + '• Unlink this domain from your account\n' + | |
| 229 | + '• Allow you to activate on a different site\n\n' + | |
| 230 | + 'You can reactivate anytime with the same license key.'; | |
| 231 | + | |
| 232 | + if (!confirm(confirmMessage)) { | |
| 233 | + return; | |
| 234 | + } | |
| 235 | + | |
| 236 | + button.prop('disabled', true).text('Deactivating...'); | |
| 237 | + status.html('<span style="color: #666;">Processing...</span>'); | |
| 238 | + | |
| 239 | + $.ajax({ | |
| 240 | + type: 'POST', | |
| 241 | + url: AJAX_URL, | |
| 242 | + data: { | |
| 243 | + action: 'mxchat_deactivate_license', | |
| 244 | + security: NONCE | |
| 245 | + }, | |
| 246 | + timeout: 45000, // Increased from 15 seconds to 45 seconds | |
| 247 | + success: function(response) { | |
| 248 | + if (response.success) { | |
| 249 | + status.html('<span style="color: green;">✓ ' + response.data.message + '</span>'); | |
| 250 | + | |
| 251 | + // Show success message | |
| 252 | + alert('License deactivated successfully!\n\nYou can now activate this license on another site.'); | |
| 253 | + | |
| 254 | + // Reload page to show deactivated state | |
| 255 | + setTimeout(function() { | |
| 256 | + location.reload(); | |
| 257 | + }, 2000); | |
| 258 | + } else { | |
| 259 | + status.html('<span style="color: red;">✗ ' + (response.data || 'Deactivation failed') + '</span>'); | |
| 260 | + button.prop('disabled', false).text(originalText); | |
| 261 | + } | |
| 262 | + }, | |
| 263 | + error: function(jqXHR, textStatus) { | |
| 264 | + if (textStatus === 'timeout') { | |
| 265 | + status.html('<span style="color: orange;">⚠ Deactivation timed out. Please check your account dashboard.</span>'); | |
| 266 | + } else { | |
| 267 | + status.html('<span style="color: red;">✗ Connection error. Please try again.</span>'); | |
| 268 | + } | |
| 269 | + button.prop('disabled', false).text(originalText); | |
| 270 | + } | |
| 271 | + }); | |
| 272 | + }); | |
| 273 | +}); | |