-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from AstrotechLabs/implements-transfer
feat: Transfer module improve
- Loading branch information
Showing
24 changed files
with
404 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/Transfer/CreateTransferCharge/CreateTransferChargeGateway.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/Transfer/CreateTransferCharge/Dto/CreateTransferChargeOutput.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.