-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: adiciona propriedades extra em "transaction"
- Loading branch information
1 parent
ad4d44c
commit c47bb91
Showing
5 changed files
with
339 additions
and
0 deletions.
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
upload/system/library/PagSeguro/src/Domains/CreditorFees.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,99 @@ | ||
<?php | ||
|
||
namespace ValdeirPsr\PagSeguro\Domains; | ||
|
||
use DOMDocument; | ||
use DOMXPath; | ||
use ValdeirPsr\PagSeguro\Interfaces\Serializer\Xml; | ||
use ValdeirPsr\PagSeguro\Interfaces\Serializer\IArray; | ||
use ValdeirPsr\PagSeguro\Parser\Xml as XmlParser; | ||
|
||
class CreditorFees implements Xml, IArray | ||
{ | ||
/** @var float */ | ||
private $installmentFeeAmount; | ||
|
||
/** @var float */ | ||
private $intermediationRateAmount; | ||
|
||
/** @var float */ | ||
private $intermediationFeeAmount; | ||
|
||
/** | ||
* @return float | ||
*/ | ||
public function getInstallmentFeeAmount(): float | ||
{ | ||
return $this->installmentFeeAmount; | ||
} | ||
|
||
/** | ||
* @return float | ||
*/ | ||
public function getIntermediationRateAmount(): float | ||
{ | ||
return $this->intermediationRateAmount; | ||
} | ||
|
||
/** | ||
* @return float | ||
*/ | ||
public function getIntermediationFeeAmount(): float | ||
{ | ||
return $this->intermediationFeeAmount; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public static function fromXml(string $value) | ||
{ | ||
$dom = new DOMDocument(); | ||
$dom->loadXml($value); | ||
|
||
$instance = new self(); | ||
|
||
$xpath = new DOMXPath($dom); | ||
|
||
$installmentFeeAmount = $xpath->query('//installmentFeeAmount'); | ||
|
||
if ($installmentFeeAmount->count() > 0) { | ||
$instance->installmentFeeAmount = $installmentFeeAmount->item(0)->textContent; | ||
} | ||
|
||
$intermediationRateAmount = $xpath->query('//intermediationRateAmount'); | ||
|
||
if ($intermediationRateAmount->count() > 0) { | ||
$instance->intermediationRateAmount = $intermediationRateAmount->item(0)->textContent; | ||
} | ||
|
||
$intermediationFeeAmount = $xpath->query('//intermediationFeeAmount'); | ||
|
||
if ($intermediationFeeAmount->count() > 0) { | ||
$instance->intermediationFeeAmount = $intermediationFeeAmount->item(0)->textContent; | ||
} | ||
|
||
return $instance; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function toXml(): string | ||
{ | ||
$parser = new XmlParser(); | ||
$result = $parser->parser([ | ||
'creditorFees' => $this->toArray() | ||
]); | ||
|
||
return $result->saveXML(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function toArray(): array | ||
{ | ||
return array_filter(get_object_vars($this)); | ||
} | ||
} |
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
54 changes: 54 additions & 0 deletions
54
upload/system/library/PagSeguro/src/Request/Notification.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,54 @@ | ||
<?php | ||
|
||
namespace ValdeirPsr\PagSeguro\Request; | ||
|
||
use ValdeirPsr\PagSeguro\Domains\Environment; | ||
use ValdeirPsr\PagSeguro\Domains\Transaction; | ||
use ValdeirPsr\PagSeguro\Exception\Auth as AuthException; | ||
use ValdeirPsr\PagSeguro\Exception\PagSeguroRequest as PagSeguroRequestException; | ||
|
||
class Notification | ||
{ | ||
private $env; | ||
|
||
public function __construct(Environment $env) | ||
{ | ||
$this->env = $env; | ||
} | ||
|
||
public function capture(string $notificationId) | ||
{ | ||
return Transaction::fromXml($this->request($notificationId)); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected function buildUrl(string $path = ''): string | ||
{ | ||
return Factory::url($this->env, "v3/transactions/notifications/{$path}", [ | ||
'email' => $this->env->getEmail(), | ||
'token' => $this->env->getToken() | ||
]); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected function request(string $path = '') | ||
{ | ||
$url = $this->buildUrl($path); | ||
|
||
$request = Factory::request($this->env); | ||
$request->get($url); | ||
$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); | ||
} | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
upload/system/library/PagSeguro/tests/data/notification/valid.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,83 @@ | ||
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?> | ||
<transaction> | ||
<date>2020-08-23T00:42:29.000-03:00</date> | ||
<code>5D4A027F-C28E-4799-8154-A154B6F7DD7B</code> | ||
<reference>R123456</reference> | ||
<type>1</type> | ||
<status>7</status> | ||
<cancellationSource>INTERNAL</cancellationSource> | ||
<lastEventDate>2020-08-27T19:55:47.000-03:00</lastEventDate> | ||
<paymentMethod> | ||
<type>1</type> | ||
<code>102</code> | ||
</paymentMethod> | ||
<grossAmount>43.40</grossAmount> | ||
<discountAmount>0.00</discountAmount> | ||
<creditorFees> | ||
<installmentFeeAmount>2.07</installmentFeeAmount> | ||
<intermediationRateAmount>0.40</intermediationRateAmount> | ||
<intermediationFeeAmount>2.17</intermediationFeeAmount> | ||
</creditorFees> | ||
<netAmount>38.76</netAmount> | ||
<extraAmount>0.00</extraAmount> | ||
<escrowEndDate>2020-09-06T00:59:05.000-03:00</escrowEndDate> | ||
<installmentCount>3</installmentCount> | ||
<itemCount>2</itemCount> | ||
<items> | ||
<item> | ||
<id>1</id> | ||
<description>Antologia poetica de Florbela Espanca</description> | ||
<quantity>1</quantity> | ||
<amount>27.80</amount> | ||
</item> | ||
<item> | ||
<id>2</id> | ||
<description>Poesia de Florbela Espanca</description> | ||
<quantity>1</quantity> | ||
<amount>15.60</amount> | ||
</item> | ||
</items> | ||
<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> | ||
<shipping> | ||
<address> | ||
<street>Av. Brigadeiro Faria Lima</street> | ||
<number>1384</number> | ||
<complement>1 andar</complement> | ||
<district>Jardim Paulistano</district> | ||
<city>Sao Paulo</city> | ||
<state>SP</state> | ||
<country>BRA</country> | ||
<postalCode>01452002</postalCode> | ||
</address> | ||
<type>3</type> | ||
<cost>0.00</cost> | ||
</shipping> | ||
<gatewaySystem> | ||
<type>cielo</type> | ||
<rawCode xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/> | ||
<rawMessage xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/> | ||
<normalizedCode xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/> | ||
<normalizedMessage xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/> | ||
<authorizationCode>0</authorizationCode> | ||
<nsu>0</nsu> | ||
<tid>0</tid> | ||
<establishmentCode>1056784170</establishmentCode> | ||
<acquirerName>CIELO</acquirerName> | ||
</gatewaySystem> | ||
<primaryReceiver> | ||
<publicKey>PUB072EA7CDF531403F8F9FA984F27141B6</publicKey> | ||
</primaryReceiver> | ||
</transaction> |
49 changes: 49 additions & 0 deletions
49
upload/system/library/PagSeguro/tests/unit/Request/NotificationTest.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,49 @@ | ||
<?php | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use ValdeirPsr\PagSeguro\Domains\Environment; | ||
use ValdeirPsr\PagSeguro\Request\Notification; | ||
|
||
class NotificationTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
* @group notification | ||
*/ | ||
public function newInstance() | ||
{ | ||
$env = Environment::sandbox('pagseguro@valdeir.dev', '1234567890'); | ||
$instance = new Notification($env); | ||
|
||
$this->assertInstanceOf(Notification::class, $instance); | ||
} | ||
|
||
/** | ||
* @test | ||
* @group notification | ||
*/ | ||
public function checkNotificationWithValidArguments() | ||
{ | ||
$env = Environment::sandbox('pagseguro@valdeir.dev', '1234567890'); | ||
|
||
$stub = $this->getMockBuilder(Notification::class) | ||
->setConstructorArgs([$env]) | ||
->setMethods(['buildUrl']) | ||
->getMock(); | ||
|
||
$stub->expects($this->once()) | ||
->method('buildUrl') | ||
->willReturn('https://f3528d51-6219-4b80-8bd3-3ab112b8094f.mock.pstmn.io/v3/transactions/notifications-valid-request'); | ||
|
||
$notificationId = 'abc123'; | ||
|
||
$transaction = $stub->capture($notificationId); | ||
|
||
$this->assertEquals('5D4A027F-C28E-4799-8154-A154B6F7DD7B', $transaction->getCode()); | ||
$this->assertEquals(2.07, $transaction->getCreditorFees()->getInstallmentFeeAmount()); | ||
$this->assertEquals(0.40, $transaction->getCreditorFees()->getIntermediationRateAmount()); | ||
$this->assertEquals(2.17, $transaction->getCreditorFees()->getIntermediationFeeAmount()); | ||
$this->assertEquals('INTERNAL', $transaction->getCancellationSource()); | ||
$this->assertEquals('2020-09-06 00:59:05', $transaction->getEscrowEndDate()->format('Y-m-d H:i:s')); | ||
} | ||
} |