Files
domain-watchdog/src/Service/Connector/NamecheapConnector.php

107 lines
3.5 KiB
PHP
Raw Normal View History

2024-08-19 21:17:57 +02:00
<?php
2024-08-20 13:56:50 +02:00
2024-08-19 21:17:57 +02:00
namespace App\Service\Connector;
use App\Entity\Domain;
2024-09-18 13:37:07 +02:00
use Psr\Cache\CacheItemInterface;
2024-09-25 14:03:16 +02:00
use Psr\Cache\CacheItemPoolInterface;
2024-09-09 14:02:33 +02:00
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
2024-08-20 13:56:50 +02:00
use Symfony\Contracts\HttpClient\HttpClientInterface;
2024-08-19 21:17:57 +02:00
2024-09-09 14:02:33 +02:00
#[Autoconfigure(public: true)]
2024-09-18 13:37:07 +02:00
class NamecheapConnector extends AbstractProvider
2024-08-19 21:17:57 +02:00
{
2024-08-20 13:56:50 +02:00
public const BASE_URL = 'https://api.namecheap.com/xml.response';
2024-08-19 21:17:57 +02:00
2024-08-20 13:56:50 +02:00
public const SANDBOX_BASE_URL = 'http://api.sandbox.namecheap.com/xml.response';
2024-08-19 21:17:57 +02:00
2024-09-25 14:03:16 +02:00
public function __construct(CacheItemPoolInterface $cacheItemPool, private HttpClientInterface $client, private readonly string $outgoingIp)
2024-08-20 13:56:50 +02:00
{
2024-09-25 14:03:16 +02:00
parent::__construct($cacheItemPool);
2024-08-20 13:56:50 +02:00
}
2024-08-19 21:17:57 +02:00
public function orderDomain(Domain $domain, $dryRun): void
2024-08-20 13:56:50 +02:00
{
2024-09-09 14:02:33 +02:00
$addressesRes = $this->call('namecheap.users.address.getList', [], $dryRun);
$addresses = $addressesRes->AddressGetListResult->List;
2024-08-20 13:56:50 +02:00
if (count($addresses) < 1) {
throw new \Exception('Namecheap account requires at least one address to purchase a domain');
}
2024-09-25 14:03:16 +02:00
$addressId = (string)$addresses->attributes()['AddressId'];
$address = (array)$this->call('namecheap.users.address.getinfo', ['AddressId' => $addressId], $dryRun)->GetAddressInfoResult;
2024-09-09 14:02:33 +02:00
if (empty($address['PostalCode'])) {
$address['PostalCode'] = $address['Zip'];
}
2024-08-19 21:17:57 +02:00
2024-08-20 13:56:50 +02:00
$domainAddresses = [];
self::mergePrefixKeys('Registrant', $address, $domainAddresses);
self::mergePrefixKeys('Tech', $address, $domainAddresses);
self::mergePrefixKeys('Admin', $address, $domainAddresses);
self::mergePrefixKeys('AuxBilling', $address, $domainAddresses);
$this->call('namecheap.domains.create', array_merge([
'DomainName' => $domain->getLdhName(),
'Years' => 1,
'AddFreeWhoisguard' => 'yes',
'WGEnabled' => 'yes',
2024-08-23 14:01:02 +02:00
], $domainAddresses), $dryRun);
2024-08-20 13:56:50 +02:00
}
private static function mergePrefixKeys(string $prefix, array|object $src, array &$dest)
{
foreach ($src as $key => $value) {
2024-09-25 14:03:16 +02:00
$dest[$prefix . $key] = $value;
2024-08-20 13:56:50 +02:00
}
}
2024-08-23 14:01:02 +02:00
private function call(string $command, array $parameters = [], bool $dryRun = true): object
2024-08-19 21:17:57 +02:00
{
$actualParams = array_merge([
2024-08-20 13:56:50 +02:00
'Command' => $command,
2024-09-09 14:02:33 +02:00
'UserName' => $this->authData['ApiUser'],
2024-08-23 14:01:02 +02:00
'ApiUser' => $this->authData['ApiUser'],
'ApiKey' => $this->authData['ApiKey'],
'ClientIp' => $this->outgoingIp,
2024-08-19 21:17:57 +02:00
], $parameters);
2024-08-23 14:01:02 +02:00
$response = $this->client->request('POST', $dryRun ? self::SANDBOX_BASE_URL : self::BASE_URL, [
2024-08-20 13:56:50 +02:00
'query' => $actualParams,
2024-08-19 21:17:57 +02:00
]);
$data = new \SimpleXMLElement($response->getContent());
2024-09-09 14:02:33 +02:00
if ($data->Errors->Error) {
throw new \Exception($data->Errors->Error); // FIXME better exception type
2024-08-19 21:17:57 +02:00
}
return $data->CommandResponse;
}
public static function verifyAuthData(array $authData, HttpClientInterface $client): array
{
2024-08-26 14:02:12 +02:00
return $authData;
2024-08-19 21:17:57 +02:00
}
2024-09-18 13:37:07 +02:00
protected function getCachedTldList(): CacheItemInterface
{
2024-09-25 14:03:16 +02:00
return $this->cacheItemPool->getItem('app.provider.namecheap.supported-tld');
2024-09-18 13:37:07 +02:00
}
protected function getSupportedTldList(): array
{
2024-09-25 14:03:16 +02:00
$supported = [];
$tlds = $this->call('namecheap.domains.gettldlist', [], false)->Tlds->Tld;
for ($i = 0; $i < $tlds->count(); ++$i) {
$supported[] = (string) $tlds[$i]['Name'];
}
return $supported;
2024-09-18 13:37:07 +02:00
}
2024-08-19 21:17:57 +02:00
}