44
55namespace Paytrail \SDK ;
66
7+ use JsonSerializable ;
8+ use Paytrail \SDK \Exception \ClientException ;
79use Paytrail \SDK \Exception \HmacException ;
810use Paytrail \SDK \Exception \ValidationException ;
911use Paytrail \SDK \Util \RequestClient ;
1012use Paytrail \SDK \Util \Signature ;
13+ use Psr \Http \Client \ClientInterface ;
14+ use Psr \Http \Message \RequestFactoryInterface ;
1115
1216abstract 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
0 commit comments