diff --git a/examples/asaas/clients.php b/examples/asaas/clients.php index f2c32ec..4b9b4cd 100644 --- a/examples/asaas/clients.php +++ b/examples/asaas/clients.php @@ -25,9 +25,7 @@ * @param array $client * @return string cliente id asaas */ -$phpay - ->client($client) - ->store(); +$phpay->client($client); /** * list all clients with filters @@ -39,7 +37,7 @@ ->with(['cpfCnpj' => '09102295466', ]) ->all(); -// print_r($response); +print_r($response); /** * get client by cpf_cnpj @@ -52,7 +50,7 @@ ->with(['cpfCnpj' => '09102295466']) ->get(); -// print_r($response); +print_r($response); /** * delete cliente no asaas @@ -61,7 +59,7 @@ * @return bool */ $phpay - ->client($client) + ->client($client, false) ->delete(); /** @@ -71,7 +69,7 @@ ->client() ->restore('cus_000006376400'); -// print_r($response); +print_r($response); /** * notifications cliente no asaas @@ -80,4 +78,4 @@ ->client() ->notifications('cus_000006376400'); -// print_r($response); +print_r($response); diff --git a/examples/asaas/invoices.php b/examples/asaas/invoices.php new file mode 100644 index 0000000..6d2dd18 --- /dev/null +++ b/examples/asaas/invoices.php @@ -0,0 +1,25 @@ + NAME, + 'cpf_cnpj' => CPF_CNPJ, +]; + +$invoice = [ + 'billingType' => 'BOLETO', + 'value' => 100.00, + 'description' => 'Teste de fatura', + 'dueDate' => date('Y-m-d', strtotime('+1 day')), +]; + +$data = PHPay::asaas(TOKEN_ASAAS_SANDBOX) + ->client($client) + ->invoice($invoice); + +print_r($data); diff --git a/src/.env b/src/.env index caee03a..e0eef7e 100644 --- a/src/.env +++ b/src/.env @@ -8,3 +8,8 @@ ASSAS_CLIENT=/customers/{id} ASSAS_CLIENT_RESTORE=/customers/{id}/restore ASAAS_CLIENT_NOTIFICATIONS=/customers/{id}/notifications +# ASAAS PAYMENTS ENDPOINTS +ASSAS_PAYMENTS=/payments +ASAAS_PAYMENT=/payments/{id} +ASAAS_PAYMENT_INFO=/payments/{id}/billingInfo +ASAAS_PAYMENT_VIEW_INFO=/payments/{id}/viewingInfo \ No newline at end of file diff --git a/src/Contracts/GatewayInterface.php b/src/Contracts/GatewayInterface.php index 46d8d2d..f13dcd4 100644 --- a/src/Contracts/GatewayInterface.php +++ b/src/Contracts/GatewayInterface.php @@ -6,4 +6,5 @@ interface GatewayInterface { public function __construct(string $token, bool $sandbox = true); public function client(array $client): object; + public function invoice(array $invoice): object; } diff --git a/src/Exceptions/AsaasExceptions.php b/src/Exceptions/AsaasExceptions.php index 6c76039..ff0dd32 100644 --- a/src/Exceptions/AsaasExceptions.php +++ b/src/Exceptions/AsaasExceptions.php @@ -13,6 +13,6 @@ final class AsaasExceptions */ public function __invoke(string $message): Exception { - return throw new Exception('Gateway Asaas: ' . $message); + return throw new Exception('ASAAS EXCEPTION: ' . $message); } } diff --git a/src/Gateways/Asaas/AsaasGateway.php b/src/Gateways/Asaas/AsaasGateway.php index 733b804..b97ba54 100644 --- a/src/Gateways/Asaas/AsaasGateway.php +++ b/src/Gateways/Asaas/AsaasGateway.php @@ -7,21 +7,31 @@ use PHPay\Exceptions\AsaasExceptions; use PHPay\Gateways\Asaas\Enums\{BillingType}; use PHPay\Gateways\Asaas\Requests\AsaasPixRequest; -use PHPay\Gateways\Asaas\Resources\{Auth, Client}; +use PHPay\Gateways\Asaas\Resources\{Auth, Client, Invoice}; use PHPay\Gateways\Gateway; class AsaasGateway extends Gateway implements GatewayInterface { /** - * @var string + * @var array */ - private string $customerId; + public array $client; + + /** + * @var array + */ + public array $invoice; /** * @var array */ private array $payment; + /** + * @var object + */ + public object $resource; + /** * construct * @@ -37,11 +47,48 @@ public function __construct(string $token, bool $sandbox = true) * set client * * @param array $client - * @return Client + * @return Client|self */ - public function client(array $client = []): Client - { - return new Client($client, $this); + public function client( + array $client = [], + bool $createOnly = true + ): Client|self { + $clientInstance = new Client($client, $this); + + if ($createOnly && !empty($client)) { + $this->client = $clientInstance->create()->client; + + return $this; + } + + return $clientInstance; + } + + /** + * set invoice + * + * @param array $invoice + * @return Invoice|self + */ + public function invoice( + array $invoice = [], + bool $createOnly = true + ): Invoice|self { + if (!isset($this->client['id'])) { + throw new \Exception('Para criar um fatura é necessário a criação de um cliente'); + } + + $invoice['customer'] = $this->client['id']; + + $invoiceInstance = new Invoice($invoice, $this); + + if ($createOnly && empty($invoice)) { + $this->invoice = $invoiceInstance->create()->invoice; + + $this; + } + + return $invoiceInstance; } /** @@ -58,7 +105,7 @@ public function pix(array $pix): self|AsaasExceptions $payment = Http::asaas() ->post('/payments', [ - 'customer' => $this->customerId, + 'customer' => $this->client['id'], 'billingType' => BillingType::PIX->value, 'value' => $amount, 'dueDate' => $due_date, diff --git a/src/Gateways/Asaas/Requests/AsaasClientRequest.php b/src/Gateways/Asaas/Requests/AsaasClientRequest.php index c77c738..ac503ce 100644 --- a/src/Gateways/Asaas/Requests/AsaasClientRequest.php +++ b/src/Gateways/Asaas/Requests/AsaasClientRequest.php @@ -28,7 +28,7 @@ public static function validate(array $client): void * * @return stdClass */ - public function messages(): stdClass + public static function messages(): stdClass { return (object) [ 'name' => 'Asaas: Nome do cliente é obrigatório para o Asaas', diff --git a/src/Gateways/Asaas/Resources/Client.php b/src/Gateways/Asaas/Resources/Client.php index 19dbf5a..80288ef 100644 --- a/src/Gateways/Asaas/Resources/Client.php +++ b/src/Gateways/Asaas/Resources/Client.php @@ -8,12 +8,17 @@ use PHPay\Gateways\Asaas\Resources\Interfaces\ClientInterface; use PHPay\Gateways\Gateway; -final class Client implements ClientInterface +final class Client extends Resource implements ClientInterface { /** * @var array */ - protected array $client; + public array $clients; + + /** + * @var array + */ + public array $client; /** * @var array @@ -42,13 +47,15 @@ public function __construct(array $client, Gateway $gateway) * * @return array */ - public function all(): array + public function all(): self { $client = Http::asaas() ->get(env('ASSAS_CLIENTS') . '/?' . http_build_query($this->filters)) ->json(); - return $client; + $this->clients = $client['data']; + + return $this; } /** @@ -97,11 +104,11 @@ public function with(array $filter): self } /** - * store client + * create client * * @return $this */ - public function store(): string|AsaasExceptions + public function create(): self|AsaasExceptions { AsaasClientRequest::validate($this->client); @@ -111,7 +118,9 @@ public function store(): string|AsaasExceptions $client = self::find($cpf_cnpj); if (!empty($client)) { - return $client[0]['id']; + $this->client = $client[0]; + + return $this; } $client = Http::asaas() @@ -124,11 +133,13 @@ public function store(): string|AsaasExceptions return (new AsaasExceptions())($client['errors'][0]['description']); } - return $client['id']; + $this->client = $client; } catch (\Exception $e) { return (new AsaasExceptions())($e->getMessage()); } + + return $this; } /** diff --git a/src/Gateways/Asaas/Resources/Interfaces/ClientInterface.php b/src/Gateways/Asaas/Resources/Interfaces/ClientInterface.php index cbfce96..7e4f802 100644 --- a/src/Gateways/Asaas/Resources/Interfaces/ClientInterface.php +++ b/src/Gateways/Asaas/Resources/Interfaces/ClientInterface.php @@ -6,11 +6,11 @@ interface ClientInterface { - public function all(): array; + public function all(): self; public function get(): array; public function find(string $cpfCnpj): array; public function with(array $filters): self; - public function store(): string|AsaasExceptions; + public function create(): self|AsaasExceptions; public function delete(): bool; public function restore(string $id): bool; public function notifications(string $id): ?array; diff --git a/src/Gateways/Asaas/Resources/Interfaces/InvoiceInterface.php b/src/Gateways/Asaas/Resources/Interfaces/InvoiceInterface.php new file mode 100644 index 0000000..d032cac --- /dev/null +++ b/src/Gateways/Asaas/Resources/Interfaces/InvoiceInterface.php @@ -0,0 +1,10 @@ +invoice = $invoice; + $this->gateway = $gateway; + } + + /** + * create invoice + * + * @return self|AsaasExceptions + */ + public function create(): self|AsaasExceptions + { + try { + $this->invoice = Http::asaas() + ->post(env('ASSAS_PAYMENTS'), $this->invoice) + ->json(); + } catch (\Exception $e) { + return (new AsaasExceptions())($e->getMessage()); + } + + return $this; + } +} diff --git a/src/Gateways/Asaas/Resources/Resource.php b/src/Gateways/Asaas/Resources/Resource.php new file mode 100644 index 0000000..108e227 --- /dev/null +++ b/src/Gateways/Asaas/Resources/Resource.php @@ -0,0 +1,36 @@ +parentGateway = $gateway; + } + + /** + * set resource data + * + * @return AsaasGateway + */ + public function back(): AsaasGateway + { + $this->parentGateway->setResource($this); + + return $this->parentGateway; + } +} diff --git a/src/Gateways/Gateway.php b/src/Gateways/Gateway.php index 61b29f4..e7d4852 100644 --- a/src/Gateways/Gateway.php +++ b/src/Gateways/Gateway.php @@ -5,4 +5,5 @@ abstract class Gateway { abstract public function client(array $client = []): object; + abstract public function invoice(array $invoice): object; } diff --git a/src/PHPay.php b/src/PHPay.php index 38dc4b1..9bc811a 100644 --- a/src/PHPay.php +++ b/src/PHPay.php @@ -21,6 +21,11 @@ class PHPay */ protected array $client; + /** + * @var array + */ + protected array $invoice; + /** * construct * @@ -72,8 +77,17 @@ public static function asaas(string $token, $sandbox = true): self */ public function client(array $client = []): object { - $this->client = $client; - return $this->gateway->client($client); } + + /** + * call invoice + * + * @param array $invoice + * @return object + */ + public function invoice(array $invoice = []): object + { + return $this->gateway->invoice($invoice); + } }