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/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 deleted file mode 100644 index 338fe09..0000000 --- a/examples/asaas.php +++ /dev/null @@ -1,33 +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==', -]; - -/* instance singleton */ -$payhub = (new Payhub()) - ->gateway(Gateways::ASAAS) - ->authorize($credentials, sandbox: true); - -$payhub - ->client($client) - ->pix($pix); - -// 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..8de41e0 --- /dev/null +++ b/examples/asaas/clients.php @@ -0,0 +1,78 @@ + NAME, + 'cpf_cnpj' => CPF_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(); + +/** + * restore cliente no asaas + * + * @param array $client + * @return bool + */ +$response = $payhub + ->client() + ->restore('cus_000006376400'); + +// print_r($response); diff --git a/examples/asaas/credentials.example.php b/examples/asaas/credentials.example.php new file mode 100644 index 0000000..9a943a7 --- /dev/null +++ b/examples/asaas/credentials.example.php @@ -0,0 +1,7 @@ +baseUrl($baseUrl) - ->withHeaders([ - 'content-type' => 'application/json', - 'user-agent' => 'payhub', - 'access_token' => $token, - ]); - }); + /** + * @var array + */ + private array $payment; - return $this; + /** + * construct + * + * @param string $token + * @param bool $sandbox + */ + public function __construct(string $token, bool $sandbox = true) + { + new Auth($token, $sandbox); } /** * set client * * @param array $client + * @return Client */ - public function client(array $client): self + public function client(array $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(); + return new Client($client, $this); + } + // { + // if (! ClientMethods::tryFrom($method)) { + // return (new AsaasExceptions())('Method not found'); + // } - $this->client = $client['id']; + // if (! method_exists(Client::class, $method)) { + // return (new AsaasExceptions())('Method not found'); + // } - return $this; - } + // try { + // $this->customerId = (new Client())::$method($client); + // } catch (\Exception $e) { + // return (new AsaasExceptions())($e->getMessage()); + // } - $this->client = $clientExists->data[0]['id']; - - return $this; - } catch (\Exception $e) { - return (new AsaasExceptions())($e->getMessage()); - } - } + // return $this; + // } /** - * set pix + * generate pix * * @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'], + 'customer' => $this->customerId, + 'billingType' => BillingType::PIX->value, + 'value' => $amount, + 'dueDate' => $due_date, + 'description' => $description, ])->json(); - print_r($pix); + $this->payment = $payment; + + echo 'Pix gerado com sucesso' . PHP_EOL; + echo 'Pix: ' . $this->payment['id'] . PHP_EOL; - return $this; } 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 @@ +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..d5fa9ea --- /dev/null +++ b/src/Gateways/Asaas/Resources/Client.php @@ -0,0 +1,192 @@ +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; + } + + /** + * 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]; + } + + /** + * check if client exists + * + * @param string $client + * @return array + */ + public function find(string $cpfCnpj): array + { + $client = (object) Http::asaas() + ->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 + * + * @return $this + */ + public function store(): string|AsaasExceptions + { + AsaasClientRequest::validate($this->client); + + try { + extract($this->client); + + $client = self::find($cpf_cnpj); + + if (! empty($client)) { + return $client[0]['id']; + } + + $client = Http::asaas() + ->post(env('ASSAS_CLIENTS'), [ + 'name' => $name, + 'cpfCnpj' => $cpf_cnpj, + ])->json(); + + if (isset($client['errors'])) { + return (new AsaasExceptions())($client['errors'][0]['description']); + } + + return $client['id']; + + } catch (\Exception $e) { + return (new AsaasExceptions())($e->getMessage()); + } + } + + /** + * delete client + * + * @return bool + */ + public function delete(): bool + { + try { + $client = $this->find($this->client['cpf_cnpj']); + + if (empty($client)) { + return false; + } + + $client = Http::asaas() + ->delete(env('ASSAS_CLIENTS') . '/' . $client[0]['id']) + ->json(); + + if ($client['deleted']) { + return true; + } + + return false; + } catch (\Exception $e) { + (new AsaasExceptions())($e->getMessage()); + + return false; + } + } + + /** + * restore client + * + * @param string $id + * @return bool + */ + public function restore(string $id): bool + { + try { + $client = Http::asaas() + ->post(str_replace('{id}', $id, env('ASSAS_CLIENTS_RESTORE'))) + ->json(); + + print_r($client); + + die(); + + // 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 new file mode 100644 index 0000000..721cc90 --- /dev/null +++ b/src/Gateways/Asaas/Resources/Interfaces/ClientInterface.php @@ -0,0 +1,16 @@ +gateway = $gateway; + } + + /** + * boot laravel facade + * + * @return AsaasGateway + */ + private static function boot() { $container = new Container(); @@ -32,24 +48,34 @@ public function __construct() return new Factory(); }); + $dotenv = Dotenv::createImmutable(__DIR__); + $dotenv->load(); + 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(string $token, $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($token, $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); } }