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-08-20 13:56:50 +02:00
|
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
2024-08-19 21:17:57 +02:00
|
|
|
|
2024-08-23 13:38:16 +02:00
|
|
|
class NamecheapConnector extends AbstractConnector
|
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-08-23 13:38:16 +02:00
|
|
|
public function __construct(private HttpClientInterface $client)
|
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
|
|
|
{
|
|
|
|
|
$addresses = $this->call('namecheap.users.address.getList');
|
|
|
|
|
|
|
|
|
|
if (count($addresses) < 1) {
|
|
|
|
|
throw new \Exception('Namecheap account requires at least one address to purchase a domain');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$addressId = $addresses->{0}->AddressId;
|
|
|
|
|
$address = $this->call('namecheap.users.address.getinfo', ['AddressId' => $addressId]);
|
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',
|
|
|
|
|
], $domainAddresses));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function mergePrefixKeys(string $prefix, array|object $src, array &$dest)
|
|
|
|
|
{
|
|
|
|
|
foreach ($src as $key => $value) {
|
|
|
|
|
$dest[$prefix.$key] = $value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function call(string $command, array $parameters = [], ?array $authData = null): object
|
2024-08-19 21:17:57 +02:00
|
|
|
{
|
|
|
|
|
if (is_null($authData)) {
|
|
|
|
|
$authData = $this->authData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$actualParams = array_merge([
|
2024-08-20 13:56:50 +02:00
|
|
|
'Command' => $command,
|
2024-08-19 21:17:57 +02:00
|
|
|
'ApiUser' => $authData['ApiUser'],
|
2024-08-20 13:56:50 +02:00
|
|
|
'ApiKey' => $authData['ApiKey'],
|
|
|
|
|
'ClientIp' => '', // TODO DW instance IP envvar
|
2024-08-19 21:17:57 +02:00
|
|
|
], $parameters);
|
|
|
|
|
|
2024-08-20 13:56:50 +02:00
|
|
|
$response = $this->client->request('POST', self::BASE_URL, [
|
|
|
|
|
'query' => $actualParams,
|
2024-08-19 21:17:57 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$data = new \SimpleXMLElement($response->getContent());
|
|
|
|
|
|
|
|
|
|
if ($data->errors->error) {
|
|
|
|
|
throw new \Exception(implode(', ', $data->errors->error)); // FIXME better exception type
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $data->CommandResponse;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function verifyAuthData(array $authData, HttpClientInterface $client): array
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|