mirror of
https://github.com/getnamingo/registry.git
synced 2025-07-03 01:23:22 +02:00
Big improvement on pricing estimation logic in CP
This commit is contained in:
parent
a9e84a855e
commit
73e19087ae
6 changed files with 305 additions and 287 deletions
|
@ -756,4 +756,26 @@ class DapiController extends Controller
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function domainPrice(Request $request, Response $response): Response
|
||||||
|
{
|
||||||
|
$params = $request->getQueryParams();
|
||||||
|
$db = $this->container->get('db');
|
||||||
|
|
||||||
|
$domain_name = $params['domain_name'] ?? '';
|
||||||
|
$date_add = (int) ($params['date_add'] ?? 12);
|
||||||
|
$command = $params['command'] ?? 'create';
|
||||||
|
$currency = $params['currency'] ?? 'USD';
|
||||||
|
$registrar_id = !empty($params['registrar_id']) ? $params['registrar_id'] : ($_SESSION['auth_registrar_id'] ?? null);
|
||||||
|
|
||||||
|
$parts = extractDomainAndTLD($domain_name);
|
||||||
|
$domain_extension = $parts['tld'];
|
||||||
|
|
||||||
|
$tld_id = $db->selectValue('SELECT id FROM domain_tld WHERE tld = ?', [ '.'.$domain_extension ]);
|
||||||
|
|
||||||
|
$result = getDomainPrice($db, $domain_name, $tld_id, $date_add, $command, $registrar_id, $currency);
|
||||||
|
|
||||||
|
$response->getBody()->write(json_encode($result));
|
||||||
|
return $response->withHeader('Content-Type', 'application/json');
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -921,12 +921,20 @@ class DomainsController extends Controller
|
||||||
$registrars = $db->select("SELECT id, clid, name FROM registrar");
|
$registrars = $db->select("SELECT id, clid, name FROM registrar");
|
||||||
if ($_SESSION["auth_roles"] != 0) {
|
if ($_SESSION["auth_roles"] != 0) {
|
||||||
$registrar = true;
|
$registrar = true;
|
||||||
|
$currency = $_SESSION['_currency'] ?? 'USD';
|
||||||
|
if (!empty($_SESSION['auth_registrar_id'])) {
|
||||||
|
$currency = $db->selectValue(
|
||||||
|
'SELECT currency FROM registrar WHERE id = ?',
|
||||||
|
[$_SESSION['auth_registrar_id']]
|
||||||
|
) ?? 'USD'; // Default to USD if no result
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
$registrar = null;
|
$registrar = null;
|
||||||
|
$currency = $_SESSION['_currency'] ?? 'USD';
|
||||||
}
|
}
|
||||||
|
$registry_currency = $_SESSION['registry_currency'] ?? 'USD';
|
||||||
|
|
||||||
$locale = (isset($_SESSION['_lang']) && !empty($_SESSION['_lang'])) ? $_SESSION['_lang'] : 'en_US';
|
$locale = (isset($_SESSION['_lang']) && !empty($_SESSION['_lang'])) ? $_SESSION['_lang'] : 'en_US';
|
||||||
$currency = $_SESSION['_currency'] ?? 'USD'; // Default to USD if not set
|
|
||||||
|
|
||||||
$formatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY);
|
$formatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY);
|
||||||
$formatter->setTextAttribute(\NumberFormatter::CURRENCY_CODE, $currency);
|
$formatter->setTextAttribute(\NumberFormatter::CURRENCY_CODE, $currency);
|
||||||
|
@ -947,6 +955,7 @@ class DomainsController extends Controller
|
||||||
'registrar' => $registrar,
|
'registrar' => $registrar,
|
||||||
'launch_phases' => $launch_phases,
|
'launch_phases' => $launch_phases,
|
||||||
'currency' => $currency,
|
'currency' => $currency,
|
||||||
|
'registry_currency' => $registry_currency,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1899,10 +1908,19 @@ class DomainsController extends Controller
|
||||||
$registrars = $db->select("SELECT id, clid, name FROM registrar");
|
$registrars = $db->select("SELECT id, clid, name FROM registrar");
|
||||||
if ($_SESSION["auth_roles"] != 0) {
|
if ($_SESSION["auth_roles"] != 0) {
|
||||||
$registrar = true;
|
$registrar = true;
|
||||||
|
$currency = $_SESSION['_currency'] ?? 'USD';
|
||||||
|
if (!empty($_SESSION['auth_registrar_id'])) {
|
||||||
|
$currency = $db->selectValue(
|
||||||
|
'SELECT currency FROM registrar WHERE id = ?',
|
||||||
|
[$_SESSION['auth_registrar_id']]
|
||||||
|
) ?? 'USD'; // Default to USD if no result
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
$registrar = null;
|
$registrar = null;
|
||||||
|
$currency = $_SESSION['_currency'] ?? 'USD';
|
||||||
}
|
}
|
||||||
|
$registry_currency = $_SESSION['registry_currency'] ?? 'USD';
|
||||||
|
|
||||||
$uri = $request->getUri()->getPath();
|
$uri = $request->getUri()->getPath();
|
||||||
|
|
||||||
if ($args) {
|
if ($args) {
|
||||||
|
@ -1943,7 +1961,6 @@ class DomainsController extends Controller
|
||||||
$maxYears = 10 - $yearsUntilExpiration;
|
$maxYears = 10 - $yearsUntilExpiration;
|
||||||
|
|
||||||
$locale = (isset($_SESSION['_lang']) && !empty($_SESSION['_lang'])) ? $_SESSION['_lang'] : 'en_US';
|
$locale = (isset($_SESSION['_lang']) && !empty($_SESSION['_lang'])) ? $_SESSION['_lang'] : 'en_US';
|
||||||
$currency = $_SESSION['_currency'] ?? 'USD'; // Default to USD if not set
|
|
||||||
|
|
||||||
$formatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY);
|
$formatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY);
|
||||||
$formatter->setTextAttribute(\NumberFormatter::CURRENCY_CODE, $currency);
|
$formatter->setTextAttribute(\NumberFormatter::CURRENCY_CODE, $currency);
|
||||||
|
@ -1970,7 +1987,8 @@ class DomainsController extends Controller
|
||||||
'currentUri' => $uri,
|
'currentUri' => $uri,
|
||||||
'currencySymbol' => $symbol,
|
'currencySymbol' => $symbol,
|
||||||
'currencyPosition' => $position,
|
'currencyPosition' => $position,
|
||||||
'currency' => $currency
|
'currency' => $currency,
|
||||||
|
'registry_currency' => $registry_currency
|
||||||
]);
|
]);
|
||||||
} else {
|
} else {
|
||||||
// Domain does not exist, redirect to the domains view
|
// Domain does not exist, redirect to the domains view
|
||||||
|
@ -2661,12 +2679,20 @@ class DomainsController extends Controller
|
||||||
$registrars = $db->select("SELECT id, clid, name FROM registrar");
|
$registrars = $db->select("SELECT id, clid, name FROM registrar");
|
||||||
if ($_SESSION["auth_roles"] != 0) {
|
if ($_SESSION["auth_roles"] != 0) {
|
||||||
$registrar = true;
|
$registrar = true;
|
||||||
|
$currency = $_SESSION['_currency'] ?? 'USD';
|
||||||
|
if (!empty($_SESSION['auth_registrar_id'])) {
|
||||||
|
$currency = $db->selectValue(
|
||||||
|
'SELECT currency FROM registrar WHERE id = ?',
|
||||||
|
[$_SESSION['auth_registrar_id']]
|
||||||
|
) ?? 'USD'; // Default to USD if no result
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
$registrar = null;
|
$registrar = null;
|
||||||
|
$currency = $_SESSION['_currency'] ?? 'USD';
|
||||||
}
|
}
|
||||||
|
$registry_currency = $_SESSION['registry_currency'] ?? 'USD';
|
||||||
|
|
||||||
$locale = (isset($_SESSION['_lang']) && !empty($_SESSION['_lang'])) ? $_SESSION['_lang'] : 'en_US';
|
$locale = (isset($_SESSION['_lang']) && !empty($_SESSION['_lang'])) ? $_SESSION['_lang'] : 'en_US';
|
||||||
$currency = $_SESSION['_currency'] ?? 'USD'; // Default to USD if not set
|
|
||||||
|
|
||||||
$formatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY);
|
$formatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY);
|
||||||
$formatter->setTextAttribute(\NumberFormatter::CURRENCY_CODE, $currency);
|
$formatter->setTextAttribute(\NumberFormatter::CURRENCY_CODE, $currency);
|
||||||
|
@ -2683,7 +2709,8 @@ class DomainsController extends Controller
|
||||||
'registrar' => $registrar,
|
'registrar' => $registrar,
|
||||||
'currencySymbol' => $symbol,
|
'currencySymbol' => $symbol,
|
||||||
'currencyPosition' => $position,
|
'currencyPosition' => $position,
|
||||||
'currency' => $currency
|
'currency' => $currency,
|
||||||
|
'registry_currency' => $registry_currency
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -244,45 +244,45 @@
|
||||||
<label class="form-check-label" for="addDnssec">{{ __('Add DNSSEC Data') }}</label>
|
<label class="form-check-label" for="addDnssec">{{ __('Add DNSSEC Data') }}</label>
|
||||||
</div>
|
</div>
|
||||||
<div id="dnssecData" style="display: none;">
|
<div id="dnssecData" style="display: none;">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-md-6">
|
<div class="col-md-6">
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<label for="dsKeyTag" class="form-label">{{ __('DS Record') }}</label>
|
<label for="dsKeyTag" class="form-label">{{ __('DS Record') }}</label>
|
||||||
<input type="text" class="form-control mb-2" placeholder="{{ __('Key Tag') }}" name="dsKeyTag" id="dsKeyTag">
|
<input type="text" class="form-control mb-2" placeholder="{{ __('Key Tag') }}" name="dsKeyTag" id="dsKeyTag">
|
||||||
<select class="form-select mb-2" name="dsAlg">
|
<select class="form-select mb-2" name="dsAlg">
|
||||||
<option value="" disabled selected>{{ __('Select Algorithm') }}</option>
|
<option value="" disabled selected>{{ __('Select Algorithm') }}</option>
|
||||||
<option value="8">RSA/SHA-256</option>
|
<option value="8">RSA/SHA-256</option>
|
||||||
<option value="13">ECDSA Curve P-256 with SHA-256</option>
|
<option value="13">ECDSA Curve P-256 with SHA-256</option>
|
||||||
<option value="14">ECDSA Curve P-384 with SHA-384</option>
|
<option value="14">ECDSA Curve P-384 with SHA-384</option>
|
||||||
<option value="15">Ed25519</option>
|
<option value="15">Ed25519</option>
|
||||||
<option value="16">Ed448</option>
|
<option value="16">Ed448</option>
|
||||||
</select>
|
</select>
|
||||||
<select class="form-select mb-2" name="dsDigestType">
|
<select class="form-select mb-2" name="dsDigestType">
|
||||||
<option value="" disabled selected>{{ __('Select Digest Type') }}</option>
|
<option value="" disabled selected>{{ __('Select Digest Type') }}</option>
|
||||||
<option value="2">SHA-256</option>
|
<option value="2">SHA-256</option>
|
||||||
<option value="4">SHA-384</option>
|
<option value="4">SHA-384</option>
|
||||||
</select>
|
</select>
|
||||||
<input type="text" class="form-control mb-2" placeholder="{{ __('Digest') }}" name="dsDigest">
|
<input type="text" class="form-control mb-2" placeholder="{{ __('Digest') }}" name="dsDigest">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="col-md-6">
|
<div class="col-md-6">
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<label for="dnskeyFlags" class="form-label">{{ __('DNSKEY Record') }}</label>
|
<label for="dnskeyFlags" class="form-label">{{ __('DNSKEY Record') }}</label>
|
||||||
<input type="number" class="form-control mb-2" placeholder="{{ __('Flags') }}" name="dnskeyFlags" id="dnskeyFlags">
|
<input type="number" class="form-control mb-2" placeholder="{{ __('Flags') }}" name="dnskeyFlags" id="dnskeyFlags">
|
||||||
<input type="number" class="form-control mb-2" placeholder="{{ __('Protocol') }}" name="dnskeyProtocol" value="3" readonly> <!-- Protocol is typically set to 3 -->
|
<input type="number" class="form-control mb-2" placeholder="{{ __('Protocol') }}" name="dnskeyProtocol" value="3" readonly> <!-- Protocol is typically set to 3 -->
|
||||||
<select class="form-select mb-2" name="dnskeyAlg">
|
<select class="form-select mb-2" name="dnskeyAlg">
|
||||||
<option value="" disabled selected>{{ __('Select Algorithm') }}</option>
|
<option value="" disabled selected>{{ __('Select Algorithm') }}</option>
|
||||||
<option value="8">RSA/SHA-256</option>
|
<option value="8">RSA/SHA-256</option>
|
||||||
<option value="13">ECDSA Curve P-256 with SHA-256</option>
|
<option value="13">ECDSA Curve P-256 with SHA-256</option>
|
||||||
<option value="14">ECDSA Curve P-384 with SHA-384</option>
|
<option value="14">ECDSA Curve P-384 with SHA-384</option>
|
||||||
<option value="15">Ed25519</option>
|
<option value="15">Ed25519</option>
|
||||||
<option value="16">Ed448</option>
|
<option value="16">Ed448</option>
|
||||||
</select>
|
</select>
|
||||||
<input type="text" class="form-control mb-2" placeholder="{{ __('Public Key') }}" name="dnskeyPubKey">
|
<input type="text" class="form-control mb-2" placeholder="{{ __('Public Key') }}" name="dnskeyPubKey">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{% if launch_phases == 'on' %}<hr />{% endif %}
|
{% if launch_phases == 'on' %}<hr />{% endif %}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{% if launch_phases == 'on' %}
|
{% if launch_phases == 'on' %}
|
||||||
|
@ -363,6 +363,7 @@ document.addEventListener("DOMContentLoaded", function() {
|
||||||
// Display year value from slider
|
// Display year value from slider
|
||||||
yearSlider.addEventListener('input', function() {
|
yearSlider.addEventListener('input', function() {
|
||||||
yearValueDisplay.textContent = `${yearSlider.value} Year${yearSlider.value > 1 ? 's' : ''}`;
|
yearValueDisplay.textContent = `${yearSlider.value} Year${yearSlider.value > 1 ? 's' : ''}`;
|
||||||
|
updatePrice(); // Call updatePrice() directly when slider moves
|
||||||
});
|
});
|
||||||
|
|
||||||
function createNameserverGroup(count) {
|
function createNameserverGroup(count) {
|
||||||
|
@ -444,102 +445,92 @@ document.addEventListener("DOMContentLoaded", function() {
|
||||||
dnssecData.style.display = 'none';
|
dnssecData.style.display = 'none';
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const domainInput = document.getElementById('domainName');
|
const domainInput = document.getElementById('domainName');
|
||||||
const yearInput = document.getElementById('registrationYears');
|
const yearInput = document.getElementById('registrationYears');
|
||||||
const priceDisplay = document.getElementById('domainPriceDisplay');
|
const priceDisplay = document.getElementById('domainPriceDisplay');
|
||||||
const priceValue = document.getElementById('domainPrice');
|
const priceValue = document.getElementById('domainPrice');
|
||||||
|
|
||||||
function extractTLD(domain) {
|
|
||||||
const parts = domain.split('.');
|
|
||||||
|
|
||||||
// If the domain has more than two segments (e.g., 'test.com.test'), return the last two segments
|
|
||||||
if (parts.length > 2) {
|
|
||||||
return parts.slice(-2).join('.').toLowerCase();
|
|
||||||
}
|
|
||||||
// If the domain has two or fewer segments (e.g., 'test.test' or 'test'), return the last segment
|
|
||||||
else {
|
|
||||||
return parts[parts.length - 1].toLowerCase();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function getDomainPrice(domain, years, registrarId) {
|
function getDomainPrice(domain, years, registrarId) {
|
||||||
const tld = extractTLD(domain);
|
const currency = "{{ currency }}";
|
||||||
if (!tld) {
|
const apiUrl = `/dapi/domain/price?domain_name=${encodeURIComponent(domain)}&date_add=${years * 12}&command=create®istrar_id=${encodeURIComponent(registrarId)}¤cy=${encodeURIComponent(currency)}`;
|
||||||
return Promise.reject("Invalid TLD");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Regular expression for exact TLD match
|
return fetch(apiUrl)
|
||||||
const tldRegex = new RegExp(`${tld.toLowerCase()}$`);
|
.then(response => response.json())
|
||||||
|
.then(data => {
|
||||||
// Fetch both promotional pricing and regular pricing
|
// If the response is a raw number (e.g., 0.5), wrap it in an object
|
||||||
return Promise.all([
|
if (typeof data === "number") {
|
||||||
fetch(`/api/records/promotion_pricing?join=domain_tld`).then(response => response.json()),
|
data = { price: data };
|
||||||
fetch(`/api/records/domain_price?join=domain_tld`).then(response => response.json())
|
|
||||||
])
|
|
||||||
.then(([promoData, pricingData]) => {
|
|
||||||
const today = new Date();
|
|
||||||
|
|
||||||
// Check for a valid promotion
|
|
||||||
const promo = promoData.records.find(record =>
|
|
||||||
record.tld_id && tldRegex.test(record.tld_id.tld.toLowerCase()) &&
|
|
||||||
new Date(record.start_date) <= today &&
|
|
||||||
new Date(record.end_date) >= today &&
|
|
||||||
(!record.years_of_promotion || record.years_of_promotion >= years)
|
|
||||||
);
|
|
||||||
|
|
||||||
// Find the regular price for the TLD with registrar ID
|
|
||||||
let tldData = pricingData.records.find(record =>
|
|
||||||
record.tldid && tldRegex.test(record.tldid.tld.toLowerCase()) &&
|
|
||||||
record.command === 'create' &&
|
|
||||||
record.registrar_id == registrarId
|
|
||||||
);
|
|
||||||
|
|
||||||
// If no registrar-specific price found, find the generic price
|
|
||||||
if (!tldData) {
|
|
||||||
tldData = pricingData.records.find(record =>
|
|
||||||
record.tldid && tldRegex.test(record.tldid.tld.toLowerCase()) &&
|
|
||||||
record.command === 'create' &&
|
|
||||||
record.registrar_id == null
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tldData) {
|
|
||||||
const priceField = `m${years * 12}`;
|
|
||||||
let price = parseFloat(tldData[priceField]);
|
|
||||||
if (!isNaN(price)) {
|
|
||||||
if (promo) {
|
|
||||||
// Apply the promotion discount
|
|
||||||
price -= (price * parseFloat(promo.discount_percentage) / 100);
|
|
||||||
}
|
|
||||||
return price;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return Promise.reject("TLD price not found");
|
if (!data || typeof data !== "object" || !("price" in data)) {
|
||||||
})
|
console.error("Invalid API response structure:", data);
|
||||||
.catch(error => {
|
return Promise.reject("Invalid API response structure");
|
||||||
console.error("Error fetching pricing data:", error);
|
}
|
||||||
return Promise.reject("Error fetching pricing data");
|
|
||||||
});
|
// Convert price to float safely
|
||||||
|
const price = parseFloat(data.price);
|
||||||
|
if (isNaN(price)) {
|
||||||
|
console.error("Invalid price received:", data.price);
|
||||||
|
return Promise.reject("Invalid price received");
|
||||||
|
}
|
||||||
|
|
||||||
|
return { price, type: data.type || "regular" };
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
console.error("Error fetching domain price:", error);
|
||||||
|
return Promise.reject("Error fetching domain price");
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatPrice(price) {
|
function formatPrice(price) {
|
||||||
switch(window.currencyPosition) {
|
switch(window.currencyPosition) {
|
||||||
case 'before':
|
case 'before':
|
||||||
return `{{ currency }} ${price.toFixed(2)}`;
|
return `${"{{ currency }}"} ${price.toFixed(2)}`;
|
||||||
case 'after':
|
case 'after':
|
||||||
return `${price.toFixed(2)} {{ currency }}`;
|
return `${price.toFixed(2)} ${"{{ currency }}"} `;
|
||||||
default:
|
default:
|
||||||
return price.toFixed(2);
|
return price.toFixed(2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function updatePrice() {
|
function updatePrice() {
|
||||||
if (domainInput.value) {
|
const domainValue = document.getElementById('domainName')?.value.trim() || "";
|
||||||
const registrarId = registrarDropdown.value;
|
const registrarId = document.getElementById('registrarDropdown')?.value || "";
|
||||||
getDomainPrice(domainInput.value, yearInput.value, registrarId).then(price => {
|
const years = parseInt(document.getElementById('registrationYears')?.value, 10) || 1;
|
||||||
priceValue.innerText = formatPrice(price);
|
|
||||||
|
if (domainValue) {
|
||||||
|
getDomainPrice(domainValue, years, registrarId).then(({ price, type }) => {
|
||||||
|
if (isNaN(price)) {
|
||||||
|
console.error("Invalid price received:", price);
|
||||||
|
priceValue.innerText = formatPrice(0.00);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Multiply price by years
|
||||||
|
const totalPrice = price * years;
|
||||||
|
|
||||||
|
priceValue.innerText = formatPrice(totalPrice);
|
||||||
priceDisplay.style.display = 'block';
|
priceDisplay.style.display = 'block';
|
||||||
|
|
||||||
|
// Remove existing color classes
|
||||||
|
priceValue.classList.remove('text-red', 'text-green', 'text-blue');
|
||||||
|
|
||||||
|
// Apply appropriate colors based on type
|
||||||
|
if (type === "promotion") {
|
||||||
|
priceValue.classList.add('text-green'); // Mark as promotion
|
||||||
|
priceDisplay.title = "Promotional Price";
|
||||||
|
} else if (type === "premium") {
|
||||||
|
priceValue.classList.add('text-red'); // Mark as premium
|
||||||
|
priceDisplay.title = "Premium Price";
|
||||||
|
} else {
|
||||||
|
priceValue.classList.add('text-blue'); // Default regular price
|
||||||
|
priceDisplay.title = "Regular Price";
|
||||||
|
}
|
||||||
|
|
||||||
|
}).catch(error => {
|
||||||
|
console.error("Error fetching price:", error);
|
||||||
|
priceDisplay.style.display = 'none';
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
priceDisplay.style.display = 'none';
|
priceDisplay.style.display = 'none';
|
||||||
|
@ -549,6 +540,7 @@ document.addEventListener("DOMContentLoaded", function() {
|
||||||
domainInput.addEventListener('input', updatePrice);
|
domainInput.addEventListener('input', updatePrice);
|
||||||
yearInput.addEventListener('input', updatePrice);
|
yearInput.addEventListener('input', updatePrice);
|
||||||
registrarDropdown.addEventListener('change', updatePrice);
|
registrarDropdown.addEventListener('change', updatePrice);
|
||||||
|
yearSlider.addEventListener('input', updatePrice);
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
{% endblock %}
|
{% endblock %}
|
|
@ -78,117 +78,103 @@ document.addEventListener("DOMContentLoaded", function() {
|
||||||
|
|
||||||
const yearSlider = document.getElementById('renewalYears');
|
const yearSlider = document.getElementById('renewalYears');
|
||||||
const yearValueDisplay = document.getElementById('yearValue');
|
const yearValueDisplay = document.getElementById('yearValue');
|
||||||
|
const domainValue = "{{ domain.name }}"; // Get domain from Twig directly
|
||||||
// Display year value from slider
|
|
||||||
yearSlider.addEventListener('input', function() {
|
|
||||||
yearValueDisplay.textContent = `${yearSlider.value} Year${yearSlider.value > 1 ? 's' : ''}`;
|
|
||||||
});
|
|
||||||
|
|
||||||
const registrarId = "{{ registrars.id }}"; // Embedded securely in JavaScript
|
|
||||||
const domainInput = document.getElementById('domainName');
|
|
||||||
const yearInput = document.getElementById('renewalYears');
|
const yearInput = document.getElementById('renewalYears');
|
||||||
const priceDisplay = document.getElementById('domainPriceDisplay');
|
const priceDisplay = document.getElementById('domainPriceDisplay');
|
||||||
const priceValue = document.getElementById('domainPrice');
|
const priceValue = document.getElementById('domainPrice');
|
||||||
|
|
||||||
function extractTLD(domain) {
|
// Display year value from slider
|
||||||
const parts = domain.split('.');
|
yearSlider.addEventListener('input', function() {
|
||||||
|
yearValueDisplay.textContent = `${yearSlider.value} Year${yearSlider.value > 1 ? 's' : ''}`;
|
||||||
// If the domain has more than two segments (e.g., 'test.com.test'), return the last two segments
|
updatePrice(); // Call updatePrice() directly when slider moves
|
||||||
if (parts.length > 2) {
|
});
|
||||||
return parts.slice(-2).join('.').toLowerCase();
|
|
||||||
}
|
|
||||||
// If the domain has two or fewer segments (e.g., 'test.test' or 'test'), return the last segment
|
|
||||||
else {
|
|
||||||
return parts[parts.length - 1].toLowerCase();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function getDomainPrice(domain, years, registrarId) {
|
function getDomainPrice(domain, years, registrarId) {
|
||||||
const tld = extractTLD(domain);
|
const currency = "{{ currency }}";
|
||||||
if (!tld) {
|
const apiUrl = `/dapi/domain/price?domain_name=${encodeURIComponent(domain)}&date_add=${years * 12}&command=renew®istrar_id=${encodeURIComponent(registrarId)}¤cy=${encodeURIComponent(currency)}`;
|
||||||
return Promise.reject("Invalid TLD");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Regular expression for exact TLD match
|
return fetch(apiUrl)
|
||||||
const tldRegex = new RegExp(`${tld.toLowerCase()}$`);
|
.then(response => response.json())
|
||||||
|
.then(data => {
|
||||||
// Fetch both promotional pricing and regular pricing
|
// If the response is a raw number (e.g., 0.5), wrap it in an object
|
||||||
return Promise.all([
|
if (typeof data === "number") {
|
||||||
fetch(`/api/records/promotion_pricing?join=domain_tld`).then(response => response.json()),
|
data = { price: data };
|
||||||
fetch(`/api/records/domain_price?join=domain_tld`).then(response => response.json())
|
|
||||||
])
|
|
||||||
.then(([promoData, pricingData]) => {
|
|
||||||
const today = new Date();
|
|
||||||
|
|
||||||
// Check for a valid promotion
|
|
||||||
const promo = promoData.records.find(record =>
|
|
||||||
record.tld_id && tldRegex.test(record.tld_id.tld.toLowerCase()) &&
|
|
||||||
new Date(record.start_date) <= today &&
|
|
||||||
new Date(record.end_date) >= today &&
|
|
||||||
(!record.years_of_promotion || record.years_of_promotion >= years)
|
|
||||||
);
|
|
||||||
|
|
||||||
// Find the regular price for the TLD with registrar ID
|
|
||||||
let tldData = pricingData.records.find(record =>
|
|
||||||
record.tldid && tldRegex.test(record.tldid.tld.toLowerCase()) &&
|
|
||||||
record.command === 'renew' &&
|
|
||||||
record.registrar_id == registrarId
|
|
||||||
);
|
|
||||||
|
|
||||||
// If no registrar-specific price found, find the generic price
|
|
||||||
if (!tldData) {
|
|
||||||
tldData = pricingData.records.find(record =>
|
|
||||||
record.tldid && tldRegex.test(record.tldid.tld.toLowerCase()) &&
|
|
||||||
record.command === 'renew' &&
|
|
||||||
record.registrar_id == null
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tldData) {
|
|
||||||
const priceField = `m${years * 12}`;
|
|
||||||
let price = parseFloat(tldData[priceField]);
|
|
||||||
if (!isNaN(price)) {
|
|
||||||
if (promo) {
|
|
||||||
// Apply the promotion discount
|
|
||||||
price -= (price * parseFloat(promo.discount_percentage) / 100);
|
|
||||||
}
|
|
||||||
return price;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return Promise.reject("TLD price not found");
|
if (!data || typeof data !== "object" || !("price" in data)) {
|
||||||
})
|
console.error("Invalid API response structure:", data);
|
||||||
.catch(error => {
|
return Promise.reject("Invalid API response structure");
|
||||||
console.error("Error fetching pricing data:", error);
|
}
|
||||||
return Promise.reject("Error fetching pricing data");
|
|
||||||
});
|
// Convert price to float safely
|
||||||
|
const price = parseFloat(data.price);
|
||||||
|
if (isNaN(price)) {
|
||||||
|
console.error("Invalid price received:", data.price);
|
||||||
|
return Promise.reject("Invalid price received");
|
||||||
|
}
|
||||||
|
|
||||||
|
return { price, type: data.type || "regular" };
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
console.error("Error fetching domain price:", error);
|
||||||
|
return Promise.reject("Error fetching domain price");
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatPrice(price) {
|
function formatPrice(price) {
|
||||||
switch(window.currencyPosition) {
|
switch(window.currencyPosition) {
|
||||||
case 'before':
|
case 'before':
|
||||||
return `{{ currency }} ${price.toFixed(2)}`;
|
return `${"{{ currency }}"} ${price.toFixed(2)}`;
|
||||||
case 'after':
|
case 'after':
|
||||||
return `${price.toFixed(2)} {{ currency }}`;
|
return `${price.toFixed(2)} ${"{{ currency }}"} `;
|
||||||
default:
|
default:
|
||||||
return price.toFixed(2);
|
return price.toFixed(2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function updatePrice() {
|
function updatePrice() {
|
||||||
if (domainInput.textContent) {
|
const domainValue = "{{ domain.name }}";
|
||||||
getDomainPrice(domainInput.textContent, yearInput.value, registrarId).then(price => {
|
const registrarId = document.getElementById('registrarDropdown')?.value || "";
|
||||||
priceValue.innerText = formatPrice(price);
|
const years = parseInt(document.getElementById('renewalYears')?.value, 10) || 1;
|
||||||
|
|
||||||
|
if (domainValue) {
|
||||||
|
getDomainPrice(domainValue, years, registrarId).then(({ price, type }) => {
|
||||||
|
if (isNaN(price)) {
|
||||||
|
console.error("Invalid price received:", price);
|
||||||
|
priceValue.innerText = formatPrice(0.00);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Multiply price by years
|
||||||
|
const totalPrice = price * years;
|
||||||
|
|
||||||
|
priceValue.innerText = formatPrice(totalPrice);
|
||||||
priceDisplay.style.display = 'block';
|
priceDisplay.style.display = 'block';
|
||||||
|
|
||||||
|
// Remove existing color classes
|
||||||
|
priceValue.classList.remove('text-red', 'text-green', 'text-blue');
|
||||||
|
|
||||||
|
// Apply appropriate colors based on type
|
||||||
|
if (type === "promotion") {
|
||||||
|
priceValue.classList.add('text-green'); // Mark as promotion
|
||||||
|
priceDisplay.title = "Promotional Price";
|
||||||
|
} else if (type === "premium") {
|
||||||
|
priceValue.classList.add('text-red'); // Mark as premium
|
||||||
|
priceDisplay.title = "Premium Price";
|
||||||
|
} else {
|
||||||
|
priceValue.classList.add('text-blue'); // Default regular price
|
||||||
|
priceDisplay.title = "Regular Price";
|
||||||
|
}
|
||||||
|
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
console.error(error);
|
console.error("Error fetching price:", error);
|
||||||
// Handle the error or display a message as needed
|
priceDisplay.style.display = 'none';
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
priceDisplay.style.display = 'none';
|
priceDisplay.style.display = 'none';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
domainInput.addEventListener('input', updatePrice);
|
|
||||||
yearInput.addEventListener('input', updatePrice);
|
yearInput.addEventListener('input', updatePrice);
|
||||||
|
|
||||||
updatePrice();
|
updatePrice();
|
||||||
|
|
|
@ -91,108 +91,97 @@ document.addEventListener("DOMContentLoaded", function() {
|
||||||
|
|
||||||
const yearSlider = document.getElementById('transferYears');
|
const yearSlider = document.getElementById('transferYears');
|
||||||
const yearValueDisplay = document.getElementById('yearValue');
|
const yearValueDisplay = document.getElementById('yearValue');
|
||||||
|
|
||||||
// Display year value from slider
|
|
||||||
yearSlider.addEventListener('input', function() {
|
|
||||||
yearValueDisplay.textContent = `${yearSlider.value} Year${yearSlider.value > 1 ? 's' : ''}`;
|
|
||||||
});
|
|
||||||
|
|
||||||
const domainInput = document.getElementById('domainName');
|
const domainInput = document.getElementById('domainName');
|
||||||
const yearInput = document.getElementById('transferYears');
|
const yearInput = document.getElementById('transferYears');
|
||||||
const priceDisplay = document.getElementById('domainPriceDisplay');
|
const priceDisplay = document.getElementById('domainPriceDisplay');
|
||||||
const priceValue = document.getElementById('domainPrice');
|
const priceValue = document.getElementById('domainPrice');
|
||||||
const registrarDropdown = document.getElementById('registrarDropdown');
|
|
||||||
|
// Display year value from slider
|
||||||
function extractTLD(domain) {
|
yearSlider.addEventListener('input', function() {
|
||||||
const parts = domain.split('.');
|
yearValueDisplay.textContent = `${yearSlider.value} Year${yearSlider.value > 1 ? 's' : ''}`;
|
||||||
|
updatePrice(); // Call updatePrice() directly when slider moves
|
||||||
// If the domain has more than two segments (e.g., 'test.com.test'), return the last two segments
|
});
|
||||||
if (parts.length > 2) {
|
|
||||||
return parts.slice(-2).join('.').toLowerCase();
|
|
||||||
}
|
|
||||||
// If the domain has two or fewer segments (e.g., 'test.test' or 'test'), return the last segment
|
|
||||||
else {
|
|
||||||
return parts[parts.length - 1].toLowerCase();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function getDomainPrice(domain, years, registrarId) {
|
function getDomainPrice(domain, years, registrarId) {
|
||||||
const tld = extractTLD(domain);
|
const currency = "{{ currency }}";
|
||||||
if (!tld) {
|
const apiUrl = `/dapi/domain/price?domain_name=${encodeURIComponent(domain)}&date_add=${years * 12}&command=transfer®istrar_id=${encodeURIComponent(registrarId)}¤cy=${encodeURIComponent(currency)}`;
|
||||||
return Promise.reject("Invalid TLD");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Regular expression for exact TLD match
|
return fetch(apiUrl)
|
||||||
const tldRegex = new RegExp(`${tld.toLowerCase()}$`);
|
.then(response => response.json())
|
||||||
|
.then(data => {
|
||||||
// Fetch both promotional pricing and regular pricing
|
// If the response is a raw number (e.g., 0.5), wrap it in an object
|
||||||
return Promise.all([
|
if (typeof data === "number") {
|
||||||
fetch(`/api/records/promotion_pricing?join=domain_tld`).then(response => response.json()),
|
data = { price: data };
|
||||||
fetch(`/api/records/domain_price?join=domain_tld`).then(response => response.json())
|
|
||||||
])
|
|
||||||
.then(([promoData, pricingData]) => {
|
|
||||||
const today = new Date();
|
|
||||||
|
|
||||||
// Check for a valid promotion
|
|
||||||
const promo = promoData.records.find(record =>
|
|
||||||
record.tld_id && tldRegex.test(record.tld_id.tld.toLowerCase()) &&
|
|
||||||
new Date(record.start_date) <= today &&
|
|
||||||
new Date(record.end_date) >= today &&
|
|
||||||
(!record.years_of_promotion || record.years_of_promotion >= years)
|
|
||||||
);
|
|
||||||
|
|
||||||
// Find the regular price for the TLD with registrar ID
|
|
||||||
let tldData = pricingData.records.find(record =>
|
|
||||||
record.tldid && tldRegex.test(record.tldid.tld.toLowerCase()) &&
|
|
||||||
record.command === 'transfer' &&
|
|
||||||
record.registrar_id == registrarId
|
|
||||||
);
|
|
||||||
|
|
||||||
// If no registrar-specific price found, find the generic price
|
|
||||||
if (!tldData) {
|
|
||||||
tldData = pricingData.records.find(record =>
|
|
||||||
record.tldid && tldRegex.test(record.tldid.tld.toLowerCase()) &&
|
|
||||||
record.command === 'transfer' &&
|
|
||||||
record.registrar_id == null
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tldData) {
|
|
||||||
const priceField = `m${years * 12}`;
|
|
||||||
let price = parseFloat(tldData[priceField]);
|
|
||||||
if (!isNaN(price)) {
|
|
||||||
if (promo) {
|
|
||||||
// Apply the promotion discount
|
|
||||||
price -= (price * parseFloat(promo.discount_percentage) / 100);
|
|
||||||
}
|
|
||||||
return price;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return Promise.reject("TLD price not found");
|
if (!data || typeof data !== "object" || !("price" in data)) {
|
||||||
})
|
console.error("Invalid API response structure:", data);
|
||||||
.catch(error => {
|
return Promise.reject("Invalid API response structure");
|
||||||
console.error("Error fetching pricing data:", error);
|
}
|
||||||
return Promise.reject("Error fetching pricing data");
|
|
||||||
});
|
// Convert price to float safely
|
||||||
|
const price = parseFloat(data.price);
|
||||||
|
if (isNaN(price)) {
|
||||||
|
console.error("Invalid price received:", data.price);
|
||||||
|
return Promise.reject("Invalid price received");
|
||||||
|
}
|
||||||
|
|
||||||
|
return { price, type: data.type || "regular" };
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
console.error("Error fetching domain price:", error);
|
||||||
|
return Promise.reject("Error fetching domain price");
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatPrice(price) {
|
function formatPrice(price) {
|
||||||
switch(window.currencyPosition) {
|
switch(window.currencyPosition) {
|
||||||
case 'before':
|
case 'before':
|
||||||
return `{{ currency }} ${price.toFixed(2)}`;
|
return `${"{{ currency }}"} ${price.toFixed(2)}`;
|
||||||
case 'after':
|
case 'after':
|
||||||
return `${price.toFixed(2)} {{ currency }}`;
|
return `${price.toFixed(2)} ${"{{ currency }}"} `;
|
||||||
default:
|
default:
|
||||||
return price.toFixed(2);
|
return price.toFixed(2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function updatePrice() {
|
function updatePrice() {
|
||||||
if (domainInput.value) {
|
const domainValue = document.getElementById('domainName')?.value.trim() || "";
|
||||||
const registrarId = registrarDropdown.value;
|
const registrarId = document.getElementById('registrarDropdown')?.value || "";
|
||||||
getDomainPrice(domainInput.value, yearInput.value, registrarId).then(price => {
|
const years = parseInt(document.getElementById('transferYears')?.value, 10) || 1;
|
||||||
priceValue.innerText = formatPrice(price);
|
|
||||||
|
if (domainValue) {
|
||||||
|
getDomainPrice(domainValue, years, registrarId).then(({ price, type }) => {
|
||||||
|
if (isNaN(price)) {
|
||||||
|
console.error("Invalid price received:", price);
|
||||||
|
priceValue.innerText = formatPrice(0.00);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Multiply price by years
|
||||||
|
const totalPrice = price * years;
|
||||||
|
|
||||||
|
priceValue.innerText = formatPrice(totalPrice);
|
||||||
priceDisplay.style.display = 'block';
|
priceDisplay.style.display = 'block';
|
||||||
|
|
||||||
|
// Remove existing color classes
|
||||||
|
priceValue.classList.remove('text-red', 'text-green', 'text-blue');
|
||||||
|
|
||||||
|
// Apply appropriate colors based on type
|
||||||
|
if (type === "promotion") {
|
||||||
|
priceValue.classList.add('text-green'); // Mark as promotion
|
||||||
|
priceDisplay.title = "Promotional Price";
|
||||||
|
} else if (type === "premium") {
|
||||||
|
priceValue.classList.add('text-red'); // Mark as premium
|
||||||
|
priceDisplay.title = "Premium Price";
|
||||||
|
} else {
|
||||||
|
priceValue.classList.add('text-blue'); // Default regular price
|
||||||
|
priceDisplay.title = "Regular Price";
|
||||||
|
}
|
||||||
|
|
||||||
|
}).catch(error => {
|
||||||
|
console.error("Error fetching price:", error);
|
||||||
|
priceDisplay.style.display = 'none';
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
priceDisplay.style.display = 'none';
|
priceDisplay.style.display = 'none';
|
||||||
|
@ -202,6 +191,7 @@ document.addEventListener("DOMContentLoaded", function() {
|
||||||
domainInput.addEventListener('input', updatePrice);
|
domainInput.addEventListener('input', updatePrice);
|
||||||
yearInput.addEventListener('input', updatePrice);
|
yearInput.addEventListener('input', updatePrice);
|
||||||
registrarDropdown.addEventListener('change', updatePrice);
|
registrarDropdown.addEventListener('change', updatePrice);
|
||||||
|
yearSlider.addEventListener('input', updatePrice);
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
{% endblock %}
|
{% endblock %}
|
|
@ -163,6 +163,7 @@ $app->group('', function ($route) {
|
||||||
$route->get('/dapi/applications', [DapiController::class, 'listApplications']);
|
$route->get('/dapi/applications', [DapiController::class, 'listApplications']);
|
||||||
$route->get('/dapi/payments', [DapiController::class, 'listPayments']);
|
$route->get('/dapi/payments', [DapiController::class, 'listPayments']);
|
||||||
$route->get('/dapi/statements', [DapiController::class, 'listStatements']);
|
$route->get('/dapi/statements', [DapiController::class, 'listStatements']);
|
||||||
|
$route->get('/dapi/domain/price', [DapiController::class, 'domainPrice']);
|
||||||
})->add(new AuthMiddleware($container));
|
})->add(new AuthMiddleware($container));
|
||||||
|
|
||||||
$app->any('/api[/{params:.*}]', function (
|
$app->any('/api[/{params:.*}]', function (
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue