-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cria _PagSeguroException_ para tratar os erros
Erros que ocorrer entre o SDK e a API
- Loading branch information
1 parent
33baa6a
commit d1ad7fe
Showing
7 changed files
with
383 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace ValdeirPsr\PagSeguro\Domains; | ||
|
||
class Error | ||
{ | ||
private $code; | ||
private $msg; | ||
|
||
public function __construct(string $msg, int $code = 0) | ||
{ | ||
$this->code = $code; | ||
$this->msg = $msg; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getCode(): int | ||
{ | ||
return $this->code; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getMessage(): int | ||
{ | ||
return $this->msg; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
upload/system/library/PagSeguro/src/Exception/PagSeguroRequest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
<?php | ||
|
||
namespace ValdeirPsr\PagSeguro\Exception; | ||
|
||
use DOMDocument; | ||
use DOMXPath; | ||
use Throwable; | ||
use ValdeirPsr\PagSeguro\Domains\Error; | ||
use Curl\Curl; | ||
|
||
class PagSeguroRequest extends \Exception | ||
{ | ||
private $request; | ||
private $requestBody; | ||
private $errors = []; | ||
|
||
public function __construct(Curl $curl, $requestBody, string $message = null, int $code = 0, Throwable $previous = null) | ||
{ | ||
parent::__construct($message, $code, $previous); | ||
|
||
$this->request = $curl; | ||
$this->requestBody = $requestBody; | ||
|
||
$this->checkErrors(); | ||
} | ||
|
||
/** | ||
* @return Curl | ||
*/ | ||
public function getRequest(): Curl | ||
{ | ||
return $request; | ||
} | ||
|
||
/** | ||
* @return mixed Retorna a resposta do servidor | ||
*/ | ||
public function getResponse() | ||
{ | ||
return $this->request->getResponse(); | ||
} | ||
|
||
/** | ||
* @return int Retorna o Status Code da resposta | ||
*/ | ||
public function getHttpStatus(): int | ||
{ | ||
return $this->request->getHttpstatus(); | ||
} | ||
|
||
/** | ||
* Define o corpo da mensagem enviada | ||
* | ||
* @param mixed $value | ||
* | ||
* @return self | ||
*/ | ||
public function setRequestBody($value) | ||
{ | ||
$this->requestBody = $value; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function getRequestBody() | ||
{ | ||
return $this->requestBody; | ||
} | ||
|
||
/** | ||
* @return Error[] | ||
*/ | ||
public function getErrors(): array | ||
{ | ||
return $this->errors; | ||
} | ||
|
||
/** | ||
* Verifica os erros recebidos pela API | ||
* | ||
* @return void | ||
*/ | ||
private function checkErrors() | ||
{ | ||
$response = $this->getResponse(); | ||
|
||
if ($response) { | ||
$responseDom = new DOMDocument(); | ||
$status = $responseDom->loadXML($response, LIBXML_NOERROR); | ||
|
||
if ($status) { | ||
$errors = $responseDom->getElementsByTagName('error'); | ||
|
||
foreach ($errors as $error) { | ||
$code = $error->getElementsByTagName('code'); | ||
$msg = $error->getElementsByTagName('message'); | ||
|
||
if ($code->count() > 0) { | ||
$code = intval($code->item(0)->textContent); | ||
} else { | ||
$code = 0; | ||
} | ||
|
||
if ($msg->count() > 0) { | ||
$msg = trim($msg->item(0)->textContent); | ||
} else { | ||
$msg = $response; | ||
} | ||
|
||
$this->errors[] = new Error($msg, $code); | ||
} | ||
} else { | ||
$this->errors[] = new Error($response); | ||
} | ||
} else { | ||
$this->errors[] = new Error('PagSeguroRequest :: Response empty'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
upload/system/library/PagSeguro/tests/data/sale/invalid-creditcard.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<payment> | ||
<mode>default</mode> | ||
<method>creditcard</method> | ||
<sender> | ||
<name>Florbela Espanca</name> | ||
<email>fulano.silva.sandbox.pagseguro.com.br</email> | ||
<phone> | ||
<areaCode>11</areaCode> | ||
<number>30380000</number> | ||
</phone> | ||
<documents> | ||
<document> | ||
<type>CPF</type> | ||
<value>72962940005</value> | ||
</document> | ||
</documents> | ||
</sender> | ||
<currency>BRL</currency> | ||
<notificationURL>https://sualoja.com.br/notificacao</notificationURL> | ||
<items> | ||
<item> | ||
<id>1</id> | ||
<description>Antologia poetica de Florbela Espanca</description> | ||
<amount>2780</amount> | ||
<quantity>1</quantity> | ||
</item> | ||
<item> | ||
<id>2</id> | ||
<description>Poesia de Florbela Espanca</description> | ||
<amount>15.600</amount> | ||
<quantity>1</quantity> | ||
</item> | ||
</items> | ||
<reference>R123456</reference> | ||
<shipping> | ||
<type>30</type> | ||
<cost>0.00</cost> | ||
<addressRequired>true</addressRequired> | ||
</shipping> | ||
<creditCard> | ||
<token>e79fc9be6fd14b3c8b6164f21ba3c464</token> | ||
<installment> | ||
<quantity>3</quantity> | ||
<value>14.46</value> | ||
</installment> | ||
<holder> | ||
<name>Nome impresso no cartao</name> | ||
<birthDate>20/10/2021</birthDate> | ||
<documents> | ||
<document> | ||
<type>CPF</type> | ||
<value>25136624078</value> | ||
</document> | ||
</documents> | ||
<phone> | ||
<areaCode>11</areaCode> | ||
<number>999991111</number> | ||
</phone> | ||
</holder> | ||
<billingAddress> | ||
<street>Av. Brigadeiro Faria Lima</street> | ||
<complement>1 andar</complement> | ||
<district>Jardim Paulistano</district> | ||
<city>Sao Paulo</city> | ||
<state>SP</state> | ||
<country>BRA</country> | ||
<postalCode>01452002</postalCode> | ||
</billingAddress> | ||
</creditCard> | ||
</payment> |
Oops, something went wrong.