diff --git a/README.md b/README.md index dc6217b..6ba47ff 100644 --- a/README.md +++ b/README.md @@ -165,6 +165,12 @@ public function createDocument(array $parameters) public function createDocumentDownloadUrl(string $identifier, CarbonInterface $expiresAt = NULL) ``` +### Oauth + +```php +public function retrievingToken(string $grantType, array $attributes) +``` + ### PaymentIntents ```php diff --git a/src/GhostZero/BitinflowAccounts/BitinflowAccounts.php b/src/GhostZero/BitinflowAccounts/BitinflowAccounts.php index 4954378..435294f 100644 --- a/src/GhostZero/BitinflowAccounts/BitinflowAccounts.php +++ b/src/GhostZero/BitinflowAccounts/BitinflowAccounts.php @@ -20,6 +20,7 @@ class BitinflowAccounts use Traits\ChargesTrait; use Traits\DocumentsTrait; + use Traits\OauthTrait; use Traits\PaymentIntentsTrait; use Traits\SshKeysTrait; use Traits\UsersTrait; diff --git a/src/GhostZero/BitinflowAccounts/Traits/OauthTrait.php b/src/GhostZero/BitinflowAccounts/Traits/OauthTrait.php new file mode 100644 index 0000000..11c1904 --- /dev/null +++ b/src/GhostZero/BitinflowAccounts/Traits/OauthTrait.php @@ -0,0 +1,44 @@ + + */ +trait OauthTrait +{ + + /** + * Retrieving a oauth token using a given grant type. + * + * @param string $grantType + * @param array $attributes + * + * @return Result + */ + public function retrievingToken(string $grantType, array $attributes): Result + { + try { + $response = $this->client->request('POST', '/oauth/token', $attributes + [ + 'form_params' => [ + 'grant_type' => $grantType, + 'client_id' => $this->getClientId(), + 'client_secret' => $this->getClientSecret(), + ], + ]); + + $result = new Result($response, null); + } catch (RequestException $exception) { + $result = new Result($exception->getResponse(), $exception); + } + + $result->bitinflow = $this; + + return $result; + } +} \ No newline at end of file diff --git a/tests/GhostZero/BitinflowAccounts/ApiOauthTest.php b/tests/GhostZero/BitinflowAccounts/ApiOauthTest.php new file mode 100644 index 0000000..f6a102d --- /dev/null +++ b/tests/GhostZero/BitinflowAccounts/ApiOauthTest.php @@ -0,0 +1,25 @@ + + */ +class ApiOauthTest extends ApiTestCase +{ + + public function testGetOauthToken(): void + { + $this->getClient()->withClientId('5'); + $this->getClient()->withClientSecret('jejmtAJJWeEesW1siWwojjLn6zW9AIcWH1wqfFPq'); + $this->registerResult($result = $this->getClient()->retrievingToken('client_credentials', [ + 'scope' => '', + ])); + $this->assertTrue($result->success()); + $this->assertNotEmpty($result->data()->access_token); + } +} \ No newline at end of file