Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions cypress/integration/02_frontend_checks.cy.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
const URLS = {
EXTEND_API_OFFER: 'https://api.helloextend.com/offers*',
WOO_ADD_TO_CART: 'https://woocommerce.woodys.extend.com/?wc-ajax=add_to_cart',
WOODYS: 'https://woocommerce.woodys.extend.com/',
WOODYS_PRODUCT: 'https://woocommerce.woodys.extend.com/product/air-purifier/',
}

describe('Frontend Page and API Validation', () => {
it('Checks Homepage for 500 Errors', () => {
cy.visit('https://woocommerce.woodys.extend.com/', { failOnStatusCode: false });
cy.visit(URLS.WOODYS, { failOnStatusCode: false });

// Ensure the page loaded without 500 errors
cy.document().then((doc) => {
Expand All @@ -9,7 +16,7 @@ describe('Frontend Page and API Validation', () => {
});

it('Checks Product Page for 500 Errors', () => {
cy.visit('https://woocommerce.woodys.extend.com/product/15in-dell-laptop/', { failOnStatusCode: false });
cy.visit(URLS.WOODYS_PRODUCT, { failOnStatusCode: false });

// Ensure the page loaded without 500 errors
cy.document().then((doc) => {
Expand All @@ -18,10 +25,10 @@ describe('Frontend Page and API Validation', () => {
});

it('Intercepts Offers API Request and Validates Query Parameters', () => {
cy.intercept('GET', 'https://api.helloextend.com/offers*').as('offersRequest');
cy.intercept('GET', URLS.EXTEND_API_OFFER).as('offersRequest');

// Visit the product page
cy.visit('https://woocommerce.woodys.extend.com/product/15in-dell-laptop/');
cy.visit(URLS.WOODYS_PRODUCT);

// Wait for the intercepted API request
cy.wait('@offersRequest').then((interception) => {
Expand All @@ -34,20 +41,20 @@ describe('Frontend Page and API Validation', () => {

// Validate expected query parameters
expect(queryParams.storeId).to.equal('0e03bcd1-8a00-4a2a-bf2d-7c4d336c07e9');
expect(queryParams.productId).to.equal('104');
expect(queryParams.productId).to.equal('15');
expect(queryParams.category).to.equal('Electronics');
expect(queryParams.price).to.equal('69900');
expect(queryParams.price).to.equal('22900');
} else {
throw new Error('API request was not intercepted.');
}
});
});

it('Intercepts Offers API Response Checks for Plans In ADH or Base', () => {
cy.intercept('GET', 'https://api.helloextend.com/offers*').as('offersResponse');
cy.intercept('GET', URLS.EXTEND_API_OFFER).as('offersResponse');

// Visit the product page
cy.visit('https://woocommerce.woodys.extend.com/product/15in-dell-laptop/');
cy.visit(URLS.WOODYS_PRODUCT);

// Wait for the API response
cy.wait('@offersResponse').then((interception) => {
Expand Down Expand Up @@ -76,7 +83,7 @@ describe('Frontend Page and API Validation', () => {

it('Validates Extend PDP Offers & Modal Are Working', () => {
// Visit product page
cy.visit('https://woocommerce.woodys.extend.com/product/15in-dell-laptop/');
cy.visit(URLS.WOODYS_PRODUCT);

// Check if the Extend Offer iFrame exists
cy.get('div.helloextend-offer > div.extend-product-offer > iframe')
Expand Down Expand Up @@ -135,11 +142,11 @@ describe('Frontend Page and API Validation', () => {
});

it('Ensures Cart Normalization and Simple Offers Are working', () => {
cy.visit('https://woocommerce.woodys.extend.com/product/15in-dell-laptop/');
cy.visit(URLS.WOODYS_PRODUCT);

cy.get('[name="add-to-cart"]').then(($button) => {
let productDetails = {
productId: '104'// Check for different attributes
productId: '15'// Check for different attributes
// ... other product details you need ...
};

Expand All @@ -151,9 +158,9 @@ describe('Frontend Page and API Validation', () => {
// the UI and directly interacts with WooCommerce's backend.
cy.request({
method: 'POST',
url: 'https://woocommerce.woodys.extend.com/?wc-ajax=add_to_cart', // Adjust if needed
url: URLS.WOO_ADD_TO_CART, // Adjust if needed
body: {
product_id: '104', // Use the extracted product ID
product_id: '15', // Use the extracted product ID
quantity: 1, // Or whatever quantity you want
// ... any other required parameters (variation IDs, etc.) ...
},
Expand Down Expand Up @@ -222,15 +229,15 @@ describe('Frontend Page and API Validation', () => {
});

it('Validates Checkout Flow and Shipping Protection', () => {
cy.visit('https://woocommerce.woodys.extend.com/product/15in-dell-laptop/');
cy.visit(URLS.WOODYS_PRODUCT);

// 1. Get Product Details (Important!)
// You'll need to extract the product ID and any other relevant info
// to properly add it to the cart via the API. Inspect the page
// to see how WooCommerce structures its product data. Here's an example:
cy.get('[name="add-to-cart"]').then(($button) => {
let productDetails = {
productId: '104'// Check for different attributes
productId: '15'// Check for different attributes
// ... other product details you need ...
};

Expand All @@ -242,9 +249,9 @@ describe('Frontend Page and API Validation', () => {
// the UI and directly interacts with WooCommerce's backend.
cy.request({
method: 'POST',
url: 'https://woocommerce.woodys.extend.com/?wc-ajax=add_to_cart', // Adjust if needed
url: URLS.WOO_ADD_TO_CART, // Adjust if needed
body: {
product_id: '104', // Use the extracted product ID
product_id: '15', // Use the extracted product ID
quantity: 1, // Or whatever quantity you want
// ... any other required parameters (variation IDs, etc.) ...
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,12 @@ public function enqueue_scripts()
$nonce = wp_create_nonce('helloextend_sync_nonce');
$helloextend_sync_batch = $this->helloextend_protection_for_woocommerce_settings_catalog_sync_options['helloextend_sync_batch'];
$debug_log_enabled = $this->helloextend_protection_for_woocommerce_settings_general_options['enable_helloextend_debug'];
$log_enabled = $this->helloextend_protection_for_woocommerce_settings_general_options['enable_helloextend_log'];

wp_enqueue_script('helloextend_script');

wp_enqueue_script('helloextend_script');
wp_enqueue_script('helloextend_sync_script');
wp_localize_script('helloextend_sync_script', 'ExtendWooCommerce', compact('store_id', 'ajaxurl', 'environment', 'nonce', 'helloextend_sync_batch', 'debug_log_enabled'));
wp_localize_script('helloextend_sync_script', 'ExtendWooCommerce', compact('store_id', 'ajaxurl', 'environment', 'nonce', 'helloextend_sync_batch', 'debug_log_enabled', 'log_enabled'));

/* end for sync */
global $current_screen;
Expand Down Expand Up @@ -480,6 +482,14 @@ public function helloextend_protection_for_woocommerce_settings_page_init()
'helloextend_setting_environment_section' // section
);

add_settings_field(
'enable_helloextend_log', // id
'Enable General Log', // title
array($this, 'enable_helloextend_log_callback'), // callback
'helloextend-protection-for-woocommerce-settings-admin-general', // page
'helloextend_setting_environment_section' // section
);

add_settings_field(
'enable_helloextend_debug', // id
'Enable Debugging Log', // title
Expand Down Expand Up @@ -568,6 +578,7 @@ public function helloextend_protection_for_woocommerce_settings_page_init()
if (get_option('helloextend_protection_for_woocommerce_general_settings') == null) {
$settings = [
'enable_helloextend_debug' => '0',
'enable_helloextend_log' => '0',
'helloextend_environment' => 'sandbox',
'helloextend_sandbox_store_id' => '',
'helloextend_live_store_id' => '',
Expand Down Expand Up @@ -682,6 +693,10 @@ public function helloextend_protection_for_woocommerce_settings_sanitize($input)
$sanitary_values['enable_helloextend_debug'] = $input['enable_helloextend_debug'];
}

if (isset($input['enable_helloextend_log'])) {
$sanitary_values['enable_helloextend_log'] = $input['enable_helloextend_log'];
}

if (isset($input['helloextend_enable_cart_offers'])) {
$sanitary_values['helloextend_enable_cart_offers'] = $input['helloextend_enable_cart_offers'];
}
Expand Down Expand Up @@ -1225,6 +1240,15 @@ public function enable_helloextend_debug_callback()
);
}

public function enable_helloextend_log_callback()
{
printf(
'<input type="checkbox" name="helloextend_protection_for_woocommerce_general_settings[enable_helloextend_log]" id="enable_helloextend_log" value="1" %s>',
(isset($this->helloextend_protection_for_woocommerce_settings_general_options['enable_helloextend_log'])
&& $this->helloextend_protection_for_woocommerce_settings_general_options['enable_helloextend_log'] === '1') ? 'checked' : ''
);
}

public function helloextend_use_special_price_callback()
{
printf(
Expand Down
49 changes: 25 additions & 24 deletions helloextend-protection/includes/class-helloextend-global.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ public static function helloextend_get_settings()
$settings['enable_helloextend_debug'] = array_key_exists('enable_helloextend_debug', $helloextend_protection_general_settings)
? $helloextend_protection_general_settings['enable_helloextend_debug'] : 0;

$settings['enable_helloextend_log'] = array_key_exists('enable_helloextend_log', $helloextend_protection_general_settings)
? $helloextend_protection_general_settings['enable_helloextend_log'] : 0;

/* shipping protection */
if ($helloextend_protection_shipping_protection_settings) {
$settings['enable_helloextend_sp'] = array_key_exists('enable_helloextend_sp', $helloextend_protection_shipping_protection_settings)
Expand Down Expand Up @@ -252,9 +255,11 @@ public static function helloextend_get_settings()
$settings['warranty_product_id'] = array_key_exists('warranty_product_id', $settings)
? $settings['warranty_product_id'] : helloextend_product_protection_id();

if (empty($settings['warranty_product_id'])) {
/* circular reference removed.
if (empty($settings['warranty_product_id'])) {
HelloExtend_Protection_Logger::helloextend_log_error('Error: Warranty product is not created.');
}
*/

return $settings;
}
Expand Down Expand Up @@ -404,29 +409,25 @@ public function helloextend_init_global()
$environment = ($environment == 'live') ? $environment : 'demo';
$helloextend_enabled = array_key_exists('enable_helloextend', $settings) ? $settings['enable_helloextend'] : 0;
$ajaxurl = admin_url('admin-ajax.php');

if ($store_id && ($helloextend_enabled === '1')) {
wp_enqueue_script('helloextend_script');
wp_enqueue_script('helloextend_global_script');
wp_localize_script('helloextend_global_script', 'ExtendWooCommerce', compact('store_id', 'ajaxurl', 'environment'));

// Get the leadToken from URL parameters
$lead_token = $this->get_lead_token_from_url();
if ($lead_token) {
// Sanitize the token for safe JavaScript output
$safe_lead_token = esc_js($lead_token);

// Output JavaScript to console
echo "<script type='text/javascript'>\n";
echo "console.log('found leadToken: ', '" . $safe_lead_token . "');\n";
echo "</script>\n";

// next step: Run Post Purchase logic to handle lead Token
$this->helloextend_post_purchase($lead_token, $store_id, $environment, $ajaxurl);
}
} else {
HelloExtend_Protection_Logger::helloextend_log_error('Store Id missing or Extend Product Protection is disabled');
}
$debug_log_enabled = array_key_exists('enable_helloextend_debug', $settings) ? $settings['enable_helloextend_debug'] : 0;
$log_enabled = array_key_exists('enable_helloextend_log', $settings) ? $settings['enable_helloextend_log'] : 0;

if ($store_id){
if ($helloextend_enabled === '1') {
wp_enqueue_script('helloextend_script');
wp_enqueue_script('helloextend_global_script');
wp_localize_script('helloextend_global_script', 'ExtendWooCommerce', compact('store_id', 'ajaxurl', 'environment', 'debug_log_enabled', 'log_enabled'));

// Get the leadToken from URL parameters
$lead_token = $this->get_lead_token_from_url();
if ($lead_token) {
// next step: Run Post Purchase logic to handle lead Token
$this->helloextend_post_purchase($lead_token, $store_id, $environment, $ajaxurl);
}
}
} else {
HelloExtend_Protection_Logger::helloextend_log_error('Store Id is missing');
}
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,24 @@
exit;
}


class HelloExtend_Protection_Logger
{
private static ?array $settings = null;

private static function helloextend_log_initialize(): void {
if (self::$settings === null) {
/* retrieve environment variables */
self::$settings = HelloExtend_Protection_Global::helloextend_get_settings();
}
}

public static function helloextend_log_error( $message )
{

self::helloextend_log_initialize();
if (self::$settings['enable_helloextend_log'] == 0) {
Comment thread
jm-extend marked this conversation as resolved.
return;
}
Comment thread
jm-extend marked this conversation as resolved.
Comment thread
jm-extend marked this conversation as resolved.
/* Get error logs from the wp_options table... */
$error_log = get_option('helloextend_error_log');

Expand Down Expand Up @@ -63,7 +75,10 @@ public static function helloextend_log_error( $message )

public static function helloextend_log_notice( $message )
{

self::helloextend_log_initialize();
if (self::$settings['enable_helloextend_log'] == 0) {
return;
}
/* Get notice logs from the wp_options table... */
$notice_log = get_option('helloextend_notice_log', true);
if (! $notice_log ) {
Expand Down Expand Up @@ -110,6 +125,13 @@ public static function helloextend_log_notice( $message )

public static function helloextend_log_debug( $message )
{
self::helloextend_log_initialize();
if (self::$settings['enable_helloextend_log'] == 0) {
return;
}
if (self::$settings['enable_helloextend_debug'] == 0) {
return;
}

/* Get debug logs from the wp_options table... */
$debug_log = get_option('helloextend_debug_log', true);
Expand Down Expand Up @@ -563,6 +585,11 @@ public static function helloextend_logger_add_to_new_logs( $id, $type )

public static function helloextend_logger_ajax_call()
{
self::helloextend_log_initialize();
if (self::$settings['enable_helloextend_log'] == 0) {
return;
}

$method = isset($_POST['method']) ? sanitize_text_field(wp_unslash($_POST['method'])) : null;
$message = isset($_POST['message']) ? sanitize_text_field(wp_unslash($_POST['message'])) : null;

Expand Down Expand Up @@ -591,4 +618,4 @@ public static function helloextend_logger_ajax_call()
add_action('wp_ajax_helloextend_logger_delete_single', array( $helloextendProtectionLogger, 'helloextend_logger_delete_single' ));

add_action('wp_ajax_nopriv_helloextend_logger_ajax_call', array( $helloextendProtectionLogger, 'helloextend_logger_ajax_call' ), 10);
add_action('wp_ajax_helloextend_logger_ajax_call', array( $helloextendProtectionLogger, 'helloextend_logger_ajax_call' ), 10);
add_action('wp_ajax_helloextend_logger_ajax_call', array( $helloextendProtectionLogger, 'helloextend_logger_ajax_call' ), 10);
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ private function is_item_helloextend_lead($item)

private function get_price_in_cents($item_price)
{
return (int) floatval($item_price * 100);
return (int) round((float) $item_price * 100);
}

private function get_purchased_leads($order)
Expand Down
5 changes: 3 additions & 2 deletions helloextend-protection/js/helloextend-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
if(!ExtendWooCommerce) { return;
}

const { store_id: storeId, ajaxurl, environment, debug_log_enabled: debugLogEnabled } = ExtendWooCommerce;
const { store_id: storeId, ajaxurl, environment, debug_log_enabled: debugLogEnabled, log_enabled: logEnabled } = ExtendWooCommerce;
Comment thread
jm-extend marked this conversation as resolved.

Extend.config({
storeId,
Expand Down Expand Up @@ -91,7 +91,8 @@
getCart,
warrantyAlreadyInCart,
extendAjaxLog,
debugLogEnabled
debugLogEnabled,
logEnabled
}

$(document).trigger('integration.extend');
Expand Down