From 6b9b04683e1d03f478a1a0ebf3baee22e810e88c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Lucas?= Date: Tue, 17 Dec 2024 03:03:40 -0300 Subject: [PATCH 1/7] PHPAY-35: refactor: refactor requests --- .../Asaas/Requests/AsaasChargeRequest.php | 62 +++++++++++++++++++ .../Asaas/Requests/AsaasCustomerRequest.php | 36 +++++++++++ .../Asaas/Requests/AsaasPixRequest.php | 39 ++++++++++++ 3 files changed, 137 insertions(+) create mode 100644 src/Gateways/Asaas/Requests/AsaasChargeRequest.php create mode 100644 src/Gateways/Asaas/Requests/AsaasCustomerRequest.php create mode 100644 src/Gateways/Asaas/Requests/AsaasPixRequest.php diff --git a/src/Gateways/Asaas/Requests/AsaasChargeRequest.php b/src/Gateways/Asaas/Requests/AsaasChargeRequest.php new file mode 100644 index 0000000..9a5f2b1 --- /dev/null +++ b/src/Gateways/Asaas/Requests/AsaasChargeRequest.php @@ -0,0 +1,62 @@ +customer->id, 400); + } + + if (!isset($charge['customer']) && !is_string($charge['customer'])) { + throw new \InvalidArgumentException(self::messages()->charge->customer, 400); + } + + if (!isset($charge['billingType'])) { + throw new \InvalidArgumentException(self::messages()->charge->billingType, 400); + } + + if (!BillingTypeEnum::tryFrom($charge['billingType'])) { + throw new \InvalidArgumentException(self::messages()->charge->billingType, 400); + } + + if (!isset($charge['value']) && !is_numeric($charge['value'])) { + throw new \InvalidArgumentException(self::messages()->charge->value, 400); + } + + if (!isset($charge['dueDate']) && !is_string($charge['dueDate'])) { + throw new \InvalidArgumentException(self::messages()->charge->dueDate, 400); + } + } + + /** + * messages for validation + * + * @return object + */ + public static function messages(): object + { + return (object) [ + 'customer' => (object) [ + 'id' => 'Asaas: Para gerar uma cobrança é necessário um id de customere do Asaas.', + ], + '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.', + 'dueDate' => 'Asaas: O campo dueDate é obrigatório e deve ser do tipo string.', + ], + ]; + } +} 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 new file mode 100644 index 0000000..2f68455 --- /dev/null +++ b/src/Gateways/Asaas/Requests/AsaasPixRequest.php @@ -0,0 +1,39 @@ +description); + } + + if (!isset($pix['amount'])) { + throw new \InvalidArgumentException(self::messages()->amount); + } + + if (!isset($pix['due_date'])) { + throw new \InvalidArgumentException(self::messages()->due_date->required); + } + + if ($pix['due_date'] < date('Y-m-d')) { + throw new \InvalidArgumentException(self::messages()->due_date->date); + } + } + + public static function messages(): stdClass + { + return (object) [ + 'description' => 'Asaas: Descrição do pagamento é obrigatório para o Asaas', + 'amount' => 'Asaas: Valor do pagamento é obrigatório para o Asaas', + 'due_date' => (object) [ + 'required' => 'Asaas: Data de vencimento do pagamento é obrigatório para o Asaas', + 'date' => 'Asaas: Data de vencimento do pagamento não pode ser menor que a data atual', + ], + ]; + } +} From 19c1b77d4cdf1ee85d7f6bd3ba0546e37c7935b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Lucas?= Date: Tue, 17 Dec 2024 03:04:20 -0300 Subject: [PATCH 2/7] PHPAY-35: refactor: refactor example file names --- examples/asaas/charges.php | 42 +++++++++++++++++++ examples/asaas/{clients.php => customers.php} | 6 +-- examples/asaas/invoices.php | 35 ---------------- 3 files changed, 45 insertions(+), 38 deletions(-) create mode 100644 examples/asaas/charges.php rename examples/asaas/{clients.php => customers.php} (93%) delete mode 100644 examples/asaas/invoices.php diff --git a/examples/asaas/charges.php b/examples/asaas/charges.php new file mode 100644 index 0000000..a504b83 --- /dev/null +++ b/examples/asaas/charges.php @@ -0,0 +1,42 @@ + NAME, + 'cpf_cnpj' => CPF_CNPJ, +]; + +$charge = [ + 'billingType' => 'BOLETO', + '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 charge + * + * @return array charges + */ +$customerId = $phpay + ->customer($customer) + ->create(); + +$charge = $phpay + ->charge($charge) + ->setCustomer($customerId) + ->create(); + +print_r($charges); diff --git a/examples/asaas/clients.php b/examples/asaas/customers.php similarity index 93% rename from examples/asaas/clients.php rename to examples/asaas/customers.php index 919125e..f789b94 100644 --- a/examples/asaas/clients.php +++ b/examples/asaas/customers.php @@ -67,7 +67,7 @@ */ $customerUpdate = $phpay ->customer([ - 'name' => 'Mário Lucas', + 'name' => 'Mário Lucas Updated', 'cpfCnpj' => '00000000000', ]) ->update($customerId); @@ -79,14 +79,14 @@ */ $customerDeleted = $phpay ->customer() - ->delete('cus_000000000000'); + ->destroy('cus_000000000000'); /** * restore customer deleted * * @return bool */ -$customerRestored = $phpay +$customerIdRestored = $phpay ->customer() ->restore('cus_000000000000'); 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); From 99d26d00595eacd9d0d727c7f4230abdaa95ca48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Lucas?= Date: Tue, 17 Dec 2024 03:24:53 -0300 Subject: [PATCH 3/7] PHPAY-35: feat: create charge and charge lean response --- {.github => .husky/.github}/CONTRIBUTING.md | 0 {.github => .husky/.github}/FUNDING.yml | 0 examples/asaas/charges.php | 27 ++++-- examples/asaas/customers.php | 12 +-- src/Contracts/GatewayInterface.php | 2 + src/Gateways/Asaas/AsaasGateway.php | 6 ++ .../{_Enums => Enums}/BillingTypeEnum.php | 0 .../{_Enums => Enums}/InvoiceFiltersEnum.php | 0 .../{_Enums => Enums}/InvoiceStatusEnum.php | 0 .../InvoiceStatusFiscalDocumentEnum.php | 0 .../Asaas/Requests/AsaasChargeRequest.php | 6 +- .../Asaas/Resources/Charge/Charge.php | 84 +++++++++++++++++++ .../Charge/Interface/ChargeInterface.php | 10 +++ .../Asaas/Resources/Customer/Customer.php | 25 +++--- .../Customer/Interface/CustomerInterface.php | 4 +- src/Gateways/Asaas/Traits/HasAsaasClient.php | 23 +++-- .../Asaas/_Requests/AsaasClientRequest.php | 38 --------- .../Asaas/_Requests/AsaasInvoiceRequest.php | 63 -------------- .../Asaas/_Requests/AsaasPixRequest.php | 39 --------- 19 files changed, 152 insertions(+), 187 deletions(-) rename {.github => .husky/.github}/CONTRIBUTING.md (100%) rename {.github => .husky/.github}/FUNDING.yml (100%) rename src/Gateways/Asaas/{_Enums => Enums}/BillingTypeEnum.php (100%) rename src/Gateways/Asaas/{_Enums => Enums}/InvoiceFiltersEnum.php (100%) rename src/Gateways/Asaas/{_Enums => Enums}/InvoiceStatusEnum.php (100%) rename src/Gateways/Asaas/{_Enums => Enums}/InvoiceStatusFiscalDocumentEnum.php (100%) create mode 100644 src/Gateways/Asaas/Resources/Charge/Charge.php create mode 100644 src/Gateways/Asaas/Resources/Charge/Interface/ChargeInterface.php delete mode 100644 src/Gateways/Asaas/_Requests/AsaasClientRequest.php delete mode 100644 src/Gateways/Asaas/_Requests/AsaasInvoiceRequest.php delete mode 100644 src/Gateways/Asaas/_Requests/AsaasPixRequest.php 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 index a504b83..019dd1f 100644 --- a/examples/asaas/charges.php +++ b/examples/asaas/charges.php @@ -9,7 +9,7 @@ $customer = [ 'name' => NAME, - 'cpf_cnpj' => CPF_CNPJ, + 'cpfCnpj' => CPF_CNPJ, ]; $charge = [ @@ -26,17 +26,30 @@ ->getGateway(); /** - * create charge + * create customer * - * @return array charges + * @return array customer */ -$customerId = $phpay +$customerCreated = $phpay ->customer($customer) ->create(); -$charge = $phpay +/** + * create charge + * + * @return array charges + */ +$charges = $phpay ->charge($charge) - ->setCustomer($customerId) + ->setCustomer($customerCreated['id']) ->create(); -print_r($charges); +/** + * create charge lean + * + * @return array charges + */ +$chargesLean = $phpay + ->charge($charge) + ->setCustomer($customerCreated['id']) + ->createLean(); diff --git a/examples/asaas/customers.php b/examples/asaas/customers.php index f789b94..7e79030 100644 --- a/examples/asaas/customers.php +++ b/examples/asaas/customers.php @@ -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(); @@ -70,7 +70,7 @@ 'name' => 'Mário Lucas Updated', 'cpfCnpj' => '00000000000', ]) - ->update($customerId); + ->update($customerCreated['id']); /** * delete cliente no asaas @@ -79,7 +79,7 @@ */ $customerDeleted = $phpay ->customer() - ->destroy('cus_000000000000'); + ->destroy($customerCreated['id']); /** * restore customer deleted @@ -88,7 +88,7 @@ */ $customerIdRestored = $phpay ->customer() - ->restore('cus_000000000000'); + ->restore($customerCreated['id']); /** * customer notifications @@ -97,4 +97,4 @@ */ $notifications = $phpay ->customer() - ->getNotifications('cus_000000000000'); + ->getNotifications($customerCreated['id']); 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/AsaasChargeRequest.php b/src/Gateways/Asaas/Requests/AsaasChargeRequest.php index 9a5f2b1..74393f4 100644 --- a/src/Gateways/Asaas/Requests/AsaasChargeRequest.php +++ b/src/Gateways/Asaas/Requests/AsaasChargeRequest.php @@ -13,12 +13,8 @@ class AsaasChargeRequest * @param array $charge * @return void */ - public static function validate(array $customer, array $charge): void + public static function validate(array $charge): void { - if (!isset($customer['id'])) { - throw new \InvalidArgumentException(self::messages()->customer->id, 400); - } - if (!isset($charge['customer']) && !is_string($charge['customer'])) { throw new \InvalidArgumentException(self::messages()->charge->customer, 400); } diff --git a/src/Gateways/Asaas/Resources/Charge/Charge.php b/src/Gateways/Asaas/Resources/Charge/Charge.php new file mode 100644 index 0000000..498fc29 --- /dev/null +++ b/src/Gateways/Asaas/Resources/Charge/Charge.php @@ -0,0 +1,84 @@ +client = $this->clientAsaasBoot(); + + if (!empty($charge)) { + $this->charge = $charge; + } + + return $this; + } + + /** + * set customer + * + * @param string $customer + * @return ChargeInterface + */ + public function setCustomer(string $customer): ChargeInterface + { + $this->charge['customer'] = $customer; + + return $this; + } + + /** + * 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); + } + + /** + * create charge lean + * + * @return string + * @see fields available in https://docs.asaas.com/reference/criar-nova-cobranca-com-dados-resumidos-na-resposta + */ + public function createLean(): array + { + AsaasChargeRequest::validate($this->charge); + + return $this->post('lean/payments', $this->charge); + } +} 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..22997fa --- /dev/null +++ b/src/Gateways/Asaas/Resources/Charge/Interface/ChargeInterface.php @@ -0,0 +1,10 @@ +client = $this->clientAsaasBoot(); if (!empty($customer)) { + AsaasCustomerRequest::validate($customer); + $this->customer = $customer; } @@ -94,16 +97,12 @@ public function get(string $id): array /** * 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(); - } + return $this->post('customers', $this->customer); } /** @@ -126,17 +125,15 @@ public function update(string $id): array } /** - * 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); + return $this->delete("customers/{$id}"); } catch (\Exception $e) { return false; } @@ -151,9 +148,7 @@ public function delete(string $id): bool public function restore(string $id): bool { try { - $response = $this->client->post("customers/{$id}/restore"); - - return ($response->getStatusCode() === 200); + return $this->post("customers/{$id}/restore"); } catch (\Exception $e) { return false; } diff --git a/src/Gateways/Asaas/Resources/Customer/Interface/CustomerInterface.php b/src/Gateways/Asaas/Resources/Customer/Interface/CustomerInterface.php index b69a06a..6374bb5 100644 --- a/src/Gateways/Asaas/Resources/Customer/Interface/CustomerInterface.php +++ b/src/Gateways/Asaas/Resources/Customer/Interface/CustomerInterface.php @@ -6,9 +6,9 @@ interface CustomerInterface { public function get(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 destroy(string $id): bool; public function restore(string $id): bool; 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..e5127e0 100644 --- a/src/Gateways/Asaas/Traits/HasAsaasClient.php +++ b/src/Gateways/Asaas/Traits/HasAsaasClient.php @@ -31,7 +31,7 @@ protected function clientAsaasBoot(): Client * @param string $endpoint * @return array */ - public function get(string $endpoint): array + protected function get(string $endpoint): array { try { $reposonse = $this->client->get($endpoint); @@ -51,9 +51,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 +64,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 +80,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 +106,10 @@ 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); - - return true; + return ($this->client->delete($endpoint) == 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', - ]; - } -} diff --git a/src/Gateways/Asaas/_Requests/AsaasInvoiceRequest.php b/src/Gateways/Asaas/_Requests/AsaasInvoiceRequest.php deleted file mode 100644 index 7d75e1a..0000000 --- a/src/Gateways/Asaas/_Requests/AsaasInvoiceRequest.php +++ /dev/null @@ -1,63 +0,0 @@ -client->id); - } - - if (!isset($invoice['customer']) && !is_string($invoice['customer'])) { - throw new \InvalidArgumentException(self::messages()->invoice->customer); - } - - if (!isset($invoice['billingType'])) { - throw new \InvalidArgumentException(self::messages()->invoice->billingType); - } - - if (!BillingTypeEnum::tryFrom($invoice['billingType'])) { - throw new \InvalidArgumentException(self::messages()->invoice->billingType); - } - - 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); - } - } - - /** - * messages for validation - * - * @return stdClass - */ - public static function messages(): stdClass - { - return (object) [ - 'client' => (object) [ - 'id' => 'Asaas: Para gerar uma cobrança é necessário um id de cliente do Asaas.', - ], - 'invoice' => (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.', - 'dueDate' => 'Asaas: O campo dueDate é obrigatório e deve ser do tipo string.', - ], - ]; - } -} diff --git a/src/Gateways/Asaas/_Requests/AsaasPixRequest.php b/src/Gateways/Asaas/_Requests/AsaasPixRequest.php deleted file mode 100644 index 2f68455..0000000 --- a/src/Gateways/Asaas/_Requests/AsaasPixRequest.php +++ /dev/null @@ -1,39 +0,0 @@ -description); - } - - if (!isset($pix['amount'])) { - throw new \InvalidArgumentException(self::messages()->amount); - } - - if (!isset($pix['due_date'])) { - throw new \InvalidArgumentException(self::messages()->due_date->required); - } - - if ($pix['due_date'] < date('Y-m-d')) { - throw new \InvalidArgumentException(self::messages()->due_date->date); - } - } - - public static function messages(): stdClass - { - return (object) [ - 'description' => 'Asaas: Descrição do pagamento é obrigatório para o Asaas', - 'amount' => 'Asaas: Valor do pagamento é obrigatório para o Asaas', - 'due_date' => (object) [ - 'required' => 'Asaas: Data de vencimento do pagamento é obrigatório para o Asaas', - 'date' => 'Asaas: Data de vencimento do pagamento não pode ser menor que a data atual', - ], - ]; - } -} From cbdec979bf140c5cde4c99e1545689b1d3b3375f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Lucas?= Date: Tue, 17 Dec 2024 03:32:00 -0300 Subject: [PATCH 4/7] PHPAY-35: feat: get all charges --- examples/asaas/charges.php | 11 +++++++++++ src/Gateways/Asaas/Resources/Charge/Charge.php | 12 ++++++++++++ .../Resources/Charge/Interface/ChargeInterface.php | 1 + 3 files changed, 24 insertions(+) diff --git a/examples/asaas/charges.php b/examples/asaas/charges.php index 019dd1f..4dfcb8c 100644 --- a/examples/asaas/charges.php +++ b/examples/asaas/charges.php @@ -53,3 +53,14 @@ ->charge($charge) ->setCustomer($customerCreated['id']) ->createLean(); + +/** + * get all charges + * + * @return array charges + */ +$charges = $phpay + ->charge() + ->getAll(); + +print_r($charges); diff --git a/src/Gateways/Asaas/Resources/Charge/Charge.php b/src/Gateways/Asaas/Resources/Charge/Charge.php index 498fc29..3e83118 100644 --- a/src/Gateways/Asaas/Resources/Charge/Charge.php +++ b/src/Gateways/Asaas/Resources/Charge/Charge.php @@ -56,6 +56,18 @@ public function setCustomer(string $customer): ChargeInterface return $this; } + /** + * get all charges + * + * @return array + */ + public function getAll(): array + { + return $this->get('payments', [ + 'query' => $this->filter, + ]); + } + /** * create charge * diff --git a/src/Gateways/Asaas/Resources/Charge/Interface/ChargeInterface.php b/src/Gateways/Asaas/Resources/Charge/Interface/ChargeInterface.php index 22997fa..2a26cbb 100644 --- a/src/Gateways/Asaas/Resources/Charge/Interface/ChargeInterface.php +++ b/src/Gateways/Asaas/Resources/Charge/Interface/ChargeInterface.php @@ -7,4 +7,5 @@ interface ChargeInterface public function setCustomer(string $customer): ChargeInterface; public function create(): array; public function createLean(): array; + public function getAll(): array; } From f2bf59c61f87e379fc5d8325f91c9cad52cc0c3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Lucas?= Date: Tue, 17 Dec 2024 03:46:04 -0300 Subject: [PATCH 5/7] PHPAY-35: feat: ading getall and find charge --- examples/asaas/charges.php | 16 ++++++-- examples/asaas/customers.php | 9 ++--- .../Asaas/Resources/Charge/Charge.php | 10 +++++ .../Charge/Interface/ChargeInterface.php | 1 + .../Asaas/Resources/Customer/Customer.php | 40 ++++--------------- .../Customer/Interface/CustomerInterface.php | 2 +- src/Gateways/Asaas/Traits/HasAsaasClient.php | 11 +++-- 7 files changed, 44 insertions(+), 45 deletions(-) diff --git a/examples/asaas/charges.php b/examples/asaas/charges.php index 4dfcb8c..6fa37a9 100644 --- a/examples/asaas/charges.php +++ b/examples/asaas/charges.php @@ -49,11 +49,23 @@ * * @return array charges */ -$chargesLean = $phpay +$chargeLean = $phpay ->charge($charge) ->setCustomer($customerCreated['id']) ->createLean(); +/** + * get all charges + * + * @return array charges + */ +$charge = $phpay + ->charge() + ->find($chargeLean['id']); + +print_r($charge); + + /** * get all charges * @@ -62,5 +74,3 @@ $charges = $phpay ->charge() ->getAll(); - -print_r($charges); diff --git a/examples/asaas/customers.php b/examples/asaas/customers.php index 7e79030..40e4445 100644 --- a/examples/asaas/customers.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, ]; /** @@ -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 @@ -68,7 +68,6 @@ $customerUpdate = $phpay ->customer([ 'name' => 'Mário Lucas Updated', - 'cpfCnpj' => '00000000000', ]) ->update($customerCreated['id']); diff --git a/src/Gateways/Asaas/Resources/Charge/Charge.php b/src/Gateways/Asaas/Resources/Charge/Charge.php index 3e83118..af5cca4 100644 --- a/src/Gateways/Asaas/Resources/Charge/Charge.php +++ b/src/Gateways/Asaas/Resources/Charge/Charge.php @@ -56,6 +56,16 @@ public function setCustomer(string $customer): ChargeInterface return $this; } + /** + * find charges by id + * + * @return array + */ + public function find(string $id): array + { + return $this->get("payments/{$id}"); + } + /** * get all charges * diff --git a/src/Gateways/Asaas/Resources/Charge/Interface/ChargeInterface.php b/src/Gateways/Asaas/Resources/Charge/Interface/ChargeInterface.php index 2a26cbb..3331eec 100644 --- a/src/Gateways/Asaas/Resources/Charge/Interface/ChargeInterface.php +++ b/src/Gateways/Asaas/Resources/Charge/Interface/ChargeInterface.php @@ -7,5 +7,6 @@ interface ChargeInterface public function setCustomer(string $customer): ChargeInterface; public function create(): array; public function createLean(): array; + public function find(string $id): array; public function getAll(): array; } diff --git a/src/Gateways/Asaas/Resources/Customer/Customer.php b/src/Gateways/Asaas/Resources/Customer/Customer.php index 0b401cb..82926d0 100644 --- a/src/Gateways/Asaas/Resources/Customer/Customer.php +++ b/src/Gateways/Asaas/Resources/Customer/Customer.php @@ -37,8 +37,6 @@ public function __construct( $this->client = $this->clientAsaasBoot(); if (!empty($customer)) { - AsaasCustomerRequest::validate($customer); - $this->customer = $customer; } @@ -52,22 +50,9 @@ public function __construct( */ public function getAll(): array { - try { - $response = $this->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, + ]); } /** @@ -76,22 +61,9 @@ 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}"); } /** @@ -102,6 +74,8 @@ public function get(string $id): array */ public function create(): array { + AsaasCustomerRequest::validate($this->customer); + return $this->post('customers', $this->customer); } diff --git a/src/Gateways/Asaas/Resources/Customer/Interface/CustomerInterface.php b/src/Gateways/Asaas/Resources/Customer/Interface/CustomerInterface.php index 6374bb5..9881086 100644 --- a/src/Gateways/Asaas/Resources/Customer/Interface/CustomerInterface.php +++ b/src/Gateways/Asaas/Resources/Customer/Interface/CustomerInterface.php @@ -4,7 +4,7 @@ interface CustomerInterface { - public function get(string $id): array; + public function find(string $id): array; public function getAll(): array; public function create(): array; public function update(string $id): array; diff --git a/src/Gateways/Asaas/Traits/HasAsaasClient.php b/src/Gateways/Asaas/Traits/HasAsaasClient.php index e5127e0..96cca87 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 */ - protected 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 @@ protected function get(string $endpoint): array return json_decode($content, true); } catch (\Exception $e) { - return $e->getMessage(); + return [ + 'error' => $e->getCode(), + 'message' => $e->getMessage(), + ]; } } From c1c211cca7b20afd565b96fa7f5966997c73c1f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Lucas?= Date: Tue, 17 Dec 2024 08:22:29 -0300 Subject: [PATCH 6/7] PHPAY-35: feat: adding charges resources --- examples/asaas/charges.php | 93 +++++++++++++--- examples/asaas/customers.php | 2 +- .../Asaas/Resources/Charge/Charge.php | 102 ++++++++++++++++-- .../Charge/Interface/ChargeInterface.php | 16 ++- .../Asaas/Resources/Customer/Customer.php | 40 ++----- .../Customer/Interface/CustomerInterface.php | 2 +- src/Gateways/Asaas/Traits/HasAsaasClient.php | 4 +- 7 files changed, 195 insertions(+), 64 deletions(-) diff --git a/examples/asaas/charges.php b/examples/asaas/charges.php index 6fa37a9..714efa3 100644 --- a/examples/asaas/charges.php +++ b/examples/asaas/charges.php @@ -8,12 +8,12 @@ require_once __DIR__ . '/credentials.php'; $customer = [ - 'name' => NAME, + 'name' => NAME, 'cpfCnpj' => CPF_CNPJ, ]; $charge = [ - 'billingType' => 'BOLETO', + 'billingType' => 'PIX', 'value' => 100.00, 'description' => 'Teste de fatura', 'dueDate' => date('Y-m-d', strtotime('+1 day')), @@ -39,38 +39,103 @@ * * @return array charges */ -$charges = $phpay +$chargeCreated = $phpay ->charge($charge) ->setCustomer($customerCreated['id']) ->create(); /** - * create charge lean + * get all charges * * @return array charges */ -$chargeLean = $phpay - ->charge($charge) - ->setCustomer($customerCreated['id']) - ->createLean(); +$phpay + ->charge() + ->find($chargeCreated['id']); /** * get all charges * * @return array charges */ -$charge = $phpay +$phpay ->charge() - ->find($chargeLean['id']); + ->getAll(); -print_r($charge); +/** + * 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']); /** - * get all charges + * 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 */ -$charges = $phpay +$confirmed = $phpay ->charge() - ->getAll(); + ->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/customers.php b/examples/asaas/customers.php index 40e4445..21353c2 100644 --- a/examples/asaas/customers.php +++ b/examples/asaas/customers.php @@ -67,7 +67,7 @@ */ $customerUpdate = $phpay ->customer([ - 'name' => 'Mário Lucas Updated', + 'name' => 'Mário Lucas Updated', ]) ->update($customerCreated['id']); diff --git a/src/Gateways/Asaas/Resources/Charge/Charge.php b/src/Gateways/Asaas/Resources/Charge/Charge.php index af5cca4..92fbd65 100644 --- a/src/Gateways/Asaas/Resources/Charge/Charge.php +++ b/src/Gateways/Asaas/Resources/Charge/Charge.php @@ -43,15 +43,28 @@ public function __construct( return $this; } + /** + * set filters + * + * @param array $filters + * @return ChargeInterface + */ + public function setFilters(array $filters): ChargeInterface + { + $this->filter = $filters; + + return $this; + } + /** * set customer * - * @param string $customer + * @param string $customerId * @return ChargeInterface */ - public function setCustomer(string $customer): ChargeInterface + public function setCustomer(string $customerId): ChargeInterface { - $this->charge['customer'] = $customer; + $this->charge['customer'] = $customerId; return $this; } @@ -92,15 +105,86 @@ public function create(): array } /** - * create charge lean + * update charge * - * @return string - * @see fields available in https://docs.asaas.com/reference/criar-nova-cobranca-com-dados-resumidos-na-resposta + * @param string $id + * @param array $data + * @return array */ - public function createLean(): array + public function update(string $id, array $data): array { - AsaasChargeRequest::validate($this->charge); + 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); + } - return $this->post('lean/payments', $this->charge); + 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 index 3331eec..25eb0b9 100644 --- a/src/Gateways/Asaas/Resources/Charge/Interface/ChargeInterface.php +++ b/src/Gateways/Asaas/Resources/Charge/Interface/ChargeInterface.php @@ -4,9 +4,17 @@ interface ChargeInterface { - public function setCustomer(string $customer): ChargeInterface; - public function create(): array; - public function createLean(): array; - public function find(string $id): array; public function getAll(): array; + public function find(string $id): array; + public function create(): array; + public function update(string $id, array $data): array; + public function destroy(string $id): bool; + public function restore(string $id): array; + public function setCustomer(string $customer): ChargeInterface; + public function setFilters(array $filters): ChargeInterface; + public function getStatus(string $id): array; + public function getDigitableLine(string $id): string; + public function getQrCodePix(string $id): array; + public function confirmReceipt(string $id, array $data): array; + public function undoConfirmReceipt(string $id): array; } diff --git a/src/Gateways/Asaas/Resources/Customer/Customer.php b/src/Gateways/Asaas/Resources/Customer/Customer.php index 82926d0..cc22c82 100644 --- a/src/Gateways/Asaas/Resources/Customer/Customer.php +++ b/src/Gateways/Asaas/Resources/Customer/Customer.php @@ -88,14 +88,7 @@ public function create(): array */ 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); } /** @@ -106,26 +99,18 @@ public function update(string $id): array */ public function destroy(string $id): bool { - try { - return $this->delete("customers/{$id}"); - } 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 { - return $this->post("customers/{$id}/restore"); - } catch (\Exception $e) { - return false; - } + return $this->post("customers/{$id}/restore"); } /** @@ -136,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 9881086..7882408 100644 --- a/src/Gateways/Asaas/Resources/Customer/Interface/CustomerInterface.php +++ b/src/Gateways/Asaas/Resources/Customer/Interface/CustomerInterface.php @@ -9,7 +9,7 @@ public function getAll(): array; public function create(): array; public function update(string $id): array; public function destroy(string $id): bool; - public function restore(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 96cca87..0dca1aa 100644 --- a/src/Gateways/Asaas/Traits/HasAsaasClient.php +++ b/src/Gateways/Asaas/Traits/HasAsaasClient.php @@ -114,7 +114,9 @@ protected function put(string $endpoint, array $data): array protected function delete(string $endpoint): bool { try { - return ($this->client->delete($endpoint) == 200); + $response = $this->client->delete($endpoint); + + return ($response->getStatusCode() == 200); } catch (\Exception $e) { return false; } From 8ee078e94b4835ac1624f1fb1955501a9df113d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Lucas?= Date: Tue, 17 Dec 2024 08:23:31 -0300 Subject: [PATCH 7/7] PHPAY-35: fix: code design pint --- examples/asaas/charges.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/asaas/charges.php b/examples/asaas/charges.php index 714efa3..45fb5d0 100644 --- a/examples/asaas/charges.php +++ b/examples/asaas/charges.php @@ -126,8 +126,8 @@ $confirmed = $phpay ->charge() ->confirmReceipt($chargeCreated['id'], [ - 'paymentDate' => date('Y-m-d'), - 'value' => 100.00, + 'paymentDate' => date('Y-m-d'), + 'value' => 100.00, 'notifyCustomer' => true, ]);