Skip to content
Open
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: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ Se já possui um arquivo `composer.json`, basta adicionar a seguinte dependênci

```json
{
"require": {
"developersrede/erede-php": "*"
}
"require": {
"developersrede/erede-php": "*"
}
}

```
Expand Down Expand Up @@ -64,7 +64,7 @@ fazer:
docker build . -t erede-docker
docker run -e REDE_PV='1234' -e REDE_TOKEN='5678' erede-docker
```
````

Caso necessário, o SDK possui a possibilidade de logs de depuração que podem ser utilizados ao executar os testes. Para isso,
basta exportar a variável de ambiente `REDE_DEBUG` com o valor 1:

Expand Down
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"name": "developersrede/erede-php",
"version": "5.2.1",
"name": "ipagdevs/erede-php",
"description": "e.Rede integration SDK",
"minimum-stability": "stable",
"license": "MIT",
Expand Down Expand Up @@ -28,6 +27,10 @@
{
"name": "João Batista Neto",
"email": "[email protected]"
},
{
"name": "João Hernandes",
"email": "[email protected]"
}
]
}
28 changes: 28 additions & 0 deletions examples/authentication/00-authentication.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Rede\v2\eRede;
use Rede\v2\Store;
use Monolog\Logger;
use Psr\Log\LogLevel;
use Rede\Environment;
use Monolog\Handler\StreamHandler;

require_once __DIR__ . '/../../vendor/autoload.php';

$client_id = getenv('EREDE_CLIENT_ID') ?: '';
$client_secret = getenv('EREDE_CLIENT_SECRET') ?: '';

$logger = new Logger('eRede');
$logger->pushHandler(new StreamHandler('php://stdout', LogLevel::DEBUG));

$eRede = new eRede(
new Store(
filiation: $client_id,
token: $client_secret,
environment: Environment::sandbox()
),
$logger
);

// echo $eRede->generateOAuthToken()->toString();
echo json_encode($eRede->generateOAuthToken()->getCredentials(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
44 changes: 44 additions & 0 deletions src/Rede/AbstractAuthentication.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Rede;

abstract class AbstractAuthentication
{
/**
* Which environment will this store used for?
* @var CredentialsEnvironment
*/
protected CredentialsEnvironment $environment;

abstract public function getCredentials(): array;
abstract public function toString(): string;

public function __construct(?CredentialsEnvironment $environment = null)
{
$this->environment = $environment ?? CredentialsEnvironment::production();
}

/**
* @return CredentialsEnvironment
*/
public function getEnvironment(): CredentialsEnvironment
{
return $this->environment;
}

/**
* @param CredentialsEnvironment $environment
*
* @return $this
*/
public function setEnvironment(CredentialsEnvironment $environment): static
{
$this->environment = $environment;
return $this;
}

public static function make(...$rest): AbstractAuthentication
{
return new static(...$rest);
}
}
2 changes: 1 addition & 1 deletion src/Rede/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class Address implements RedeSerializable
{
use SerializeTrait;
use SerializeTrait, CreateTrait;

public const BILLING = 1;
public const SHIPPING = 2;
Expand Down
22 changes: 22 additions & 0 deletions src/Rede/AuthenticationCredentials.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Rede;

class AuthenticationCredentials
{
public function __construct(
private string $clientId,
private string $clientSecret,
) {
}

public function getClientId(): string
{
return $this->clientId;
}

public function getClientSecret(): string
{
return $this->clientSecret;
}
}
45 changes: 45 additions & 0 deletions src/Rede/Authorization.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ class Authorization
*/
private ?string $tid = null;

/**
* @var Brand|null
*/
private ?Brand $brand = null;
/**
* @var string|null
*/
private ?string $txId = null;

/**
* @return string|null
*/
Expand Down Expand Up @@ -398,4 +407,40 @@ public function setTid(?string $tid): static
$this->tid = $tid;
return $this;
}

/**
* @return Brand|null
*/
public function getBrand(): ?Brand
{
return $this->brand;
}

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

/**
* @return string|null
*/
public function getTxId(): ?string
{
return $this->txId;
}

/**
* @param string|null $txId
* @return $this
*/
public function setTxId(?string $txId): static
{
$this->txId = $txId;
return $this;
}
}
44 changes: 44 additions & 0 deletions src/Rede/BasicAuthentication.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Rede;

class BasicAuthentication extends AbstractAuthentication
{
public function __construct(private Store $store, ?CredentialsEnvironment $environment)
{
parent::__construct($environment);
}

public function getUsername(): string
{
return $this->store->getFiliation();
}

public function getPassword(): string
{
return $this->store->getToken();
}

public function setUsername(string $username): void
{
$this->store->setFiliation($username);
}

public function setPassword(string $password): void
{
$this->store->setToken($password);
}

public function getCredentials(): array
{
return [
'username' => $this->store->getFiliation(),
'password' => $this->store->getToken(),
];
}

public function toString(): string
{
return 'Basic ' . base64_encode(sprintf('%s:%s', $this->store->getFiliation(), $this->store->getToken()));
}
}
76 changes: 76 additions & 0 deletions src/Rede/BearerAuthentication.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Rede;

class BearerAuthentication extends AbstractAuthentication
{
private ?int $expiresIn = null;
private ?string $token = null;
private string $type = 'Bearer';

public function getToken(): ?string
{
return $this->token;
}

public function getExpiresIn(): ?int
{
return $this->expiresIn;
}

public function getType(): string
{
return $this->type;
}

public function setToken(?string $token): self
{
$this->token = $token;
return $this;
}

public function setExpiresIn(?int $expiresIn): self
{
$this->expiresIn = $expiresIn;
return $this;
}

public function setType(string $type): self
{
$this->type = $type;
return $this;
}

public function getCredentials(): array
{
return [
'type' => $this->type,
'token' => $this->token,
'expires_in' => $this->expiresIn,
];
}

public static function withCredentials(array $credentials): self
{
$instance = new self();

if (isset($credentials['token_type'])) {
$instance->type = $credentials['token_type'];
}

if (isset($credentials['access_token'])) {
$instance->token = $credentials['access_token'];
}

if (isset($credentials['expires_in'])) {
$instance->expiresIn = $credentials['expires_in'];
}

return $instance;
}

public function toString(): string
{
return sprintf('%s %s', $this->type, $this->token);
}
}
Loading