-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: propoe uma nova integracao com a Asaas via adapter
- Loading branch information
1 parent
294bed8
commit 11de7d6
Showing
12 changed files
with
405 additions
and
14 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -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 | ||
|
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
21 changes: 21 additions & 0 deletions
21
app/Services/PaymentGateway/Connectors/Asaas/Concerns/AsaasConfig.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,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); | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
app/Services/PaymentGateway/Connectors/Asaas/Concerns/HasFilter.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,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); | ||
} | ||
} |
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,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); | ||
} | ||
} |
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 | ||
|
||
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" ); | ||
} | ||
} |
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,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
17
app/Services/PaymentGateway/Contracts/AdapterInterface.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,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
17
app/Services/PaymentGateway/Contracts/CustomerInterface.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,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
22
app/Services/PaymentGateway/Contracts/PaymentInterface.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,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; | ||
|
||
} |
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,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); | ||
} | ||
} |
Oops, something went wrong.