Added new pricing calculation method; testing needed

This commit is contained in:
Pinga 2023-12-15 07:06:36 +02:00
parent a07c407543
commit a8a4c44590
10 changed files with 250 additions and 92 deletions

View file

@ -48,7 +48,8 @@ try {
}
}
[$price] = $dbh->query("SELECT m$date_add FROM domain_price WHERE tldid = '$tld_id' AND command = 'transfer' LIMIT 1")->fetch(PDO::FETCH_NUM);
$returnValue = getDomainPrice($dbh, $name, $tld_id, $date_add, 'transfer');
$price = $returnValue['price'];
if (($registrar_balance + $creditLimit) < $price) {
$log->notice($name . ': The registrar who took over this domain has no money to pay the renewal period that resulted from the transfer request');

View file

@ -44,7 +44,8 @@ try {
if ($set_autorenewPeriod) {
list($registrar_balance, $creditLimit) = $dbh->query("SELECT accountBalance, creditLimit FROM registrar WHERE id = '$clid' LIMIT 1")->fetch(PDO::FETCH_NUM);
$price = $dbh->query("SELECT m12 FROM domain_price WHERE tldid = '$tldid' AND command = 'renew' LIMIT 1")->fetchColumn();
$returnValue = getDomainPrice($dbh, $name, $tldid, 12, 'renew');
$price = $returnValue['price'];
if (($registrar_balance + $creditLimit) > $price) {
$dbh->exec("UPDATE domain SET rgpstatus = 'autoRenewPeriod', exdate = DATE_ADD(exdate, INTERVAL 12 MONTH), autoRenewPeriod = '12', renewedDate = exdate WHERE id = '$domain_id'");

View file

@ -103,3 +103,66 @@ function processUrlhausData($data) {
function checkUrlhaus($domain, Map $urlhausData) {
return $urlhausData->get($domain, false);
}
function getDomainPrice($pdo, $domain_name, $tld_id, $date_add = 12, $command = 'create') {
// Check if the domain is a premium domain
$stmt = $pdo->prepare("
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 = ?
");
$stmt->execute([$domain_name, $tld_id]);
if ($stmt->rowCount() > 0) {
return ['type' => 'premium', 'price' => $stmt->fetch()['category_price']];
}
// Check if there is a promotion for the domain
$currentDate = date('Y-m-d');
$stmt = $pdo->prepare("
SELECT discount_percentage, discount_amount
FROM promotion_pricing
WHERE tld_id = ?
AND promo_type = 'full'
AND status = 'active'
AND start_date <= ?
AND end_date >= ?
");
$stmt->execute([$tld_id, $currentDate, $currentDate]);
if ($stmt->rowCount() > 0) {
$promo = $stmt->fetch();
$discount = null;
// Determine discount based on percentage or amount
if (!empty($promo['discount_percentage'])) {
$discount = $promo['discount_percentage']; // Percentage discount
} elseif (!empty($promo['discount_amount'])) {
$discount = $promo['discount_amount']; // Fixed amount discount
}
} else {
$discount = null;
}
// Get regular price for the specified period
$priceColumn = "m" . $date_add;
$stmt = $pdo->prepare("SELECT $priceColumn FROM domain_price WHERE tldid = ? AND command = '$command' LIMIT 1");
$stmt->execute([$tld_id]);
if ($stmt->rowCount() > 0) {
$regularPrice = $stmt->fetch()[$priceColumn];
if ($discount !== null) {
if (isset($promo['discount_percentage'])) {
$discountAmount = $regularPrice * ($promo['discount_percentage'] / 100);
} else {
$discountAmount = $discount;
}
$price = $regularPrice - $discountAmount;
return ['type' => 'promotion', 'price' => $price];
}
return ['type' => 'regular', 'price' => $regularPrice];
}
return ['type' => 'not_found', 'price' => 0];
}

View file

@ -193,11 +193,8 @@ class DomainsController extends Controller
$registrar_balance = $result['accountBalance'];
$creditLimit = $result['creditLimit'];
$priceColumn = "m" . $date_add;
$price = $db->selectValue(
'SELECT ' . $db->quoteIdentifier($priceColumn) . ' FROM domain_price WHERE tldid = ? AND command = "create" LIMIT 1',
[$tld_id]
);
$returnValue = getDomainPrice($db, $domainName, $tld_id, $date_add, 'create');
$price = $returnValue['price'];
if (!$price) {
return view($response, 'admin/domains/createDomain.twig', [
@ -1473,11 +1470,8 @@ class DomainsController extends Controller
$registrar_balance = $result['accountBalance'];
$creditLimit = $result['creditLimit'];
$priceColumn = "m" . $date_add;
$price = $db->selectValue(
'SELECT ' . $db->quoteIdentifier($priceColumn) . ' FROM domain_price WHERE tldid = ? AND command = "renew" LIMIT 1',
[$tld_id]
);
$returnValue = getDomainPrice($db, $domainName, $tld_id, $date_add, 'renew');
$price = $returnValue['price'];
if (!$price) {
$this->container->get('flash')->addMessage('error', 'The price, period and currency for such TLD are not declared');
@ -1773,11 +1767,8 @@ class DomainsController extends Controller
]
);
if ($addPeriod_id) {
$priceColumn = "m" . $addPeriod;
$price = $db->selectValue(
'SELECT ' . $db->quoteIdentifier($priceColumn) . ' FROM domain_price WHERE tldid = ? AND command = "create" LIMIT 1',
[$tld_id]
);
$returnValue = getDomainPrice($db, $domainName, $tld_id, $addPeriod, 'create');
$price = $returnValue['price'];
if (!$price) {
$this->container->get('flash')->addMessage('error', 'The price, period and currency for such TLD are not declared');
@ -1901,11 +1892,8 @@ class DomainsController extends Controller
]
);
if ($autoRenewPeriod_id) {
$priceColumn = "m" . $autoRenewPeriod;
$price = $db->selectValue(
'SELECT ' . $db->quoteIdentifier($priceColumn) . ' FROM domain_price WHERE tldid = ? AND command = "renew" LIMIT 1',
[$tld_id]
);
$returnValue = getDomainPrice($db, $domainName, $tld_id, $autoRenewPeriod, 'renew');
$price = $returnValue['price'];
if (!$price) {
$this->container->get('flash')->addMessage('error', 'The price, period and currency for such TLD are not declared');
@ -1931,11 +1919,8 @@ class DomainsController extends Controller
]
);
if ($renewPeriod_id) {
$priceColumn = "m" . $renewPeriod;
$price = $db->selectValue(
'SELECT ' . $db->quoteIdentifier($priceColumn) . ' FROM domain_price WHERE tldid = ? AND command = "renew" LIMIT 1',
[$tld_id]
);
$returnValue = getDomainPrice($db, $domainName, $tld_id, $renewPeriod, 'renew');
$price = $returnValue['price'];
if (!$price) {
$this->container->get('flash')->addMessage('error', 'The price, period and currency for such TLD are not declared');
@ -1961,11 +1946,8 @@ class DomainsController extends Controller
]
);
if ($transferPeriod_id) {
$priceColumn = "m" . $transferPeriod;
$price = $db->selectValue(
'SELECT ' . $db->quoteIdentifier($priceColumn) . ' FROM domain_price WHERE tldid = ? AND command = "renew" LIMIT 1',
[$tld_id]
);
$returnValue = getDomainPrice($db, $domainName, $tld_id, $transferPeriod, 'renew');
$price = $returnValue['price'];
if (!$price) {
$this->container->get('flash')->addMessage('error', 'The price, period and currency for such TLD are not declared');
@ -2150,11 +2132,8 @@ class DomainsController extends Controller
$registrar_balance = $result['accountBalance'];
$creditLimit = $result['creditLimit'];
$priceColumn = "m" . $date_add;
$price = $db->selectValue(
'SELECT ' . $db->quoteIdentifier($priceColumn) . ' FROM domain_price WHERE tldid = ? AND command = "transfer" LIMIT 1',
[$tldid]
);
$returnValue = getDomainPrice($db, $domainName, $tldid, $date_add, 'transfer');
$price = $returnValue['price'];
if (!$price) {
$this->container->get('flash')->addMessage('error', 'The price, period and currency for such TLD are not declared');
@ -2381,11 +2360,8 @@ class DomainsController extends Controller
]
);
$priceColumn = "m" . $date_add;
$price = $db->selectValue(
'SELECT ' . $db->quoteIdentifier($priceColumn) . ' FROM domain_price WHERE tldid = ? AND command = "transfer" LIMIT 1',
[$tldid]
);
$returnValue = getDomainPrice($db, $domainName, $tldid, $date_add, 'transfer');
$price = $returnValue['price'];
if (($registrar_balance + $creditLimit) < $price) {
$this->container->get('flash')->addMessage('error', 'The registrar who took over this domain has no money to pay the renewal period that resulted from the transfer request');

View file

@ -308,3 +308,63 @@ function extractDomainAndTLD($urlString) {
return ['domain' => $sld, 'tld' => $tld];
}
function getDomainPrice($db, $domain_name, $tld_id, $date_add = 12, $command = 'create') {
// Check if the domain is a premium domain
$premiumDomain = $db->selectRow(
'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 ($premiumDomain) {
return ['type' => 'premium', 'price' => $premiumDomain['category_price']];
}
// Check if there is a promotion for the domain
$currentDate = date('Y-m-d');
$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]
);
$discount = null;
if ($promo) {
if (!empty($promo['discount_percentage'])) {
$discount = $promo['discount_percentage']; // Percentage discount
} elseif (!empty($promo['discount_amount'])) {
$discount = $promo['discount_amount']; // Fixed amount discount
}
}
// Get regular price for the specified period
$priceColumn = "m" . $date_add;
$regularPrice = $db->selectValue(
"SELECT $priceColumn FROM domain_price WHERE tldid = ? AND command = ? LIMIT 1",
[$tld_id, $command]
);
if ($regularPrice !== false) {
if ($discount !== null) {
if (isset($promo['discount_percentage'])) {
$discountAmount = $regularPrice * ($promo['discount_percentage'] / 100);
} else {
$discountAmount = $discount;
}
$price = $regularPrice - $discountAmount;
return ['type' => 'promotion', 'price' => $price];
}
return ['type' => 'regular', 'price' => $regularPrice];
}
return ['type' => 'not_found', 'price' => 0];
}

View file

@ -672,11 +672,8 @@ function processDomainCreate($conn, $db, $xml, $clid, $database_type, $trans) {
$registrar_balance = $result['accountBalance'];
$creditLimit = $result['creditLimit'];
$priceColumn = "m" . $date_add;
$stmt = $db->prepare("SELECT $priceColumn FROM domain_price WHERE tldid = :tld_id AND command = 'create' LIMIT 1");
$stmt->bindParam(':tld_id', $tld_id, PDO::PARAM_INT);
$stmt->execute();
$price = $stmt->fetchColumn();
$returnValue = getDomainPrice($db, $domainName, $tld_id, $date_add, 'create');
$price = $returnValue['price'];
if (!$price) {
sendEppError($conn, $db, 2400, 'The price, period and currency for such TLD are not declared', $clTRID, $trans);

View file

@ -174,6 +174,13 @@ function processDomainDelete($conn, $db, $xml, $clid, $database_type, $trans) {
return;
}
$invalid_domain = validate_label($domainName, $db);
if ($invalid_domain) {
sendEppError($conn, $db, 2306, 'Invalid domain:name', $clTRID, $trans);
return;
}
$stmt = $db->prepare("SELECT id, tldid, registrant, crdate, exdate, clid, crid, upid, trdate, trstatus, reid, redate, acid, acdate, rgpstatus, addPeriod, autoRenewPeriod, renewPeriod, renewedDate, transferPeriod FROM domain WHERE name = :name LIMIT 1");
$stmt->execute([':name' => $domainName]);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
@ -246,9 +253,8 @@ function processDomainDelete($conn, $db, $xml, $clid, $database_type, $trans) {
$addPeriod_id = $stmt->fetchColumn();
if ($addPeriod_id) {
$stmt = $db->prepare("SELECT m$addPeriod FROM domain_price WHERE tldid = ? AND command = 'create' LIMIT 1");
$stmt->execute([$tldid]);
$price = $stmt->fetchColumn();
$returnValue = getDomainPrice($db, $domainName, $tldid, $addPeriod, 'create');
$price = $returnValue['price'];
if (!isset($price)) {
sendEppError($conn, $db, 2400, 'The price, period and currency for such TLD are not declared', $clTRID, $trans);
@ -305,9 +311,8 @@ function processDomainDelete($conn, $db, $xml, $clid, $database_type, $trans) {
$autoRenewPeriod_id = $stmt->fetchColumn();
if ($autoRenewPeriod_id) {
$stmt = $db->prepare("SELECT m$autoRenewPeriod FROM domain_price WHERE tldid = ? AND command = 'renew' LIMIT 1");
$stmt->execute([$tldid]);
$price = $stmt->fetchColumn();
$returnValue = getDomainPrice($db, $domainName, $tldid, $autoRenewPeriod, 'renew');
$price = $returnValue['price'];
if (!isset($price)) {
sendEppError($conn, $db, 2400, 'The price, period and currency for such TLD are not declared', $clTRID, $trans);
@ -329,9 +334,8 @@ function processDomainDelete($conn, $db, $xml, $clid, $database_type, $trans) {
$renewPeriod_id = $stmt->fetchColumn();
if ($renewPeriod_id) {
$stmt = $db->prepare("SELECT m$renewPeriod FROM domain_price WHERE tldid = ? AND command = 'renew' LIMIT 1");
$stmt->execute([$tldid]);
$price = $stmt->fetchColumn();
$returnValue = getDomainPrice($db, $domainName, $tldid, $renewPeriod, 'renew');
$price = $returnValue['price'];
if (!isset($price)) {
sendEppError($conn, $db, 2400, 'The price, period and currency for such TLD are not declared', $clTRID, $trans);
@ -355,9 +359,8 @@ function processDomainDelete($conn, $db, $xml, $clid, $database_type, $trans) {
if ($transferPeriod_id) {
// Return money if a transfer was also a renew
if ($transferPeriod > 0) {
$stmt = $db->prepare("SELECT m$transferPeriod FROM domain_price WHERE tldid = ? AND command = 'renew' LIMIT 1");
$stmt->execute([$tldid]);
$price = $stmt->fetchColumn();
$returnValue = getDomainPrice($db, $domainName, $tldid, $transferPeriod, 'renew');
$price = $returnValue['price'];
if (!isset($price)) {
sendEppError($conn, $db, 2400, 'The price, period and currency for such TLD are not declared', $clTRID, $trans);

View file

@ -43,7 +43,7 @@ function processDomainRenew($conn, $db, $xml, $clid, $database_type, $trans) {
$stmt->execute();
$clid = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt = $db->prepare("SELECT id, tldid, exdate, clid FROM domain WHERE name = :domainName LIMIT 1");
$stmt = $db->prepare("SELECT id, name, tldid, exdate, clid FROM domain WHERE name = :domainName LIMIT 1");
$stmt->bindParam(':domainName', $domainName, PDO::PARAM_STR);
$stmt->execute();
$domainData = $stmt->fetch(PDO::FETCH_ASSOC);
@ -113,11 +113,8 @@ function processDomainRenew($conn, $db, $xml, $clid, $database_type, $trans) {
$registrar_balance = $row['accountBalance'];
$creditLimit = $row['creditLimit'];
$columnName = "m$date_add";
$stmt = $db->prepare("SELECT $columnName FROM domain_price WHERE tldid = :tldid AND command = 'renew' LIMIT 1");
$stmt->bindParam(':tldid', $domainData['tldid'], PDO::PARAM_INT);
$stmt->execute();
$price = $stmt->fetchColumn();
$returnValue = getDomainPrice($db, $domainData['name'], $domainData['tldid'], $date_add, 'renew');
$price = $returnValue['price'];
if (($registrar_balance + $creditLimit) < $price) {
sendEppError($conn, $db, 2104, 'There is no money on the account to renew', $clTRID, $trans);

View file

@ -466,9 +466,8 @@ function processDomainTransfer($conn, $db, $xml, $clid, $database_type, $trans)
$stmt->execute([$domainName]);
$date_add = $stmt->fetchColumn();
$stmt = $db->prepare("SELECT m$date_add FROM domain_price WHERE tldid = ? AND command = 'transfer' LIMIT 1");
$stmt->execute([$tldid]);
$price = $stmt->fetchColumn();
$returnValue = getDomainPrice($db, $domainName, $tldid, $date_add, 'transfer');
$price = $returnValue['price'];
if (($registrar_balance + $creditLimit) < $price) {
sendEppError($conn, $db, 2104, 'The registrar who took over this domain has no money to pay the renewal period that resulted from the transfer request', $clTRID, $trans);
@ -866,10 +865,8 @@ function processDomainTransfer($conn, $db, $xml, $clid, $database_type, $trans)
$registrar_balance = $result["accountBalance"];
$creditLimit = $result["creditLimit"];
$stmt = $db->prepare("SELECT m$date_add FROM domain_price WHERE tldid = :tldid AND command = 'transfer' LIMIT 1");
$stmt->execute([':tldid' => $tldid]);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$price = $result["m$date_add"];
$returnValue = getDomainPrice($db, $domainName, $tldid, $date_add, 'transfer');
$price = $returnValue['price'];
if (($registrar_balance + $creditLimit) < $price) {
sendEppError($conn, $db, 2104, 'The registrar who wants to take over this domain has no money', $clTRID, $trans);

View file

@ -491,3 +491,66 @@ function updatePermittedIPs($pool, $permittedIPsTable) {
$permittedIPsTable->set($ip, ['addr' => $ip]);
}
}
function getDomainPrice($pdo, $domain_name, $tld_id, $date_add = 12, $command = 'create') {
// Check if the domain is a premium domain
$stmt = $pdo->prepare("
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 = ?
");
$stmt->execute([$domain_name, $tld_id]);
if ($stmt->rowCount() > 0) {
return ['type' => 'premium', 'price' => $stmt->fetch()['category_price']];
}
// Check if there is a promotion for the domain
$currentDate = date('Y-m-d');
$stmt = $pdo->prepare("
SELECT discount_percentage, discount_amount
FROM promotion_pricing
WHERE tld_id = ?
AND promo_type = 'full'
AND status = 'active'
AND start_date <= ?
AND end_date >= ?
");
$stmt->execute([$tld_id, $currentDate, $currentDate]);
if ($stmt->rowCount() > 0) {
$promo = $stmt->fetch();
$discount = null;
// Determine discount based on percentage or amount
if (!empty($promo['discount_percentage'])) {
$discount = $promo['discount_percentage']; // Percentage discount
} elseif (!empty($promo['discount_amount'])) {
$discount = $promo['discount_amount']; // Fixed amount discount
}
} else {
$discount = null;
}
// Get regular price for the specified period
$priceColumn = "m" . $date_add;
$stmt = $pdo->prepare("SELECT $priceColumn FROM domain_price WHERE tldid = ? AND command = '$command' LIMIT 1");
$stmt->execute([$tld_id]);
if ($stmt->rowCount() > 0) {
$regularPrice = $stmt->fetch()[$priceColumn];
if ($discount !== null) {
if (isset($promo['discount_percentage'])) {
$discountAmount = $regularPrice * ($promo['discount_percentage'] / 100);
} else {
$discountAmount = $discount;
}
$price = $regularPrice - $discountAmount;
return ['type' => 'promotion', 'price' => $price];
}
return ['type' => 'regular', 'price' => $regularPrice];
}
return ['type' => 'not_found', 'price' => 0];
}