Skip to content

Commit

Permalink
feat: propoe uma nova integracao com a Asaas via adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianosfbr committed Feb 25, 2024
1 parent 294bed8 commit 11de7d6
Show file tree
Hide file tree
Showing 12 changed files with 405 additions and 14 deletions.
77 changes: 68 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,84 @@ Agora, basta acessar a URL `http://laravel.test`
- Correr pro abraço!

### INTEGRAÇÃO COM GATEWAY DE PAGAMENTOS
Listar Clientes:
```php
$customers = (new App\Services\AsaasPhp\Customer\CustomerList)->handle();
Instanciar o conector (adapter) do gateway de pagamento que deseja
```bash
$adapter = new App\Services\Gateway\Connectors\AsaasConnector();
```

dd($customers);
Instanciar o cliente Gateway usando a do adapter cirado préviamente
```bash
$gateway = new App\Services\Gateway\Gateway($adapter);
```

Criar Novo Cliente:

Clientes:
```php
// Insere um novo cliente
$data = [
'name' => 'Rick Tortorelli',
'name' => 'Fabiano Fernandes',
'cpfCnpj' => '21115873709',
'email' => 'rick@test.com.br',
'email' => 'fabianofernandes@test.com.br',
'mobilePhone' => '16992222222',
];

$customer = (new App\Services\AsaasPhp\Customer\CustomerCreate(data: $data))->handle();
$customer = $gateway->customer()->create($data);

dd($customer);
// Atualizar um cliente
$newData = [
'name' => 'Tio Jobs',
'cpfCnpj' => '21115873709',
'email' => '[email protected]',
'mobilePhone' => '16992222222',
];
$customer = $gateway->customer()->update('cus_000005891625', $newData);

// Retorna a listagem de clientes
$customers = $gateway->customer()->list();

// Retorna clientes utilizando filtros
$customers = $gateway->customer()->list(['cpfCnpj' => '21115873709']);

// Remove um cliente
$customer = $gateway->customer()->delete('cus_000005891625');
```

Cobrança:
```php
// Criar uma nova cobrança
$data = [
"billingType" => "BOLETO", // "CREDIT_CARD", "PIX", "BOLETO"
"discount" => [
"value" => 10,
"dueDateLimitDays" => 0
],
"interest" => [
"value" => 2
],
"fine" => [
"value" => 1
],
"customer" => "cus_000005891625",
"dueDate" => "2024-02-29",
"value" => 100,
"description" => "Pedido 056984",
"daysAfterDueDateToCancellationRegistration" => 1,
"externalReference" => "056984",
"postalService" => false
];
$payment = $gateway->payment()->create($data);

// Atualiza uma cobrança
$payment = $gateway->payment()->update('cus_000005891625', $newData);

// Retorna a listagem de cobranças
$payments = $gateway->payment()->list();

// Retorna cobranças utilizando filtros
$payments = $gateway->payment()->list(['customer' => 'cus_000005891625', 'status' => 'RECEIVED']);

// Remove uma cobrança
$customer = $gateway->payment()->delete('cus_000005891625');
```

### PARTE 02
Expand Down
4 changes: 4 additions & 0 deletions app/Services/AsaasPhp/Concerns/AsaasClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Services\AsaasPhp\Concerns;

use App\Models\Customer;

trait AsaasClient
{
public function __construct(
Expand All @@ -15,3 +17,5 @@ public function __construct(
$this->url = config("asaas.{$this->environment}.url");
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace App\Services\PaymentGateway\Connectors\Asaas\Concerns;

use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Http;

trait AsaasConfig
{
public function __construct(
protected ?PendingRequest $http = null,
) {
$environment = app()->isLocal() ? 'sandbox' : 'production';
$token = config("asaas.{$environment}.token");
$url = config("asaas.{$environment}.url");
$this->http = Http::withHeader('access_token', $token)->baseUrl($url);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace App\Services\PaymentGateway\Connectors\Asaas\Concerns;

trait HasFilter
{

public function filter(array $filters = []):string
{
return empty($filters) ? '' : '?' . http_build_query($filters);
}
}
39 changes: 39 additions & 0 deletions app/Services/PaymentGateway/Connectors/Asaas/Customer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace App\Services\PaymentGateway\Connectors\Asaas;

use App\Services\PaymentGateway\Connectors\Asaas\Concerns\HasFilter;
use App\Services\PaymentGateway\Contracts\AdapterInterface;
use App\Services\PaymentGateway\Contracts\CustomerInterface;

class Customer implements CustomerInterface
{
use HasFilter;

public function __construct(
public AdapterInterface $http,
) {
}

public function list(array $filters = []): array
{
return $this->http->get('/customers' . $this->filter($filters));
}

public function create(array $data): array
{
return $this->http->post('/customers', $data);
}

public function update(int|string $id, array $data): array
{
return $this->http->put('/customers/' . $id, $data);
}

public function delete(int|string $id): array
{
return $this->http->delete('/customers/' . $id);
}
}
54 changes: 54 additions & 0 deletions app/Services/PaymentGateway/Connectors/Asaas/Payment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace App\Services\PaymentGateway\Connectors\Asaas;

use App\Services\PaymentGateway\Connectors\Asaas\Concerns\HasFilter;
use App\Services\PaymentGateway\Contracts\AdapterInterface;
use App\Services\PaymentGateway\Contracts\PaymentInterface;

class Payment implements PaymentInterface
{

use HasFilter;
public function __construct(
public AdapterInterface $http,
) {
}

public function list(array $filters = []): array
{
return $this->http->get('/payments' . $this->filter($filters));
}

public function create(array $data): array
{
return $this->http->post('/payments', $data);
}

public function update(int|string $id, array $data): array
{
return $this->http->put("/payments/{$id}", $data);
}

public function delete(int|string $id): array
{
return $this->http->delete("/payments/{$id}");
}

public function getPaymentStatus(int|string $id): array
{
return $this->http->get("/payments/{$id}/status" );
}

public function getInvoiceCode(int|string $id): array
{
return $this->http->get("/payments/{$id}/identificationField" );
}

public function getPixQrCode(int|string $id): array
{
return $this->http->get("/payments/{$id}/pixQrCode" );
}
}
62 changes: 62 additions & 0 deletions app/Services/PaymentGateway/Connectors/AsaasConnector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace App\Services\PaymentGateway\Connectors;

use App\Services\PaymentGateway\Connectors\Asaas\Concerns\AsaasConfig;
use App\Services\PaymentGateway\Contracts\AdapterInterface;
use Illuminate\Support\Facades\Http;

class AsaasConnector implements AdapterInterface
{
use AsaasConfig;

public function get(string $url)
{
try {
return $this->http
->get($url)
->throw()
->json();
} catch (\Exception $exception) {
return ['error' => $exception->getMessage()];
}
}

public function post(string $url, array $params)
{
try {
return $this->http
->post($url, $params)
->throw()
->json();
} catch (\Exception $exception) {
return ['error' => $exception->getMessage()];
}
}

public function delete(string $url)
{
try {
return $this->http
->delete($url)
->throw()
->json();
} catch (\Exception $exception) {
return ['error' => $exception->getMessage()];
}
}

public function put(string $url, array $params)
{
try {
return $this->http
->put($url, $params)
->throw()
->json();
} catch (\Exception $exception) {
return ['error' => $exception->getMessage()];
}
}
}
17 changes: 17 additions & 0 deletions app/Services/PaymentGateway/Contracts/AdapterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace App\Services\PaymentGateway\Contracts;

interface AdapterInterface
{

public function get(string $url);

public function post(string $url, array $params);

public function delete(string $url);

public function put(string $url, array $params);
}
17 changes: 17 additions & 0 deletions app/Services/PaymentGateway/Contracts/CustomerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace App\Services\PaymentGateway\Contracts;

interface CustomerInterface
{

public function list(array $filters = []): array;

public function create(array $data): array;

public function update(int|string $id, array $data): array;

public function delete(int|string $id): array;
}
22 changes: 22 additions & 0 deletions app/Services/PaymentGateway/Contracts/PaymentInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Services\PaymentGateway\Contracts;

interface PaymentInterface
{

public function list(array $filters = []): array;

public function create(array $data): array;

public function update(int|string $id, array $data): array;

public function delete(int|string $id): array;

public function getPaymentStatus(int|string $id): array;

public function getInvoiceCode(int|string $id): array;

public function getPixQrCode(int|string $id): array;

}
28 changes: 28 additions & 0 deletions app/Services/PaymentGateway/Gateway.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace App\Services\PaymentGateway;

use App\Services\PaymentGateway\Connectors\Asaas\Customer;
use App\Services\PaymentGateway\Connectors\Asaas\Payment;
use App\Services\PaymentGateway\Contracts\AdapterInterface;

class Gateway
{

public function __construct(
public AdapterInterface $adapter
) {
}

public function customer(): Customer
{
return new Customer($this->adapter);
}

public function payment(): Payment
{
return new Payment($this->adapter);
}
}
Loading

0 comments on commit 11de7d6

Please sign in to comment.