diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fcaf8f3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +# VSCode +########################## +.history/ +.svn/ + +!.vendor/ +.vendor/* +!.vendor/.gitkeep \ No newline at end of file diff --git a/README.md b/README.md index 7daa960..205124d 100644 --- a/README.md +++ b/README.md @@ -183,10 +183,10 @@ $action123 = $asaas->city()->getById(123); Documentação Oficial -------------------- -Obs.: Esta é uma API não oficial. Foi feita com base na documentação disponibilizada [neste link](https://docs.google.com/document/d/1XUJRHY_0nd45CzFK5EmjDK92qgaQJGMxT0rjZriTk-g). +Obs.: Esta é uma API não oficial baseada na API ASAAS v3. A documentação oficial da API ASAAS está disponibilizada [neste link](https://asaasv3.docs.apiary.io). -Creditos +Créditos -------- * [Agência Softr Ltda - www.softr.com.br](http://www.softr.com.br) diff --git a/composer.json b/composer.json index a0f9fbf..022922b 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name" : "softr/asaas-php-sdk", + "name" : "lupainformatica/asaas-php-sdk", "type" : "library", "description": "Asaas.com PHP API Wrapper", "keywords" : ["Asaas", "API", "Payment SaaS", "Credit Card", "Boleto"], @@ -31,4 +31,4 @@ } }, "minimum-stability": "dev" -} \ No newline at end of file +} diff --git a/src/Adapter/GuzzleHttpAdapter.php b/src/Adapter/GuzzleHttpAdapter.php index 3498a7f..7b2f595 100644 --- a/src/Adapter/GuzzleHttpAdapter.php +++ b/src/Adapter/GuzzleHttpAdapter.php @@ -37,11 +37,11 @@ class GuzzleHttpAdapter implements AdapterInterface * Constructor * * @param string $token Access Token - * @param ClientInterface|null $client Client Instance + * @param ?ClientInterface $client Client Instance */ - public function __construct($token, ClientInterface $client = null) + public function __construct($token, ?ClientInterface $client = null) { - if(version_compare(ClientInterface::VERSION, '6') === 1) + if(version_compare(ClientInterface::MAJOR_VERSION, '6') === 1) { $this->client = $client ?: new Client(['headers' => ['access_token' => $token]]); } @@ -119,7 +119,7 @@ public function put($url, $content = '') public function post($url, $content = '') { $options = []; - $options['form_params'] = $content; + $options['json'] = $content; try { @@ -161,7 +161,18 @@ protected function handleError() $code = (int) $this->response->getStatusCode(); $content = json_decode($body); + + $errors = []; + if (isset($content->errors)) { + foreach ((array)$content->errors as $error) { + $errors[] = $error->code . ': ' . $error->description; + } + } + + if (!empty($errors)) { + throw new HttpException(implode('
', $errors), $code); + } throw new HttpException(isset($content->message) ? $content->message : 'Request not processed.', $code); } -} \ No newline at end of file +} diff --git a/src/Api/AbstractApi.php b/src/Api/AbstractApi.php index 4cd5626..be5ef16 100644 --- a/src/Api/AbstractApi.php +++ b/src/Api/AbstractApi.php @@ -16,14 +16,21 @@ abstract class AbstractApi * * @var string */ - const ENDPOINT_PRODUCAO = 'https://www.asaas.com/api/v2'; + const ENDPOINT_PRODUCAO = 'https://api.asaas.com/v3'; /** * Endpoint Homologação * * @var string */ - const ENDPOINT_HOMOLOGACAO = 'http://homolog.asaas.com/api/v2'; + const ENDPOINT_HOMOLOGACAO = 'https://sandbox.asaas.com/api/v3'; + + /** + * Endpoint Sandbox + * + * @var string + */ + const ENDPOINT_SANDBOX = 'https://sandbox.asaas.com/api/v3'; /** * Http Adapter Instance @@ -54,7 +61,16 @@ public function __construct(AdapterInterface $adapter, $ambiente = 'producao') { $this->adapter = $adapter; - $this->endpoint = $ambiente == 'homologacao' ? static::ENDPOINT_HOMOLOGACAO : static::ENDPOINT_PRODUCAO; + switch ($ambiente) { + case 'sandbox': + $this->endpoint = static::ENDPOINT_SANDBOX; + break; + case 'homologacao': + $this->endpoint = static::ENDPOINT_HOMOLOGACAO; + break; + default: + $this->endpoint = static::ENDPOINT_PRODUCAO; + } } /** @@ -79,4 +95,4 @@ public function getMeta() { return $this->meta; } -} \ No newline at end of file +} diff --git a/src/Api/Customer.php b/src/Api/Customer.php index 9d25793..1d2533b 100644 --- a/src/Api/Customer.php +++ b/src/Api/Customer.php @@ -27,7 +27,7 @@ public function getAll(array $filters = []) return array_map(function($customer) { - return new CustomerEntity($customer->customer); + return new CustomerEntity($customer); }, $customers->data); } @@ -46,23 +46,37 @@ public function getById($id) return new CustomerEntity($customer); } + /** + * Get Customer By Name + * + * @param string $name Customer Name + * @return CustomerEntity + */ + public function getByName($name) + { + return $this->getAll(['name' => $name]); + } + + /** + * Get Customer By CPF/CNPJ + * + * @param string $cpfCnpj Customer CPF / CNPJ + * @return CustomerEntity + */ + public function getByCpfCnpj($cpfCnpj) + { + return $this->getAll(['cpfCnpj' => $cpfCnpj]); + } + /** * Get Customer By Email * - * @param string $email Customer Id + * @param string $email Customer e-mail * @return CustomerEntity */ public function getByEmail($email) { - foreach($this->getAll(['name' => $email]) as $customer) - { - if($customer->email == $email) - { - return $customer; - } - } - - return; + return $this->getAll(['email' => $email]); } /** diff --git a/src/Api/Payment.php b/src/Api/Payment.php index f060751..5711400 100644 --- a/src/Api/Payment.php +++ b/src/Api/Payment.php @@ -1,8 +1,12 @@ adapter->get(sprintf('%s/payments?%s', $this->endpoint, http_build_query($filters))); + + $payments = json_decode($payments); + + $this->extractMeta($payments); + + return $payments; + } + /** * Get all payments * @@ -25,8 +46,7 @@ public function getAll(array $filters = []) $this->extractMeta($payments); - return array_map(function($payment) - { + return array_map(function ($payment) { return new PaymentEntity($payment); }, $payments->data); } @@ -51,7 +71,7 @@ public function getById($id) * * @param int $customerId Customer Id * @param array $filters (optional) Filters Array - * @return PaymentEntity + * @return array|PaymentEntity[] */ public function getByCustomer($customerId, array $filters = []) { @@ -61,8 +81,7 @@ public function getByCustomer($customerId, array $filters = []) $this->extractMeta($payments); - return array_map(function($payment) - { + return array_map(function ($payment) { return new PaymentEntity($payment); }, $payments->data); } @@ -72,9 +91,9 @@ public function getByCustomer($customerId, array $filters = []) * * @param int $subscriptionId Subscription Id * @param array $filters (optional) Filters Array - * @return PaymentEntity + * @return array|PaymentEntity[] */ - public function getBySubscription($subscriptionId) + public function getBySubscription($subscriptionId, array $filters = []) { $payments = $this->adapter->get(sprintf('%s/subscriptions/%s/payments?%s', $this->endpoint, $subscriptionId, http_build_query($filters))); @@ -82,8 +101,7 @@ public function getBySubscription($subscriptionId) $this->extractMeta($payments); - return array_map(function($payment) - { + return array_map(function ($payment) { return new PaymentEntity($payment); }, $payments->data); } @@ -103,11 +121,44 @@ public function create(array $data) return new PaymentEntity($payment); } + /** + * Return Pìx Qr Code + * + * @param int $id Payment Id + * @return PixQrCodeEntity + * @throws HttpException + */ + public function getPixQrCode($id) + { + $response = $this->adapter->post(sprintf('%s/payments/%s/pixQrCode', $this->endpoint, $id)); + + $qrCode = json_decode($response); + + return new PixQrCodeEntity($qrCode); + } + + /** + * Return the Bank Slip payment identification field + * + * @param int $id Payment Id + * @return BankSlipLineEntity + * @throws HttpException + */ + public function getBankSlipIdentificationField($id) + { + $response = $this->adapter->post(sprintf('%s/payments/%s/identificationField', $this->endpoint, $id)); + + $line = json_decode($response); + + return new BankSlipLineEntity($line); + } + /** * Update Payment By Id * * @param string $id Payment Id * @param array $data Payment Data + * @throws HttpException * @return PaymentEntity */ public function update($id, array $data) @@ -119,6 +170,22 @@ public function update($id, array $data) return new PaymentEntity($payment); } + /** + * Refund a Payment By Id + * + * @param string $id Payment Id + * @param array $data Payment Data + * @return PaymentEntity + */ + public function refund($id, array $data) + { + $payment = $this->adapter->post(sprintf('%s/payments/%s/refund', $this->endpoint, $id), $data); + + $payment = json_decode($payment); + + return new PaymentEntity($payment); + } + /** * Delete Payment By Id * @@ -128,4 +195,14 @@ public function delete($id) { $this->adapter->delete(sprintf('%s/payments/%s', $this->endpoint, $id)); } -} \ No newline at end of file + + /** + * Delete Payment By Id + * + * @param string|int $id Payment Id + */ + public function deleteInstallment($id) + { + $this->adapter->delete(sprintf('%s/installments/%s', $this->endpoint, $id)); + } +} diff --git a/src/Api/Subscription.php b/src/Api/Subscription.php index a76cfe0..0046559 100644 --- a/src/Api/Subscription.php +++ b/src/Api/Subscription.php @@ -1,4 +1,5 @@ adapter->get(sprintf('%s/subscriptions?%s', $this->endpoint, http_build_query($filters))); + + $subscriptions = json_decode($subscriptions); + + $this->extractMeta($subscriptions); + + return $subscriptions; + } + /** * Get all subscriptions * @@ -25,9 +43,8 @@ public function getAll(array $filters = []) $this->extractMeta($subscriptions); - return array_map(function($subscription) - { - return new SubscriptionEntity($subscription->subscription); + return array_map(function ($subscription) { + return new SubscriptionEntity($subscription); }, $subscriptions->data); } @@ -61,9 +78,8 @@ public function getByCustomer($customerId) $this->extractMeta($subscriptions); - return array_map(function($subscription) - { - return new SubscriptionEntity($subscription->subscription); + return array_map(function ($subscription) { + return new SubscriptionEntity($subscription); }, $subscriptions->data); } @@ -107,4 +123,4 @@ public function delete($id) { $this->adapter->delete(sprintf('%s/subscriptions/%s', $this->endpoint, $id)); } -} \ No newline at end of file +} diff --git a/src/Entity/AbstractEntity.php b/src/Entity/AbstractEntity.php index 5d3f780..f07ecb3 100644 --- a/src/Entity/AbstractEntity.php +++ b/src/Entity/AbstractEntity.php @@ -66,7 +66,7 @@ protected static function convertDateTime($date) return; } - $date = \DateTime::createFromFormat('d/m/Y', $date); + $date = new \DateTime($date); if(!$date) { @@ -91,4 +91,4 @@ protected static function convertToCamelCase($str) return lcfirst(preg_replace_callback('/(^|_)([a-z])/', $callback, $str)); } -} \ No newline at end of file +} diff --git a/src/Entity/BankSlipLineEntity.php b/src/Entity/BankSlipLineEntity.php new file mode 100644 index 0000000..bd3095e --- /dev/null +++ b/src/Entity/BankSlipLineEntity.php @@ -0,0 +1,52 @@ + + */ +final class BankSlipLineEntity extends \Softr\Asaas\Entity\AbstractEntity +{ + /** + * Identification Field + * @var string + */ + public $identificationField; + + /** + * Nosso Número + * @var string + */ + public $nossoNumero; + + /** + * Bar Code + * @var string + */ + public $barCode; + + /** + * @param string $identificationField + */ + public function setIdentificationField($identificationField) + { + $this->identificationField = $identificationField; + } + + /** + * @param string $nossoNumero + */ + public function setNossoNumero($nossoNumero) + { + $this->nossoNumero = $nossoNumero; + } + + /** + * @param string $barCode + */ + public function setBarCode($barCode) + { + $this->barCode = $barCode; + } +} \ No newline at end of file diff --git a/src/Entity/Customer.php b/src/Entity/Customer.php index 2b1d6c2..5e31ff1 100644 --- a/src/Entity/Customer.php +++ b/src/Entity/Customer.php @@ -58,8 +58,14 @@ final class Customer extends \Softr\Asaas\Entity\AbstractEntity */ public $province; + /** + * @var string Identificador do cliente no seu sistema + */ + public $externalReference; + /** * @var bool + * @deprecated Property not found on v3 API */ public $foreignCustomer; @@ -98,6 +104,26 @@ final class Customer extends \Softr\Asaas\Entity\AbstractEntity */ public $personType; + /** + * @var string + */ + public $additionalEmails; + + /** + * @var string + */ + public $municipalInscription; + + /** + * @var string + */ + public $stateInscription; + + /** + * @var string + */ + public $groupName; + /** * @var array */ diff --git a/src/Entity/Installment.php b/src/Entity/Installment.php new file mode 100644 index 0000000..8dedb2e --- /dev/null +++ b/src/Entity/Installment.php @@ -0,0 +1,17 @@ + + */ +final class Installment extends \Softr\Asaas\Entity\AbstractEntity +{ + /** + * @var array + */ + public $data; + + +} \ No newline at end of file diff --git a/src/Entity/Payment.php b/src/Entity/Payment.php index 72082c4..9e42d7c 100644 --- a/src/Entity/Payment.php +++ b/src/Entity/Payment.php @@ -8,6 +8,11 @@ */ final class Payment extends \Softr\Asaas\Entity\AbstractEntity { + /** + * @var string + */ + public $object; + /** * @var int */ @@ -18,6 +23,16 @@ final class Payment extends \Softr\Asaas\Entity\AbstractEntity */ public $customer; + /** + * @var string + */ + public $installment; + + /** + * @var string + */ + public $paymentLink; + /** * @var string */ @@ -28,6 +43,36 @@ final class Payment extends \Softr\Asaas\Entity\AbstractEntity */ public $billingType; + /** + * @var bool + */ + public $canBePaidAfterDueDate; + + /** + * @var bool + */ + public $deleted; + + /** + * @var bool + */ + public $anticipated; + + /** + * @var bool + */ + public $anticipable; + + /** + * @var bool + */ + public $postalService; + + /** + * @var string + */ + public $pixTransaction; + /** * @var float */ @@ -83,11 +128,21 @@ final class Payment extends \Softr\Asaas\Entity\AbstractEntity */ public $invoiceUrl; + /** + * @var string + */ + public $transactionReceiptUrl; + /** * @var string */ public $boletoUrl; + /** + * @var string + */ + public $bankSlipUrl; + /** * @var int */ @@ -193,6 +248,36 @@ final class Payment extends \Softr\Asaas\Entity\AbstractEntity */ public $creditCardHolderMobilePhoneDDD; + /** + * @var string Identificador do título no sistema origem + */ + public $externalReference; + + /** + * @var int + */ + public $installmentNumber; + + /** + * @var array|string + */ + public $discount; + + /** + * @var array|string + */ + public $fine; + + /** + * @var array|string + */ + public $interest; + + /** + * @var string + */ + public $confirmedDate; + /** * @param string $dueDate */ @@ -200,4 +285,52 @@ public function setDueDate($dueDate) { $this->dueDate = static::convertDateTime($dueDate); } -} \ No newline at end of file + + /** + * @param string $originalDueDate + */ + public function setOriginalDueDate($originalDueDate) + { + $this->originalDueDate = static::convertDateTime($originalDueDate); + } + + /** + * @param string $paymentDate + */ + public function setPaymentDate($paymentDate) + { + $this->paymentDate = static::convertDateTime($paymentDate); + } + + /** + * @param string $clientPaymentDate + */ + public function setClientPaymentDate($clientPaymentDate) + { + $this->clientPaymentDate = static::convertDateTime($clientPaymentDate); + } + + /** + * @param string $creditDate + */ + public function setCreditDate($creditDate) + { + $this->creditDate = static::convertDateTime($creditDate); + } + + /** + * @param string $estimatedCreditDate + */ + public function setEstimatedCreditDate($estimatedCreditDate) + { + $this->estimatedCreditDate = static::convertDateTime($estimatedCreditDate); + } + + /** + * @param string $confirmedDate + */ + public function setConfirmedDate($confirmedDate) + { + $this->confirmedDate = static::convertDateTime($confirmedDate); + } +} diff --git a/src/Entity/PixQrCodeEntity.php b/src/Entity/PixQrCodeEntity.php new file mode 100644 index 0000000..2be6d1a --- /dev/null +++ b/src/Entity/PixQrCodeEntity.php @@ -0,0 +1,66 @@ + + */ +final class PixQrCodeEntity extends \Softr\Asaas\Entity\AbstractEntity +{ + /** + * Encoded Image + * @var string + */ + public $encodedImage; + + /** + * QRcode Copy and Paste + * @var string + */ + public $payload; + + /** + * QRCode Expiration Date + * @var \DateTime + */ + public $expirationDate; + + /** + * QRCode Description + * @var string + */ + public $description; + + /** + * @param string $encodedImage + */ + public function setEncodedImage($encodedImage) + { + $this->encodedImage = $encodedImage; + } + + /** + * @param string $payload + */ + public function setPayload($payload) + { + $this->payload = $payload; + } + + /** + * @param string $expirationDate + */ + public function setExpirationDate($expirationDate) + { + $this->expirationDate = static::convertDateTime($expirationDate); + } + + /** + * @param string $description + */ + public function setDescription($description) + { + $this->description = $description; + } +} \ No newline at end of file diff --git a/src/Entity/Subscription.php b/src/Entity/Subscription.php index 67bcd10..fa9ff2d 100644 --- a/src/Entity/Subscription.php +++ b/src/Entity/Subscription.php @@ -1,4 +1,5 @@ endDate = static::convertDateTime($endDate); } -} \ No newline at end of file +}