Skip to content

Commit cef036a

Browse files
committed
WIP: Use PHP-HTTP Discovery, PSR-7, PSR-17 & PSR-18
1 parent 37ed9c6 commit cef036a

File tree

7 files changed

+190
-256
lines changed

7 files changed

+190
-256
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.idea
12
test-reports/
3+
vendor/
24
composer.lock
35
*.cache

composer.json

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,20 @@
1616
"require": {
1717
"php": ">=7.3",
1818
"ext-json": "*",
19-
"ext-curl": "*"
19+
"psr/http-client": "^1.0",
20+
"psr/http-factory": "^1.0",
21+
"psr/http-message": "^1.0 || ^2.0"
2022
},
2123
"require-dev": {
2224
"phpunit/phpunit": "^10.0 || ^9.0",
2325
"squizlabs/php_codesniffer": "^3.6",
26+
"php-http/discovery": "^1.19",
2427
"phpcompatibility/php-compatibility": "^9.3",
2528
"phpmd/phpmd": "^2.10",
2629
"phpstan/phpstan": "1.9.14",
2730
"mockery/mockery": "^1.5",
28-
"guzzlehttp/guzzle": "^7.0|^6.0"
31+
"guzzlehttp/guzzle": "^7.0|^6.0",
32+
"roave/security-advisories": "dev-latest"
2933
},
3034
"autoload": {
3135
"psr-4": {
@@ -45,5 +49,13 @@
4549
"\"vendor/bin/phpmd\" src,tests text unusedcode || RETVAL=1",
4650
"\"vendor/bin/phpcs\" src tests --extensions=\"php\" --encoding=\"UTF-8\" --standard=\"PSR12\""
4751
]
52+
},
53+
"suggest": {
54+
"php-http/discovery": "Automatic discovery for existing PSR-7, PSR-17 & PSR-18 functionalities"
55+
},
56+
"config": {
57+
"allow-plugins": {
58+
"php-http/discovery": true
59+
}
4860
}
4961
}

src/Client.php

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
use Paytrail\SDK\Exception\RequestException;
4040
use Paytrail\SDK\Exception\ClientException;
4141
use Paytrail\SDK\Response\AddCardPaymentResponse;
42+
use Psr\Http\Client\ClientInterface;
43+
use Psr\Http\Message\RequestFactoryInterface;
4244

4345
/**
4446
* Class Client
@@ -65,14 +67,21 @@ class Client extends PaytrailClient
6567
* @param int $merchantId The merchant.
6668
* @param string $secretKey The secret key.
6769
* @param string $platformName Platform name.
70+
* @param ClientInterface|null $client HTTP client
71+
* @param RequestFactoryInterface|null $requestFactory HTTP request factory
6872
*/
69-
public function __construct(int $merchantId, string $secretKey, string $platformName)
70-
{
73+
public function __construct(
74+
int $merchantId,
75+
string $secretKey,
76+
string $platformName,
77+
?ClientInterface $client = null,
78+
?RequestFactoryInterface $requestFactory = null
79+
) {
7180
$this->merchantId = $merchantId;
7281
$this->secretKey = $secretKey;
7382
$this->platformName = $platformName;
7483

75-
$this->createHttpClient();
84+
$this->createHttpClient($client, $requestFactory);
7685
}
7786

7887
/**
@@ -94,18 +103,21 @@ public function getPaymentProviders(int $amount = null): array
94103

95104
// Sign the request.
96105
$headers['signature'] = $mac;
97-
$request_params = [
98-
'headers' => $headers,
99-
];
106+
$queryParams = [];
100107

101108
// Set the amount query parameter.
102109
if ($amount !== null) {
103-
$request_params['query'] = [
110+
$queryParams = [
104111
'amount' => $amount
105112
];
106113
}
107114

108-
$response = $this->http_client->request('GET', $uri, $request_params);
115+
$response = $this->http_client->request(
116+
'GET',
117+
$uri,
118+
$queryParams,
119+
$headers
120+
);
109121
$body = (string)$response->getBody();
110122

111123
// Validate the signature.
@@ -144,23 +156,25 @@ public function getGroupedPaymentProviders(int $amount = null, string $locale =
144156

145157
// Sign the request.
146158
$headers['signature'] = $mac;
147-
$request_params = [
148-
'headers' => $headers,
149-
'query' => [
150-
'language' => $locale
151-
]
159+
$queryParams = [
160+
'language' => $locale
152161
];
153162

154163
// Set the amount query parameter.
155164
if (null !== $amount) {
156-
$request_params['query']['amount'] = $amount;
165+
$queryParams['amount'] = $amount;
157166
}
158167

159168
if (!empty($groups)) {
160-
$request_params['query']['groups'] = join(',', $groups);
169+
$queryParams['groups'] = join(',', $groups);
161170
}
162171

163-
$response = $this->http_client->request('GET', $uri, $request_params);
172+
$response = $this->http_client->request(
173+
'GET',
174+
$uri,
175+
$queryParams,
176+
$headers
177+
);
164178
$body = (string)$response->getBody();
165179

166180
// Validate the signature.

src/PaytrailClient.php

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44

55
namespace Paytrail\SDK;
66

7+
use JsonSerializable;
8+
use Paytrail\SDK\Exception\ClientException;
79
use Paytrail\SDK\Exception\HmacException;
810
use Paytrail\SDK\Exception\ValidationException;
911
use Paytrail\SDK\Util\RequestClient;
1012
use Paytrail\SDK\Util\Signature;
13+
use Psr\Http\Client\ClientInterface;
14+
use Psr\Http\Message\RequestFactoryInterface;
1115

1216
abstract class PaytrailClient
1317
{
@@ -39,9 +43,17 @@ abstract class PaytrailClient
3943
*/
4044
protected $http_client;
4145

42-
protected function createHttpClient()
43-
{
44-
$this->http_client = new RequestClient();
46+
/**
47+
* @param ClientInterface|null $client
48+
* @param RequestFactoryInterface|null $requestFactory
49+
*
50+
* @return void
51+
*/
52+
protected function createHttpClient(
53+
?ClientInterface $client = null,
54+
?RequestFactoryInterface $requestFactory = null
55+
): void {
56+
$this->http_client = new RequestClient($client, $requestFactory);
4557
}
4658

4759
/**
@@ -59,21 +71,22 @@ abstract public function validateHmac(array $response = [], string $body = '', s
5971
/**
6072
* A wrapper for post requests.
6173
*
62-
* @param string $uri The uri for the request.
63-
* @param \JsonSerializable|null $data The request payload.
64-
* @param callable|null $callback The callback method to run for the decoded response.
65-
* If left empty, the response is returned.
66-
* @param string|null $transactionId Paytrail transaction ID when accessing single transaction.
67-
* Not required for a new payment request.
68-
* @param bool $signatureInHeader Checks if signature is calculated from header/body parameters
69-
* @param string|null $paytrailTokenizationId Paytrail tokenization ID for getToken request
74+
* @param string $uri The uri for the request.
75+
* @param JsonSerializable|null $data The request payload.
76+
* @param callable|null $callback The callback method to run for the decoded response.
77+
* If left empty, the response is returned.
78+
* @param string|null $transactionId Paytrail transaction ID when accessing single transaction.
79+
* Not required for a new payment request.
80+
* @param bool $signatureInHeader Checks if signature is calculated from header/body parameters
81+
* @param string|null $paytrailTokenizationId Paytrail tokenization ID for getToken request
7082
*
7183
* @return mixed
7284
* @throws HmacException
85+
* @throws ClientException
7386
*/
7487
protected function post(
7588
string $uri,
76-
\JsonSerializable $data = null,
89+
JsonSerializable $data = null,
7790
callable $callback = null,
7891
string $transactionId = null,
7992
bool $signatureInHeader = true,
@@ -86,11 +99,15 @@ protected function post(
8699
$mac = $this->calculateHmac($headers, $body);
87100
$headers['signature'] = $mac;
88101

89-
$response = $this->http_client->request('POST', $uri, [
90-
'headers' => $headers,
91-
'body' => $body,
92-
'allow_redirects' => false
93-
]);
102+
$response = $this->http_client->request(
103+
'POST',
104+
$uri,
105+
[
106+
'allow_redirects' => false
107+
],
108+
$headers,
109+
$body
110+
);
94111

95112
$body = (string)$response->getBody();
96113

@@ -105,10 +122,15 @@ protected function post(
105122
// @phpstan-ignore-next-line FIXME
106123
$body = json_encode($data->toArray(), JSON_UNESCAPED_SLASHES);
107124

108-
$response = $this->http_client->request('POST', $uri, [
109-
'body' => $body,
110-
'allow_redirects' => false
111-
], true);
125+
$response = $this->http_client->request(
126+
'POST',
127+
$uri,
128+
[
129+
'allow_redirects' => false
130+
],
131+
[],
132+
$body
133+
);
112134

113135
$body = (string)$response->getBody();
114136
}
@@ -124,13 +146,14 @@ protected function post(
124146
/**
125147
* A wrapper for get requests.
126148
*
127-
* @param string $uri The uri for the request.
128-
* @param callable|null $callback The callback method to run for the decoded response.
129-
* If left empty, the response is returned.
130-
* @param string|null $transactionId Paytrail transaction ID when accessing single transaction.
131-
* Not required for a new payment request.
149+
* @param string $uri The uri for the request.
150+
* @param callable|null $callback The callback method to run for the decoded response.
151+
* If left empty, the response is returned.
152+
* @param string|null $transactionId Paytrail transaction ID when accessing single transaction.
153+
* Not required for a new payment request.
132154
*
133155
* @return mixed
156+
* @throws ClientException
134157
* @throws HmacException
135158
*/
136159
protected function get(string $uri, callable $callback = null, string $transactionId = null)
@@ -140,9 +163,12 @@ protected function get(string $uri, callable $callback = null, string $transacti
140163

141164
$headers['signature'] = $mac;
142165

143-
$response = $this->http_client->request('GET', $uri, [
144-
'headers' => $headers
145-
]);
166+
$response = $this->http_client->request(
167+
'GET',
168+
$uri,
169+
[],
170+
$headers
171+
);
146172

147173
$body = (string)$response->getBody();
148174

src/Response/CurlResponse.php

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

0 commit comments

Comments
 (0)