diff --git a/.github/CONTRIBUTING.md b/.husky/.github/CONTRIBUTING.md similarity index 100% rename from .github/CONTRIBUTING.md rename to .husky/.github/CONTRIBUTING.md diff --git a/.github/FUNDING.yml b/.husky/.github/FUNDING.yml similarity index 100% rename from .github/FUNDING.yml rename to .husky/.github/FUNDING.yml diff --git a/examples/asaas/charges.php b/examples/asaas/charges.php new file mode 100644 index 0000000..45fb5d0 --- /dev/null +++ b/examples/asaas/charges.php @@ -0,0 +1,141 @@ + NAME, + 'cpfCnpj' => CPF_CNPJ, +]; + +$charge = [ + 'billingType' => 'PIX', + 'value' => 100.00, + 'description' => 'Teste de fatura', + 'dueDate' => date('Y-m-d', strtotime('+1 day')), +]; + +/** + * initialize phpay + */ +$phpay = PHPay::getInstance(new AsaasGateway(TOKEN_ASAAS_SANDBOX)) + ->getGateway(); + +/** + * create customer + * + * @return array customer + */ +$customerCreated = $phpay + ->customer($customer) + ->create(); + +/** + * create charge + * + * @return array charges + */ +$chargeCreated = $phpay + ->charge($charge) + ->setCustomer($customerCreated['id']) + ->create(); + +/** + * get all charges + * + * @return array charges + */ +$phpay + ->charge() + ->find($chargeCreated['id']); + +/** + * get all charges + * + * @return array charges + */ +$phpay + ->charge() + ->getAll(); + +/** + * update charge + * + * @return array charge + */ +$phpay + ->charge() + ->update($chargeCreated['id'], [ + 'description' => 'Teste de fatura atualizado', + ]); + +/** + * destroy charge + * + * @return array bool + */ +$phpay + ->charge() + ->destroy($chargeCreated['id']); + +/** + * restore charge + * + * @return array charge + */ +$phpay + ->charge() + ->restore($chargeCreated['id']); + +/** + * get charge status + * + * @return array charge status + */ +$phpay + ->charge() + ->getStatus($chargeCreated['id']); + +/** + * get digitable line charge + * + * @return string + */ +$phpay + ->charge() + ->getDigitableLine($chargeCreated['id']); + +/** + * get qrcode charge + * + * @return string + */ +$qrcode = $phpay + ->charge() + ->getQrCodePix($chargeCreated['id']); + +/** + * confirm receipt charge + * + * @return array charges + */ +$confirmed = $phpay + ->charge() + ->confirmReceipt($chargeCreated['id'], [ + 'paymentDate' => date('Y-m-d'), + 'value' => 100.00, + 'notifyCustomer' => true, + ]); + +/** + * undo confirm receipt + * + * @return array charges + */ +$phpay + ->charge() + ->undoConfirmReceipt($chargeCreated['id']); diff --git a/examples/asaas/clients.php b/examples/asaas/customers.php similarity index 75% rename from examples/asaas/clients.php rename to examples/asaas/customers.php index 919125e..21353c2 100644 --- a/examples/asaas/clients.php +++ b/examples/asaas/customers.php @@ -8,8 +8,8 @@ require_once __DIR__ . '/credentials.php'; $customer = [ - 'name' => 'Mário Lucas', - 'cpfCnpj' => '00000000000', + 'name' => NAME, + 'cpfCnpj' => CPF_CNPJ, ]; /** @@ -22,10 +22,10 @@ * store asaas customer * * @param array $customer - * @return string customer id + * @return array * @see available fields in https://docs.asaas.com/reference/criar-novo-cliente */ -$customerId = $phpay +$customerCreated = $phpay ->customer($customer) ->create(); @@ -47,7 +47,7 @@ $customersFiltred = $phpay ->customer() ->setFilter([ - 'cpfCnpj' => '00000000000', + 'cpfCnpj' => $customerCreated['cpfCnpj'], ]) ->getAll(); @@ -58,7 +58,7 @@ */ $customerById = $phpay ->customer() - ->get('cus_000000000000'); + ->find($customerCreated['id']); /** * update customer @@ -67,10 +67,9 @@ */ $customerUpdate = $phpay ->customer([ - 'name' => 'Mário Lucas', - 'cpfCnpj' => '00000000000', + 'name' => 'Mário Lucas Updated', ]) - ->update($customerId); + ->update($customerCreated['id']); /** * delete cliente no asaas @@ -79,16 +78,16 @@ */ $customerDeleted = $phpay ->customer() - ->delete('cus_000000000000'); + ->destroy($customerCreated['id']); /** * restore customer deleted * * @return bool */ -$customerRestored = $phpay +$customerIdRestored = $phpay ->customer() - ->restore('cus_000000000000'); + ->restore($customerCreated['id']); /** * customer notifications @@ -97,4 +96,4 @@ */ $notifications = $phpay ->customer() - ->getNotifications('cus_000000000000'); + ->getNotifications($customerCreated['id']); diff --git a/examples/asaas/invoices.php b/examples/asaas/invoices.php deleted file mode 100644 index 30ef963..0000000 --- a/examples/asaas/invoices.php +++ /dev/null @@ -1,35 +0,0 @@ - NAME, - 'cpf_cnpj' => CPF_CNPJ, -]; - -$invoice = [ - 'billingType' => 'BOLETO', - 'value' => 100.00, - 'description' => 'Teste de fatura', - 'dueDate' => date('Y-m-d', strtotime('+1 day')), -]; - -$assas = PHPay::asaas(TOKEN_ASAAS_SANDBOX); - -$invoices = $assas - ->invoice(createOnly: false) - ->with([]) - ->all(); - -print_r($invoices); - -$qrcode = $assas - ->client($client) - ->invoice($invoice, false) - ->qrCodePix(); - -print_r($qrcode); diff --git a/src/Contracts/GatewayInterface.php b/src/Contracts/GatewayInterface.php index 30482e1..9279805 100644 --- a/src/Contracts/GatewayInterface.php +++ b/src/Contracts/GatewayInterface.php @@ -2,9 +2,11 @@ namespace PHPay\Contracts; +use Asaas\Resources\Charge\Charge as AsaasCharge; use Asaas\Resources\Customer\Customer as AsaasCustomer; interface GatewayInterface { public function customer(array $customer = []): AsaasCustomer; + public function charge(array $charge = []): AsaasCharge; } diff --git a/src/Gateways/Asaas/AsaasGateway.php b/src/Gateways/Asaas/AsaasGateway.php index f691882..a09b7ca 100644 --- a/src/Gateways/Asaas/AsaasGateway.php +++ b/src/Gateways/Asaas/AsaasGateway.php @@ -2,6 +2,7 @@ namespace PHPay\Gateways\Asaas; +use Asaas\Resources\Charge\Charge; use Asaas\Resources\Customer\Customer; use GuzzleHttp\Client; use PHPay\Contracts\GatewayInterface; @@ -44,4 +45,9 @@ public function customer(array $customer = []): Customer return $this->customer; } + + public function charge(array $charge = []): Charge + { + return new Charge($charge, $this->token, $this->sandbox); + } } diff --git a/src/Gateways/Asaas/_Enums/BillingTypeEnum.php b/src/Gateways/Asaas/Enums/BillingTypeEnum.php similarity index 100% rename from src/Gateways/Asaas/_Enums/BillingTypeEnum.php rename to src/Gateways/Asaas/Enums/BillingTypeEnum.php diff --git a/src/Gateways/Asaas/_Enums/InvoiceFiltersEnum.php b/src/Gateways/Asaas/Enums/InvoiceFiltersEnum.php similarity index 100% rename from src/Gateways/Asaas/_Enums/InvoiceFiltersEnum.php rename to src/Gateways/Asaas/Enums/InvoiceFiltersEnum.php diff --git a/src/Gateways/Asaas/_Enums/InvoiceStatusEnum.php b/src/Gateways/Asaas/Enums/InvoiceStatusEnum.php similarity index 100% rename from src/Gateways/Asaas/_Enums/InvoiceStatusEnum.php rename to src/Gateways/Asaas/Enums/InvoiceStatusEnum.php diff --git a/src/Gateways/Asaas/_Enums/InvoiceStatusFiscalDocumentEnum.php b/src/Gateways/Asaas/Enums/InvoiceStatusFiscalDocumentEnum.php similarity index 100% rename from src/Gateways/Asaas/_Enums/InvoiceStatusFiscalDocumentEnum.php rename to src/Gateways/Asaas/Enums/InvoiceStatusFiscalDocumentEnum.php diff --git a/src/Gateways/Asaas/_Requests/AsaasInvoiceRequest.php b/src/Gateways/Asaas/Requests/AsaasChargeRequest.php similarity index 53% rename from src/Gateways/Asaas/_Requests/AsaasInvoiceRequest.php rename to src/Gateways/Asaas/Requests/AsaasChargeRequest.php index 7d75e1a..74393f4 100644 --- a/src/Gateways/Asaas/_Requests/AsaasInvoiceRequest.php +++ b/src/Gateways/Asaas/Requests/AsaasChargeRequest.php @@ -1,58 +1,53 @@ client->id); + if (!isset($charge['customer']) && !is_string($charge['customer'])) { + throw new \InvalidArgumentException(self::messages()->charge->customer, 400); } - if (!isset($invoice['customer']) && !is_string($invoice['customer'])) { - throw new \InvalidArgumentException(self::messages()->invoice->customer); + if (!isset($charge['billingType'])) { + throw new \InvalidArgumentException(self::messages()->charge->billingType, 400); } - if (!isset($invoice['billingType'])) { - throw new \InvalidArgumentException(self::messages()->invoice->billingType); + if (!BillingTypeEnum::tryFrom($charge['billingType'])) { + throw new \InvalidArgumentException(self::messages()->charge->billingType, 400); } - if (!BillingTypeEnum::tryFrom($invoice['billingType'])) { - throw new \InvalidArgumentException(self::messages()->invoice->billingType); + if (!isset($charge['value']) && !is_numeric($charge['value'])) { + throw new \InvalidArgumentException(self::messages()->charge->value, 400); } - if (!isset($invoice['value']) && !is_numeric($invoice['value'])) { - throw new \InvalidArgumentException(self::messages()->invoice->value); - } - - if (!isset($invoice['dueDate']) && !is_string($invoice['dueDate'])) { - throw new \InvalidArgumentException(self::messages()->invoice->dueDate); + if (!isset($charge['dueDate']) && !is_string($charge['dueDate'])) { + throw new \InvalidArgumentException(self::messages()->charge->dueDate, 400); } } /** * messages for validation * - * @return stdClass + * @return object */ - public static function messages(): stdClass + public static function messages(): object { return (object) [ - 'client' => (object) [ - 'id' => 'Asaas: Para gerar uma cobrança é necessário um id de cliente do Asaas.', + 'customer' => (object) [ + 'id' => 'Asaas: Para gerar uma cobrança é necessário um id de customere do Asaas.', ], - 'invoice' => (object) [ + 'charge' => (object) [ 'customer' => 'Asaas: O campo customer é obrigatório e deve ser do tipo string.', 'billingType' => 'Asaas: O campo billingType é obrigatório, e tem como disponível as seguintes opções: UNDEFINED, BOLETO, CREDIT_CARD, PIX', 'value' => 'Asaas: O campo value é obrigatório e deve ser do tipo numérico.', diff --git a/src/Gateways/Asaas/Requests/AsaasCustomerRequest.php b/src/Gateways/Asaas/Requests/AsaasCustomerRequest.php new file mode 100644 index 0000000..4b485b4 --- /dev/null +++ b/src/Gateways/Asaas/Requests/AsaasCustomerRequest.php @@ -0,0 +1,36 @@ +name, 400); + } + + if (!isset($customer['cpfCnpj'])) { + throw new \InvalidArgumentException(self::messages()->cpfCnpj, 400); + } + } + + /** + * messages for validation + * + * @return object + */ + public static function messages(): object + { + return (object) [ + 'name' => 'Asaas: Nome do cliente é obrigatório para o Asaas', + 'cpfCnpj' => 'Asaas: CPF/CNPJ do cliente é obrigatório para o Asaas', + ]; + } +} diff --git a/src/Gateways/Asaas/_Requests/AsaasPixRequest.php b/src/Gateways/Asaas/Requests/AsaasPixRequest.php similarity index 100% rename from src/Gateways/Asaas/_Requests/AsaasPixRequest.php rename to src/Gateways/Asaas/Requests/AsaasPixRequest.php diff --git a/src/Gateways/Asaas/Resources/Charge/Charge.php b/src/Gateways/Asaas/Resources/Charge/Charge.php new file mode 100644 index 0000000..92fbd65 --- /dev/null +++ b/src/Gateways/Asaas/Resources/Charge/Charge.php @@ -0,0 +1,190 @@ +client = $this->clientAsaasBoot(); + + if (!empty($charge)) { + $this->charge = $charge; + } + + return $this; + } + + /** + * set filters + * + * @param array $filters + * @return ChargeInterface + */ + public function setFilters(array $filters): ChargeInterface + { + $this->filter = $filters; + + return $this; + } + + /** + * set customer + * + * @param string $customerId + * @return ChargeInterface + */ + public function setCustomer(string $customerId): ChargeInterface + { + $this->charge['customer'] = $customerId; + + return $this; + } + + /** + * find charges by id + * + * @return array + */ + public function find(string $id): array + { + return $this->get("payments/{$id}"); + } + + /** + * get all charges + * + * @return array + */ + public function getAll(): array + { + return $this->get('payments', [ + 'query' => $this->filter, + ]); + } + + /** + * create charge + * + * @return string + * @see fields available in https://docs.asaas.com/reference/criar-nova-cobranca + */ + public function create(): array + { + AsaasChargeRequest::validate($this->charge); + + return $this->post('payments', $this->charge); + } + + /** + * update charge + * + * @param string $id + * @param array $data + * @return array + */ + public function update(string $id, array $data): array + { + return $this->put("payments/{$id}", $data); + } + + /** + * destroy charge + * + * @param string $id + * @return array + */ + public function destroy(string $id): bool + { + return $this->delete("payments/{$id}"); + } + + /** + * restore charge + * + * @param string $id + * @return array + */ + public function restore(string $id): array + { + return $this->post("payments/{$id}/restore"); + } + + /** + * get status charge + * + * @param string $id + * @return array + */ + public function getStatus(string $id): array + { + return $this->get("payments/{$id}/status"); + } + + /** + * get digitable line + * + * @param string $id + * @return array + */ + public function getDigitableLine(string $id): string + { + return $this->get("payments/{$id}/identificationField")['identificationField']; + } + + /** + * get qrcode pix + * + * @param string $id + * @return array + */ + public function getQrCodePix(string $id): array + { + return $this->get("payments/{$id}/pixQrCode"); + } + + /** + * confirm receipt + * + * @param string $id + * @param array $data + * @return array + */ + public function confirmReceipt(string $id, array $data): array + { + return $this->post("payments/{$id}/receiveInCash", $data); + } + + public function undoConfirmReceipt(string $id): array + { + return $this->post("payments/{$id}/undoReceivedInCash"); + } +} diff --git a/src/Gateways/Asaas/Resources/Charge/Interface/ChargeInterface.php b/src/Gateways/Asaas/Resources/Charge/Interface/ChargeInterface.php new file mode 100644 index 0000000..25eb0b9 --- /dev/null +++ b/src/Gateways/Asaas/Resources/Charge/Interface/ChargeInterface.php @@ -0,0 +1,20 @@ +client->get('customers', [ - 'query' => $this->filter, - ]); - - $content = $response - ->getBody() - ->getContents(); - - return json_decode($content, true); - } catch (\Exception $e) { - return [ - 'error' => $e->getCode(), - 'message' => $e->getMessage(), - ]; - } + return $this->get('customers', [ + 'query' => $this->filter, + ]); } /** @@ -73,37 +61,22 @@ public function getAll(): array * @param string $id * @return array */ - public function get(string $id): array + public function find(string $id): array { - try { - $response = $this->client->get("customers/{$id}"); - - $content = $response - ->getBody() - ->getContents(); - - return json_decode($content, true); - } catch (\Exception $e) { - return [ - 'error' => $e->getCode(), - 'message' => $e->getMessage(), - ]; - } + return $this->get("customers/{$id}"); } /** * create customer * - * @return string + * @return array * @see available fields in https://docs.asaas.com/reference/criar-novo-cliente */ - public function create(): string + public function create(): array { - try { - return $this->post('customers', $this->customer); - } catch (\Exception $e) { - return $e->getMessage(); - } + AsaasCustomerRequest::validate($this->customer); + + return $this->post('customers', $this->customer); } /** @@ -115,48 +88,29 @@ public function create(): string */ public function update(string $id): array { - try { - return $this->put("customers/{$id}", $this->customer); - } catch (\Exception $e) { - return [ - 'error' => $e->getCode(), - 'message' => $e->getMessage(), - ]; - } + return $this->put("customers/{$id}", $this->customer); } /** - * delete customer + * destroy customer * * @param string $id * @return bool */ - public function delete(string $id): bool + public function destroy(string $id): bool { - try { - $response = $this->client->delete("customers/{$id}"); - - return ($response->getStatusCode() === 200); - } catch (\Exception $e) { - return false; - } + return $this->delete("customers/{$id}"); } /** * restore customer deleted * * @param string $id - * @return bool + * @return array */ - public function restore(string $id): bool + public function restore(string $id): array { - try { - $response = $this->client->post("customers/{$id}/restore"); - - return ($response->getStatusCode() === 200); - } catch (\Exception $e) { - return false; - } + return $this->post("customers/{$id}/restore"); } /** @@ -167,20 +121,7 @@ public function restore(string $id): bool */ public function getNotifications(string $id): array { - try { - $response = $this->client->get("customers/{$id}/notifications"); - - $content = $response - ->getBody() - ->getContents(); - - return json_decode($content, true); - } catch (\Exception $e) { - return [ - 'error' => $e->getCode(), - 'message' => $e->getMessage(), - ]; - } + return $this->get("customers/{$id}/notifications"); } /** diff --git a/src/Gateways/Asaas/Resources/Customer/Interface/CustomerInterface.php b/src/Gateways/Asaas/Resources/Customer/Interface/CustomerInterface.php index b69a06a..7882408 100644 --- a/src/Gateways/Asaas/Resources/Customer/Interface/CustomerInterface.php +++ b/src/Gateways/Asaas/Resources/Customer/Interface/CustomerInterface.php @@ -4,12 +4,12 @@ interface CustomerInterface { - public function get(string $id): array; + public function find(string $id): array; public function getAll(): array; - public function create(): string; + public function create(): array; public function update(string $id): array; - public function delete(string $id): bool; - public function restore(string $id): bool; + public function destroy(string $id): bool; + public function restore(string $id): array; public function getNotifications(string $id): array; public function setFilter(array $filter = []): self; } diff --git a/src/Gateways/Asaas/Traits/HasAsaasClient.php b/src/Gateways/Asaas/Traits/HasAsaasClient.php index c1c0400..0dca1aa 100644 --- a/src/Gateways/Asaas/Traits/HasAsaasClient.php +++ b/src/Gateways/Asaas/Traits/HasAsaasClient.php @@ -31,10 +31,12 @@ protected function clientAsaasBoot(): Client * @param string $endpoint * @return array */ - public function get(string $endpoint): array + protected function get(string $endpoint, array $filters = []): array { try { - $reposonse = $this->client->get($endpoint); + $reposonse = $this->client->get($endpoint, [ + 'query' => $filters, + ]); $content = $reposonse ->getBody() @@ -42,7 +44,10 @@ public function get(string $endpoint): array return json_decode($content, true); } catch (\Exception $e) { - return $e->getMessage(); + return [ + 'error' => $e->getCode(), + 'message' => $e->getMessage(), + ]; } } @@ -51,9 +56,9 @@ public function get(string $endpoint): array * * @param string $endpoint * @param array $data - * @return string + * @return array */ - public function post(string $endpoint, array $data): string + protected function post(string $endpoint, array $data = []): array { try { $reposonse = $this->client->post($endpoint, [ @@ -64,11 +69,12 @@ public function post(string $endpoint, array $data): string ->getBody() ->getContents(); - $reposonses = json_decode($content, true); - - return $reposonses['id']; + return json_decode($content, true); } catch (\Exception $e) { - return $e->getMessage(); + return [ + 'error' => $e->getCode(), + 'message' => $e->getMessage(), + ]; } } @@ -79,7 +85,7 @@ public function post(string $endpoint, array $data): string * @param array $data * @return array */ - public function put(string $endpoint, array $data): array + protected function put(string $endpoint, array $data): array { try { $response = $this->client->put($endpoint, [ @@ -105,12 +111,12 @@ public function put(string $endpoint, array $data): array * @param string $endpoint * @return bool */ - public function delete(string $endpoint): bool + protected function delete(string $endpoint): bool { try { - $this->client->delete($endpoint); + $response = $this->client->delete($endpoint); - return true; + return ($response->getStatusCode() == 200); } catch (\Exception $e) { return false; } diff --git a/src/Gateways/Asaas/_Requests/AsaasClientRequest.php b/src/Gateways/Asaas/_Requests/AsaasClientRequest.php deleted file mode 100644 index ac503ce..0000000 --- a/src/Gateways/Asaas/_Requests/AsaasClientRequest.php +++ /dev/null @@ -1,38 +0,0 @@ -name); - } - - if (!isset($client['cpf_cnpj'])) { - throw new \InvalidArgumentException(self::messages()->cpf_cnpj); - } - } - - /** - * messages for validation - * - * @return stdClass - */ - public static function messages(): stdClass - { - return (object) [ - 'name' => 'Asaas: Nome do cliente é obrigatório para o Asaas', - 'cpf_cnpj' => 'Asaas: CPF/CNPJ do cliente é obrigatório para o Asaas', - ]; - } -}