From 891517eb2a30dde5e26c13dfd313f4db121b6e3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matou=C5=A1=20Dzivjak?= Date: Sun, 27 Apr 2025 14:54:38 +0200 Subject: [PATCH] feat: add support for api keys API keys are an easier authentication approach for integrations that don't act as clients, authorizing other users and acting on their behalf. Add support for API keys and a small example of their usage. --- .github/workflows/notification.yaml | 21 --------------- composer.json | 3 +++ examples/simple.php | 13 +++++++++ .../Application/ApplicationConfiguration.php | 27 ++++++++++++++++--- .../ApplicationConfigurationInterface.php | 7 +++++ src/SumUp/Services/Authorization.php | 10 +++++++ 6 files changed, 56 insertions(+), 25 deletions(-) delete mode 100644 .github/workflows/notification.yaml create mode 100644 examples/simple.php diff --git a/.github/workflows/notification.yaml b/.github/workflows/notification.yaml deleted file mode 100644 index 94ef458..0000000 --- a/.github/workflows/notification.yaml +++ /dev/null @@ -1,21 +0,0 @@ -on: - release: - types: [published] - -permissions: - contents: read - -jobs: - notify: - name: Notify - runs-on: ubuntu-latest - steps: - - name: Send release notification - uses: slackapi/slack-github-action@485a9d42d3a73031f12ec201c457e2162c45d02d # v2.0.0 - with: - webhook: ${{ secrets.SLACK_WEBHOOK_URL }} - webhook-type: webhook-trigger - payload: | - repo: "${{ github.repository }}" - url: "${{ github.event.release.html_url }}" - version: "${{ github.event.release.tag_name }}" diff --git a/composer.json b/composer.json index c61cf6c..bd0d6f3 100644 --- a/composer.json +++ b/composer.json @@ -20,6 +20,9 @@ "homepage": "https://github.com/sumup" } ], + "scripts": { + "cs": "php-cs-fixer fix --dry-run --diff" + }, "require": { "php": "^5.6|^7.0|^8.0" }, diff --git a/examples/simple.php b/examples/simple.php new file mode 100644 index 0000000..631efba --- /dev/null +++ b/examples/simple.php @@ -0,0 +1,13 @@ + getenv('SUMUP_API_KEY'), +]); + +$dba = $sumup->getMerchantService()->getDoingBusinessAs(); +print_r($dba->getBody()); diff --git a/src/SumUp/Application/ApplicationConfiguration.php b/src/SumUp/Application/ApplicationConfiguration.php index 3bd2f36..40a0701 100644 --- a/src/SumUp/Application/ApplicationConfiguration.php +++ b/src/SumUp/Application/ApplicationConfiguration.php @@ -105,6 +105,13 @@ class ApplicationConfiguration implements ApplicationConfigurationInterface */ protected $customHeaders; + /** + * The API key for authentication. + * + * @var string|null + */ + protected $apiKey; + /** * Create a new application configuration. * @@ -115,6 +122,7 @@ class ApplicationConfiguration implements ApplicationConfigurationInterface public function __construct(array $config = []) { $config = array_merge([ + 'api_key' => null, 'app_id' => null, 'app_secret' => null, 'grant_type' => 'authorization_code', @@ -129,6 +137,7 @@ public function __construct(array $config = []) 'custom_headers' => [] ], $config); + $this->apiKey = $config['api_key']; $this->setAppId($config['app_id']); $this->setAppSecret($config['app_secret']); $this->setScopes($config['scopes']); @@ -273,6 +282,16 @@ public function getCustomHeaders() return $this->customHeaders; } + /** + * Returns the API key if set. + * + * @return string|null + */ + public function getApiKey() + { + return $this->apiKey; + } + /** * Set application ID. * @@ -282,8 +301,8 @@ public function getCustomHeaders() */ protected function setAppId($appId) { - if (empty($appId)) { - throw new SumUpConfigurationException('Missing mandatory parameter app_id'); + if (empty($appId) && empty($this->apiKey)) { + throw new SumUpConfigurationException('Missing mandatory parameter app_id or api_key'); } $this->appId = $appId; } @@ -297,8 +316,8 @@ protected function setAppId($appId) */ protected function setAppSecret($appSecret) { - if (empty($appSecret)) { - throw new SumUpConfigurationException('Missing mandatory parameter app_secret'); + if (empty($appSecret) && empty($this->apiKey)) { + throw new SumUpConfigurationException('Missing mandatory parameter app_secret or api_key'); } $this->appSecret = $appSecret; } diff --git a/src/SumUp/Application/ApplicationConfigurationInterface.php b/src/SumUp/Application/ApplicationConfigurationInterface.php index d66d03d..2be4592 100644 --- a/src/SumUp/Application/ApplicationConfigurationInterface.php +++ b/src/SumUp/Application/ApplicationConfigurationInterface.php @@ -92,4 +92,11 @@ public function getForceGuzzle(); * @return array */ public function getCustomHeaders(); + + /** + * Returns the API key if set. + * + * @return string|null + */ + public function getApiKey(); } diff --git a/src/SumUp/Services/Authorization.php b/src/SumUp/Services/Authorization.php index 9e301d7..e6d5053 100644 --- a/src/SumUp/Services/Authorization.php +++ b/src/SumUp/Services/Authorization.php @@ -57,6 +57,16 @@ public function __construct(SumUpHttpClientInterface $client, ApplicationConfigu */ public function getToken() { + if (!empty($this->appConfig->getApiKey())) { + return new AccessToken( + $this->appConfig->getApiKey(), + 'Bearer', + null, + [], + null + ); + } + $accessToken = null; if (!empty($this->appConfig->getAccessToken())) { $accessToken = new AccessToken(