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
5 changes: 3 additions & 2 deletions src/Domains/Registrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,12 @@ public function available(string $domain): bool
* @param array|Contact $contacts
* @param array $nameservers
* @param bool $autorenewEnabled
* @param float|null $purchasePrice Required if domain is premium
* @return string Order ID
*/
public function purchase(string $domain, array|Contact $contacts, int $periodYears = 1, array $nameservers = [], bool $autorenewEnabled = false): string
public function purchase(string $domain, array|Contact $contacts, int $periodYears = 1, array $nameservers = [], bool $autorenewEnabled = false, ?float $purchasePrice = null): string
{
return $this->adapter->purchase($domain, $contacts, $periodYears, $nameservers, $autorenewEnabled);
return $this->adapter->purchase($domain, $contacts, $periodYears, $nameservers, $autorenewEnabled, $purchasePrice);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Domains/Registrar/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,10 @@ abstract public function available(string $domain): bool;
* @param int $periodYears
* @param array $nameservers
* @param bool $autorenewEnabled
* @param float|null $purchasePrice Required if domain is premium
* @return string Order ID
*/
abstract public function purchase(string $domain, array|Contact $contacts, int $periodYears = 1, array $nameservers = [], bool $autorenewEnabled = false): string;
abstract public function purchase(string $domain, array|Contact $contacts, int $periodYears = 1, array $nameservers = [], bool $autorenewEnabled = false, ?float $purchasePrice = null): string;

/**
* Suggest domain names
Expand Down
3 changes: 2 additions & 1 deletion src/Domains/Registrar/Adapter/Mock.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,12 @@ public function available(string $domain): bool
* @param int $periodYears
* @param array $nameservers
* @param bool $autorenewEnabled
* @param float|null $purchasePrice Required if domain is premium
* @return string Order ID
* @throws DomainTakenException
* @throws InvalidContactException
*/
public function purchase(string $domain, array|Contact $contacts, int $periodYears = 1, array $nameservers = [], bool $autorenewEnabled = false): string
public function purchase(string $domain, array|Contact $contacts, int $periodYears = 1, array $nameservers = [], bool $autorenewEnabled = false, ?float $purchasePrice = null): string
{
if (!$this->available($domain)) {
throw new DomainTakenException("Domain {$domain} is not available for registration", self::RESPONSE_CODE_DOMAIN_TAKEN);
Expand Down
59 changes: 55 additions & 4 deletions src/Domains/Registrar/Adapter/NameCom.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,10 @@ public function updateNameservers(string $domain, array $nameservers): array
* @param int $periodYears Registration period in years
* @param array $nameservers Nameservers to use
* @param bool $autorenewEnabled Whether autorenew should be enabled
* @param float|null $purchasePrice Required if domain is premium
* @return string Order ID
*/
public function purchase(string $domain, array|Contact $contacts, int $periodYears = 1, array $nameservers = [], bool $autorenewEnabled = false): string
public function purchase(string $domain, array|Contact $contacts, int $periodYears = 1, array $nameservers = [], bool $autorenewEnabled = false, ?float $purchasePrice = null): string
{
try {
$contacts = \is_array($contacts) ? $contacts : [$contacts];
Expand All @@ -190,6 +191,10 @@ public function purchase(string $domain, array|Contact $contacts, int $periodYea
'years' => $periodYears,
];

if ($purchasePrice !== null) {
$data['purchasePrice'] = $purchasePrice;
}

$result = $this->send('POST', '/core/v1/domains', $data);
return (string) ($result['order'] ?? '');

Expand Down Expand Up @@ -320,7 +325,13 @@ public function suggest(array|string $query, array $tlds = [], int|null $limit =

$purchasable = $domainResult['purchasable'] ?? false;
$price = isset($domainResult['purchasePrice']) ? (float) $domainResult['purchasePrice'] : null;
$isPremium = isset($domainResult['premium']) && $domainResult['premium'] === true;
$renewalPrice = isset($domainResult['renewalPrice']) ? (float) $domainResult['renewalPrice'] : null;
$purchaseType = $domainResult['purchaseType'] ?? 'registration';

// Aftermarket listings (purchaseType other than 'registration')
// are premium even when the premium flag is not set
$isPremium = (isset($domainResult['premium']) && $domainResult['premium'] === true)
|| ($purchaseType !== '' && $purchaseType !== 'registration');

// Apply price filters
if ($price !== null) {
Expand All @@ -343,6 +354,8 @@ public function suggest(array|string $query, array $tlds = [], int|null $limit =
$items[$domain] = [
'available' => $purchasable,
'price' => $price,
'renewalPrice' => $renewalPrice,
'purchaseType' => $purchaseType,
'type' => $isPremium ? 'premium' : 'suggestion',
];

Expand Down Expand Up @@ -371,6 +384,9 @@ public function getPrice(string $domain, int $periodYears = 1, string $regType =
if ($this->cache) {
$cached = $this->cache->load($cacheKey, $ttl);
if (\is_array($cached[$regType] ?? null)) {
if (($cached[$regType]['price'] ?? null) === null) {
throw new PriceNotFoundException("Price not found for domain: {$domain}", 400);
}
return new Price($cached[$regType]['price'], $cached[$regType]['premium']);
}
}
Expand All @@ -385,13 +401,48 @@ public function getPrice(string $domain, int $periodYears = 1, string $regType =
Registrar::REG_TYPE_TRANSFER => $result['transferPrice'] ?? null,
];

// getPricing only covers standard registry registrations. Premium
// aftermarket listings are priced by the availability endpoint, so
// without this merge a premium domain is quoted at the base TLD price.
$availability = null;
$availabilityFailed = false;
try {
$availabilityResult = $this->send('POST', '/core/v1/domains:checkAvailability', [
'domainNames' => [$domain],
]);
$availability = $availabilityResult['results'][0] ?? null;
} catch (RateLimitException $e) {
throw $e;
} catch (Exception $e) {
// Registry pricing is still usable for standard domains; skip
// the premium override and skip caching so the merge is
// retried on the next request
$availabilityFailed = true;
}

$purchaseType = $availability['purchaseType'] ?? 'registration';
if (
!empty($availability['purchasable'])
&& (($availability['premium'] ?? false) === true || ($purchaseType !== '' && $purchaseType !== 'registration'))
) {
$isPremium = true;
if (isset($availability['purchasePrice'])) {
$priceMap[Registrar::REG_TYPE_NEW] = (float) $availability['purchasePrice'];
}
// A renewal price of 0 means name.com has no renewal data for
// the listing, so keep the registry renewal price in that case
if (!empty($availability['renewalPrice'])) {
$priceMap[Registrar::REG_TYPE_RENEWAL] = (float) $availability['renewalPrice'];
}
Comment thread
ChiragAgg5k marked this conversation as resolved.
}

if (!array_filter($priceMap, fn ($p) => $p !== null)) {
throw new PriceNotFoundException("Price not found for domain: {$domain}", 400);
}

if ($this->cache) {
if ($this->cache && !$availabilityFailed) {
$cacheData = array_map(
fn ($price) => ['price' => (float) ($price ?? 0), 'premium' => $isPremium],
fn ($price) => ['price' => $price !== null ? (float) $price : null, 'premium' => $isPremium],
$priceMap
);
$this->cache->save($cacheKey, $cacheData);
Expand Down
4 changes: 2 additions & 2 deletions src/Domains/Registrar/Adapter/OpenSRS.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private function register(string $domain, string $regType, array $user, array $c
return $result;
}

public function purchase(string $domain, array|Contact $contacts, int $periodYears = 1, array $nameservers = [], bool $autorenewEnabled = false): string
public function purchase(string $domain, array|Contact $contacts, int $periodYears = 1, array $nameservers = [], bool $autorenewEnabled = false, ?float $purchasePrice = null): string
{
try {
$contacts = \is_array($contacts) ? $contacts : [$contacts];
Expand All @@ -191,7 +191,7 @@ public function purchase(string $domain, array|Contact $contacts, int $periodYea

$regType = Registrar::REG_TYPE_NEW;

$result = $this->register($domain, $regType, $this->user, $contacts, $nameservers, $periodYears, null, null, $autorenewEnabled);
$result = $this->register($domain, $regType, $this->user, $contacts, $nameservers, $periodYears, null, $purchasePrice, $autorenewEnabled);
$result = $this->response($result);
return $result['id'];

Expand Down
Loading