Skip to content

release v1.0.1 #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 2, 2025
Merged
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
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Este SDK possui as seguintes funcionalidades:

## Dependências

- PHP >= 8.1
- PHP >= 8.2

## Instalando o SDK

Expand Down Expand Up @@ -380,3 +380,9 @@ if ($transaction->getReturnCode() == '00') {
);
}
```

## Observações

- Ao criar uma transação com `$transaction = (new eRede($store))->create($transaction)` não vai retornar o campo `authorization`, para retornar o campo é preciso fazer uma consulta `$transaction = (new eRede($store))->get('TID123')`
- O campo `$transaction->getAuthorizationCode()` não está retornando nada, use `$transaction->getBrand()?->getAuthorizationCode()` ou `$transaction->getAuthorization()?->getBrand()?->getAuthorizationCode()`
- Caso precise acessar o JSON original do response utilize `$transaction?->getHttpResponse()->getBody()`
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "silbeckdevs/erede-php",
"version": "1.0.0",
"version": "1.0.1",
"description": "e.Rede integration SDK",
"minimum-stability": "stable",
"license": "MIT",
Expand All @@ -12,11 +12,11 @@
"psr/log": "*"
},
"require-dev": {
"phpunit/phpunit": "^11.5.6",
"phpstan/phpstan": "^1.12.16",
"phpunit/phpunit": "^12.1.6",
"phpstan/phpstan": "^1.12.27",
"kint-php/kint": "^6.0.1",
"monolog/monolog": "^3.8.1",
"friendsofphp/php-cs-fixer": "^3.68.5",
"monolog/monolog": "^3.9.0",
"friendsofphp/php-cs-fixer": "^3.75.0",
"brainmaestro/composer-git-hooks": "^3.0.0"
},
"autoload": {
Expand Down
26 changes: 26 additions & 0 deletions src/Rede/Authorization.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ class Authorization

private ?string $tid = null;

private ?Brand $brand = null;

/**
* @return array<string, class-string>
*/
protected function getObjectMapping(): array
{
return ['brand' => Brand::class];
}

// gets and sets
public function getAffiliation(): ?string
{
return $this->affiliation;
Expand Down Expand Up @@ -294,4 +305,19 @@ public function setTid(?string $tid): static

return $this;
}

public function getBrand(): ?Brand
{
return $this->brand;
}

/**
* @return $this
*/
public function setBrand(Brand $brand): static
{
$this->brand = $brand;

return $this;
}
}
42 changes: 42 additions & 0 deletions src/Rede/Brand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ class Brand

private ?string $returnMessage = null;

private ?string $merchantAdviceCode = null;

private ?string $authorizationCode = null;

private ?string $brandTid = null;

public function getName(): ?string
{
return $this->name;
Expand Down Expand Up @@ -47,4 +53,40 @@ public function setReturnMessage(?string $returnMessage): Brand

return $this;
}

public function getMerchantAdviceCode(): ?string
{
return $this->merchantAdviceCode;
}

public function setMerchantAdviceCode(?string $merchantAdviceCode): Brand
{
$this->merchantAdviceCode = $merchantAdviceCode;

return $this;
}

public function getAuthorizationCode(): ?string
{
return $this->authorizationCode;
}

public function setAuthorizationCode(?string $authorizationCode): Brand
{
$this->authorizationCode = $authorizationCode;

return $this;
}

public function getBrandTid(): ?string
{
return $this->brandTid;
}

public function setBrandTid(?string $brandTid): Brand
{
$this->brandTid = $brandTid;

return $this;
}
}
4 changes: 4 additions & 0 deletions src/Rede/CreateTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ public function populate(object $body): static
{
$bodyKeys = get_object_vars($body);
$dateTimeProps = ['requestDateTime', 'dateTime', 'refundDateTime', 'dateTimeExpiration', 'expirationQrCode'];
$objectMapping = method_exists($this, 'getObjectMapping') ? $this->getObjectMapping() : [];

foreach ($bodyKeys as $property => $value) {
if (property_exists($this, $property) && null !== $value) {
if (in_array($property, $dateTimeProps) && is_string($value)) {
$value = new \DateTime($value);
} elseif ($objectMapping && isset($objectMapping[$property]) && !empty($value)) {
// @phpstan-ignore-next-line
$value = (new $objectMapping[$property]())?->populate($value);
}

$this->{$property} = $value;
Expand Down
20 changes: 20 additions & 0 deletions src/Rede/Http/RedeResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Rede\Http;

class RedeResponse
{
public function __construct(private int $statusCode, private string $body)
{
}

public function getStatusCode(): int
{
return $this->statusCode;
}

public function getBody(): string
{
return $this->body;
}
}
22 changes: 22 additions & 0 deletions src/Rede/ResponseTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Rede;

use Rede\Http\RedeResponse;

trait ResponseTrait
{
private ?RedeResponse $httpResponse = null;

public function getHttpResponse(): ?RedeResponse
{
return $this->httpResponse;
}

public function setHttpResponse(?RedeResponse $httpResponse): static
{
$this->httpResponse = $httpResponse;

return $this;
}
}
1 change: 1 addition & 0 deletions src/Rede/Service/AbstractTransactionsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ protected function parseResponse(string $response, int $statusCode): Transaction
}

try {
$this->transaction->setHttpResponse(new \Rede\Http\RedeResponse($statusCode, $response));
$this->transaction->jsonUnserialize($response);
} catch (\InvalidArgumentException $e) {
$previous = $e;
Expand Down
5 changes: 4 additions & 1 deletion src/Rede/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

class Transaction implements RedeSerializable, RedeUnserializable
{
use ResponseTrait;

public const CREDIT = 'credit';

public const DEBIT = 'debit';
Expand Down Expand Up @@ -733,7 +735,8 @@ public function jsonUnserialize(string $serialized): static
}

foreach (get_object_vars($properties) as $property => $value) {
if ('links' == $property) {
// TODO verify why use urls in request and not use links in response
if ('links' === $property) {
continue;
}

Expand Down
152 changes: 152 additions & 0 deletions tests/Unit/TransactionUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php

namespace Rede\Tests\Unit;

use Rede\Tests\BaseTestCase;
use Rede\Transaction;

class TransactionUnitTest extends BaseTestCase
{
public function testShouldPopulateTransaction(): void
{
$transaction = (new Transaction())->jsonUnserialize($this->getJsonTransactionMock());

$this->assertNull($transaction->getAuthorization());
$this->assertNull($transaction->getCapture());

$this->assertSame(20099, $transaction->getAmount());
$this->assertSame('12345678', $transaction->getTid());
$this->assertSame('306718396', $transaction->getNsu());
$this->assertSame('544828', $transaction->getCardBin());
$this->assertSame('0007', $transaction->getLast4());
$this->assertSame('00', $transaction->getReturnCode());
$this->assertSame('2025-06-02T13:15:39-03:00', $transaction->getDateTime()?->format('c'));

$this->assertIsObject($transaction->getBrand());
$this->assertSame('Mastercard', $transaction->getBrand()->getName());
$this->assertSame('00', $transaction->getBrand()->getReturnCode());
$this->assertSame('Success.', $transaction->getBrand()->getReturnMessage());
$this->assertSame('MCS1616339888484', $transaction->getBrand()->getBrandTid());
$this->assertSame('67404', $transaction->getBrand()->getAuthorizationCode());
}

public function testShouldPopulateAuthorization(): void
{
$transaction = (new Transaction())->jsonUnserialize($this->getJsonAuthorizationMock());

$this->assertNull($transaction->getBrand());

$this->assertIsObject($transaction->getAuthorization());
$this->assertSame('Approved', $transaction->getAuthorization()->getStatus());
$this->assertSame(20099, $transaction->getAuthorization()->getAmount());
$this->assertSame('12345678', $transaction->getAuthorization()->getTid());
$this->assertSame('306718396', $transaction->getAuthorization()->getNsu());
$this->assertSame('544828', $transaction->getAuthorization()->getCardBin());
$this->assertSame('0007', $transaction->getAuthorization()->getLast4());
$this->assertSame('00', $transaction->getAuthorization()->getReturnCode());
$this->assertSame('Credit', $transaction->getAuthorization()->getKind());
$this->assertSame('2025-06-02T13:15:39-03:00', $transaction->getAuthorization()->getDateTime()?->format('c'));
$this->assertSame('John Snow', $transaction->getAuthorization()->getCardHolderName());
$this->assertSame('1', $transaction->getAuthorization()->getOrigin());
$this->assertEmpty($transaction->getAuthorization()->getSubscription());

$this->assertIsObject($transaction->getAuthorization()->getBrand());
$this->assertSame('Mastercard', $transaction->getAuthorization()->getBrand()->getName());
$this->assertSame('Success.', $transaction->getAuthorization()->getBrand()->getReturnMessage());
$this->assertSame('00', $transaction->getAuthorization()->getBrand()->getReturnCode());
$this->assertSame('MCS1616339888484', $transaction->getAuthorization()->getBrand()->getBrandTid());
$this->assertSame('67404', $transaction->getAuthorization()->getBrand()->getAuthorizationCode());

$this->assertIsObject($transaction->getCapture());
$this->assertSame('2025-06-02T13:15:39-03:00', $transaction->getCapture()->getDateTime()?->format('c'));
$this->assertSame(20099, $transaction->getCapture()->getAmount());
$this->assertSame('306718396', $transaction->getCapture()->getNsu());
}

private function getJsonTransactionMock(): string
{
return '
{
"reference": "683dce2f41d8a",
"tid": "12345678",
"nsu": "306718396",
"dateTime": "2025-06-02T13:15:39-03:00",
"amount": 20099,
"cardBin": "544828",
"last4": "0007",
"brand": {
"returnCode": "00",
"brandTid": "MCS1616339888484",
"authorizationCode": "67404",
"name": "Mastercard",
"returnMessage": "Success."
},
"returnCode": "00",
"returnMessage": "Success.",
"links": [
{
"method": "GET",
"rel": "transaction",
"href": "https://sandbox-erede.useredecloud.com.br/v1/transactions/12345678"
},
{
"method": "POST",
"rel": "refund",
"href": "https://sandbox-erede.useredecloud.com.br/v1/transactions/12345678/refunds"
}
]
}
';
}

private function getJsonAuthorizationMock(): string
{
return '
{
"requestDateTime": "2025-06-02T13:15:40-03:00",
"authorization": {
"dateTime": "2025-06-02T13:15:39-03:00",
"returnCode": "00",
"returnMessage": "Success.",
"affiliation": 38421438,
"status": "Approved",
"reference": "683dce2f41d8a",
"tid": "12345678",
"nsu": "306718396",
"kind": "Credit",
"amount": 20099,
"installments": 0,
"cardHolderName": "John Snow",
"cardBin": "544828",
"last4": "0007",
"origin": 1,
"subscription": false,
"brand": {
"name": "Mastercard",
"returnMessage": "Success.",
"returnCode": "00",
"brandTid": "MCS1616339888484",
"authorizationCode": "67404"
}
},
"capture": {
"dateTime": "2025-06-02T13:15:39-03:00",
"nsu": "306718396",
"amount": 20099
},
"links": [
{
"method": "GET",
"rel": "refunds",
"href": "https://sandbox-erede.useredecloud.com.br/v1/transactions/12345678/refunds"
},
{
"method": "POST",
"rel": "refund",
"href": "https://sandbox-erede.useredecloud.com.br/v1/transactions/12345678/refunds"
}
]
}
';
}
}