diff --git a/README.md b/README.md index 4145443..0f7ffa3 100644 --- a/README.md +++ b/README.md @@ -23,18 +23,19 @@ na seção `require` do seu arquivo `composer.json`. ## Como Usar? ### Minimo para utilização +### Criação de um deposito (PIX) ```php use AstrotechLabs\AsaasSdk\AssasGateway; -use AstrotechLabs\AsaasSdk\Enum\BillingTypes; use AstrotechLabs\AsaasSdk\AssasGatewayParams; -use AstrotechLabs\AsaasSdk\CreatePixCharge\Dto\PixData; +use AstrotechLabs\AsaasSdk\Pix\CreatePixCharge\Dto\PixData; +use AstrotechLabs\AsaasSdk\Pix\Enum\BillingTypes; $asaasGateway = new AssasGateway(new AssasGatewayParams( apiKey: 'xxxxxxxxxx', // isSandBox: true (opcional) )); -$pixChargeResponse = $asaasGateway->createCharge(new PixData( +$pixChargeResponse = $asaasGateway->createPixCharge(new PixData( customer: new CustomerData( name: 'Joãozinho Barbosa', phone: '999999999', @@ -66,6 +67,45 @@ print_r($pixChargeResponse); ] ``` +### Criação de uma transferência (PIX) +```php +use AstrotechLabs\AsaasSdk\AssasGateway; +use AstrotechLabs\AsaasSdk\AssasGatewayParams; +use AstrotechLabs\AsaasSdk\Transfer\CreateTransferCharge\Dto\TransferData; +use AstrotechLabs\AsaasSdk\Transfer\Enum\PixKeyTypes; + +$asaasGateway = new AssasGateway(new AssasGatewayParams( + apiKey: $_ENV['ASAAS_API_KEY'], + isSandBox: false +)); + +$transferChargeResponse = $asaasGateway->createTransferCharge(new TransferData( + value: 1, + pixAddressKey: 'xxxxxxxx-xxxxx-xxxxx-xxxx', + pixAddressKeyType: PixKeyTypes::RANDOM_KEY +)); + +print_r($transferChargeResponse); +``` + +### Saída +``` +[ + 'gatewayId' => 'fb408225-8d98-4afe-b193-894cbdf1db55' + 'status' => 'PENDING' + 'fee' => 0 + 'value' => 1 + 'authorized' => false + 'details' => [ + 'object' => 'transfer' + 'id' => 'fb408225-8d98-4afe-b193-894cbdf1db55' + 'value' => 1.0, + ........ + ] +] +``` + + ## Contributing Pull Request são bem-vindas. Para mudanças importantes, abra primeiro uma issue para discutir o que você gostaria de mudar. diff --git a/src/AssasGateway.php b/src/AssasGateway.php index 2b1b26b..97be68b 100644 --- a/src/AssasGateway.php +++ b/src/AssasGateway.php @@ -4,8 +4,10 @@ namespace AstrotechLabs\AsaasSdk; -use AstrotechLabs\AsaasSdk\CreatePixCharge\Dto\PixData; -use AstrotechLabs\AsaasSdk\CreatePixCharge\CreatePixChargeGateway; +use AstrotechLabs\AsaasSdk\Pix\CreatePixCharge\CreatePixChargeGateway; +use AstrotechLabs\AsaasSdk\Pix\CreatePixCharge\Dto\PixData; +use AstrotechLabs\AsaasSdk\Transfer\CreateTransferCharge\CreateTransferChargeGateway; +use AstrotechLabs\AsaasSdk\Transfer\CreateTransferCharge\Dto\TransferData; class AssasGateway { @@ -14,7 +16,7 @@ public function __construct( ) { } - public function createCharge(PixData $pixData): array + public function createPixCharge(PixData $pixData): array { $createPixChargeGateway = new CreatePixChargeGateway( apiKey: $this->params->apiKey, @@ -23,4 +25,14 @@ public function createCharge(PixData $pixData): array return $createPixChargeGateway->createCharge($pixData)->toArray(); } + + public function createTransferCharge(TransferData $transferData): array + { + $createTransferChargeGateway = new CreateTransferChargeGateway( + apiKey: $this->params->apiKey, + isSandBox: $this->params->isSandBox + ); + + return $createTransferChargeGateway->createCharge($transferData)->toArray(); + } } diff --git a/src/CreatePixCharge/CreatePixChargeGateway.php b/src/Pix/CreatePixCharge/CreatePixChargeGateway.php similarity index 89% rename from src/CreatePixCharge/CreatePixChargeGateway.php rename to src/Pix/CreatePixCharge/CreatePixChargeGateway.php index fe70227..fafdcf4 100644 --- a/src/CreatePixCharge/CreatePixChargeGateway.php +++ b/src/Pix/CreatePixCharge/CreatePixChargeGateway.php @@ -2,14 +2,14 @@ declare(strict_types=1); -namespace AstrotechLabs\AsaasSdk\CreatePixCharge; +namespace AstrotechLabs\AsaasSdk\Pix\CreatePixCharge; +use AstrotechLabs\AsaasSdk\Pix\CreatePixCharge\Dto\CreatePixChargeOutput; +use AstrotechLabs\AsaasSdk\Pix\CreatePixCharge\Dto\PixData; +use AstrotechLabs\AsaasSdk\Pix\CreatePixCharge\Dto\QrCodeOutput; +use AstrotechLabs\AsaasSdk\Pix\CreatePixCharge\Exceptions\CreatePixChargeException; use GuzzleHttp\Client as GuzzleClient; use GuzzleHttp\Exception\ClientException; -use AstrotechLabs\AsaasSdk\CreatePixCharge\Dto\PixData; -use AstrotechLabs\AsaasSdk\CreatePixCharge\Dto\QrCodeOutput; -use AstrotechLabs\AsaasSdk\CreatePixCharge\Dto\CreatePixChargeOutput; -use AstrotechLabs\AsaasSdk\CreatePixCharge\Exceptions\CreatePixChargeException; final class CreatePixChargeGateway { diff --git a/src/CreatePixCharge/Dto/CreatePixChargeOutput.php b/src/Pix/CreatePixCharge/Dto/CreatePixChargeOutput.php similarity index 90% rename from src/CreatePixCharge/Dto/CreatePixChargeOutput.php rename to src/Pix/CreatePixCharge/Dto/CreatePixChargeOutput.php index dae796c..d9de2e8 100644 --- a/src/CreatePixCharge/Dto/CreatePixChargeOutput.php +++ b/src/Pix/CreatePixCharge/Dto/CreatePixChargeOutput.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace AstrotechLabs\AsaasSdk\CreatePixCharge\Dto; +namespace AstrotechLabs\AsaasSdk\Pix\CreatePixCharge\Dto; use JsonSerializable; diff --git a/src/CreatePixCharge/Dto/PixData.php b/src/Pix/CreatePixCharge/Dto/PixData.php similarity index 81% rename from src/CreatePixCharge/Dto/PixData.php rename to src/Pix/CreatePixCharge/Dto/PixData.php index ff756b4..98782c7 100644 --- a/src/CreatePixCharge/Dto/PixData.php +++ b/src/Pix/CreatePixCharge/Dto/PixData.php @@ -2,10 +2,10 @@ declare(strict_types=1); -namespace AstrotechLabs\AsaasSdk\CreatePixCharge\Dto; +namespace AstrotechLabs\AsaasSdk\Pix\CreatePixCharge\Dto; -use AstrotechLabs\AsaasSdk\CustomerIdentifierCreator\Dto\CustomerData; -use AstrotechLabs\AsaasSdk\Enum\BillingTypes; +use AstrotechLabs\AsaasSdk\Pix\CustomerIdentifierCreator\Dto\CustomerData; +use AstrotechLabs\AsaasSdk\Pix\Enum\BillingTypes; final class PixData { diff --git a/src/CreatePixCharge/Dto/QrCodeOutput.php b/src/Pix/CreatePixCharge/Dto/QrCodeOutput.php similarity index 92% rename from src/CreatePixCharge/Dto/QrCodeOutput.php rename to src/Pix/CreatePixCharge/Dto/QrCodeOutput.php index ee1de52..fdc34f7 100644 --- a/src/CreatePixCharge/Dto/QrCodeOutput.php +++ b/src/Pix/CreatePixCharge/Dto/QrCodeOutput.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace AstrotechLabs\AsaasSdk\CreatePixCharge\Dto; +namespace AstrotechLabs\AsaasSdk\Pix\CreatePixCharge\Dto; final class QrCodeOutput { diff --git a/src/CreatePixCharge/Exceptions/CreatePixChargeException.php b/src/Pix/CreatePixCharge/Exceptions/CreatePixChargeException.php similarity index 94% rename from src/CreatePixCharge/Exceptions/CreatePixChargeException.php rename to src/Pix/CreatePixCharge/Exceptions/CreatePixChargeException.php index 770cb44..871e527 100644 --- a/src/CreatePixCharge/Exceptions/CreatePixChargeException.php +++ b/src/Pix/CreatePixCharge/Exceptions/CreatePixChargeException.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace AstrotechLabs\AsaasSdk\CreatePixCharge\Exceptions; +namespace AstrotechLabs\AsaasSdk\Pix\CreatePixCharge\Exceptions; use Exception; diff --git a/src/CustomerIdentifierCreator/CustomerIdentifierCreator.php b/src/Pix/CustomerIdentifierCreator/CustomerIdentifierCreator.php similarity index 83% rename from src/CustomerIdentifierCreator/CustomerIdentifierCreator.php rename to src/Pix/CustomerIdentifierCreator/CustomerIdentifierCreator.php index db05e3a..ad845dd 100644 --- a/src/CustomerIdentifierCreator/CustomerIdentifierCreator.php +++ b/src/Pix/CustomerIdentifierCreator/CustomerIdentifierCreator.php @@ -2,13 +2,13 @@ declare(strict_types=1); -namespace AstrotechLabs\AsaasSdk\CustomerIdentifierCreator; +namespace AstrotechLabs\AsaasSdk\Pix\CustomerIdentifierCreator; +use AstrotechLabs\AsaasSdk\Pix\CustomerIdentifierCreator\Dto\CustomerData; +use AstrotechLabs\AsaasSdk\Pix\CustomerIdentifierCreator\Dto\CustomerIdentifierOutput; +use AstrotechLabs\AsaasSdk\Pix\CustomerIdentifierCreator\Exceptions\CreateCustomerIdentifierException; use GuzzleHttp\Client as GuzzleClient; use GuzzleHttp\Exception\ClientException; -use AstrotechLabs\AsaasSdk\CustomerIdentifierCreator\Dto\CustomerData; -use AstrotechLabs\AsaasSdk\CustomerIdentifierCreator\Dto\CustomerIdentifierOutput; -use AstrotechLabs\AsaasSdk\CustomerIdentifierCreator\Exceptions\CreateCustomerIdentifierException; class CustomerIdentifierCreator { diff --git a/src/CustomerIdentifierCreator/Dto/CustomerData.php b/src/Pix/CustomerIdentifierCreator/Dto/CustomerData.php similarity index 95% rename from src/CustomerIdentifierCreator/Dto/CustomerData.php rename to src/Pix/CustomerIdentifierCreator/Dto/CustomerData.php index fbeabd5..98d3fff 100644 --- a/src/CustomerIdentifierCreator/Dto/CustomerData.php +++ b/src/Pix/CustomerIdentifierCreator/Dto/CustomerData.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace AstrotechLabs\AsaasSdk\CustomerIdentifierCreator\Dto; +namespace AstrotechLabs\AsaasSdk\Pix\CustomerIdentifierCreator\Dto; class CustomerData { diff --git a/src/CustomerIdentifierCreator/Dto/CustomerIdentifierOutput.php b/src/Pix/CustomerIdentifierCreator/Dto/CustomerIdentifierOutput.php similarity index 70% rename from src/CustomerIdentifierCreator/Dto/CustomerIdentifierOutput.php rename to src/Pix/CustomerIdentifierCreator/Dto/CustomerIdentifierOutput.php index 6c5e7b7..f96c69b 100644 --- a/src/CustomerIdentifierCreator/Dto/CustomerIdentifierOutput.php +++ b/src/Pix/CustomerIdentifierCreator/Dto/CustomerIdentifierOutput.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace AstrotechLabs\AsaasSdk\CustomerIdentifierCreator\Dto; +namespace AstrotechLabs\AsaasSdk\Pix\CustomerIdentifierCreator\Dto; class CustomerIdentifierOutput { diff --git a/src/CustomerIdentifierCreator/Exceptions/CreateCustomerIdentifierException.php b/src/Pix/CustomerIdentifierCreator/Exceptions/CreateCustomerIdentifierException.php similarity index 93% rename from src/CustomerIdentifierCreator/Exceptions/CreateCustomerIdentifierException.php rename to src/Pix/CustomerIdentifierCreator/Exceptions/CreateCustomerIdentifierException.php index 0a7010d..dee775c 100644 --- a/src/CustomerIdentifierCreator/Exceptions/CreateCustomerIdentifierException.php +++ b/src/Pix/CustomerIdentifierCreator/Exceptions/CreateCustomerIdentifierException.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace AstrotechLabs\AsaasSdk\CustomerIdentifierCreator\Exceptions; +namespace AstrotechLabs\AsaasSdk\Pix\CustomerIdentifierCreator\Exceptions; use Exception; diff --git a/src/Enum/BillingTypes.php b/src/Pix/Enum/BillingTypes.php similarity index 81% rename from src/Enum/BillingTypes.php rename to src/Pix/Enum/BillingTypes.php index 68713ef..d35599c 100644 --- a/src/Enum/BillingTypes.php +++ b/src/Pix/Enum/BillingTypes.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace AstrotechLabs\AsaasSdk\Enum; +namespace AstrotechLabs\AsaasSdk\Pix\Enum; enum BillingTypes: string { diff --git a/src/Webhook/AsaasWebhookEvents.php b/src/Pix/Webhook/AsaasWebhookEvents.php similarity index 89% rename from src/Webhook/AsaasWebhookEvents.php rename to src/Pix/Webhook/AsaasWebhookEvents.php index e43e128..996c7dd 100644 --- a/src/Webhook/AsaasWebhookEvents.php +++ b/src/Pix/Webhook/AsaasWebhookEvents.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace AstrotechLabs\AsaasSdk\Webhook; +namespace AstrotechLabs\AsaasSdk\Pix\Webhook; /** * @see https://docs.asaas.com/docs/webhook-para-cobrancas diff --git a/src/Webhook/AsaasWebhookPaymentType.php b/src/Pix/Webhook/AsaasWebhookPaymentType.php similarity index 86% rename from src/Webhook/AsaasWebhookPaymentType.php rename to src/Pix/Webhook/AsaasWebhookPaymentType.php index 2bf3d60..df8a752 100644 --- a/src/Webhook/AsaasWebhookPaymentType.php +++ b/src/Pix/Webhook/AsaasWebhookPaymentType.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace AstrotechLabs\AsaasSdk\Webhook; +namespace AstrotechLabs\AsaasSdk\Pix\Webhook; enum AsaasWebhookPaymentType: string { diff --git a/src/Webhook/AsaasWebhookStatus.php b/src/Pix/Webhook/AsaasWebhookStatus.php similarity index 74% rename from src/Webhook/AsaasWebhookStatus.php rename to src/Pix/Webhook/AsaasWebhookStatus.php index 8ca36fc..875b419 100644 --- a/src/Webhook/AsaasWebhookStatus.php +++ b/src/Pix/Webhook/AsaasWebhookStatus.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace AstrotechLabs\AsaasSdk\Webhook; +namespace AstrotechLabs\AsaasSdk\Pix\Webhook; enum AsaasWebhookStatus: string { diff --git a/src/Transfer/CreateTransferCharge/CreateTransferChargeGateway.php b/src/Transfer/CreateTransferCharge/CreateTransferChargeGateway.php new file mode 100644 index 0000000..a645a87 --- /dev/null +++ b/src/Transfer/CreateTransferCharge/CreateTransferChargeGateway.php @@ -0,0 +1,71 @@ +baseUrl = $this->isSandBox ? + 'https://sandbox.asaas.com/api/v3/' : + 'https://www.asaas.com/api/v3/'; + + $this->httpClient = new GuzzleClient([ + 'base_uri' => $this->baseUrl, + 'timeout' => 10 + ]); + } + + public function createCharge(TransferData $transferData): CreateTransferChargeOutput + { + $headers = [ + "Content-Type" => "application/json", + "access_token" => $this->apiKey + ]; + + try { + $response = $this->httpClient->post("transfers", [ + 'headers' => $headers, + 'json' => $transferData->values() + ]); + } catch (ClientException $e) { + $responsePayload = json_decode($e->getResponse()->getBody()->getContents(), true); + throw new CreateTransferChargeException( + 1001, + $responsePayload['errors'][0]['description'], + $responsePayload['errors'][0]['code'], + $transferData->values(), + $responsePayload + ); + } + + $responsePayload = json_decode($response->getBody()->getContents(), true); + + return new CreateTransferChargeOutput( + gatewayId: $responsePayload['id'], + status: $responsePayload['status'], + fee: $responsePayload['transferFee'], + value: $responsePayload['value'], + authorized: $responsePayload['authorized'], + details: $responsePayload, + ); + } + + public function getBaseUrl(): string + { + return $this->baseUrl; + } +} diff --git a/src/Transfer/CreateTransferCharge/Dto/CreateTransferChargeOutput.php b/src/Transfer/CreateTransferCharge/Dto/CreateTransferChargeOutput.php new file mode 100644 index 0000000..10beb08 --- /dev/null +++ b/src/Transfer/CreateTransferCharge/Dto/CreateTransferChargeOutput.php @@ -0,0 +1,30 @@ +toArray(); + } +} diff --git a/src/Transfer/CreateTransferCharge/Dto/TransferData.php b/src/Transfer/CreateTransferCharge/Dto/TransferData.php new file mode 100644 index 0000000..8c392f4 --- /dev/null +++ b/src/Transfer/CreateTransferCharge/Dto/TransferData.php @@ -0,0 +1,48 @@ + $value = $this->get($property)); + return $values; + } + + public function get(string $property): mixed + { + $getter = "get" . ucfirst($property); + + if (method_exists($this, $getter)) { + return $this->{$getter}(); + } + + return $this->{$property}; + } + + public function getPixKeyType(): string + { + return $this->pixKeyType->value; + } + + public function getOperationType(): string + { + return $this->operationType->value; + } +} diff --git a/src/Transfer/CreateTransferCharge/Exceptions/CreateTransferChargeException.php b/src/Transfer/CreateTransferCharge/Exceptions/CreateTransferChargeException.php new file mode 100644 index 0000000..1601507 --- /dev/null +++ b/src/Transfer/CreateTransferCharge/Exceptions/CreateTransferChargeException.php @@ -0,0 +1,50 @@ +code = $code; + $this->type = $type; + $this->description = $description; + $this->requestPayload = $requestPayload; + $this->responsePayload = $responsePayload; + parent::__construct("$description - [{$code}]"); + } + + public function getDescription(): string + { + return $this->description; + } + + public function getType(): string + { + return $this->type; + } + + public function getRequestPayload(): array + { + return $this->requestPayload; + } + + public function getResponsePayload(): array + { + return $this->responsePayload; + } +} diff --git a/src/Transfer/Enum/OperationTypes.php b/src/Transfer/Enum/OperationTypes.php new file mode 100644 index 0000000..508bedc --- /dev/null +++ b/src/Transfer/Enum/OperationTypes.php @@ -0,0 +1,10 @@ +assertIsString($sut->getBaseUrl()); + $this->assertNotEmpty($sut->getBaseUrl()); + $this->assertSame('https://www.asaas.com/api/v3/', $sut->getBaseUrl()); + } + + public function testItShouldDefineCorrectUrlWhenAsInSandbox() + { + $sut = new CreateTransferChargeGateway($_ENV['ASAAS_API_KEY'], true); + + $this->assertIsString($sut->getBaseUrl()); + $this->assertNotEmpty($sut->getBaseUrl()); + $this->assertSame('https://sandbox.asaas.com/api/v3/', $sut->getBaseUrl()); + } + + private function makeSut(): CreateTransferChargeGateway + { + return new CreateTransferChargeGateway($_ENV['ASAAS_API_KEY'], true); + } + + public function testItShouldCreateTransferChargeWithMinimumProvided() + { + $sut = $this->makeSut(); + + $response = $sut->createCharge(new TransferData( + value: 200, + pixAddressKey: 'c39ab866-b4f0-4646-a931-b09d892b1c46', + pixAddressKeyType: PixKeyTypes::RANDOM_KEY + )); + + $this->assertIsObject($response); + $this->assertNotEmpty($response->gatewayId); + $this->assertNotEmpty($response->status); + $this->assertSame(0.0, $response->fee); + $this->assertNotEmpty($response->value); + $this->assertIsBool($response->authorized); + $this->assertNotEmpty($response->details); + $this->assertIsArray($response->details); + $this->assertArrayHasKey('id', $response->details); + $this->assertArrayHasKey('status', $response->details); + $this->assertArrayHasKey('transferFee', $response->details); + $this->assertArrayHasKey('value', $response->details); + $this->assertArrayHasKey('authorized', $response->details); + $this->assertSame($response->gatewayId, $response->details['id']); + $this->assertSame($response->status, $response->details['status']); + $this->assertSame((float)$response->fee, (float)$response->details['transferFee']); + $this->assertSame($response->value, $response->details['value']); + $this->assertSame($response->authorized, $response->details['authorized']); + } + + public function testItShouldThrowAnExceptionWhenTryCreateNewChargeWhenBeforeCreatedNotHasCompleted() + { + $this->expectException(CreateTransferChargeException::class); + $this->expectExceptionCode(1001); + + $sut = $this->makeSut(); + + $response = $sut->createCharge(new TransferData( + value: 200, + pixAddressKey: 'c39ab866-b4f0-4646-a931-b09d892b1c46', + pixAddressKeyType: PixKeyTypes::RANDOM_KEY + )); + } + + public function testItShouldThrowAnExceptionWhenNotProvideValidPixAddressKeyValue() + { + $this->expectException(CreateTransferChargeException::class); + $this->expectExceptionCode(1001); + $this->expectExceptionMessage('Necessário informar uma chave Pix válida.'); + + $sut = $this->makeSut(); + + $response = $sut->createCharge(new TransferData( + value: 0, + pixAddressKey: 'abcde', + pixAddressKeyType: PixKeyTypes::RANDOM_KEY + )); + } +} diff --git a/tests/Integration/CustomerIdentifierCreator/CustomerIdentifierCreatorTest.php b/tests/Integration/CustomerIdentifierCreator/CustomerIdentifierCreatorTest.php index 49e02d1..2d537ea 100644 --- a/tests/Integration/CustomerIdentifierCreator/CustomerIdentifierCreatorTest.php +++ b/tests/Integration/CustomerIdentifierCreator/CustomerIdentifierCreatorTest.php @@ -4,9 +4,9 @@ namespace Tests\Integration\CustomerIdentifierCreator; +use AstrotechLabs\AsaasSdk\Pix\CustomerIdentifierCreator\CustomerIdentifierCreator; +use AstrotechLabs\AsaasSdk\Pix\CustomerIdentifierCreator\Dto\CustomerData; use Tests\TestCase; -use AstrotechLabs\AsaasSdk\CustomerIdentifierCreator\Dto\CustomerData; -use AstrotechLabs\AsaasSdk\CustomerIdentifierCreator\CustomerIdentifierCreator; final class CustomerIdentifierCreatorTest extends TestCase {