Skip to content

Commit 4ed5d07

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

File tree

7 files changed

+98
-226
lines changed

7 files changed

+98
-226
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: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
"require": {
1717
"php": ">=7.3",
1818
"ext-json": "*",
19-
"ext-curl": "*"
19+
"php-http/discovery": "^1.19",
20+
"psr/http-client": "^1.0",
21+
"psr/http-factory": "^1.0",
22+
"psr/http-message": "^1.0 || ^2.0"
2023
},
2124
"require-dev": {
2225
"phpunit/phpunit": "^10.0 || ^9.0",
@@ -25,7 +28,8 @@
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,10 @@
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+
"config": {
54+
"allow-plugins": {
55+
"php-http/discovery": true
56+
}
4857
}
4958
}

src/Client.php

Lines changed: 12 additions & 3 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
/**

src/PaytrailClient.php

Lines changed: 31 additions & 17 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,
@@ -124,13 +137,14 @@ protected function post(
124137
/**
125138
* A wrapper for get requests.
126139
*
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.
140+
* @param string $uri The uri for the request.
141+
* @param callable|null $callback The callback method to run for the decoded response.
142+
* If left empty, the response is returned.
143+
* @param string|null $transactionId Paytrail transaction ID when accessing single transaction.
144+
* Not required for a new payment request.
132145
*
133146
* @return mixed
147+
* @throws ClientException
134148
* @throws HmacException
135149
*/
136150
protected function get(string $uri, callable $callback = null, string $transactionId = null)

src/Response/CurlResponse.php

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

src/Util/CurlClient.php

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

0 commit comments

Comments
 (0)