Skip to content

Commit f5ef00a

Browse files
authored
feat: add support for api keys (#56)
1 parent 2af99fe commit f5ef00a

File tree

6 files changed

+56
-25
lines changed

6 files changed

+56
-25
lines changed

.github/workflows/notification.yaml

Lines changed: 0 additions & 21 deletions
This file was deleted.

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
"homepage": "https://github.com/sumup"
2121
}
2222
],
23+
"scripts": {
24+
"cs": "php-cs-fixer fix --dry-run --diff"
25+
},
2326
"require": {
2427
"php": "^5.6|^7.0|^8.0"
2528
},

examples/simple.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
/*
4+
* Run with `php simple.php`, don't forget to set SUMUP_API_KEY anv variable with your API key
5+
*/
6+
7+
require __DIR__ . '/../vendor/autoload.php';
8+
$sumup = new \SumUp\SumUp([
9+
'api_key' => getenv('SUMUP_API_KEY'),
10+
]);
11+
12+
$dba = $sumup->getMerchantService()->getDoingBusinessAs();
13+
print_r($dba->getBody());

src/SumUp/Application/ApplicationConfiguration.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ class ApplicationConfiguration implements ApplicationConfigurationInterface
105105
*/
106106
protected $customHeaders;
107107

108+
/**
109+
* The API key for authentication.
110+
*
111+
* @var string|null
112+
*/
113+
protected $apiKey;
114+
108115
/**
109116
* Create a new application configuration.
110117
*
@@ -115,6 +122,7 @@ class ApplicationConfiguration implements ApplicationConfigurationInterface
115122
public function __construct(array $config = [])
116123
{
117124
$config = array_merge([
125+
'api_key' => null,
118126
'app_id' => null,
119127
'app_secret' => null,
120128
'grant_type' => 'authorization_code',
@@ -129,6 +137,7 @@ public function __construct(array $config = [])
129137
'custom_headers' => []
130138
], $config);
131139

140+
$this->apiKey = $config['api_key'];
132141
$this->setAppId($config['app_id']);
133142
$this->setAppSecret($config['app_secret']);
134143
$this->setScopes($config['scopes']);
@@ -273,6 +282,16 @@ public function getCustomHeaders()
273282
return $this->customHeaders;
274283
}
275284

285+
/**
286+
* Returns the API key if set.
287+
*
288+
* @return string|null
289+
*/
290+
public function getApiKey()
291+
{
292+
return $this->apiKey;
293+
}
294+
276295
/**
277296
* Set application ID.
278297
*
@@ -282,8 +301,8 @@ public function getCustomHeaders()
282301
*/
283302
protected function setAppId($appId)
284303
{
285-
if (empty($appId)) {
286-
throw new SumUpConfigurationException('Missing mandatory parameter app_id');
304+
if (empty($appId) && empty($this->apiKey)) {
305+
throw new SumUpConfigurationException('Missing mandatory parameter app_id or api_key');
287306
}
288307
$this->appId = $appId;
289308
}
@@ -297,8 +316,8 @@ protected function setAppId($appId)
297316
*/
298317
protected function setAppSecret($appSecret)
299318
{
300-
if (empty($appSecret)) {
301-
throw new SumUpConfigurationException('Missing mandatory parameter app_secret');
319+
if (empty($appSecret) && empty($this->apiKey)) {
320+
throw new SumUpConfigurationException('Missing mandatory parameter app_secret or api_key');
302321
}
303322
$this->appSecret = $appSecret;
304323
}

src/SumUp/Application/ApplicationConfigurationInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,11 @@ public function getForceGuzzle();
9292
* @return array
9393
*/
9494
public function getCustomHeaders();
95+
96+
/**
97+
* Returns the API key if set.
98+
*
99+
* @return string|null
100+
*/
101+
public function getApiKey();
95102
}

src/SumUp/Services/Authorization.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ public function __construct(SumUpHttpClientInterface $client, ApplicationConfigu
5757
*/
5858
public function getToken()
5959
{
60+
if (!empty($this->appConfig->getApiKey())) {
61+
return new AccessToken(
62+
$this->appConfig->getApiKey(),
63+
'Bearer',
64+
null,
65+
[],
66+
null
67+
);
68+
}
69+
6070
$accessToken = null;
6171
if (!empty($this->appConfig->getAccessToken())) {
6272
$accessToken = new AccessToken(

0 commit comments

Comments
 (0)