22
33namespace SettleApi ;
44
5+ use DateTime ;
6+ use DateTimeZone ;
7+
58/**
69 * Class SettleApiClient
710 * @package SettleApi
@@ -14,6 +17,7 @@ class SettleApiClient
1417 protected string $ publicKey ;
1518 protected string $ privateKey ;
1619 protected bool $ isSandbox ;
20+ protected bool $ validateShapes = true ;
1721
1822 const BASE_URL_PRODUCTION = 'https://api.settle.eu/merchant/v1/ ' ;
1923 const BASE_URL_SANDBOX = 'https://api.sandbox.settle.eu/merchant/v1/ ' ;
@@ -69,15 +73,37 @@ public function setIsSandbox($isSandbox)
6973 $ this ->isSandbox = $ isSandbox ;
7074 }
7175
76+ /**
77+ * @return bool
78+ */
79+ public function getValidateShapes ()
80+ {
81+ return $ this ->validateShapes ;
82+ }
83+
84+ /**
85+ * @param $validateShapes
86+ */
87+ public function setValidateShapes ($ validateShapes )
88+ {
89+ $ this ->validateShapes = $ validateShapes ;
90+ }
91+
7292 /**
7393 * @param string $method
7494 * @param string $path
7595 * @param array $postFields
96+ * @param array $shape
7697 * @return array|bool
7798 * @throws SettleApiException
7899 */
79- public function call (string $ method , string $ path , array $ postFields = [])
100+ public function call (string $ method , string $ path , array $ postFields = [], $ shape = [] )
80101 {
102+ if ($ this ->validateShapes && !empty ($ shape )) {
103+ $ validator = new ShapeValidator ($ shape , $ postFields );
104+ $ validator ->validate ();
105+ }
106+
81107 $ method = strtoupper ($ method );
82108 $ path = ltrim ($ path , '/ ' );
83109 $ url = $ this ->getApiBaseUrl () . $ path ;
@@ -111,12 +137,17 @@ public function call(string $method, string $path, array $postFields = [])
111137 case $ httpCode >= 200 && $ httpCode < 300 :
112138 return $ response ;
113139
140+ case $ httpCode == 404 :
141+ throw new SettleApiException ("The resource could not be found. " , $ httpCode );
142+
114143 default :
115144 if ($ response !== null ) {
116145 $ message = $ response ['error_description ' ] ?? $ response ['error_detail ' ] ?? $ response ['error_type ' ] ?? 'Something went wrong. ' ;
117146 throw new SettleApiException ($ message , $ httpCode );
118147 }
119- throw new SettleApiException ('Something went wrong. ' , $ httpCode );
148+
149+ $ errorMessage = !empty ($ response_body ) ? $ response_body : 'Something went wrong. ' ;
150+ throw new SettleApiException ($ errorMessage , $ httpCode );
120151 }
121152 }
122153
@@ -146,10 +177,11 @@ protected function getHeaders(string $method, string $url, array $postFields): a
146177 'Content-Length: ' . strlen ($ content ),
147178 ];
148179
180+ $ date = (new DateTime ())->setTimezone (new DateTimeZone ('UTC ' ));
149181 $ settle_headers = [
150182 'x-settle-merchant ' => $ this ->merchantId ,
151183 'x-settle-user ' => $ this ->userId ,
152- 'x-settle-timestamp ' => date ('Y-m-d H:i:s ' ),
184+ 'x-settle-timestamp ' => $ date-> format ('Y-m-d H:i:s ' ),
153185 'x-settle-content-digest ' => 'SHA256= ' . $ content_digest ,
154186 ];
155187
0 commit comments