From 2dacb9576d14c28fedc573e7a7b61877512bf900 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Lucas?= Date: Sat, 30 Nov 2024 23:23:21 -0300 Subject: [PATCH 1/8] PAYH-4: refactor: auth and client resouce --- examples/asaas.php | 8 ++- src/Contracts/GatewayInterface.php | 6 +-- src/Gateways/Asaas/AsaasGateway.php | 66 ++++++------------------- src/Gateways/Asaas/Resources/Auth.php | 38 ++++++++++++++ src/Gateways/Asaas/Resources/Client.php | 58 ++++++++++++++++++++++ 5 files changed, 117 insertions(+), 59 deletions(-) create mode 100644 src/Gateways/Asaas/Resources/Auth.php create mode 100644 src/Gateways/Asaas/Resources/Client.php diff --git a/examples/asaas.php b/examples/asaas.php index 338fe09..5eee182 100644 --- a/examples/asaas.php +++ b/examples/asaas.php @@ -23,11 +23,9 @@ /* instance singleton */ $payhub = (new Payhub()) ->gateway(Gateways::ASAAS) - ->authorize($credentials, sandbox: true); - -$payhub - ->client($client) - ->pix($pix); + ->auth($credentials, sandbox: true) + ->client() + ->store($client); // TODO: create feature to extend resources // TODO: command with stubs to create resources diff --git a/src/Contracts/GatewayInterface.php b/src/Contracts/GatewayInterface.php index 11c3e38..d741006 100644 --- a/src/Contracts/GatewayInterface.php +++ b/src/Contracts/GatewayInterface.php @@ -6,9 +6,7 @@ interface GatewayInterface { - public function authorize(array $credentials, bool $sandbox = true): self|Exception; - - public function client(array $client): self|Exception; - + public function auth(array $credentials, bool $sandbox = true): object; + public function client(): object; public function pix(array $pix): self|Exception; } diff --git a/src/Gateways/Asaas/AsaasGateway.php b/src/Gateways/Asaas/AsaasGateway.php index ad99f1d..82ef253 100644 --- a/src/Gateways/Asaas/AsaasGateway.php +++ b/src/Gateways/Asaas/AsaasGateway.php @@ -5,37 +5,27 @@ use Illuminate\Support\Facades\Http; use Payhub\Contracts\GatewayInterface; use Payhub\Exceptions\AsaasExceptions; -use Payhub\Gateways\Asaas\Requests\AsaasClientRequest; use Payhub\Gateways\Asaas\Requests\AsaasPixRequest; +use Payhub\Gateways\Asaas\Resources\Auth; +use Payhub\Gateways\Asaas\Resources\Client; class AsaasGateway implements GatewayInterface { /** - * @var string + * @var array */ - protected string $client; + protected array $client; - public function authorize(array $credentials, bool $sandbox = true): self + /** + * auth + * + * @param array $credentials + * @param bool $sandbox + * @return $this + */ + public function auth(array $credentials, bool $sandbox = true): self { - $baseUrl = $sandbox ? - 'https://sandbox.asaas.com/api/v3' : - 'https://api.asaas.com/v3'; - - extract($credentials); - - if (! isset($token)) { - return (new AsaasExceptions())('As credenciais do asaas precisam de um parâmetro token.'); - } - - Http::macro('asaas', function () use ($token, $baseUrl) { - return Http::acceptJson() - ->baseUrl($baseUrl) - ->withHeaders([ - 'content-type' => 'application/json', - 'user-agent' => 'payhub', - 'access_token' => $token, - ]); - }); + (new Auth())($credentials, $sandbox); return $this; } @@ -44,35 +34,11 @@ public function authorize(array $credentials, bool $sandbox = true): self * set client * * @param array $client + * @return Client */ - public function client(array $client): self + public function client(): Client { - AsaasClientRequest::validate($client); - - try { - $clientExists = (object) Http::asaas() - ->get('/customers', [ - 'cpfCnpj' => $client['cpf_cnpj'], - ])->json(); - - if (empty($clientExists->data)) { - $client = Http::asaas() - ->post('/customers', [ - 'name' => $client['name'], - 'cpfCnpj' => $client['cpf_cnpj'], - ])->json(); - - $this->client = $client['id']; - - return $this; - } - - $this->client = $clientExists->data[0]['id']; - - return $this; - } catch (\Exception $e) { - return (new AsaasExceptions())($e->getMessage()); - } + return new Client(); } /** diff --git a/src/Gateways/Asaas/Resources/Auth.php b/src/Gateways/Asaas/Resources/Auth.php new file mode 100644 index 0000000..78bdfbf --- /dev/null +++ b/src/Gateways/Asaas/Resources/Auth.php @@ -0,0 +1,38 @@ +baseUrl($baseUrl) + ->withHeaders([ + 'content-type' => 'application/json', + 'user-agent' => 'payhub', + 'access_token' => $token, + ]); + }); + } +} diff --git a/src/Gateways/Asaas/Resources/Client.php b/src/Gateways/Asaas/Resources/Client.php new file mode 100644 index 0000000..8ba97f8 --- /dev/null +++ b/src/Gateways/Asaas/Resources/Client.php @@ -0,0 +1,58 @@ +get('/customers', [ + 'cpfCnpj' => $client['cpf_cnpj'], + ])->json(); + + if (empty($clientExists->data)) { + $client = Http::asaas() + ->post('/customers', [ + 'name' => $client['name'], + 'cpfCnpj' => $client['cpf_cnpj'], + ])->json(); + + $this->clientId = $client['id']; + + return $this; + } + + $this->clientId = $clientExists->data[0]['id']; + + return $this; + } catch (\Exception $e) { + return (new AsaasExceptions())($e->getMessage()); + } + } +} From f2bb7ece03592c7c8992fb9097cd3b37d65e969e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Lucas?= Date: Sun, 1 Dec 2024 06:02:12 -0300 Subject: [PATCH 2/8] PAYH-4: feat: addeed remove client action --- composer.json | 5 +- composer.lock | 2 +- examples/asaas.php | 20 +++++- src/.env | 10 +++ src/Contracts/GatewayInterface.php | 4 +- src/Exceptions/AsaasExceptions.php | 2 +- src/Gateways/Asaas/AsaasGateway.php | 51 ++++++++++---- src/Gateways/Asaas/Enums/BillingType.php | 11 +++ src/Gateways/Asaas/Enums/ClientMethods.php | 12 ++++ src/Gateways/Asaas/Resources/Auth.php | 4 +- src/Gateways/Asaas/Resources/Client.php | 79 +++++++++++++++------- src/Payhub.php | 5 ++ 12 files changed, 155 insertions(+), 50 deletions(-) create mode 100644 src/.env create mode 100644 src/Gateways/Asaas/Enums/BillingType.php create mode 100644 src/Gateways/Asaas/Enums/ClientMethods.php diff --git a/composer.json b/composer.json index 7b96380..8084fa9 100644 --- a/composer.json +++ b/composer.json @@ -21,9 +21,10 @@ } }, "require": { - "laravel/framework": "^11.32" + "laravel/framework": "^11.32", + "vlucas/phpdotenv": "^5.6" }, "scripts": { "test": "vendor/bin/pest" } -} \ No newline at end of file +} diff --git a/composer.lock b/composer.lock index 3204ff3..abc04f2 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "53cb6b935788dbf3b6d475dea016f054", + "content-hash": "b595cbd9e5385c047b710d39017efcd0", "packages": [ { "name": "brick/math", diff --git a/examples/asaas.php b/examples/asaas.php index 5eee182..cedd99a 100644 --- a/examples/asaas.php +++ b/examples/asaas.php @@ -2,6 +2,7 @@ use Payhub\Payhub; use Payhub\Enums\Gateways; +use Payhub\Gateways\Asaas\Enums\ClientMethods; require_once __DIR__ . '/../vendor/autoload.php'; @@ -23,9 +24,22 @@ /* instance singleton */ $payhub = (new Payhub()) ->gateway(Gateways::ASAAS) - ->auth($credentials, sandbox: true) - ->client() - ->store($client); + ->auth($credentials, sandbox: true); + +/* cadastrar client no asaas */ +$payhub + ->client($client) + ->pix($pix); + +/* excluir cliente */ +// $payhub +// ->client($client, method: 'delete'); + +/* localizar cliente no asaas pelo documento */ +// $payhub +// ->client() +// ->find('09102295466'); + // TODO: create feature to extend resources // TODO: command with stubs to create resources diff --git a/src/.env b/src/.env new file mode 100644 index 0000000..caee03a --- /dev/null +++ b/src/.env @@ -0,0 +1,10 @@ +# ASAAS API BASE +ASSAS_URL=https://api.asaas.com/v3 +ASAAS_URL_SANDBOX=https://sandbox.asaas.com/api/v3 + +# ASAAS CLIENTS ENDPOINTS +ASSAS_CLIENTS=/customers +ASSAS_CLIENT=/customers/{id} +ASSAS_CLIENT_RESTORE=/customers/{id}/restore +ASAAS_CLIENT_NOTIFICATIONS=/customers/{id}/notifications + diff --git a/src/Contracts/GatewayInterface.php b/src/Contracts/GatewayInterface.php index d741006..3618d00 100644 --- a/src/Contracts/GatewayInterface.php +++ b/src/Contracts/GatewayInterface.php @@ -7,6 +7,6 @@ interface GatewayInterface { public function auth(array $credentials, bool $sandbox = true): object; - public function client(): object; - public function pix(array $pix): self|Exception; + public function client(array $client): object; + public function pix(array $pix): object; } diff --git a/src/Exceptions/AsaasExceptions.php b/src/Exceptions/AsaasExceptions.php index 227c430..0fc9928 100644 --- a/src/Exceptions/AsaasExceptions.php +++ b/src/Exceptions/AsaasExceptions.php @@ -4,7 +4,7 @@ use Exception; -class AsaasExceptions +final class AsaasExceptions { /** * AsaasExceptions invokable. diff --git a/src/Gateways/Asaas/AsaasGateway.php b/src/Gateways/Asaas/AsaasGateway.php index 82ef253..dee3a3b 100644 --- a/src/Gateways/Asaas/AsaasGateway.php +++ b/src/Gateways/Asaas/AsaasGateway.php @@ -5,16 +5,21 @@ use Illuminate\Support\Facades\Http; use Payhub\Contracts\GatewayInterface; use Payhub\Exceptions\AsaasExceptions; +use Payhub\Gateways\Asaas\Enums\{BillingType, ClientMethods}; use Payhub\Gateways\Asaas\Requests\AsaasPixRequest; -use Payhub\Gateways\Asaas\Resources\Auth; -use Payhub\Gateways\Asaas\Resources\Client; +use Payhub\Gateways\Asaas\Resources\{Auth, Client}; class AsaasGateway implements GatewayInterface { + /** + * @var string + */ + private string $client; + /** * @var array */ - protected array $client; + private array $payment; /** * auth @@ -34,11 +39,25 @@ public function auth(array $credentials, bool $sandbox = true): self * set client * * @param array $client - * @return Client + * @return self */ - public function client(): Client + public function client(array $client, string $method = 'store'): self|AsaasExceptions { - return new Client(); + if (! ClientMethods::tryFrom($method)) { + return (new AsaasExceptions())('Method not found'); + } + + if (! method_exists(Client::class, $method)) { + return (new AsaasExceptions())('Method not found'); + } + + try { + $this->client = (new Client())::$method($client); + } catch (\Exception $e) { + return (new AsaasExceptions())($e->getMessage()); + } + + return $this; } /** @@ -46,25 +65,29 @@ public function client(): Client * * @param array $pix */ - public function pix(array $pix): self + public function pix(array $pix): self|AsaasExceptions { try { AsaasPixRequest::validate($pix); - $pix = Http::asaas() + extract($pix); + + $payment = Http::asaas() ->post('/payments', [ 'customer' => $this->client, - 'billingType' => 'BOLETO', - 'value' => $pix['amount'], - 'dueDate' => $pix['due_date'], - 'description' => $pix['description'], + 'billingType' => BillingType::PIX->value, + 'value' => $amount, + 'dueDate' => $due_date, + 'description' => $description, ])->json(); - print_r($pix); + print_r($payment); - return $this; + $this->payment = $payment; } catch (\Exception $e) { return (new AsaasExceptions())($e->getMessage()); } + + return $this; } } diff --git a/src/Gateways/Asaas/Enums/BillingType.php b/src/Gateways/Asaas/Enums/BillingType.php new file mode 100644 index 0000000..30d5888 --- /dev/null +++ b/src/Gateways/Asaas/Enums/BillingType.php @@ -0,0 +1,11 @@ +post(env('ASSAS_CLIENTS'), [ + 'name' => $name, + 'cpfCnpj' => $cpf_cnpj, + ])->json(); + + return $client['id']; + + } catch (\Exception $e) { + return (new AsaasExceptions())($e->getMessage()); + } + } /** - * Client constructor. + * check if client exists + * + * @param string $client + * @return array */ - public function __construct() + public static function find(string $cpfCnpj): array { + $client = (object) Http::asaas() + ->get(env('ASSAS_CLIENTS'), [ + 'cpfCnpj' => $cpfCnpj, + ])->json(); + + return $client->data; } /** - * store client + * update client * * @param array $client - * @return $this + * @return string */ - public function store(array $client): self + public static function delete(array $client): string { - AsaasClientRequest::validate($client); - try { - $clientExists = (object) Http::asaas() - ->get('/customers', [ - 'cpfCnpj' => $client['cpf_cnpj'], - ])->json(); + $client = self::find($client['cpf_cnpj']); - if (empty($clientExists->data)) { - $client = Http::asaas() - ->post('/customers', [ - 'name' => $client['name'], - 'cpfCnpj' => $client['cpf_cnpj'], - ])->json(); + if (empty($client)) { + return 'Client not found'; + } - $this->clientId = $client['id']; + $client = Http::asaas() + ->delete(env('ASSAS_CLIENTS') . '/' . $client[0]['id']) + ->json(); - return $this; - } + echo 'Client deleted'; - $this->clientId = $clientExists->data[0]['id']; + return $client['id']; - return $this; } catch (\Exception $e) { return (new AsaasExceptions())($e->getMessage()); } diff --git a/src/Payhub.php b/src/Payhub.php index 8cfb1cc..65672a3 100644 --- a/src/Payhub.php +++ b/src/Payhub.php @@ -2,6 +2,7 @@ namespace Payhub; +use Dotenv\Dotenv; use Exception; use Illuminate\Container\Container; use Illuminate\Http\Client\Factory; @@ -29,9 +30,13 @@ public function __construct() Facade::setFacadeApplication($container); $container->singleton('http', function () { + return new Factory(); }); + $dotenv = Dotenv::createImmutable(__DIR__); + $dotenv->load(); + class_alias(Http::class, 'Http'); } From 6ce5cb7da3663bee3bde3ef3ef069bc5990f40b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Lucas?= Date: Sun, 1 Dec 2024 06:04:00 -0300 Subject: [PATCH 3/8] PAYH-4: fix: method docblock --- src/Gateways/Asaas/Resources/Client.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Gateways/Asaas/Resources/Client.php b/src/Gateways/Asaas/Resources/Client.php index b5128f7..da6da3f 100644 --- a/src/Gateways/Asaas/Resources/Client.php +++ b/src/Gateways/Asaas/Resources/Client.php @@ -19,7 +19,6 @@ public static function store(array $client): string AsaasClientRequest::validate($client); try { - extract($client); $client = self::find($cpf_cnpj); @@ -58,7 +57,7 @@ public static function find(string $cpfCnpj): array } /** - * update client + * delete client * * @param array $client * @return string From 0cb84f7228a3f30c6fc27dcd30fb9e4e34257b38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Lucas?= Date: Sun, 1 Dec 2024 16:18:32 -0300 Subject: [PATCH 4/8] PAYH-4: feat: adding get all clients with filters --- examples/asaas.php | 56 +++++++++--- src/Contracts/GatewayInterface.php | 4 +- src/Enums/Gateways.php | 8 -- src/Gateways/Asaas/AsaasGateway.php | 57 ++++++------ src/Gateways/Asaas/Resources/Auth.php | 4 +- src/Gateways/Asaas/Resources/Client.php | 87 ++++++++++++++++--- .../Resources/Interfaces/ClientInterface.php | 11 +++ src/Gateways/Gateway.php | 8 ++ src/Payhub.php | 55 ++++++++---- 9 files changed, 207 insertions(+), 83 deletions(-) delete mode 100644 src/Enums/Gateways.php create mode 100644 src/Gateways/Asaas/Resources/Interfaces/ClientInterface.php create mode 100644 src/Gateways/Gateway.php diff --git a/examples/asaas.php b/examples/asaas.php index cedd99a..b098150 100644 --- a/examples/asaas.php +++ b/examples/asaas.php @@ -1,8 +1,7 @@ '$aact_YTU5YTE0M2M2N2I4MTliNzk0YTI5N2U5MzdjNWZmNDQ6OjAwMDAwMDAwMDAwMDAwOTQ5MzU6OiRhYWNoXzY0ZjVjOGZlLTljMDMtNDM0MS05MTQ3LWRjYTkxNDdmMzBkZA==', ]; -/* instance singleton */ -$payhub = (new Payhub()) - ->gateway(Gateways::ASAAS) - ->auth($credentials, sandbox: true); +/** + * initialize payhub + * + * @param array $credentials + * @param bool $sandbox + * + * @return AsaasGateway + */ +$payhub = Payhub::asaas( + $credentials, + sandbox: true +); -/* cadastrar client no asaas */ -$payhub +/** + * store asaas cliente + * + * @param array $client + * @return string cliente id asaas + */ +$response = $payhub ->client($client) - ->pix($pix); + ->store(); + +/** + * list all clients with filters + * + * @return array clients + */ +$response = $payhub + ->client() + ->with(['cpfCnpj' => '09102295466',]) + ->all(); + +/** + * delete cliente no asaas + * + * @param array $client + * @return bool + */ +$payhub->client($client) + ->delete(); + + +/* editar cliente no asaas */ +// $payhub->client($client, method: 'update'); -/* excluir cliente */ -// $payhub -// ->client($client, method: 'delete'); /* localizar cliente no asaas pelo documento */ // $payhub diff --git a/src/Contracts/GatewayInterface.php b/src/Contracts/GatewayInterface.php index 3618d00..5589049 100644 --- a/src/Contracts/GatewayInterface.php +++ b/src/Contracts/GatewayInterface.php @@ -2,11 +2,9 @@ namespace Payhub\Contracts; -use Exception; - interface GatewayInterface { - public function auth(array $credentials, bool $sandbox = true): object; + public function __construct(array $credentials, bool $sandbox = true); public function client(array $client): object; public function pix(array $pix): object; } diff --git a/src/Enums/Gateways.php b/src/Enums/Gateways.php deleted file mode 100644 index 941a9a4..0000000 --- a/src/Enums/Gateways.php +++ /dev/null @@ -1,8 +0,0 @@ -client = (new Client())::$method($client); - } catch (\Exception $e) { - return (new AsaasExceptions())($e->getMessage()); - } + // try { + // $this->customerId = (new Client())::$method($client); + // } catch (\Exception $e) { + // return (new AsaasExceptions())($e->getMessage()); + // } - return $this; - } + // return $this; + // } /** - * set pix + * generate pix * * @param array $pix */ @@ -74,16 +75,18 @@ public function pix(array $pix): self|AsaasExceptions $payment = Http::asaas() ->post('/payments', [ - 'customer' => $this->client, + 'customer' => $this->customerId, 'billingType' => BillingType::PIX->value, 'value' => $amount, 'dueDate' => $due_date, 'description' => $description, ])->json(); - print_r($payment); - $this->payment = $payment; + + echo 'Pix gerado com sucesso' . PHP_EOL; + echo 'Pix: ' . $this->payment['id'] . PHP_EOL; + } catch (\Exception $e) { return (new AsaasExceptions())($e->getMessage()); } diff --git a/src/Gateways/Asaas/Resources/Auth.php b/src/Gateways/Asaas/Resources/Auth.php index a3ecf8c..070f6fc 100644 --- a/src/Gateways/Asaas/Resources/Auth.php +++ b/src/Gateways/Asaas/Resources/Auth.php @@ -5,7 +5,7 @@ use Illuminate\Support\Facades\Http; use Payhub\Exceptions\AsaasExceptions; -class Auth +final class Auth { /** * Auth constructor. @@ -13,7 +13,7 @@ class Auth * @param array $credentials * @param bool $sandbox */ - public function __invoke(array $credentials, bool $sandbox): void + public function __construct(array $credentials, bool $sandbox) { $baseUrl = $sandbox ? env('ASAAS_URL_SANDBOX') : diff --git a/src/Gateways/Asaas/Resources/Client.php b/src/Gateways/Asaas/Resources/Client.php index da6da3f..6d2423e 100644 --- a/src/Gateways/Asaas/Resources/Client.php +++ b/src/Gateways/Asaas/Resources/Client.php @@ -5,21 +5,63 @@ use Illuminate\Support\Facades\Http; use Payhub\Exceptions\AsaasExceptions; use Payhub\Gateways\Asaas\Requests\AsaasClientRequest; +use Payhub\Gateways\Asaas\Resources\Interfaces\ClientInterface; +use Payhub\Gateways\Gateway; -final class Client +final class Client implements ClientInterface { /** - * store client + * @var array + */ + protected array $client; + + /** + * @var array + */ + protected array $filters = []; + + /** + * @var Gateway + */ + protected Gateway $gateway; + + /** + * construct * * @param array $client + * @param Gateway $gateway + */ + public function __construct(array $client, Gateway $gateway) + { + $this->client = $client; + $this->gateway = $gateway; + } + + /** + * get all clients + * + * @return array + */ + public function all(): array + { + $client = Http::asaas() + ->get(env('ASSAS_CLIENTS') . '/?' . http_build_query($this->filters)) + ->json(); + + return $client; + } + + /** + * store client + * * @return $this */ - public static function store(array $client): string + public function store(): string { - AsaasClientRequest::validate($client); + AsaasClientRequest::validate($this->client); try { - extract($client); + extract($this->client); $client = self::find($cpf_cnpj); @@ -59,28 +101,45 @@ public static function find(string $cpfCnpj): array /** * delete client * - * @param array $client - * @return string + * @return bool */ - public static function delete(array $client): string + public function delete(): bool { try { - $client = self::find($client['cpf_cnpj']); + $client = self::find($this->client['cpf_cnpj']); if (empty($client)) { - return 'Client not found'; + return false; } $client = Http::asaas() ->delete(env('ASSAS_CLIENTS') . '/' . $client[0]['id']) ->json(); - echo 'Client deleted'; - - return $client['id']; + if ($client['deleted']) { + return true; + } + return false; } catch (\Exception $e) { - return (new AsaasExceptions())($e->getMessage()); + (new AsaasExceptions())($e->getMessage()); + + return false; } } + + /** + * set filters + * to ready about filters, see + * https://docs.asaas.com/reference/listar-clientes + * + * @param array $filter + * @return $this + */ + public function with(array $filter): self + { + $this->filters = $filter; + + return $this; + } } diff --git a/src/Gateways/Asaas/Resources/Interfaces/ClientInterface.php b/src/Gateways/Asaas/Resources/Interfaces/ClientInterface.php new file mode 100644 index 0000000..806abce --- /dev/null +++ b/src/Gateways/Asaas/Resources/Interfaces/ClientInterface.php @@ -0,0 +1,11 @@ +gateway = $gateway; + } + + /** + * boot laravel facade + * + * @return AsaasGateway + */ + private static function boot() { $container = new Container(); Facade::setFacadeApplication($container); $container->singleton('http', function () { - return new Factory(); }); @@ -41,20 +55,27 @@ class_alias(Http::class, 'Http'); } /** - * load Gateway class + * call asaas gateway * - * @param Gateways $gateway + * @return AsaasGateway */ - public static function gateway(Gateways $gateway): GatewayInterface|Exception + public static function asaas(array $credentials, $sandbox = true): self { - $class = "Payhub\\Gateways\\{$gateway->value}\\{$gateway->value}Gateway"; + self::boot(); - if (! class_exists($class)) { - return throw new \InvalidArgumentException('Gateway not found'); - } + return new self(new AsaasGateway($credentials, $sandbox)); + } - $gateway = new $class(); + /** + * call gateway + * + * @param array $client + * @return object + */ + public function client(array $client = []): object + { + $this->client = $client; - return $gateway; + return $this->gateway->client($client); } } From 03bf447dc8516ed740b2907f47cfc7b3fef500ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Lucas?= Date: Sun, 1 Dec 2024 16:26:06 -0300 Subject: [PATCH 5/8] PAYH-4: feat: get client with filters --- examples/asaas.php | 22 ++++++++++++++++++- src/Gateways/Asaas/Resources/Client.php | 14 ++++++++++++ .../Resources/Interfaces/ClientInterface.php | 1 + 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/examples/asaas.php b/examples/asaas.php index b098150..5726f33 100644 --- a/examples/asaas.php +++ b/examples/asaas.php @@ -39,7 +39,11 @@ * @param array $client * @return string cliente id asaas */ -$response = $payhub +$payhub + ->client($client) + ->store(); + +$payhub ->client($client) ->store(); @@ -53,6 +57,22 @@ ->with(['cpfCnpj' => '09102295466',]) ->all(); +print_r($response); + +/** + * get client by cpf_cnpj + * + * @param array $client + * @return array client + */ +$response = $payhub + ->client() + ->with(['cpfCnpj' => '09102295466']) + ->get(); + +echo 'Cliente: ' . $response['name'] . PHP_EOL; +print_r($response); + /** * delete cliente no asaas * diff --git a/src/Gateways/Asaas/Resources/Client.php b/src/Gateways/Asaas/Resources/Client.php index 6d2423e..47d2368 100644 --- a/src/Gateways/Asaas/Resources/Client.php +++ b/src/Gateways/Asaas/Resources/Client.php @@ -51,6 +51,20 @@ public function all(): array return $client; } + /** + * get client + * + * @return array + */ + public function get(): array + { + $client = Http::asaas() + ->get(env('ASSAS_CLIENTS') . '/?' . http_build_query($this->filters)) + ->json(); + + return $client['data'][0]; + } + /** * store client * diff --git a/src/Gateways/Asaas/Resources/Interfaces/ClientInterface.php b/src/Gateways/Asaas/Resources/Interfaces/ClientInterface.php index 806abce..a6a989d 100644 --- a/src/Gateways/Asaas/Resources/Interfaces/ClientInterface.php +++ b/src/Gateways/Asaas/Resources/Interfaces/ClientInterface.php @@ -5,6 +5,7 @@ interface ClientInterface { public function all(): array; + public function get(): array; public function with(array $filter): self; public function store(): string; public function delete(): bool; From f3e7ac1496c291f857bb654ca9788a2b7b2da847 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Lucas?= Date: Sun, 1 Dec 2024 16:44:08 -0300 Subject: [PATCH 6/8] PAYH-4: feat: list and list one client --- .gitignore | 3 +- examples/asaas.php | 97 ------------------- examples/asaas/clients.php | 66 +++++++++++++ examples/asaas/credentials.example.php | 3 + src/Contracts/GatewayInterface.php | 3 +- src/Gateways/Asaas/AsaasGateway.php | 6 +- src/Gateways/Asaas/Resources/Auth.php | 10 +- src/Gateways/Asaas/Resources/Client.php | 6 +- .../Resources/Interfaces/ClientInterface.php | 4 +- src/Payhub.php | 4 +- 10 files changed, 87 insertions(+), 115 deletions(-) delete mode 100644 examples/asaas.php create mode 100644 examples/asaas/clients.php create mode 100644 examples/asaas/credentials.example.php diff --git a/.gitignore b/.gitignore index ff87634..bbc3072 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ vendor/ -node_modules/ \ No newline at end of file +node_modules/ +examples/asaas/credentials.php \ No newline at end of file diff --git a/examples/asaas.php b/examples/asaas.php deleted file mode 100644 index 5726f33..0000000 --- a/examples/asaas.php +++ /dev/null @@ -1,97 +0,0 @@ - 'Mário Lucas', - 'cpf_cnpj' => '09102295466', -]; - -$pix = [ - 'description' => 'Teste de pagamento', - 'amount' => 100.00, - 'due_date' => date('Y-m-d'), -]; - -$credentials = [ - 'token' => '$aact_YTU5YTE0M2M2N2I4MTliNzk0YTI5N2U5MzdjNWZmNDQ6OjAwMDAwMDAwMDAwMDAwOTQ5MzU6OiRhYWNoXzY0ZjVjOGZlLTljMDMtNDM0MS05MTQ3LWRjYTkxNDdmMzBkZA==', -]; - -/** - * initialize payhub - * - * @param array $credentials - * @param bool $sandbox - * - * @return AsaasGateway - */ -$payhub = Payhub::asaas( - $credentials, - sandbox: true -); - -/** - * store asaas cliente - * - * @param array $client - * @return string cliente id asaas - */ -$payhub - ->client($client) - ->store(); - -$payhub - ->client($client) - ->store(); - -/** - * list all clients with filters - * - * @return array clients - */ -$response = $payhub - ->client() - ->with(['cpfCnpj' => '09102295466',]) - ->all(); - -print_r($response); - -/** - * get client by cpf_cnpj - * - * @param array $client - * @return array client - */ -$response = $payhub - ->client() - ->with(['cpfCnpj' => '09102295466']) - ->get(); - -echo 'Cliente: ' . $response['name'] . PHP_EOL; -print_r($response); - -/** - * delete cliente no asaas - * - * @param array $client - * @return bool - */ -$payhub->client($client) - ->delete(); - - -/* editar cliente no asaas */ -// $payhub->client($client, method: 'update'); - - -/* localizar cliente no asaas pelo documento */ -// $payhub -// ->client() -// ->find('09102295466'); - - -// TODO: create feature to extend resources -// TODO: command with stubs to create resources diff --git a/examples/asaas/clients.php b/examples/asaas/clients.php new file mode 100644 index 0000000..2ce87d5 --- /dev/null +++ b/examples/asaas/clients.php @@ -0,0 +1,66 @@ + '', /* client name */ + 'cpf_cnpj' => '' /* valid cpf or cnpj */, +]; + +/** + * initialize payhub + * + * @param array $credentials + * @param bool $sandbox + * + * @return AsaasGateway + */ +$payhub = Payhub::asaas(TOKEN_ASAAS_SANDBOX); + +/** + * store asaas cliente + * + * @param array $client + * @return string cliente id asaas + */ +$payhub + ->client($client) + ->store(); + +/** + * list all clients with filters + * + * @return array clients + */ +$response = $payhub + ->client() + ->with(['cpfCnpj' => '09102295466',]) + ->all(); + +// print_r($response); + +/** + * get client by cpf_cnpj + * + * @param array $client + * @return array client + */ +$response = $payhub + ->client() + ->with(['cpfCnpj' => '09102295466']) + ->get(); + +// print_r($response); + +/** + * delete cliente no asaas + * + * @param array $client + * @return bool + */ +$payhub->client($client) + ->delete(); diff --git a/examples/asaas/credentials.example.php b/examples/asaas/credentials.example.php new file mode 100644 index 0000000..29bb7e1 --- /dev/null +++ b/examples/asaas/credentials.example.php @@ -0,0 +1,3 @@ +baseUrl($baseUrl) diff --git a/src/Gateways/Asaas/Resources/Client.php b/src/Gateways/Asaas/Resources/Client.php index 47d2368..c59914d 100644 --- a/src/Gateways/Asaas/Resources/Client.php +++ b/src/Gateways/Asaas/Resources/Client.php @@ -70,7 +70,7 @@ public function get(): array * * @return $this */ - public function store(): string + public function store(): string|AsaasExceptions { AsaasClientRequest::validate($this->client); @@ -89,6 +89,10 @@ public function store(): string 'cpfCnpj' => $cpf_cnpj, ])->json(); + if (isset($client['errors'])) { + return (new AsaasExceptions())($client['errors'][0]['description']); + } + return $client['id']; } catch (\Exception $e) { diff --git a/src/Gateways/Asaas/Resources/Interfaces/ClientInterface.php b/src/Gateways/Asaas/Resources/Interfaces/ClientInterface.php index a6a989d..d74a9a1 100644 --- a/src/Gateways/Asaas/Resources/Interfaces/ClientInterface.php +++ b/src/Gateways/Asaas/Resources/Interfaces/ClientInterface.php @@ -2,11 +2,13 @@ namespace Payhub\Gateways\Asaas\Resources\Interfaces; +use Payhub\Exceptions\AsaasExceptions; + interface ClientInterface { public function all(): array; public function get(): array; public function with(array $filter): self; - public function store(): string; + public function store(): string|AsaasExceptions; public function delete(): bool; } diff --git a/src/Payhub.php b/src/Payhub.php index 67df48a..7b89f9b 100644 --- a/src/Payhub.php +++ b/src/Payhub.php @@ -59,11 +59,11 @@ class_alias(Http::class, 'Http'); * * @return AsaasGateway */ - public static function asaas(array $credentials, $sandbox = true): self + public static function asaas(string $token, $sandbox = true): self { self::boot(); - return new self(new AsaasGateway($credentials, $sandbox)); + return new self(new AsaasGateway($token, $sandbox)); } /** From 21faac55ecacb632c6df7713715cfbcbd17d2841 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Lucas?= Date: Sun, 1 Dec 2024 17:15:59 -0300 Subject: [PATCH 7/8] PAYH-4: feat: adding restore client --- examples/asaas/clients.php | 13 ++- examples/asaas/credentials.example.php | 4 + src/Gateways/Asaas/Resources/Client.php | 80 +++++++++++++------ .../Resources/Interfaces/ClientInterface.php | 4 +- 4 files changed, 73 insertions(+), 28 deletions(-) diff --git a/examples/asaas/clients.php b/examples/asaas/clients.php index 2ce87d5..fae29bc 100644 --- a/examples/asaas/clients.php +++ b/examples/asaas/clients.php @@ -7,8 +7,8 @@ require_once __DIR__ . '/credentials.php'; $client = [ - 'name' => '', /* client name */ - 'cpf_cnpj' => '' /* valid cpf or cnpj */, + 'name' => NAME, + 'cpf_cnpj' => CPF_CNPJ ]; /** @@ -64,3 +64,12 @@ */ $payhub->client($client) ->delete(); + +/** + * restore cliente no asaas + * + * @param array $client + * @return bool + */ +$payhub->client($client) + ->restore(); diff --git a/examples/asaas/credentials.example.php b/examples/asaas/credentials.example.php index 29bb7e1..9a943a7 100644 --- a/examples/asaas/credentials.example.php +++ b/examples/asaas/credentials.example.php @@ -1,3 +1,7 @@ get(env('ASSAS_CLIENTS'), [ + 'cpfCnpj' => $cpfCnpj, + ])->json(); + + return $client->data; + } + + /** + * set filters + * to ready about filters, see + * https://docs.asaas.com/reference/listar-clientes + * + * @param array $filter + * @return $this + */ + public function with(array $filter): self + { + $this->filters = $filter; + + return $this; + } + /** * store client * @@ -100,22 +131,6 @@ public function store(): string|AsaasExceptions } } - /** - * check if client exists - * - * @param string $client - * @return array - */ - public static function find(string $cpfCnpj): array - { - $client = (object) Http::asaas() - ->get(env('ASSAS_CLIENTS'), [ - 'cpfCnpj' => $cpfCnpj, - ])->json(); - - return $client->data; - } - /** * delete client * @@ -124,7 +139,7 @@ public static function find(string $cpfCnpj): array public function delete(): bool { try { - $client = self::find($this->client['cpf_cnpj']); + $client = $this->find($this->client['cpf_cnpj']); if (empty($client)) { return false; @@ -147,17 +162,32 @@ public function delete(): bool } /** - * set filters - * to ready about filters, see - * https://docs.asaas.com/reference/listar-clientes + * restore client * - * @param array $filter - * @return $this + * @return bool */ - public function with(array $filter): self + public function restore(): bool { - $this->filters = $filter; + try { + $client = $this->find($this->client['cpf_cnpj']); - return $this; + if (empty($client)) { + return false; + } + + $client = Http::asaas() + ->post(env('ASSAS_CLIENTS') . '/' . $client[0]['id'] . '/restore') + ->json(); + + if ($client['restored']) { + return true; + } + + return false; + } catch (\Exception $e) { + (new AsaasExceptions())($e->getMessage()); + + return false; + } } } diff --git a/src/Gateways/Asaas/Resources/Interfaces/ClientInterface.php b/src/Gateways/Asaas/Resources/Interfaces/ClientInterface.php index d74a9a1..2cb3d0e 100644 --- a/src/Gateways/Asaas/Resources/Interfaces/ClientInterface.php +++ b/src/Gateways/Asaas/Resources/Interfaces/ClientInterface.php @@ -8,7 +8,9 @@ interface ClientInterface { public function all(): array; public function get(): array; - public function with(array $filter): self; + public function find(string $cpfCnpj): array; + public function with(array $filters): self; public function store(): string|AsaasExceptions; public function delete(): bool; + public function restore(): bool; } From 22b90c0d64e7a2cc5f561721f7b9ecc7db595fcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Lucas?= Date: Sun, 1 Dec 2024 17:32:10 -0300 Subject: [PATCH 8/8] PAYH-4: feat: adding restore client --- examples/asaas/clients.php | 7 +++++-- src/Gateways/Asaas/Resources/Client.php | 21 +++++++++---------- .../Resources/Interfaces/ClientInterface.php | 2 +- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/examples/asaas/clients.php b/examples/asaas/clients.php index fae29bc..8de41e0 100644 --- a/examples/asaas/clients.php +++ b/examples/asaas/clients.php @@ -71,5 +71,8 @@ * @param array $client * @return bool */ -$payhub->client($client) - ->restore(); +$response = $payhub + ->client() + ->restore('cus_000006376400'); + +// print_r($response); diff --git a/src/Gateways/Asaas/Resources/Client.php b/src/Gateways/Asaas/Resources/Client.php index 31b1e1c..d5fa9ea 100644 --- a/src/Gateways/Asaas/Resources/Client.php +++ b/src/Gateways/Asaas/Resources/Client.php @@ -164,24 +164,23 @@ public function delete(): bool /** * restore client * + * @param string $id * @return bool */ - public function restore(): bool + public function restore(string $id): bool { try { - $client = $this->find($this->client['cpf_cnpj']); - - if (empty($client)) { - return false; - } - $client = Http::asaas() - ->post(env('ASSAS_CLIENTS') . '/' . $client[0]['id'] . '/restore') + ->post(str_replace('{id}', $id, env('ASSAS_CLIENTS_RESTORE'))) ->json(); - if ($client['restored']) { - return true; - } + print_r($client); + + die(); + + // if ($client['restored']) { + // return true; + // } return false; } catch (\Exception $e) { diff --git a/src/Gateways/Asaas/Resources/Interfaces/ClientInterface.php b/src/Gateways/Asaas/Resources/Interfaces/ClientInterface.php index 2cb3d0e..721cc90 100644 --- a/src/Gateways/Asaas/Resources/Interfaces/ClientInterface.php +++ b/src/Gateways/Asaas/Resources/Interfaces/ClientInterface.php @@ -12,5 +12,5 @@ public function find(string $cpfCnpj): array; public function with(array $filters): self; public function store(): string|AsaasExceptions; public function delete(): bool; - public function restore(): bool; + public function restore(string $id): bool; }