Skip to content

Commit

Permalink
feat: cria _PagSeguroException_ para tratar os erros
Browse files Browse the repository at this point in the history
Erros que ocorrer entre o SDK e a API
  • Loading branch information
valdeir2000 committed Aug 25, 2020
1 parent 33baa6a commit d1ad7fe
Show file tree
Hide file tree
Showing 7 changed files with 383 additions and 29 deletions.
31 changes: 31 additions & 0 deletions upload/system/library/PagSeguro/src/Domains/Error.php
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;
}
}
12 changes: 11 additions & 1 deletion upload/system/library/PagSeguro/src/Domains/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,12 @@ public static function fromXml(string $value)
$instance->shipping = Shipping::fromXml($dom->saveXML($shipping->item(0)));
}

$creditCard = $xpath->query('/payment/creditCard');

if ($creditCard->count() > 0) {
$instance->creditCard = creditCard::fromXml($dom->saveXML($shipping->item(0)));
}

return $instance;
}

Expand Down Expand Up @@ -392,12 +398,16 @@ public function toXml()
if ($this->shipping) {
$arr['shipping'] = $this->shipping->toArray(true);
}

if ($this->payment instanceof CreditCard) {
$arr['creditCard'] = $this->payment->toArray(true);
}

$parser = new XmlParser();
$result = $parser->parser([
'payment' => $arr
]);

return $result->saveXML();
return $result->saveXML(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
use DOMDocument;
use DOMXPath;
use ValdeirPsr\PagSeguro\Interfaces\Serializer\Xml;
use ValdeirPsr\PagSeguro\Interfaces\Serializer\IArray;
use ValdeirPsr\PagSeguro\Parser\Xml as XmlParser;
use ValdeirPsr\PagSeguro\Domains\User\Holder;
use ValdeirPsr\PagSeguro\Domains\Address;

class CreditCard extends AbstractPaymentMethod implements Xml
class CreditCard extends AbstractPaymentMethod implements IArray, Xml
{
/** @var string */
private $token;
Expand Down Expand Up @@ -200,18 +201,26 @@ public function toXml(): string
{
$parser = new XmlParser();
$result = $parser->parser([
'creditCard' => [
'token' => $this->token,
'installment' => [
'quantity' => $this->installmentQuantity,
'value' => $this->installmentValue,
'noInterestInstallmentQuantity' => $this->noInterestInstallmentQuantity
],
'holder' => $this->holder->toArray(),
'billingAddress' => $this->billingAddress->toArray()
]
'creditCard' => $this->toArray()
]);

return $result->saveXML();
}

/**
* {@inheritDoc}
*/
public function toArray(): array
{
return array_filter([
'token' => $this->token,
'installment' => array_filter([
'quantity' => $this->installmentQuantity,
'value' => $this->installmentValue,
'noInterestInstallmentQuantity' => $this->noInterestInstallmentQuantity
]),
'holder' => $this->holder->toArray(),
'billingAddress' => $this->billingAddress->toArray()
]);
}
}
122 changes: 122 additions & 0 deletions upload/system/library/PagSeguro/src/Exception/PagSeguroRequest.php
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');
}
}
}
38 changes: 24 additions & 14 deletions upload/system/library/PagSeguro/src/Request/Sale.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use ValdeirPsr\PagSeguro\Domains\Payment;
use ValdeirPsr\PagSeguro\Domains\Transaction;
use ValdeirPsr\PagSeguro\Exception\Auth as AuthException;
use ValdeirPsr\PagSeguro\Exception\PagSeguroRequest as PagSeguroRequestException;

class Sale
{
Expand All @@ -18,20 +19,7 @@ public function __construct(Environment $env)

public function create(Payment $payment)
{
$url = $this->buildUrl();

$request = Factory::request($this->env);
$request->setHeader('Content-Type', 'application/xml; charset=ISO-8859-1');
$request->post($url, $payment->toXml());
$request->close();

if ($request->isSuccess()) {
return Transaction::fromXml($request->getResponse());
} elseif ($request->getHttpStatus() === 401) {
throw new AuthException('Check your credentials', 1000);
} else {
throw new PagSeguroRequestException($request);
}
return Transaction::fromXml($this->request($payment));
}

/**
Expand All @@ -44,4 +32,26 @@ protected function buildUrl(): string
'token' => $this->env->getToken()
]);
}

/**
* {@inheritDoc}
*/
protected function request(Payment $payment)
{
$url = $this->buildUrl();
$data = $payment->toXml();

$request = Factory::request($this->env);
$request->setHeader('Content-Type', 'application/xml; charset=ISO-8859-1');
$request->post($url, $data);
$request->close();

if ($request->isSuccess()) {
return $request->getResponse();
} elseif ($request->getHttpStatus() === 401) {
throw new AuthException('Check your credentials', 1000);
} else {
throw new PagSeguroRequestException($request, $data);
}
}
}
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>
Loading

0 comments on commit d1ad7fe

Please sign in to comment.