Skip to content

Commit

Permalink
Merge pull request #1 from marketgoo/main
Browse files Browse the repository at this point in the history
changePackage() implemented for marketgoo
  • Loading branch information
uphlewis authored Sep 29, 2023
2 parents 9ae5118 + 847a43c commit e21f77b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
48 changes: 37 additions & 11 deletions src/Providers/Marketgoo/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
use Illuminate\Support\Str;
use Upmind\ProvisionBase\Helper;
use Upmind\ProvisionProviders\Seo\Category;
use Upmind\ProvisionBase\Provider\Contract\ProviderInterface;
use Upmind\ProvisionBase\Provider\DataSet\AboutData;
Expand Down Expand Up @@ -62,14 +61,24 @@ public function create(CreateParams $params): CreateResult
->setMessage('Account created');
}

public function changePackage(ChangePackageParams $params): EmptyResult
public function login(AccountIdentifierParams $params): LoginResult
{
throw $this->errorResult('Operation not supported');
return LoginResult::create()->setUrl($this->getLoginUrl($params->username));
}

public function login(AccountIdentifierParams $params): LoginResult
public function changePackage(ChangePackageParams $params): EmptyResult
{
return LoginResult::create()->setUrl($this->getLoginUrl($params->username));
try {
$this->upgradeAccount($params->username, $params->package_identifier);
} catch (OperationFailed $e) {
if (Str::contains($e->getMessage(), 'product token')) {
return EmptyResult::create()->setMessage('Invalid product token');
}

throw $e;
}

return EmptyResult::create()->setMessage('Account updated');
}

public function suspend(AccountIdentifierParams $params): EmptyResult
Expand Down Expand Up @@ -124,6 +133,14 @@ protected function client(): Client
]);
}

protected function getLoginUrl(string $username): string
{
// get 5-minute ttl sso link
$response = $this->client()->get(sprintf('accounts/%s/login?expires=%s', $username, 5));
$handler = new LoginResponseHandler($response);
return $handler->getLoginUrl();
}

private function createAccount(
string $domainName,
string $productKey,
Expand All @@ -141,7 +158,6 @@ private function createAccount(
'name' => $name,
'email' => $email,
'promo' => $promoCode,
'password' => Helper::generateStrictPassword(15, true, true, true)
],
],
],
Expand All @@ -151,12 +167,22 @@ private function createAccount(
return $handler->getAccountIdentifier('create');
}

protected function getLoginUrl(string $username): string
private function upgradeAccount(string $accountId, string $productKey): void
{
// get 5-minute ttl sso link
$response = $this->client()->get(sprintf('accounts/%s/login?expires=%s', $username, 5));
$handler = new LoginResponseHandler($response);
return $handler->getLoginUrl();
$response = $this->client()->patch("accounts/{$accountId}/upgrade", [
RequestOptions::FORM_PARAMS => [
'data' => [
'type' => 'account',
'id' => $accountId,
'attributes' => [
'product' => $productKey,
'force' => true,
],
],
],
]);
$handler = new ResponseHandler($response);
$handler->assertSuccess('upgrade');
}

private function suspendAccount(string $accountId): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Upmind\ProvisionProviders\Seo\Providers\Marketgoo\ResponseHandlers;

use Psr\Http\Message\ResponseInterface;
use Upmind\ProvisionProviders\Seo\Providers\Marketgoo\Exceptions\CannotParseResponse;
use Upmind\ProvisionProviders\Seo\Providers\Marketgoo\Exceptions\OperationFailed;

Expand Down

0 comments on commit e21f77b

Please sign in to comment.