2024-07-28 23:05:41 +02:00
|
|
|
<?php
|
|
|
|
|
|
2024-07-29 15:28:05 +02:00
|
|
|
namespace App\Config\Connector;
|
2024-07-28 23:05:41 +02:00
|
|
|
|
2024-07-29 15:28:05 +02:00
|
|
|
use App\Entity\Domain;
|
2024-07-30 14:09:01 +02:00
|
|
|
use DateTime;
|
2024-07-29 03:27:55 +02:00
|
|
|
use Exception;
|
|
|
|
|
use Ovh\Api;
|
2024-07-28 23:05:41 +02:00
|
|
|
|
2024-07-29 15:28:05 +02:00
|
|
|
readonly class OvhConnector implements ConnectorInterface
|
2024-07-28 23:05:41 +02:00
|
|
|
{
|
2024-07-28 23:39:38 +02:00
|
|
|
|
2024-07-29 15:28:05 +02:00
|
|
|
public function __construct(private array $authData)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-29 03:27:55 +02:00
|
|
|
/**
|
2024-07-29 11:54:47 +02:00
|
|
|
* Order a domain name with the OVH API
|
2024-07-29 03:27:55 +02:00
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2024-07-29 11:54:47 +02:00
|
|
|
public function orderDomain(Domain $domain,
|
|
|
|
|
bool $acceptConditions,
|
|
|
|
|
bool $ownerLegalAge,
|
|
|
|
|
bool $waiveRetractationPeriod,
|
2024-07-30 14:09:01 +02:00
|
|
|
bool $dryRun = false
|
2024-07-29 11:54:47 +02:00
|
|
|
): void
|
2024-07-29 03:27:55 +02:00
|
|
|
{
|
2024-07-29 15:28:05 +02:00
|
|
|
if (!$domain->getDeleted()) throw new Exception('The domain name still appears in the WHOIS database');
|
2024-07-29 11:54:47 +02:00
|
|
|
|
2024-07-29 03:27:55 +02:00
|
|
|
$ldhName = $domain->getLdhName();
|
2024-07-29 15:28:05 +02:00
|
|
|
if (!$ldhName) throw new Exception("Domain name cannot be null");
|
2024-07-29 03:27:55 +02:00
|
|
|
|
2024-07-29 15:28:05 +02:00
|
|
|
$authData = self::verifyAuthData($this->authData);
|
2024-07-29 03:27:55 +02:00
|
|
|
|
|
|
|
|
$appKey = $authData['appKey'];
|
|
|
|
|
$appSecret = $authData['appSecret'];
|
|
|
|
|
$apiEndpoint = $authData['apiEndpoint'];
|
|
|
|
|
$consumerKey = $authData['consumerKey'];
|
|
|
|
|
$ovhSubsidiary = $authData['ovhSubsidiary'];
|
|
|
|
|
$pricingMode = $authData['pricingMode'];
|
|
|
|
|
|
|
|
|
|
$conn = new Api(
|
|
|
|
|
$appKey,
|
|
|
|
|
$appSecret,
|
|
|
|
|
$apiEndpoint,
|
|
|
|
|
$consumerKey
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$cart = $conn->post('/order/cart', [
|
|
|
|
|
"ovhSubsidiary" => $ovhSubsidiary,
|
|
|
|
|
"description" => "Domain Watchdog"
|
|
|
|
|
]);
|
|
|
|
|
$cartId = $cart['cartId'];
|
|
|
|
|
|
|
|
|
|
$offers = $conn->get("/order/cart/{$cartId}/domain", [
|
|
|
|
|
"domain" => $ldhName
|
|
|
|
|
]);
|
|
|
|
|
$offer = array_filter($offers, fn($offer) => $offer['action'] === 'create' &&
|
|
|
|
|
$offer['orderable'] === true &&
|
2024-07-29 16:13:46 +02:00
|
|
|
$offer['pricingMode'] === $pricingMode
|
2024-07-29 03:27:55 +02:00
|
|
|
);
|
2024-07-30 01:01:04 +02:00
|
|
|
if (empty($offer)) {
|
|
|
|
|
$conn->delete("/order/cart/{$cartId}");
|
|
|
|
|
throw new Exception('Cannot buy this domain name');
|
|
|
|
|
}
|
2024-07-29 03:27:55 +02:00
|
|
|
|
|
|
|
|
$item = $conn->post("/order/cart/{$cartId}/domain", [
|
|
|
|
|
"domain" => $ldhName,
|
|
|
|
|
"duration" => "P1Y"
|
|
|
|
|
]);
|
|
|
|
|
$itemId = $item['itemId'];
|
|
|
|
|
|
|
|
|
|
//$conn->get("/order/cart/{$cartId}/summary");
|
|
|
|
|
$conn->post("/order/cart/{$cartId}/assign");
|
|
|
|
|
$conn->get("/order/cart/{$cartId}/item/{$itemId}/requiredConfiguration");
|
|
|
|
|
|
|
|
|
|
$configuration = [
|
|
|
|
|
"ACCEPT_CONDITIONS" => $acceptConditions,
|
|
|
|
|
"OWNER_LEGAL_AGE" => $ownerLegalAge
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
foreach ($configuration as $label => $value) {
|
|
|
|
|
$conn->post("/order/cart/{$cartId}/item/{$itemId}/configuration", [
|
|
|
|
|
"cartId" => $cartId,
|
|
|
|
|
"itemId" => $itemId,
|
|
|
|
|
"label" => $label,
|
|
|
|
|
"value" => $value
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
$conn->get("/order/cart/{$cartId}/checkout");
|
2024-07-29 11:54:47 +02:00
|
|
|
|
2024-07-30 14:09:01 +02:00
|
|
|
if ($dryRun) return;
|
2024-07-29 03:27:55 +02:00
|
|
|
$conn->post("/order/cart/{$cartId}/checkout", [
|
|
|
|
|
"autoPayWithPreferredPaymentMethod" => true,
|
|
|
|
|
"waiveRetractationPeriod" => $waiveRetractationPeriod
|
|
|
|
|
]);
|
|
|
|
|
}
|
2024-07-29 15:28:05 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public static function verifyAuthData(array $authData): array
|
|
|
|
|
{
|
|
|
|
|
$appKey = $authData['appKey'];
|
|
|
|
|
$appSecret = $authData['appSecret'];
|
|
|
|
|
$apiEndpoint = $authData['apiEndpoint'];
|
|
|
|
|
$consumerKey = $authData['consumerKey'];
|
|
|
|
|
$ovhSubsidiary = $authData['ovhSubsidiary'];
|
|
|
|
|
$pricingMode = $authData['pricingMode'];
|
|
|
|
|
|
|
|
|
|
if (!is_string($appKey) || empty($appKey) ||
|
|
|
|
|
!is_string($appSecret) || empty($appSecret) ||
|
|
|
|
|
!is_string($consumerKey) || empty($consumerKey) ||
|
|
|
|
|
!is_string($apiEndpoint) || empty($apiEndpoint) ||
|
|
|
|
|
!is_string($ovhSubsidiary) || empty($ovhSubsidiary) ||
|
|
|
|
|
!is_string($pricingMode) || empty($pricingMode)
|
2024-07-30 14:09:01 +02:00
|
|
|
) throw new Exception("Bad authData schema");
|
|
|
|
|
|
|
|
|
|
$conn = new Api(
|
|
|
|
|
$appKey,
|
|
|
|
|
$appSecret,
|
|
|
|
|
$apiEndpoint,
|
|
|
|
|
$consumerKey
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$res = $conn->get('/auth/currentCredential');
|
|
|
|
|
if ($res['expiration'] !== null && new DateTime($res['expiration']) < new DateTime())
|
|
|
|
|
throw new Exception('These credentials have expired');
|
|
|
|
|
|
|
|
|
|
$status = $res['status'];
|
|
|
|
|
if ($status !== 'validated') throw new Exception("The status of these credentials is not valid ($status)");
|
2024-07-29 15:28:05 +02:00
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
"appKey" => $appKey,
|
|
|
|
|
"appSecret" => $appSecret,
|
|
|
|
|
"apiEndpoint" => $apiEndpoint,
|
|
|
|
|
"consumerKey" => $consumerKey,
|
|
|
|
|
"ovhSubsidiary" => $ovhSubsidiary,
|
|
|
|
|
"pricingMode" => $pricingMode
|
|
|
|
|
];
|
|
|
|
|
}
|
2024-07-28 23:05:41 +02:00
|
|
|
}
|