Fixed issue with application pricing

This commit is contained in:
Pinga 2025-03-14 18:26:47 +02:00
parent 8dc01d65d0
commit a0a16f4bfb
4 changed files with 190 additions and 102 deletions

View file

@ -228,7 +228,11 @@ 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(
$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
@ -237,6 +241,11 @@ function getDomainPrice($pdo, $domain_name, $tld_id, $date_add = 12, $command =
[$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);
$result = ['type' => 'premium', 'price' => formatMoney($money)];
@ -247,7 +256,11 @@ 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(
$promoCacheKey = "promo_{$tld_id}";
$promo = json_decode($redis->get($promoCacheKey), true);
if ($promo === null) {
$promo = fetchSingleRow(
$pdo,
"SELECT discount_percentage, discount_amount
FROM promotion_pricing
@ -260,12 +273,17 @@ function getDomainPrice($pdo, $domain_name, $tld_id, $date_add = 12, $command =
);
if ($promo) {
$redis->setex("promo_{$tld_id}", 3600, json_encode($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(
$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
@ -275,11 +293,17 @@ function getDomainPrice($pdo, $domain_name, $tld_id, $date_add = 12, $command =
[$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 ($finalPrice > 0) {
if (!empty($promo['discount_percentage'])) {
$discountAmount = (int) ($finalPrice * ($promo['discount_percentage'] / 100));
} else {
@ -287,6 +311,10 @@ function getDomainPrice($pdo, $domain_name, $tld_id, $date_add = 12, $command =
}
$finalPrice = max(0, $finalPrice - $discountAmount);
$type = 'promotion';
} else {
$finalPrice = 0;
$type = 'promotion';
}
} else {
$type = 'regular';
}

View file

@ -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();

View file

@ -467,7 +467,9 @@ 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(
$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
@ -475,6 +477,11 @@ function getDomainPrice($db, $domain_name, $tld_id, $date_add = 12, $command = '
[$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);
$result = ['type' => 'premium', 'price' => formatMoney($money)];
@ -485,7 +492,9 @@ 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(
$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 = ?
@ -499,10 +508,13 @@ function getDomainPrice($db, $domain_name, $tld_id, $date_add = 12, $command = '
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(
$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 = ?
@ -511,11 +523,17 @@ function getDomainPrice($db, $domain_name, $tld_id, $date_add = 12, $command = '
[$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 ($finalPrice > 0) {
if (!empty($promo['discount_percentage'])) {
$discountAmount = (int) ($finalPrice * ($promo['discount_percentage'] / 100));
} else {
@ -523,6 +541,10 @@ function getDomainPrice($db, $domain_name, $tld_id, $date_add = 12, $command = '
}
$finalPrice = max(0, $finalPrice - $discountAmount);
$type = 'promotion';
} else {
$finalPrice = 0;
$type = 'promotion';
}
} else {
$type = 'regular';
}

View file

@ -671,7 +671,11 @@ 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(
$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
@ -680,6 +684,11 @@ function getDomainPrice($pdo, $domain_name, $tld_id, $date_add = 12, $command =
[$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);
$result = ['type' => 'premium', 'price' => formatMoney($money)];
@ -690,7 +699,11 @@ 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(
$promoCacheKey = "promo_{$tld_id}";
$promo = json_decode($redis->get($promoCacheKey), true);
if ($promo === null) {
$promo = fetchSingleRow(
$pdo,
"SELECT discount_percentage, discount_amount
FROM promotion_pricing
@ -703,12 +716,17 @@ function getDomainPrice($pdo, $domain_name, $tld_id, $date_add = 12, $command =
);
if ($promo) {
$redis->setex("promo_{$tld_id}", 3600, json_encode($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(
$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
@ -718,11 +736,17 @@ function getDomainPrice($pdo, $domain_name, $tld_id, $date_add = 12, $command =
[$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 ($finalPrice > 0) {
if (!empty($promo['discount_percentage'])) {
$discountAmount = (int) ($finalPrice * ($promo['discount_percentage'] / 100));
} else {
@ -730,6 +754,10 @@ function getDomainPrice($pdo, $domain_name, $tld_id, $date_add = 12, $command =
}
$finalPrice = max(0, $finalPrice - $discountAmount);
$type = 'promotion';
} else {
$finalPrice = 0;
$type = 'promotion';
}
} else {
$type = 'regular';
}