Skip to content

Commit

Permalink
refactor: oauth
Browse files Browse the repository at this point in the history
  • Loading branch information
martenb committed Jan 25, 2024
1 parent d1f9d69 commit f6b78a2
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Shoptet SDK
Simple Shoptet SDK.

## Installation
```shell
composer require adbros/shoptet-sdk
```

## Usage

```php
<?php declare(strict_types = 1);

use Adbros\Shoptet\OAuth;
use Adbros\Shoptet\Sdk;

// Initialize OAuth
$oAuth = new OAuth(
clientId: getenv('CLIENT_ID'),
clientSecret: getenv('CLIENT_SECRET'),
eshopUrl: getenv('ESHOP_URL'),
);

// Get oAuthAccessToken in installation process
$oAuthAccessToken = $oAuth->getOAuthAccessToken(
code: $_GET['code'],
redirectUri: 'https://example.com/install',
);

// Get apiAccessToken before API call
$apiAccessToken = $oAuth->getApiAccessToken(
oAuthAccessToken: $oAuthAccessToken->accessToken,
);

// Initialize SDK
$sdk = new Sdk(
apiAccessToken: $apiAccessToken->accessToken,
);

// Get customers
$page = 1;

do {
$customers = $sdk->getCustomers($page);

foreach ($customers->customers as $customer) {
// Get customer
$customerDetail = $sdk->getCustomer($customer->guid);
}
} while ($page++ < $customers->paginator->pageCount);
```
10 changes: 8 additions & 2 deletions src/OAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@ class OAuth

private Client $httpClient;

private readonly string $oAuthAccessTokenUrl;

private readonly string $apiAccessTokenUrl;

public function __construct(
private readonly string $clientId,
private readonly string $clientSecret,
private readonly string $oAuthAccessTokenUrl,
private readonly string $apiAccessTokenUrl,
private readonly string $eshopUrl,
)
{
$this->httpClient = new Client();

$this->oAuthAccessTokenUrl = rtrim($this->eshopUrl, '/') . '/action/ApiOAuthServer/token';
$this->apiAccessTokenUrl = rtrim($this->eshopUrl, '/') . '/action/ApiOAuthServer/getAccessToken';
}

public function getOAuthAccessToken(string $code, string $redirectUri): OAuthAccessToken // @phpcs:ignore Generic.NamingConventions.CamelCapsFunctionName.ScopeNotCamelCaps
Expand Down

0 comments on commit f6b78a2

Please sign in to comment.