diff --git a/automation/helpers.php b/automation/helpers.php index 6a0c665..5e26e87 100644 --- a/automation/helpers.php +++ b/automation/helpers.php @@ -228,14 +228,23 @@ function getDomainPrice($pdo, $domain_name, $tld_id, $date_add = 12, $command = $exchangeRate = $exchangeRates['rates'][$currency] ?? 1.0; // Check for premium pricing - $premiumPrice = $redis->get("premium_price_{$domain_name}_{$tld_id}") ?: fetchSingleValue( - $pdo, - 'SELECT c.category_price - FROM premium_domain_pricing p - JOIN premium_domain_categories c ON p.category_id = c.category_id - WHERE p.domain_name = ? AND p.tld_id = ?', - [$domain_name, $tld_id] - ); + $premiumCacheKey = "premium_price_{$domain_name}_{$tld_id}"; + $premiumPrice = json_decode($redis->get($premiumCacheKey), true); + + if ($premiumPrice === null || $premiumPrice == 0) { + $premiumPrice = fetchSingleValue( + $pdo, + 'SELECT c.category_price + FROM premium_domain_pricing p + JOIN premium_domain_categories c ON p.category_id = c.category_id + WHERE p.domain_name = ? AND p.tld_id = ?', + [$domain_name, $tld_id] + ); + + if (!is_null($premiumPrice) && $premiumPrice !== false) { + $redis->setex($premiumCacheKey, 1800, json_encode($premiumPrice)); + } + } if (!is_null($premiumPrice) && $premiumPrice !== false) { $money = convertMoney(new Money((int) ($premiumPrice * 100), new Currency($baseCurrency)), $exchangeRate, $currency); @@ -247,46 +256,65 @@ function getDomainPrice($pdo, $domain_name, $tld_id, $date_add = 12, $command = // Check for active promotions $currentDate = date('Y-m-d'); - $promo = json_decode($redis->get("promo_{$tld_id}"), true) ?: fetchSingleRow( - $pdo, - "SELECT discount_percentage, discount_amount - FROM promotion_pricing - WHERE tld_id = ? - AND promo_type = 'full' - AND status = 'active' - AND start_date <= ? - AND end_date >= ?", - [$tld_id, $currentDate, $currentDate] - ); + $promoCacheKey = "promo_{$tld_id}"; + $promo = json_decode($redis->get($promoCacheKey), true); - if ($promo) { - $redis->setex("promo_{$tld_id}", 3600, json_encode($promo)); + if ($promo === null) { + $promo = fetchSingleRow( + $pdo, + "SELECT discount_percentage, discount_amount + FROM promotion_pricing + WHERE tld_id = ? + AND promo_type = 'full' + AND status = 'active' + AND start_date <= ? + AND end_date >= ?", + [$tld_id, $currentDate, $currentDate] + ); + + if ($promo) { + $redis->setex($promoCacheKey, 3600, json_encode($promo)); + } } // Get regular price from DB $priceColumn = "m" . (int) $date_add; - $regularPrice = json_decode($redis->get("regular_price_{$tld_id}_{$command}_{$registrar_id}"), true) ?: fetchSingleValue( - $pdo, - "SELECT $priceColumn - FROM domain_price - WHERE tldid = ? AND command = ? - AND (registrar_id = ? OR registrar_id IS NULL) - ORDER BY registrar_id DESC LIMIT 1", - [$tld_id, $command, $registrar_id] - ); + $regularPriceCacheKey = "regular_price_{$tld_id}_{$command}_{$date_add}_{$registrar_id}"; + $regularPrice = json_decode($redis->get($regularPriceCacheKey), true); + + if ($regularPrice === null || $regularPrice == 0) { + $regularPrice = fetchSingleValue( + $pdo, + "SELECT $priceColumn + FROM domain_price + WHERE tldid = ? AND command = ? + AND (registrar_id = ? OR registrar_id IS NULL) + ORDER BY registrar_id DESC LIMIT 1", + [$tld_id, $command, $registrar_id] + ); + + if (!is_null($regularPrice) && $regularPrice !== false) { + $redis->setex($regularPriceCacheKey, 1800, json_encode($regularPrice)); + } + } if (!is_null($regularPrice) && $regularPrice !== false) { $redis->setex("regular_price_{$tld_id}_{$command}_{$registrar_id}", 1800, json_encode($regularPrice)); $finalPrice = $regularPrice * 100; // Convert DB float to cents if ($promo) { - if (!empty($promo['discount_percentage'])) { - $discountAmount = (int) ($finalPrice * ($promo['discount_percentage'] / 100)); + if ($finalPrice > 0) { + if (!empty($promo['discount_percentage'])) { + $discountAmount = (int) ($finalPrice * ($promo['discount_percentage'] / 100)); + } else { + $discountAmount = (int) ($promo['discount_amount'] * 100); + } + $finalPrice = max(0, $finalPrice - $discountAmount); + $type = 'promotion'; } else { - $discountAmount = (int) ($promo['discount_amount'] * 100); + $finalPrice = 0; + $type = 'promotion'; } - $finalPrice = max(0, $finalPrice - $discountAmount); - $type = 'promotion'; } else { $type = 'regular'; } diff --git a/cp/app/Controllers/ApplicationsController.php b/cp/app/Controllers/ApplicationsController.php index d5a8c6d..55bef50 100644 --- a/cp/app/Controllers/ApplicationsController.php +++ b/cp/app/Controllers/ApplicationsController.php @@ -1134,6 +1134,16 @@ class ApplicationsController extends Controller $returnValue = getDomainPrice($db, $domainName, $tld_id, $date_add, 'create', $clid, $currency); $price = $returnValue['price']; + if (!$price) { + $this->container->get('flash')->addMessage('error', 'Error creating domain: The price, period and currency for such TLD are not declared'); + return $response->withHeader('Location', '/application/create')->withStatus(302); + } + + if (($registrar_balance + $creditLimit) < $price) { + $this->container->get('flash')->addMessage('error', 'Error creating domain: Low credit: minimum threshold reached'); + return $response->withHeader('Location', '/application/create')->withStatus(302); + } + try { $db->beginTransaction(); diff --git a/cp/bootstrap/helper.php b/cp/bootstrap/helper.php index 855b702..0a6f77b 100644 --- a/cp/bootstrap/helper.php +++ b/cp/bootstrap/helper.php @@ -467,13 +467,20 @@ function getDomainPrice($db, $domain_name, $tld_id, $date_add = 12, $command = ' $exchangeRate = $exchangeRates['rates'][$currency] ?? 1.0; // Check for premium pricing - $premiumPrice = $redis->get("premium_price_{$domain_name}_{$tld_id}") ?: $db->selectValue( - 'SELECT c.category_price - FROM premium_domain_pricing p - JOIN premium_domain_categories c ON p.category_id = c.category_id - WHERE p.domain_name = ? AND p.tld_id = ?', - [$domain_name, $tld_id] - ); + $premiumPrice = $redis->get("premium_price_{$domain_name}_{$tld_id}"); + if ($premiumPrice === null || $premiumPrice == "0") { + $premiumPrice = $db->selectValue( + 'SELECT c.category_price + FROM premium_domain_pricing p + JOIN premium_domain_categories c ON p.category_id = c.category_id + WHERE p.domain_name = ? AND p.tld_id = ?', + [$domain_name, $tld_id] + ); + + if (!is_null($premiumPrice) && $premiumPrice !== false) { + $redis->setex("premium_price_{$domain_name}_{$tld_id}", 1800, json_encode($premiumPrice)); + } + } if (!is_null($premiumPrice) && $premiumPrice !== false) { $money = convertMoney(new Money((int) ($premiumPrice * 100), new Currency($baseCurrency)), $exchangeRate, $currency); @@ -485,44 +492,59 @@ function getDomainPrice($db, $domain_name, $tld_id, $date_add = 12, $command = ' // Check for active promotions $currentDate = date('Y-m-d'); - $promo = json_decode($redis->get("promo_{$tld_id}"), true) ?: $db->selectRow( - "SELECT discount_percentage, discount_amount - FROM promotion_pricing - WHERE tld_id = ? - AND promo_type = 'full' - AND status = 'active' - AND start_date <= ? - AND end_date >= ?", - [$tld_id, $currentDate, $currentDate] - ); + $promo = json_decode($redis->get("promo_{$tld_id}"), true); + if ($promo === null) { + $promo = $db->selectRow( + "SELECT discount_percentage, discount_amount + FROM promotion_pricing + WHERE tld_id = ? + AND promo_type = 'full' + AND status = 'active' + AND start_date <= ? + AND end_date >= ?", + [$tld_id, $currentDate, $currentDate] + ); - if ($promo) { - $redis->setex("promo_{$tld_id}", 3600, json_encode($promo)); + if ($promo) { + $redis->setex("promo_{$tld_id}", 3600, json_encode($promo)); + } } // Get regular price from DB $priceColumn = "m" . (int) $date_add; - $regularPrice = json_decode($redis->get("regular_price_{$tld_id}_{$command}_{$registrar_id}"), true) ?: $db->selectValue( - "SELECT $priceColumn - FROM domain_price - WHERE tldid = ? AND command = ? - AND (registrar_id = ? OR registrar_id IS NULL) - ORDER BY registrar_id DESC LIMIT 1", - [$tld_id, $command, $registrar_id] - ); + $regularPrice = json_decode($redis->get("regular_price_{$tld_id}_{$command}_{$date_add}_{$registrar_id}"), true); + if ($regularPrice === null || $regularPrice == 0) { + $regularPrice = $db->selectValue( + "SELECT $priceColumn + FROM domain_price + WHERE tldid = ? AND command = ? + AND (registrar_id = ? OR registrar_id IS NULL) + ORDER BY registrar_id DESC LIMIT 1", + [$tld_id, $command, $registrar_id] + ); + + if (!is_null($regularPrice) && $regularPrice !== false) { + $redis->setex("regular_price_{$tld_id}_{$command}_{$registrar_id}", 1800, json_encode($regularPrice)); + } + } if (!is_null($regularPrice) && $regularPrice !== false) { $redis->setex("regular_price_{$tld_id}_{$command}_{$registrar_id}", 1800, json_encode($regularPrice)); $finalPrice = $regularPrice * 100; // Convert DB float to cents if ($promo) { - if (!empty($promo['discount_percentage'])) { - $discountAmount = (int) ($finalPrice * ($promo['discount_percentage'] / 100)); + if ($finalPrice > 0) { + if (!empty($promo['discount_percentage'])) { + $discountAmount = (int) ($finalPrice * ($promo['discount_percentage'] / 100)); + } else { + $discountAmount = (int) ($promo['discount_amount'] * 100); + } + $finalPrice = max(0, $finalPrice - $discountAmount); + $type = 'promotion'; } else { - $discountAmount = (int) ($promo['discount_amount'] * 100); + $finalPrice = 0; + $type = 'promotion'; } - $finalPrice = max(0, $finalPrice - $discountAmount); - $type = 'promotion'; } else { $type = 'regular'; } diff --git a/epp/src/helpers.php b/epp/src/helpers.php index 7873c79..d85b3e6 100644 --- a/epp/src/helpers.php +++ b/epp/src/helpers.php @@ -671,14 +671,23 @@ function getDomainPrice($pdo, $domain_name, $tld_id, $date_add = 12, $command = $exchangeRate = $exchangeRates['rates'][$currency] ?? 1.0; // Check for premium pricing - $premiumPrice = $redis->get("premium_price_{$domain_name}_{$tld_id}") ?: fetchSingleValue( - $pdo, - 'SELECT c.category_price - FROM premium_domain_pricing p - JOIN premium_domain_categories c ON p.category_id = c.category_id - WHERE p.domain_name = ? AND p.tld_id = ?', - [$domain_name, $tld_id] - ); + $premiumCacheKey = "premium_price_{$domain_name}_{$tld_id}"; + $premiumPrice = json_decode($redis->get($premiumCacheKey), true); + + if ($premiumPrice === null || $premiumPrice == 0) { + $premiumPrice = fetchSingleValue( + $pdo, + 'SELECT c.category_price + FROM premium_domain_pricing p + JOIN premium_domain_categories c ON p.category_id = c.category_id + WHERE p.domain_name = ? AND p.tld_id = ?', + [$domain_name, $tld_id] + ); + + if (!is_null($premiumPrice) && $premiumPrice !== false) { + $redis->setex($premiumCacheKey, 1800, json_encode($premiumPrice)); + } + } if (!is_null($premiumPrice) && $premiumPrice !== false) { $money = convertMoney(new Money((int) ($premiumPrice * 100), new Currency($baseCurrency)), $exchangeRate, $currency); @@ -690,46 +699,65 @@ function getDomainPrice($pdo, $domain_name, $tld_id, $date_add = 12, $command = // Check for active promotions $currentDate = date('Y-m-d'); - $promo = json_decode($redis->get("promo_{$tld_id}"), true) ?: fetchSingleRow( - $pdo, - "SELECT discount_percentage, discount_amount - FROM promotion_pricing - WHERE tld_id = ? - AND promo_type = 'full' - AND status = 'active' - AND start_date <= ? - AND end_date >= ?", - [$tld_id, $currentDate, $currentDate] - ); + $promoCacheKey = "promo_{$tld_id}"; + $promo = json_decode($redis->get($promoCacheKey), true); - if ($promo) { - $redis->setex("promo_{$tld_id}", 3600, json_encode($promo)); + if ($promo === null) { + $promo = fetchSingleRow( + $pdo, + "SELECT discount_percentage, discount_amount + FROM promotion_pricing + WHERE tld_id = ? + AND promo_type = 'full' + AND status = 'active' + AND start_date <= ? + AND end_date >= ?", + [$tld_id, $currentDate, $currentDate] + ); + + if ($promo) { + $redis->setex($promoCacheKey, 3600, json_encode($promo)); + } } // Get regular price from DB $priceColumn = "m" . (int) $date_add; - $regularPrice = json_decode($redis->get("regular_price_{$tld_id}_{$command}_{$registrar_id}"), true) ?: fetchSingleValue( - $pdo, - "SELECT $priceColumn - FROM domain_price - WHERE tldid = ? AND command = ? - AND (registrar_id = ? OR registrar_id IS NULL) - ORDER BY registrar_id DESC LIMIT 1", - [$tld_id, $command, $registrar_id] - ); + $regularPriceCacheKey = "regular_price_{$tld_id}_{$command}_{$date_add}_{$registrar_id}"; + $regularPrice = json_decode($redis->get($regularPriceCacheKey), true); + + if ($regularPrice === null || $regularPrice == 0) { + $regularPrice = fetchSingleValue( + $pdo, + "SELECT $priceColumn + FROM domain_price + WHERE tldid = ? AND command = ? + AND (registrar_id = ? OR registrar_id IS NULL) + ORDER BY registrar_id DESC LIMIT 1", + [$tld_id, $command, $registrar_id] + ); + + if (!is_null($regularPrice) && $regularPrice !== false) { + $redis->setex($regularPriceCacheKey, 1800, json_encode($regularPrice)); + } + } if (!is_null($regularPrice) && $regularPrice !== false) { $redis->setex("regular_price_{$tld_id}_{$command}_{$registrar_id}", 1800, json_encode($regularPrice)); $finalPrice = $regularPrice * 100; // Convert DB float to cents if ($promo) { - if (!empty($promo['discount_percentage'])) { - $discountAmount = (int) ($finalPrice * ($promo['discount_percentage'] / 100)); + if ($finalPrice > 0) { + if (!empty($promo['discount_percentage'])) { + $discountAmount = (int) ($finalPrice * ($promo['discount_percentage'] / 100)); + } else { + $discountAmount = (int) ($promo['discount_amount'] * 100); + } + $finalPrice = max(0, $finalPrice - $discountAmount); + $type = 'promotion'; } else { - $discountAmount = (int) ($promo['discount_amount'] * 100); + $finalPrice = 0; + $type = 'promotion'; } - $finalPrice = max(0, $finalPrice - $discountAmount); - $type = 'promotion'; } else { $type = 'regular'; }