Skip to content

Commit

Permalink
Merge pull request #3 from AstrotechLabs/implements-transfer
Browse files Browse the repository at this point in the history
feat: Transfer module improve
  • Loading branch information
dersonsena authored Dec 24, 2023
2 parents d48747c + abe5553 commit fed56a1
Show file tree
Hide file tree
Showing 24 changed files with 404 additions and 36 deletions.
46 changes: 43 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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.
Expand Down
18 changes: 15 additions & 3 deletions src/AssasGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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,
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace AstrotechLabs\AsaasSdk\CreatePixCharge\Dto;
namespace AstrotechLabs\AsaasSdk\Pix\CreatePixCharge\Dto;

use JsonSerializable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace AstrotechLabs\AsaasSdk\CreatePixCharge\Dto;
namespace AstrotechLabs\AsaasSdk\Pix\CreatePixCharge\Dto;

final class QrCodeOutput
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace AstrotechLabs\AsaasSdk\CreatePixCharge\Exceptions;
namespace AstrotechLabs\AsaasSdk\Pix\CreatePixCharge\Exceptions;

use Exception;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace AstrotechLabs\AsaasSdk\CustomerIdentifierCreator\Dto;
namespace AstrotechLabs\AsaasSdk\Pix\CustomerIdentifierCreator\Dto;

class CustomerData
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace AstrotechLabs\AsaasSdk\CustomerIdentifierCreator\Dto;
namespace AstrotechLabs\AsaasSdk\Pix\CustomerIdentifierCreator\Dto;

class CustomerIdentifierOutput
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace AstrotechLabs\AsaasSdk\CustomerIdentifierCreator\Exceptions;
namespace AstrotechLabs\AsaasSdk\Pix\CustomerIdentifierCreator\Exceptions;

use Exception;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace AstrotechLabs\AsaasSdk\Enum;
namespace AstrotechLabs\AsaasSdk\Pix\Enum;

enum BillingTypes: string
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace AstrotechLabs\AsaasSdk\Webhook;
namespace AstrotechLabs\AsaasSdk\Pix\Webhook;

enum AsaasWebhookPaymentType: string
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace AstrotechLabs\AsaasSdk\Webhook;
namespace AstrotechLabs\AsaasSdk\Pix\Webhook;

enum AsaasWebhookStatus: string
{
Expand Down
71 changes: 71 additions & 0 deletions src/Transfer/CreateTransferCharge/CreateTransferChargeGateway.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace AstrotechLabs\AsaasSdk\Transfer\CreateTransferCharge;

use AstrotechLabs\AsaasSdk\Transfer\CreateTransferCharge\Dto\CreateTransferChargeOutput;
use AstrotechLabs\AsaasSdk\Transfer\CreateTransferCharge\Dto\TransferData;
use AstrotechLabs\AsaasSdk\Transfer\CreateTransferCharge\Exceptions\CreateTransferChargeException;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\ClientException;

final class CreateTransferChargeGateway
{
private GuzzleClient $httpClient;
private string $baseUrl;

public function __construct(
private readonly string $apiKey,
private readonly bool $isSandBox = false
) {
$this->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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace AstrotechLabs\AsaasSdk\Transfer\CreateTransferCharge\Dto;

use JsonSerializable;

final class CreateTransferChargeOutput implements JsonSerializable
{
public function __construct(
public readonly string $gatewayId,
public readonly string $status,
public readonly float $fee,
public readonly float $value,
public readonly bool $authorized,
public readonly array $details,
) {
}

public function toArray(): array
{
return get_object_vars($this);
}

public function jsonSerialize(): array
{
return $this->toArray();
}
}
48 changes: 48 additions & 0 deletions src/Transfer/CreateTransferCharge/Dto/TransferData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace AstrotechLabs\AsaasSdk\Transfer\CreateTransferCharge\Dto;

use AstrotechLabs\AsaasSdk\Transfer\Enum\OperationTypes;
use AstrotechLabs\AsaasSdk\Transfer\Enum\PixKeyTypes;
use DateTimeImmutable;

class TransferData
{
public function __construct(
public readonly float $value,
public readonly string $pixAddressKey,
public readonly PixKeyTypes $pixAddressKeyType,
public readonly OperationTypes $operationType = OperationTypes::PIX,
public readonly ?DateTimeImmutable $scheduleDate = null,
public readonly ?string $description = null,
) {
}

public function values(): array
{
$values = get_object_vars($this);
array_walk($values, fn (&$value, $property) => $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;
}
}
Loading

0 comments on commit fed56a1

Please sign in to comment.