I wish to create a license number generator, for a product sold on my WordPress site, that is identical to the one in my Filemaker (standalone/unserved) database.
The only language common to both web and FM is Javascript.
I created an FM script that scrapes my FM solution’s webviewer for the license number created after a client enters their name.
All efforts to create a matching process on my website – that sends a license number to a client when they complete an order – have failed.
Is is possible to generate a license number using Javascript; then, incorporate the result in a Woocommerce order completed email?
I’ve tried various methods to generate a license number, and dispatch it on completion of a Woocommerce order.
A PHP translation of my FM code, added to my website’s function.php, file ‘worked’ (i.e. a license number was generated and sent via email), but it failed to produce a license number that matched either the FM or Javascript ones created within FM.
Efforts to have Javascript create the license number and PHP run the code and email – either via my website’s function.php file, a pair of code snippets, or a DIY plugin – did nothing at all, and nor could I work out what the problem was because no debug file could be generated.
Here is a PHP snippet used to call the Javascript:
add_action('wp_enqueue_scripts', 'enqueue_custom_js');
function enqueue_custom_js() {
wp_enqueue_script('custom-reg-code-js', plugin_dir_url(__FILE__) . 'reg-code.js', array('jquery'), null, true);
wp_localize_script('custom-reg-code-js', 'customData', array(
'ajaxUrl' => admin_url('admin-ajax.php'),
));
}
add_action('wp_ajax_custom_send_email', 'custom_send_email');
add_action('wp_ajax_nopriv_custom_send_email', 'custom_send_email');
function custom_send_email() {
$regName = sanitize_text_field($_POST['regName']);
$users = intval($_POST['users']);
$email = sanitize_email($_POST['email']);
$regCode = sanitize_text_field($_POST['regCode']);
error_log("Received AJAX: RegName - $regName, Users - $users, Email - $email, RegCode - $regCode");
if (empty($regCode) || empty($email)) {
error_log("Registration code or email is empty.");
wp_send_json_error('Registration code or email is empty.');
return;
}
$email_sent = send_registration_email($email, $regName, $regCode);
if ($email_sent) {
error_log("Email sent successfully.");
wp_send_json_success('Email sent successfully.');
} else {
error_log("Failed to send email.");
wp_send_json_error('Failed to send email.');
}
}
function send_registration_email($to, $name, $registration_code) {
$subject = 'Your Registration Code New';
$message = "<html><body><h2>Thank you for your purchase!</h2><p>Dear {$name},</p><p>Your registration code is: <strong>{$registration_code}</strong></p><p>Please use this code to activate your new software.</p><p>If you have any questions, please don't hesitate to contact our support team.</p><p>Best regards,<br>Your Company Name</p></body></html>";
$headers = array('Content-Type: text/html; charset=UTF-8');
return wp_mail($to, $subject, $message, $headers);
}
And here is the Javascript it should ‘call’:
function generateRegistrationCode(regName, users) {
const solutionKey = 'tcuXd6A4WQ';
if (!regName || !solutionKey) {
console.error('Registration name or solution key is empty.');
return '';
}
// Always append users (which is set to 1)
regName += users;
let len = regName.length;
let chr = 0, pos = 0;
for (let loop = 0; loop < len; loop++) {
const ascii = regName.charCodeAt(loop);
chr += ascii;
pos += (ascii * (loop + 1));
}
const sum = chr + pos + len;
const numString = `${sum}${pos}${chr}${len}${sum * sum}`;
let codePreFormatted = '';
let loop = 0;
while (loop < numString.length) {
const convertNum = (parseInt(numString.substr(loop, 2)) > 56) ? parseInt(numString.substr(loop, 1)) : parseInt(numString.substr(loop, 2));
codePreFormatted += (convertNum == 0) ? solutionKey[0] + solutionKey[1] : solutionKey[convertNum];
loop += convertNum.toString().length;
}
let codeFormatted = '';
for (let loop = 0; loop < codePreFormatted.length; loop += 4) {
if (codeFormatted) codeFormatted += '-';
codeFormatted += codePreFormatted.substr(loop, 4);
}
// Append users (always 1) to the final code
codeFormatted += `-[${users}]`;
console.log(`Generated registration code: ${codeFormatted}`);
// Append the registration code to the checkout page
document.getElementById('registration-code-display').innerText = codeFormatted || 'Registration code generation failed.';
return codeFormatted;
}
// Send registration code via AJAX
function sendRegistrationCode(email, regName, users) {
const regCode = generateRegistrationCode(regName, users);
if (!regCode) {
alert("Registration code generation failed. Please check the input.");
console.error("Failed to generate registration code.");
return;
}
// Perform AJAX request to send the email
jQuery.ajax({
url: customData.ajaxUrl,
method: 'POST',
data: {
action: 'custom_send_email',
regName: regName,
users: users,
email: email,
regCode: regCode,
},
success: function(response) {
if (response.success) {
alert('Registration code sent successfully!');
} else {
alert('Failed to send the registration code: ' + response.data);
console.error('Failed to send the registration code:', response.data);
}
},
error: function(xhr, status, error) {
alert('An error occurred: ' + error);
console.error('AJAX error:', error);
}
});
}
The number of users argument could probably be removed, as it will always be 1.
The customer orders are completed automatically, using the 'standard' Woocomerce procedure ('standard' because I haven't changed the default code created after I installed the Woocommerce plugin, and added 1 product).
The auto-generated 'order complete' email is successfully sent once the user submits (test) payment on my checkout page.
If I translate the license number generation sequence into PHP, the license number is sent in a separate email, but this license number doesn't match the one created in FM.