/**
* Enhanced MxChat License Activation JS
* This handles BOTH local activation AND domain tracking
* Plus deactivation functionality
*/
jQuery(document).ready(function($) {
// Get configuration from hidden fields
const AJAX_URL = $('#mxchat-ajax-url').val() || ajaxurl;
const NONCE = $('#mxchat-nonce').val();
const API_URL = $('#mxchat-api-url').val() || 'https://mxchat.ai';
// Elements
const form = $('#mxchat-activation-form');
const spinner = $('#mxchat-activation-spinner');
const submitButton = $('#activate_license_button');
const licenseStatus = $('#mxchat-license-status');
// Activation handling
if (form.length && licenseStatus.length && submitButton.length) {
function handleActivationResponse(response) {
if (response.success) {
licenseStatus.text('Active');
licenseStatus.removeClass('inactive').addClass('active');
form.hide();
// Track domain BEFORE showing success
trackDomainActivation()
.done(function(trackResponse) {
console.log('Domain tracking successful:', trackResponse);
spinner.hide();
alert('License activated and domain linked successfully!');
setTimeout(function() {
location.reload();
}, 1000);
})
.fail(function(jqXHR, textStatus, errorThrown) {
console.error('Domain tracking failed:', textStatus, errorThrown);
spinner.hide();
// Still show success but warn about domain linking
alert('License activated successfully!\n\nNote: Domain linking failed. You may need to use the "Link Domain" button after the page reloads.');
setTimeout(function() {
location.reload();
}, 2000);
});
} else {
spinner.hide();
licenseStatus.text('Inactive');
alert(response.data || 'Activation failed. Please check your input.');
submitButton.prop('disabled', false);
}
}
function checkActivationStatus() {
const email = $('#mxchat_pro_email').val();
const key = $('#mxchat_activation_key').val();
$.ajax({
type: 'POST',
url: AJAX_URL,
data: {
action: 'mxchat_check_license_status',
email: email,
key: key,
security: NONCE
},
success: function(response) {
if (response.is_active) {
licenseStatus.text('Active');
licenseStatus.removeClass('inactive').addClass('active');
form.hide();
// Try to track domain for timeout scenario
trackDomainActivation()
.always(function() {
alert('Your license has been activated successfully!');
setTimeout(function() {
location.reload();
}, 1500);
});
} else {
alert('License activation timed out. Please try again or contact support if the problem persists.');
submitButton.prop('disabled', false);
}
},
error: function() {
alert('Unable to verify license status. Please refresh the page to check if activation was successful.');
submitButton.prop('disabled', false);
}
});
}
function trackDomainActivation() {
const email = $('#mxchat_pro_email').val();
const key = $('#mxchat_activation_key').val();
const domain = $('#mxchat_domain').val();
console.log('Attempting to track domain:', domain);
return $.ajax({
type: 'POST',
url: API_URL + '/mxchat-api/activate-license',
data: {
mxchat_pro_email: email,
mxchat_activation_key: key,
domain: domain
},
timeout: 10000, // 10 second timeout
xhrFields: {
withCredentials: false // Ensure CORS compatibility
},
crossDomain: true
});
}
form.on('submit', function(event) {
event.preventDefault();
spinner.show();
submitButton.prop('disabled', true);
var formData = {
action: 'mxchat_handle_activate_license',
mxchat_pro_email: $('#mxchat_pro_email').val(),
mxchat_activation_key: $('#mxchat_activation_key').val(),
security: NONCE
};
$.ajax({
type: 'POST',
url: AJAX_URL,
data: formData,
timeout: 70000,
success: function(response) {
handleActivationResponse(response);
},
error: function(jqXHR, textStatus) {
if (textStatus === 'timeout') {
spinner.hide();
alert('Activation is taking longer than expected. Checking status...');
checkActivationStatus();
} else {
alert('Activation failed due to a server error: ' + (jqXHR.responseText || textStatus));
spinner.hide();
submitButton.prop('disabled', false);
}
}
});
});
}
// Domain linking for existing activations
$('#link-domain-button').on('click', function() {
const button = $(this);
const status = $('#domain-link-status');
const originalText = button.text();
// Get stored values from the page
const email = $('input#mxchat_pro_email').val() ||
$('p:contains("Email:")').text().split(':')[1]?.trim() ||
'';
const license_key = $('input#mxchat_activation_key').val() ||
$('code:contains("-")').text().trim() ||
'';
const domain = $('#mxchat_domain').val();
if (!email || !license_key) {
status.html('✗ Missing email or license key');
return;
}
button.prop('disabled', true).text('Linking...');
status.html('Processing...');
$.ajax({
type: 'POST',
url: API_URL + '/mxchat-api/link-domain',
data: {
email: email,
license_key: license_key,
domain: domain
},
timeout: 10000,
success: function(response) {
if (response.success) {
status.html('✓ Domain linked successfully!');
button.hide();
setTimeout(function() {
location.reload();
}, 2000);
} else {
status.html('✗ ' + (response.data || 'Failed to link domain') + '');
button.prop('disabled', false).text(originalText);
}
},
error: function(jqXHR, textStatus, errorThrown) {
console.error('Domain linking error:', textStatus, errorThrown);
status.html('✗ Connection error. Please try again.');
button.prop('disabled', false).text(originalText);
}
});
});
// License deactivation (unchanged)
$('#deactivate-license-button').on('click', function() {
const button = $(this);
const status = $('#deactivate-status');
const originalText = button.text();
// Confirmation dialog
const confirmMessage = 'Are you sure you want to deactivate this license?\n\n' +
'This will:\n' +
'• Disable pro features on this site\n' +
'• Unlink this domain from your account\n' +
'• Allow you to activate on a different site\n\n' +
'You can reactivate anytime with the same license key.';
if (!confirm(confirmMessage)) {
return;
}
button.prop('disabled', true).text('Deactivating...');
status.html('Processing...');
$.ajax({
type: 'POST',
url: AJAX_URL,
data: {
action: 'mxchat_deactivate_license',
security: NONCE
},
timeout: 15000,
success: function(response) {
if (response.success) {
status.html('✓ ' + response.data.message + '');
// Show success message
alert('License deactivated successfully!\n\nYou can now activate this license on another site.');
// Reload page to show deactivated state
setTimeout(function() {
location.reload();
}, 2000);
} else {
status.html('✗ ' + (response.data || 'Deactivation failed') + '');
button.prop('disabled', false).text(originalText);
}
},
error: function(jqXHR, textStatus) {
if (textStatus === 'timeout') {
status.html('⚠ Deactivation timed out. Please check your account dashboard.');
} else {
status.html('✗ Connection error. Please try again.');
}
button.prop('disabled', false).text(originalText);
}
});
});
});