From 2a42fdc438540160071992ee2a33febf23d02d58 Mon Sep 17 00:00:00 2001 From: Cristiano Soares Date: Fri, 19 Apr 2019 06:49:01 -0300 Subject: [PATCH 1/3] =?UTF-8?q?Alterando=20para=20permitir=20atualiza?= =?UTF-8?q?=C3=A7=C3=A3o=20para=20novas=20vers=C3=B5es=20do=20guzzle=20Rem?= =?UTF-8?q?ovendo=20require-dev?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/composer.json b/composer.json index baea060..8e7fef3 100644 --- a/composer.json +++ b/composer.json @@ -11,11 +11,7 @@ ], "require": { "php": ">=5.4.0", - "guzzlehttp/guzzle": "6.0.*" - }, - "require-dev": { - "phpunit/phpunit": "~4.0", - "phpspec/phpspec": "~2.1" + "guzzlehttp/guzzle": "^6.0" }, "autoload": { "psr-0": { From 6bfd4121a1878696786efc94abd6325492731973 Mon Sep 17 00:00:00 2001 From: Cristiano Soares Date: Sat, 20 Apr 2019 04:29:20 -0300 Subject: [PATCH 2/3] =?UTF-8?q?Altera=C3=A7=C3=A3o=20realizadas=20a=20part?= =?UTF-8?q?ir=20de=20https://github.com/preetender/moip-assinaturas-php/co?= =?UTF-8?q?mmit/9c542f0317fc896074de59a3cd37bf278fb3bd5e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Subscription/Contracts/Renderable.php | 17 ++++++++++ src/Prettus/Moip/Subscription/MoipClient.php | 18 ++++++++-- .../Moip/Subscription/Resources/Customers.php | 11 +++--- .../Moip/Subscription/Resources/Invoices.php | 9 ++--- .../Moip/Subscription/Resources/Payments.php | 4 +-- .../Moip/Subscription/Resources/Plans.php | 17 +++++----- .../Subscription/Resources/Preferences.php | 5 ++- .../Subscription/Resources/Subscriptions.php | 21 +++++------- .../Subscription/Webservice/FormatInJson.php | 20 +++++++++++ .../Subscription/Webservice/Formatter.php | 24 +++++++++++++ .../Subscription/Webservice/ResourceUtils.php | 25 ++++++++++++++ .../Subscription/Webservice/Webservice.php | 34 +++++++++++++++++++ 12 files changed, 163 insertions(+), 42 deletions(-) create mode 100644 src/Prettus/Moip/Subscription/Contracts/Renderable.php create mode 100644 src/Prettus/Moip/Subscription/Webservice/FormatInJson.php create mode 100644 src/Prettus/Moip/Subscription/Webservice/Formatter.php create mode 100644 src/Prettus/Moip/Subscription/Webservice/ResourceUtils.php create mode 100644 src/Prettus/Moip/Subscription/Webservice/Webservice.php diff --git a/src/Prettus/Moip/Subscription/Contracts/Renderable.php b/src/Prettus/Moip/Subscription/Contracts/Renderable.php new file mode 100644 index 0000000..2800d5a --- /dev/null +++ b/src/Prettus/Moip/Subscription/Contracts/Renderable.php @@ -0,0 +1,17 @@ +setCredential(['token'=>$apiToken,'key'=>$apiKey]); $this->setEnvironment($environment); @@ -84,6 +91,7 @@ public function __construct( $apiToken, $apiKey, $environment = MoipHttpClient:: public function setCredential($credentials = []){ $this->apiKey = $credentials['key']; $this->apiToken = $credentials['token']; + return $this; } @@ -127,7 +135,8 @@ public function getApiVersion(){ public function get($url = null, $options = []) { $response = $this->client->get($url, $this->getOptions($options)); - return $response->getBody()->getContents(); + + return Utils::formatInJson( $response ); } /** @@ -141,6 +150,7 @@ public function get($url = null, $options = []) public function post($url = null, $options = []) { $response = $this->client->post($url, $this->getOptions($options)); + return $response->getBody()->getContents(); } @@ -155,6 +165,7 @@ public function post($url = null, $options = []) public function put($url = null, $options = []) { $response = $this->client->put($url, $this->getOptions($options) ); + return $response->getBody()->getContents(); } @@ -169,6 +180,7 @@ public function put($url = null, $options = []) public function delete($url = null, $options = []) { $response = $this->client->delete($url, $this->getOptions($options)); + return $response->getBody()->getContents(); } diff --git a/src/Prettus/Moip/Subscription/Resources/Customers.php b/src/Prettus/Moip/Subscription/Resources/Customers.php index c58ef47..b824388 100644 --- a/src/Prettus/Moip/Subscription/Resources/Customers.php +++ b/src/Prettus/Moip/Subscription/Resources/Customers.php @@ -35,7 +35,7 @@ public function __construct(MoipHttpClient $client){ * @param bool $new_vault * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function create(array $data, $new_vault = false, array $options = []){ @@ -55,7 +55,7 @@ public function create(array $data, $new_vault = false, array $options = []){ * * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function all(array $options = []){ @@ -73,7 +73,7 @@ public function all(array $options = []){ * @param $code * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function find($code, array $options = []){ @@ -93,7 +93,7 @@ public function find($code, array $options = []){ * @param array $data * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function update($code, array $data, array $options = []){ @@ -106,7 +106,6 @@ public function update($code, array $data, array $options = []){ $options = array_merge($options,['body'=>json_encode($data)]); return $this->client->put($url, $options); - } /** @@ -114,7 +113,7 @@ public function update($code, array $data, array $options = []){ * @param array $data * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function updateBillingInfo($code, array $data, array $options = []){ diff --git a/src/Prettus/Moip/Subscription/Resources/Invoices.php b/src/Prettus/Moip/Subscription/Resources/Invoices.php index 72364a1..cdb7d3f 100644 --- a/src/Prettus/Moip/Subscription/Resources/Invoices.php +++ b/src/Prettus/Moip/Subscription/Resources/Invoices.php @@ -3,7 +3,6 @@ use GuzzleHttp\Exception\ClientException; use Prettus\Moip\Subscription\Contracts\MoipHttpClient; use Prettus\Moip\Subscription\ResourceUtils; -use Psr\Http\Message\ResponseInterface; /** * Class Invoices @@ -31,7 +30,7 @@ public function __construct(MoipHttpClient $client){ * @param $code * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function find($code, array $options = []){ @@ -50,7 +49,7 @@ public function find($code, array $options = []){ * @param $code * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function payments($code, array $options = []){ @@ -61,7 +60,6 @@ public function payments($code, array $options = []){ ]); return $this->client->get($url, $options); - } /** @@ -70,7 +68,7 @@ public function payments($code, array $options = []){ * @param $code * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function retryPayment($code, array $options = []){ @@ -81,6 +79,5 @@ public function retryPayment($code, array $options = []){ ]); return $this->client->get($url, $options); - } } \ No newline at end of file diff --git a/src/Prettus/Moip/Subscription/Resources/Payments.php b/src/Prettus/Moip/Subscription/Resources/Payments.php index ee0ead5..2b72be1 100644 --- a/src/Prettus/Moip/Subscription/Resources/Payments.php +++ b/src/Prettus/Moip/Subscription/Resources/Payments.php @@ -3,7 +3,6 @@ use GuzzleHttp\Exception\ClientException; use Prettus\Moip\Subscription\Contracts\MoipHttpClient; use Prettus\Moip\Subscription\ResourceUtils; -use Psr\Http\Message\ResponseInterface; /** * Class Payments @@ -34,7 +33,7 @@ public function __construct(MoipHttpClient $client){ * @param $code * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function find($code, array $options = []){ @@ -46,5 +45,4 @@ public function find($code, array $options = []){ return $this->client->get($url, $options); } - } \ No newline at end of file diff --git a/src/Prettus/Moip/Subscription/Resources/Plans.php b/src/Prettus/Moip/Subscription/Resources/Plans.php index 9ade03a..ab2a047 100644 --- a/src/Prettus/Moip/Subscription/Resources/Plans.php +++ b/src/Prettus/Moip/Subscription/Resources/Plans.php @@ -3,7 +3,7 @@ use GuzzleHttp\Exception\ClientException; use Prettus\Moip\Subscription\Contracts\MoipHttpClient; use Prettus\Moip\Subscription\ResourceUtils; -use Psr\Http\Message\ResponseInterface; +use Symfony\Component\HttpFoundation\Response; /** * Class Plans @@ -31,7 +31,7 @@ public function __construct(MoipHttpClient $client){ * @param array $data * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function create(array $data, array $options = []){ @@ -50,7 +50,7 @@ public function create(array $data, array $options = []){ * * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function all(array $options = []){ @@ -68,7 +68,7 @@ public function all(array $options = []){ * @param $code * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function find($code, array $options = []){ @@ -88,7 +88,7 @@ public function find($code, array $options = []){ * @param array $data * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function update($code, array $data, array $options = []){ @@ -101,7 +101,6 @@ public function update($code, array $data, array $options = []){ $options = array_merge($options,['body'=>json_encode($data)]); return $this->client->put($url, $options); - } /** @@ -110,7 +109,7 @@ public function update($code, array $data, array $options = []){ * @param $code * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function active($code, array $options = []){ return $this->toogleActive($code, "activate", $options); @@ -122,7 +121,7 @@ public function active($code, array $options = []){ * @param $code * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function deactivate($code, array $options = []){ return $this->toogleActive($code, "inactivate", $options); @@ -135,7 +134,7 @@ public function deactivate($code, array $options = []){ * @param $status [activate, inactivate] * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ protected function toogleActive($code, $status, array $options = []){ diff --git a/src/Prettus/Moip/Subscription/Resources/Preferences.php b/src/Prettus/Moip/Subscription/Resources/Preferences.php index f9331a5..0d3c63a 100644 --- a/src/Prettus/Moip/Subscription/Resources/Preferences.php +++ b/src/Prettus/Moip/Subscription/Resources/Preferences.php @@ -3,7 +3,6 @@ use GuzzleHttp\Exception\ClientException; use Prettus\Moip\Subscription\Contracts\MoipHttpClient; use Prettus\Moip\Subscription\ResourceUtils; -use Psr\Http\Message\ResponseInterface; /** * Class Preferences @@ -31,7 +30,7 @@ public function __construct(MoipHttpClient $client){ * @param array $data * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function setPreferences(array $data, array $options = []){ @@ -52,7 +51,7 @@ public function setPreferences(array $data, array $options = []){ * @param array $data * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function setPreferencesRetry(array $data, array $options = []){ diff --git a/src/Prettus/Moip/Subscription/Resources/Subscriptions.php b/src/Prettus/Moip/Subscription/Resources/Subscriptions.php index fefed36..853761e 100644 --- a/src/Prettus/Moip/Subscription/Resources/Subscriptions.php +++ b/src/Prettus/Moip/Subscription/Resources/Subscriptions.php @@ -3,7 +3,6 @@ use GuzzleHttp\Exception\ClientException; use Prettus\Moip\Subscription\Contracts\MoipHttpClient; use Prettus\Moip\Subscription\ResourceUtils; -use Psr\Http\Message\ResponseInterface; /** * Class Subscriptions @@ -31,7 +30,7 @@ public function __construct(MoipHttpClient $client){ * @param array $data * @param bool $new_customer * @param array $options - * @return ResponseInterface + * @return mixed */ public function create(array $data, $new_customer = false, array $options = []){ @@ -51,7 +50,7 @@ public function create(array $data, $new_customer = false, array $options = []){ * * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function all(array $options = []){ @@ -69,7 +68,7 @@ public function all(array $options = []){ * @param $code * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function find($code, array $options = []){ @@ -89,7 +88,7 @@ public function find($code, array $options = []){ * @param array $data * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function update($code, array $data, array $options = []){ @@ -102,7 +101,6 @@ public function update($code, array $data, array $options = []){ $options = array_merge($options,['body'=>json_encode($data)]); return $this->client->put($url, $options); - } /** @@ -111,7 +109,7 @@ public function update($code, array $data, array $options = []){ * @param $code * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function invoices($code, array $options = []){ @@ -122,7 +120,6 @@ public function invoices($code, array $options = []){ ]); return $this->client->get($url, $options); - } /** @@ -131,7 +128,7 @@ public function invoices($code, array $options = []){ * @param $code * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function suspend($code, array $options = []){ return $this->toogleStatus($code, "suspend", $options); @@ -143,7 +140,7 @@ public function suspend($code, array $options = []){ * @param $code * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function activate($code, array $options = []){ return $this->toogleStatus($code, "activate", $options); @@ -155,7 +152,7 @@ public function activate($code, array $options = []){ * @param $code * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function cancel($code, array $options = []){ return $this->toogleStatus($code, "cancel", $options); @@ -168,7 +165,7 @@ public function cancel($code, array $options = []){ * @param $status [activate, inactivate] * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ protected function toogleStatus($code, $status, array $options = []){ diff --git a/src/Prettus/Moip/Subscription/Webservice/FormatInJson.php b/src/Prettus/Moip/Subscription/Webservice/FormatInJson.php new file mode 100644 index 0000000..ec95a28 --- /dev/null +++ b/src/Prettus/Moip/Subscription/Webservice/FormatInJson.php @@ -0,0 +1,20 @@ +renderer->format(), true); + } +} \ No newline at end of file diff --git a/src/Prettus/Moip/Subscription/Webservice/Formatter.php b/src/Prettus/Moip/Subscription/Webservice/Formatter.php new file mode 100644 index 0000000..1a9b8ba --- /dev/null +++ b/src/Prettus/Moip/Subscription/Webservice/Formatter.php @@ -0,0 +1,24 @@ +renderer = $renderer; + } +} \ No newline at end of file diff --git a/src/Prettus/Moip/Subscription/Webservice/ResourceUtils.php b/src/Prettus/Moip/Subscription/Webservice/ResourceUtils.php new file mode 100644 index 0000000..2cc1985 --- /dev/null +++ b/src/Prettus/Moip/Subscription/Webservice/ResourceUtils.php @@ -0,0 +1,25 @@ +getBody()->getContents() ); + $renderTo = new FormatInJson( $webservice ); + + return $renderTo->format(); + } +} \ No newline at end of file diff --git a/src/Prettus/Moip/Subscription/Webservice/Webservice.php b/src/Prettus/Moip/Subscription/Webservice/Webservice.php new file mode 100644 index 0000000..7dfe1dc --- /dev/null +++ b/src/Prettus/Moip/Subscription/Webservice/Webservice.php @@ -0,0 +1,34 @@ +data = $data; + } + + /** + * Collection to be formatted + * + * @return mixed + */ + public function format() + { + return $this->data; + } +} \ No newline at end of file From 79d625771c747ee589ef7fd9f3b88153402f9e3e Mon Sep 17 00:00:00 2001 From: Cristiano Soares Date: Thu, 2 May 2019 17:38:51 -0300 Subject: [PATCH 3/3] Implementado captura e retorno de erros --- src/Prettus/Moip/Subscription/MoipClient.php | 86 +++++++++++++++++--- 1 file changed, 74 insertions(+), 12 deletions(-) diff --git a/src/Prettus/Moip/Subscription/MoipClient.php b/src/Prettus/Moip/Subscription/MoipClient.php index ca130fd..9169c2e 100644 --- a/src/Prettus/Moip/Subscription/MoipClient.php +++ b/src/Prettus/Moip/Subscription/MoipClient.php @@ -6,6 +6,8 @@ use Prettus\Moip\Subscription\Webservice\ResourceUtils as Utils; use Prettus\Moip\Subscription\Webservice\Webservice; use Prettus\Moip\Subscription\Contracts\MoipHttpClient; +use GuzzleHttp\Exception\RequestException; +use Psr\Http\Message\ResponseInterface; /** * Class MoipClient @@ -129,14 +131,23 @@ public function getApiVersion(){ * * @param null $url * @param array $options + * * @throws ClientException - * @return string + * @return array */ public function get($url = null, $options = []) { - $response = $this->client->get($url, $this->getOptions($options)); + try { + $response = $this->client->get($url, $this->getOptions($options)); - return Utils::formatInJson( $response ); + return Utils::formatInJson( $response ); + } catch(RequestException $e) { + if ($e->hasResponse()) { + return $this->composeError($e->getResponse()); + } + + throw $e; + } } /** @@ -144,14 +155,23 @@ public function get($url = null, $options = []) * * @param null $url * @param array $options + * * @throws ClientException - * @return string + * @return string|array */ public function post($url = null, $options = []) { - $response = $this->client->post($url, $this->getOptions($options)); + try { + $response = $this->client->post($url, $this->getOptions($options)); - return $response->getBody()->getContents(); + return $response->getBody()->getContents(); + } catch(RequestException $e) { + if ($e->hasResponse()) { + return $this->composeError($e->getResponse()); + } + + throw $e; + } } /** @@ -159,14 +179,23 @@ public function post($url = null, $options = []) * * @param null $url * @param array $options + * * @throws ClientException - * @return string + * @return string|array */ public function put($url = null, $options = []) { - $response = $this->client->put($url, $this->getOptions($options) ); + try { + $response = $this->client->put($url, $this->getOptions($options) ); - return $response->getBody()->getContents(); + return $response->getBody()->getContents(); + } catch(RequestException $e) { + if ($e->hasResponse()) { + return $this->composeError($e->getResponse()); + } + + throw $e; + } } /** @@ -174,14 +203,23 @@ public function put($url = null, $options = []) * * @param null $url * @param array $options + * * @throws ClientException - * @return string + * @return string|array */ public function delete($url = null, $options = []) { - $response = $this->client->delete($url, $this->getOptions($options)); + try { + $response = $this->client->delete($url, $this->getOptions($options)); - return $response->getBody()->getContents(); + return $response->getBody()->getContents(); + } catch(RequestException $e) { + if ($e->hasResponse()) { + return $this->composeError($e->getResponse()); + } + + throw $e; + } } /** @@ -191,4 +229,28 @@ public function delete($url = null, $options = []) public function getOptions($options = []){ return array_merge($this->requestOptions, $options); } + + /** + * Monta um array contendo os erros da requisição + * + * @param ResponseInterface $response + * + * @return array + */ + private function composeError(ResponseInterface $response) + { + $error = array( + 'error' => true, + 'http_code' => $response->getStatusCode(), + 'http_reason' => $response->getReasonPhrase() + ); + + $message = Utils::formatInJson($response); + + if ($message) { + $error = $error + $message; + } + + return $error; + } } \ No newline at end of file