feat: domain order call

a bisto de nas
This commit is contained in:
Vincent 2024-08-20 13:56:50 +02:00
parent f225213c49
commit 700e531e22
No known key found for this signature in database
GPG Key ID: 056662E78D70E358
2 changed files with 46 additions and 13 deletions

View File

@ -73,7 +73,8 @@
"symfony/yaml": "7.1.*", "symfony/yaml": "7.1.*",
"symfonycasts/verify-email-bundle": "*", "symfonycasts/verify-email-bundle": "*",
"twig/extra-bundle": "^2.12|^3.0", "twig/extra-bundle": "^2.12|^3.0",
"twig/twig": "^2.12|^3.0" "twig/twig": "^2.12|^3.0",
"ext-simplexml": "*"
}, },
"config": { "config": {
"allow-plugins": { "allow-plugins": {

View File

@ -1,35 +1,68 @@
<?php <?php
namespace App\Service\Connector; namespace App\Service\Connector;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use App\Entity\Domain; use App\Entity\Domain;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class NamecheapConnector implements ConnectorInterface readonly class NamecheapConnector implements ConnectorInterface
{ {
public const BASE_URL = 'https://api.namecheap.com/xml.response';
const BASE_URL = 'https://api.namecheap.com/xml.response'; public const SANDBOX_BASE_URL = 'http://api.sandbox.namecheap.com/xml.response';
const SANDBOX_BASE_URL = 'http://www.sandbox.namecheap.com';
public function __construct(private array $authData, private HttpClientInterface $client) public function __construct(private array $authData, private HttpClientInterface $client)
{} {
}
public function orderDomain(Domain $domain, $dryRun): void public function orderDomain(Domain $domain, $dryRun): void
{} {
$addresses = $this->call('namecheap.users.address.getList');
private function call(string $command, array $parameters, array $authData = null): object 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]);
$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
{ {
if (is_null($authData)) { if (is_null($authData)) {
$authData = $this->authData; $authData = $this->authData;
} }
$actualParams = array_merge([ $actualParams = array_merge([
'Command' => $command,
'ApiUser' => $authData['ApiUser'], 'ApiUser' => $authData['ApiUser'],
'ApiKey' => $authData['ApiKey'] 'ApiKey' => $authData['ApiKey'],
'ClientIp' => '', // TODO DW instance IP envvar
], $parameters); ], $parameters);
$response = $this->client->request('POST', BASE_URL, [ $response = $this->client->request('POST', self::BASE_URL, [
'query' => $actualParams 'query' => $actualParams,
]); ]);
$data = new \SimpleXMLElement($response->getContent()); $data = new \SimpleXMLElement($response->getContent());
@ -45,4 +78,3 @@ class NamecheapConnector implements ConnectorInterface
{ {
} }
} }