Skip to content

Commit

Permalink
Implement domain management functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
sweep-ai[bot] authored Aug 23, 2024
1 parent 697ae16 commit 2f13003
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/Models/HostingAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ public function isActive()
{
return $this->status === 'active';
}

public function hasDomain()
{
return !empty($this->domain);
}
}
10 changes: 9 additions & 1 deletion app/Models/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ class Subscription extends Model
'end_date',
'renewal_period',
'status',
'domain_name',
'domain_registrar',
'domain_expiration_date',
];

protected $dates = ['start_date', 'end_date'];
protected $dates = ['start_date', 'end_date', 'domain_expiration_date'];

public function customer()
{
Expand All @@ -41,4 +44,9 @@ public function isActive()
{
return $this->status === 'active' && $this->end_date->isFuture();
}

public function isDomainActive()
{
return $this->domain_name && $this->domain_expiration_date && $this->domain_expiration_date->isFuture();
}
}
88 changes: 88 additions & 0 deletions app/Services/DomainService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace App\Services;

use App\Services\Registrars\EnomClient;
use App\Services\Registrars\ResellerClubClient;
use App\Models\Subscription;
use App\Models\HostingAccount;

class DomainService
{
protected $enomClient;
protected $resellerClubClient;

public function __construct(EnomClient $enomClient, ResellerClubClient $resellerClubClient)
{
$this->enomClient = $enomClient;
$this->resellerClubClient = $resellerClubClient;
}

public function registerDomain(Subscription $subscription, $domainName, $registrar = 'enom')
{
$client = $this->getClientForRegistrar($registrar);
$result = $client->registerDomain($domainName, $subscription->customer->id);

if ($result) {
$subscription->domain_name = $domainName;
$subscription->domain_registrar = $registrar;
$subscription->domain_expiration_date = $result['expiration_date'];
$subscription->save();

// Update or create HostingAccount
$hostingAccount = HostingAccount::updateOrCreate(
['subscription_id' => $subscription->id],
['domain' => $domainName]
);
}

return $result;
}

public function renewDomain(Subscription $subscription, $period = 1)
{
$client = $this->getClientForRegistrar($subscription->domain_registrar);
$result = $client->renewDomain($subscription->domain_name, $period);

if ($result) {
$subscription->domain_expiration_date = $result['new_expiration_date'];
$subscription->save();
}

return $result;
}

public function transferDomain(Subscription $subscription, $domainName, $authCode, $newRegistrar)
{
$client = $this->getClientForRegistrar($newRegistrar);
$result = $client->transferDomain($domainName, $authCode, $subscription->customer->id);

if ($result) {
$subscription->domain_name = $domainName;
$subscription->domain_registrar = $newRegistrar;
$subscription->domain_expiration_date = $result['expiration_date'];
$subscription->save();

// Update HostingAccount
$hostingAccount = HostingAccount::where('subscription_id', $subscription->id)->first();
if ($hostingAccount) {
$hostingAccount->domain = $domainName;
$hostingAccount->save();
}
}

return $result;
}

protected function getClientForRegistrar($registrar)
{
switch ($registrar) {
case 'enom':
return $this->enomClient;
case 'resellerclub':
return $this->resellerClubClient;
default:
throw new \Exception("Unsupported domain registrar: $registrar");
}
}
}
55 changes: 55 additions & 0 deletions app/Services/Registrars/EnomClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace App\Services\Registrars;

use GuzzleHttp\Client;

class EnomClient
{
protected $client;
protected $apiUrl;
protected $username;
protected $password;

public function __construct()
{
$this->client = new Client();
$this->apiUrl = config('services.enom.api_url');
$this->username = config('services.enom.username');
$this->password = config('services.enom.password');
}

public function registerDomain($domainName, $customerId)
{
// Implement eNom API call to register domain
// Return result with expiration date
}

public function renewDomain($domainName, $period)
{
// Implement eNom API call to renew domain
// Return result with new expiration date
}

public function transferDomain($domainName, $authCode, $customerId)
{
// Implement eNom API call to transfer domain
// Return result with expiration date
}

protected function makeApiCall($command, $params)
{
$params = array_merge([
'command' => $command,
'uid' => $this->username,
'pw' => $this->password,
'responsetype' => 'xml',
], $params);

$response = $this->client->get($this->apiUrl, [
'query' => $params,
]);

// Parse XML response and return result
}
}
53 changes: 53 additions & 0 deletions app/Services/Registrars/ResellerClubClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace App\Services\Registrars;

use GuzzleHttp\Client;

class ResellerClubClient
{
protected $client;
protected $apiUrl;
protected $authUserId;
protected $apiKey;

public function __construct()
{
$this->client = new Client();
$this->apiUrl = config('services.resellerclub.api_url');
$this->authUserId = config('services.resellerclub.auth_userid');
$this->apiKey = config('services.resellerclub.api_key');
}

public function registerDomain($domainName, $customerId)
{
// Implement ResellerClub API call to register domain
// Return result with expiration date
}

public function renewDomain($domainName, $period)
{
// Implement ResellerClub API call to renew domain
// Return result with new expiration date
}

public function transferDomain($domainName, $authCode, $customerId)
{
// Implement ResellerClub API call to transfer domain
// Return result with expiration date
}

protected function makeApiCall($action, $params)
{
$params = array_merge([
'auth-userid' => $this->authUserId,
'api-key' => $this->apiKey,
], $params);

$response = $this->client->post($this->apiUrl . $action, [
'form_params' => $params,
]);

// Parse JSON response and return result
}
}

0 comments on commit 2f13003

Please sign in to comment.