Skip to content
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: 1 addition & 7 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,10 @@ composer require phpay-io/phpay
### Asaas

```php
use PHPay\Gateways\Asaas\AsaasGateway;
use PHPay\PHPay;

/**
* @var AsaasGateway $phpay
*/
$asaas = new PHPay(new AsaasGateway(TOKEN_ASAAS_SANDBOX));
$phpay = new PHPay(new AsaasGateway(TOKEN_ASAAS_SANDBOX));
```

### Efí
Expand All @@ -43,9 +40,6 @@ O Efí exige um token do tipo Bearer que é gerado com o
envio do client_id e client_secret, para isso, basta utilizar o método do phpay de autorização.

```php
use PHPay\Gateways\Efi\EfiGateway;
use PHPay\PHPay;

/**
* @var EfiGateway $phpay
*/
Expand Down
13 changes: 2 additions & 11 deletions examples/asaas/charges.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,7 @@
*
* @var AsaasGateway $phpay
*/
$asaas = new PHPay(new AsaasGateway(TOKEN_ASAAS_SANDBOX));

/**
* create customer
*
* @return array customer
*/
$customerCreated = $phpay
->customer()
->create($customer);
$phpay = new PHPay(new AsaasGateway(TOKEN_ASAAS_SANDBOX));

/**
* create charge
Expand All @@ -42,7 +33,7 @@
*/
$chargeCreated = $phpay
->charge($charge)
->setCustomer($customerCreated['id'])
->setCustomer($customer)
->create();

/**
Expand Down
23 changes: 8 additions & 15 deletions examples/efi/charges.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,27 @@
require_once __DIR__ . '/credentials.php';

$customer = [
'name' => NAME,
'cpfCnpj' => CPF_CNPJ,
'name' => NAME,
'cpf_cnpj' => CPF_CNPJ,
];

$charge = [
'billingType' => 'PIX',
'value' => 100.00,
'description' => 'Teste de fatura',
'dueDate' => date('Y-m-d', strtotime('+1 day')),
'expire_at' => date('Y-m-d', strtotime('+1 day')),
'message' => 'Teste de mensagem',
];

/**
* initialize phpay
*
* @var EfiGateway $phpay
*/
$phpay = new PHPay(new EfiGateway());
$phpay = new PHPay(new EfiGateway(CLIENT_ID, CLIENT_SECRET));

$token = $phpay
->authorization(CLIENT_ID, CLIENT_SECRET)
->getToken();

/**
* create charge
*
* @return array charges
*/
$chargeCreated = $phpay
$charge = $phpay
->charge($charge)
->setCustomer($customer)
->create();

print_r($charge);
13 changes: 10 additions & 3 deletions src/Gateways/Asaas/Resources/Charge/Charge.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Asaas\Requests\AsaasChargeRequest;
use Asaas\Resources\Charge\Interface\ChargeInterface;
use Asaas\Resources\Customer\Customer;
use Asaas\Traits\HasAsaasClient;
use GuzzleHttp\Client;

Expand Down Expand Up @@ -57,12 +58,18 @@ public function setFilters(array $filters): ChargeInterface
/**
* set customer
*
* @param string $customerId
* @param array<mixed> $customer
* @return ChargeInterface
*/
public function setCustomer(string $customerId): ChargeInterface
public function setCustomer(array $customer): ChargeInterface
{
$this->charge['customer'] = $customerId;
$customer = (new Customer(
$this->token,
$customer,
$this->sandbox
))->create();

$this->charge['customer'] = $customer['id'];

return $this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ public function destroy(string $id): bool;
public function restore(string $id): array;

/**
* get charge notifications by customer
* set customer
*
* @param string $customer
* @param array<mixed> $customer
* @return ChargeInterface
*/
public function setCustomer(string $customer): ChargeInterface;
public function setCustomer(array $customer): ChargeInterface;

/**
* set filter
Expand Down
53 changes: 43 additions & 10 deletions src/Gateways/Efi/EfiGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Efi\Interface\EfiGatewayInterface;
use Efi\Resources\Authorization\Authorization;
use Efi\Resources\Charge\Charge;
use Efi\Traits\HasEfiClient;
use GuzzleHttp\Client;
use stdClass;
Expand All @@ -17,37 +18,49 @@ class EfiGateway implements EfiGatewayInterface
*/
public Client $client;

/**
* @var array<string> $token
*/
private array $token;

/**
* construct
*
* @param string $clientId
* @param string $clientSecret
* @param bool $sandbox
*/
public function __construct(
private bool $sandbox = true
private string $clientId,
private string $clientSecret,
private bool $sandbox = true,
) {
$this->authorization();
}

/**
* autorization
* get token
*
* @param string $clientId
* @param string $clientSecret
* @return Authorization
* @return array<mixed> token
*/
public function authorization(string $clientId, string $clientSecret): Authorization
public function getToken(): array
{
return new Authorization($clientId, $clientSecret, $this->sandbox);
return $this->token;
}

/**
* create charge
*
* @param array<string> $charge
* @return object charge
* @return Charge
*/
public function charge(array $charge = []): object
public function charge(array $charge = []): Charge
{
return new stdClass();
return new Charge(
$this->token,
$charge,
$this->sandbox
);
}

/**
Expand All @@ -71,4 +84,24 @@ public function webhook(array $webhook = []): object
{
return new stdClass();
}

/**
* authorization
*
* @return void
*/
private function authorization(): void
{
$authorization = new Authorization(
$this->clientId,
$this->clientSecret,
$this->sandbox
);

$this->token = $authorization->getToken();

if (!isset($this->token['access_token'])) {
throw new \Exception('Token not generated');
}
}
}
17 changes: 15 additions & 2 deletions src/Gateways/Efi/Interface/EfiGatewayInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,23 @@

namespace Efi\Interface;

use Efi\Resources\Authorization\Authorization;
use Efi\Resources\Charge\Charge;
use PHPay\Contracts\GatewayInterface;

interface EfiGatewayInterface extends GatewayInterface
{
public function authorization(string $clientId, string $clientSecrete): Authorization;
/**
* get token
*
* @return array<mixed> token
*/
public function getToken(): array;

/**
* create charge
*
* @param array<string> $charge
* @return Charge charge
*/
public function charge(array $charge = []): Charge;
}
9 changes: 7 additions & 2 deletions src/Gateways/Efi/Resources/Authorization/Authorization.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,19 @@ public function __construct(
/**
* get token
*
* @return array<mixed>
* @return array<string>
*/
public function getToken(): array
{
$this->client = $this->clientEfiAuthorize($this->clientId, $this->clientSecret);

return $this->post("v1/authorize", [
/**
* @var array<string> $response
*/
$response = $this->post("v1/authorize", [
'grant_type' => 'client_credentials',
]);

return $response;
}
}
Loading