diff --git a/.husky/pre-commit b/.husky/pre-commit index 3f9f510..20d18fa 100644 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1 +1,3 @@ -./vendor/bin/pint \ No newline at end of file +./vendor/bin/pint + +git add . \ No newline at end of file diff --git a/src/Gateways/Asaas/AsaasGateway.php b/src/Gateways/Asaas/AsaasGateway.php index b97ba54..021a37f 100644 --- a/src/Gateways/Asaas/AsaasGateway.php +++ b/src/Gateways/Asaas/AsaasGateway.php @@ -74,12 +74,6 @@ 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)) { diff --git a/src/Gateways/Asaas/Requests/AsaasInvoiceRequest.php b/src/Gateways/Asaas/Requests/AsaasInvoiceRequest.php new file mode 100644 index 0000000..395b800 --- /dev/null +++ b/src/Gateways/Asaas/Requests/AsaasInvoiceRequest.php @@ -0,0 +1,63 @@ +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 (!BillingType::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/Resources/Invoice.php b/src/Gateways/Asaas/Resources/Invoice.php index d8dbea9..ca7a484 100644 --- a/src/Gateways/Asaas/Resources/Invoice.php +++ b/src/Gateways/Asaas/Resources/Invoice.php @@ -4,6 +4,7 @@ use Illuminate\Support\Facades\Http; use PHPay\Exceptions\AsaasExceptions; +use PHPay\Gateways\Asaas\Requests\AsaasInvoiceRequest; use PHPay\Gateways\Asaas\Resources\Interfaces\InvoiceInterface; use PHPay\Gateways\Gateway; @@ -14,6 +15,11 @@ final class Invoice extends Resource implements InvoiceInterface */ public array $invoice; + /** + * @var array + */ + public array $client; + /** * @var array */ @@ -34,6 +40,7 @@ public function __construct(array $invoice, Gateway $gateway) { $this->invoice = $invoice; $this->gateway = $gateway; + $this->client = $gateway->client; } /** @@ -43,13 +50,16 @@ public function __construct(array $invoice, Gateway $gateway) */ 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()); - } + AsaasInvoiceRequest::validate( + $this->client, + $this->invoice + ); + + $this->invoice['customer'] = $this->client['id']; + + $this->invoice = Http::asaas() + ->post(env('ASSAS_PAYMENTS'), $this->invoice) + ->json(); return $this; } diff --git a/src/Gateways/Gateway.php b/src/Gateways/Gateway.php index e7d4852..55770b9 100644 --- a/src/Gateways/Gateway.php +++ b/src/Gateways/Gateway.php @@ -4,6 +4,7 @@ abstract class Gateway { + public array $client; abstract public function client(array $client = []): object; abstract public function invoice(array $invoice): object; }