From de3b12025b9942d99fd11172e1e3d6dcbd5b608b Mon Sep 17 00:00:00 2001 From: Pedro Date: Fri, 28 Sep 2018 13:05:00 -0300 Subject: [PATCH] =?UTF-8?q?Otimiza=C3=A7=C3=B5es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Updated comments - Removed "pt_en" from files name (all files are now in English) - Simplified response() method in Notification - Simplified cancel() method in Billet - Minor improvements --- README.md | 9 ++--- src/Core/Interfaces/BankAccountsInterface.php | 2 +- src/Core/Interfaces/BilletInterface.php | 22 ++++++++++++ src/Core/Interfaces/BoletoInterface.php | 24 ------------- src/Core/Interfaces/NotificationInterface.php | 6 ++-- src/Core/Request/Request.php | 11 +++--- src/PagHiper.php | 1 - src/v1/Bank/Accounts.php | 11 ++++-- src/v1/Payment/Billet.php | 36 ++++++++++--------- src/v1/Payment/Notification/Notification.php | 21 +++++++++-- 10 files changed, 82 insertions(+), 61 deletions(-) create mode 100644 src/Core/Interfaces/BilletInterface.php delete mode 100644 src/Core/Interfaces/BoletoInterface.php diff --git a/README.md b/README.md index 1f3a671..422d96e 100644 --- a/README.md +++ b/README.md @@ -59,9 +59,7 @@ Você pode obter a lista de dados que você pode enviar no seguinte link: [https use WebMaster\PagHiper\PagHiper; $pagHiper = new PagHiper(); -$transaction = $pagHiper->billet()->cancel([ - 'transaction_id' => 'JKP03X9KN0RELVLH', - ]); +$transaction = $pagHiper->billet()->cancel('JKP03X9KN0RELVLH'); ``` **Para obter informações do pagamento via retorno automático:** @@ -70,10 +68,7 @@ $transaction = $pagHiper->billet()->cancel([ use WebMaster\PagHiper\PagHiper; $pagHiper = new PagHiper(); -$transaction = $pagHiper->notification()->response([ - 'notification_id' => $_POST['notification_id'], - 'transaction_id' => $_POST['idTransacao'] -]); +$transaction = $pagHiper->notification()->response($_POST['notification_id'], $_POST['idTransacao']); ``` **Para obter a lista de suas contas bancárias:** diff --git a/src/Core/Interfaces/BankAccountsInterface.php b/src/Core/Interfaces/BankAccountsInterface.php index fbe837b..8ceaeb8 100644 --- a/src/Core/Interfaces/BankAccountsInterface.php +++ b/src/Core/Interfaces/BankAccountsInterface.php @@ -5,7 +5,7 @@ interface BankAccountsInterface { /** - * Lista as contas bancárias para saque via API. + * Retrieves a list of bank accounts. * * @return void */ diff --git a/src/Core/Interfaces/BilletInterface.php b/src/Core/Interfaces/BilletInterface.php new file mode 100644 index 0000000..205635f --- /dev/null +++ b/src/Core/Interfaces/BilletInterface.php @@ -0,0 +1,22 @@ + $credentials::TOKEN, 'apiKey' => $credentials::API_KEY, ], $params); + // Send the request $request = $client->request('POST', $endpoint, [ 'json' => $params, ]); @@ -50,7 +51,7 @@ public function __construct(string $endpoint = '', array $params = []) } /** - * Pega a resposta do PagHiper + * Get PagHiper's response. * * @return void */ diff --git a/src/PagHiper.php b/src/PagHiper.php index 1395a08..c19359e 100644 --- a/src/PagHiper.php +++ b/src/PagHiper.php @@ -8,7 +8,6 @@ class PagHiper { - protected $request; protected $billet; protected $bank; protected $notification; diff --git a/src/v1/Bank/Accounts.php b/src/v1/Bank/Accounts.php index 71051e0..80f2660 100644 --- a/src/v1/Bank/Accounts.php +++ b/src/v1/Bank/Accounts.php @@ -3,6 +3,7 @@ namespace WebMaster\PagHiper\v1\Bank; use WebMaster\PagHiper\Core\Request\Request; +use WebMaster\PagHiper\Core\Exceptions\PagHiperException; use WebMaster\PagHiper\Core\Interfaces\BankAccountsInterface; class Accounts implements BankAccountsInterface @@ -10,7 +11,7 @@ class Accounts implements BankAccountsInterface protected $accountsUri = '/bank_accounts/list/'; /** - * Lista as contas bancárias para saque via API. + * Retrieves a list of bank accounts. * * @return void */ @@ -18,6 +19,12 @@ public function accounts() { $bankAccounts = new Request($this->accountsUri); - return $bankAccounts->getResponse()['bank_account_list_request']; + $response = $bankAccounts->getResponse()['bank_account_list_request']; + + if ($response['result'] === 'reject') { + throw new PagHiperException($response['response_message'], $response['http_code']); + } + + return $response; } } diff --git a/src/v1/Payment/Billet.php b/src/v1/Payment/Billet.php index 1965c1d..6320ac7 100644 --- a/src/v1/Payment/Billet.php +++ b/src/v1/Payment/Billet.php @@ -4,22 +4,21 @@ use GuzzleHttp\Client; use WebMaster\PagHiper\Core\Request\Request; -use WebMaster\PagHiper\Core\Interfaces\BoletoInterface; +use WebMaster\PagHiper\Core\Interfaces\BilletInterface; use WebMaster\PagHiper\Core\Configuration\Configuration; use WebMaster\PagHiper\Core\Exceptions\PagHiperException; -class Billet implements BoletoInterface +class Billet implements BilletInterface { protected $createUri = '/transaction/create/'; protected $cancelUri = '/transaction/cancel/'; /** - * Realiza a emissão do boleto - * - * @param array $data Dados para geração do boleto. Os dados necessários para gerar o boleto - * podem ser encontrados em: https://dev.paghiper.com/reference#gerar-boleto - * @return void - */ + * Create a new billet. + * + * @param array $data Billet data. Full list of all available data can be found at: https://dev.paghiper.com/reference#gerar-boleto + * @return void + */ public function create(array $data) { $request = new Request($this->createUri, $data); @@ -34,21 +33,26 @@ public function create(array $data) } /** - * Efetua o cancelamento boleto. + * Cancels a billet. * - * @param array $data Dados para cancelamento do boleto. Os dados para cancelar o boleto podem ser encontrados em: - * https://dev.paghiper.com/reference#boleto + * @param string $transactionId Transaction ID to cancel. * @return boolean */ - public function cancel(array $data) + public function cancel(string $transactionId) { - // Define o status da transação com 'canceled' - $data = array_merge([ + $data = [ + 'transaction_id' => $transactionId, 'status' => 'canceled' - ], $data); + ]; $request = new Request($this->cancelUri, $data); - return $request->getResponse()['cancellation_request']; + $response = $request->getResponse()['cancellation_request']; + + if ($response['result'] === 'reject') { + throw new PagHiperException($response['response_message'], $response['http_code']); + } + + return $response; } } diff --git a/src/v1/Payment/Notification/Notification.php b/src/v1/Payment/Notification/Notification.php index 1ef3f6b..e5fecdb 100644 --- a/src/v1/Payment/Notification/Notification.php +++ b/src/v1/Payment/Notification/Notification.php @@ -3,16 +3,31 @@ namespace WebMaster\PagHiper\v1\Payment\Notification; use WebMaster\PagHiper\Core\Request\Request; +use WebMaster\PagHiper\Core\Exceptions\PagHiperException; use WebMaster\PagHiper\Core\Interfaces\NotificationInterface; class Notification implements NotificationInterface { protected $notificationUri = '/transaction/notification/'; - public function response(array $data) + /** + * Get notification's response. + * + * @return void + */ + public function response(string $notificationId = '', string $transactionId = '') { - $notification = new Request($this->notificationUri, $data); + $notification = new Request($this->notificationUri, [ + 'notification_id' => $notificationId, + 'transaction_id' => $transactionId + ]); - return $notification->getResponse()['status_request']; + $response = $notification->getResponse()['status_request']; + + if ($response['result'] === 'reject') { + throw new PagHiperException($response['response_message'], $response['http_code']); + } + + return $response; } }