Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
17 changes: 17 additions & 0 deletions src/Prettus/Moip/Subscription/Contracts/Renderable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Prettus\Moip\Subscription\Contracts;

/**
* Interface Renderable
* @package Prettus\Moip\Subscription\Contracts
*/
interface Renderable
{
/**
* Collection to be formatted
*
* @return mixed
*/
public function format();
}
102 changes: 88 additions & 14 deletions src/Prettus/Moip/Subscription/MoipClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use Psr\Http\Message\ResponseInterface;
use Prettus\Moip\Subscription\Webservice\RenderToJson;
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
* @package Prettus\Moip\Subscription
*/
class MoipClient implements MoipHttpClient {

use Utils;

/**
* @var Client
*/
Expand Down Expand Up @@ -49,6 +55,9 @@ class MoipClient implements MoipHttpClient {
*/
protected $apiUrl = "https://{environment}.moip.com.br";

/**
* @var array
*/
protected $requestOptions = [];

/**
Expand All @@ -58,7 +67,7 @@ class MoipClient implements MoipHttpClient {
* @param $apiKey
* @param string $environment
*/
public function __construct( $apiToken, $apiKey, $environment = MoipHttpClient::PRODUCTION ){
public function __construct($apiToken, $apiKey, $environment = MoipHttpClient::PRODUCTION){

$this->setCredential(['token'=>$apiToken,'key'=>$apiKey]);
$this->setEnvironment($environment);
Expand All @@ -84,6 +93,7 @@ public function __construct( $apiToken, $apiKey, $environment = MoipHttpClient::
public function setCredential($credentials = []){
$this->apiKey = $credentials['key'];
$this->apiToken = $credentials['token'];

return $this;
}

Expand Down Expand Up @@ -121,55 +131,95 @@ 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));
return $response->getBody()->getContents();
try {
$response = $this->client->get($url, $this->getOptions($options));

return Utils::formatInJson( $response );
} catch(RequestException $e) {
if ($e->hasResponse()) {
return $this->composeError($e->getResponse());
}

throw $e;
}
}

/**
* Executa uma requisição do tipo POST
*
* @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));
return $response->getBody()->getContents();
try {
$response = $this->client->post($url, $this->getOptions($options));

return $response->getBody()->getContents();
} catch(RequestException $e) {
if ($e->hasResponse()) {
return $this->composeError($e->getResponse());
}

throw $e;
}
}

/**
* Executa uma requisição do tipo PUT
*
* @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) );
return $response->getBody()->getContents();
try {
$response = $this->client->put($url, $this->getOptions($options) );

return $response->getBody()->getContents();
} catch(RequestException $e) {
if ($e->hasResponse()) {
return $this->composeError($e->getResponse());
}

throw $e;
}
}

/**
* Executa uma requisição do tipo DELETE
*
* @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));
return $response->getBody()->getContents();
try {
$response = $this->client->delete($url, $this->getOptions($options));

return $response->getBody()->getContents();
} catch(RequestException $e) {
if ($e->hasResponse()) {
return $this->composeError($e->getResponse());
}

throw $e;
}
}

/**
Expand All @@ -179,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;
}
}
11 changes: 5 additions & 6 deletions src/Prettus/Moip/Subscription/Resources/Customers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []){

Expand All @@ -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 = []){

Expand All @@ -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 = []){

Expand All @@ -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 = []){

Expand All @@ -106,15 +106,14 @@ public function update($code, array $data, array $options = []){
$options = array_merge($options,['body'=>json_encode($data)]);

return $this->client->put($url, $options);

}

/**
* @param $code
* @param array $data
* @param array $options
* @throws ClientException
* @return ResponseInterface
* @return mixed
*/
public function updateBillingInfo($code, array $data, array $options = []){

Expand Down
9 changes: 3 additions & 6 deletions src/Prettus/Moip/Subscription/Resources/Invoices.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = []){

Expand All @@ -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 = []){

Expand All @@ -61,7 +60,6 @@ public function payments($code, array $options = []){
]);

return $this->client->get($url, $options);

}

/**
Expand All @@ -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 = []){

Expand All @@ -81,6 +79,5 @@ public function retryPayment($code, array $options = []){
]);

return $this->client->get($url, $options);

}
}
4 changes: 1 addition & 3 deletions src/Prettus/Moip/Subscription/Resources/Payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = []){

Expand All @@ -46,5 +45,4 @@ public function find($code, array $options = []){

return $this->client->get($url, $options);
}

}
Loading