diff --git a/composer.json b/composer.json index b04efed..c792eef 100644 --- a/composer.json +++ b/composer.json @@ -12,10 +12,10 @@ "homepage": "https://github.com/kamerk22/amazongiftcode", "keywords": ["Laravel", "AmazonGiftCode", "Amazon", "GiftCard", "AGCOD", "Incentives API", "Amazon Incentives API"], "require": { - "illuminate/support": "~5" + "illuminate/support": "~5|^6.0|^7.0" }, "require-dev": { - "phpunit/phpunit": "~7.0", + "phpunit/phpunit": "~7.0|~8.0", "mockery/mockery": "^1.1", "orchestra/testbench": "~3.0", "sempro/phpunit-pretty-print": "^1.0" diff --git a/readme.md b/readme.md index 2f3096f..31cd93f 100644 --- a/readme.md +++ b/readme.md @@ -42,6 +42,10 @@ To Cancel Amazon Gift Card ```php $aws = AmazonGiftCode::make()->cancelGiftCard($creationRequestId, $gcId); ``` +To Get Available Funds Balance +```php +$aws = AmazonGiftCode::make()->getAvailableFunds(); +``` ## Available Methods @@ -156,6 +160,54 @@ $rawJson = $aws->getRawJson(); ``` +------------------- +### GetAvailableFunds + +`getStatus()` + +Get the status of perform request. (`status`) + +```php +$status = $aws->getStatus(); +``` +------------------- +`getAmount()` + +To get available balance amount. (`amount`) + + +```php +$amount = $aws->getAmount(); +``` +------------------- +`getCurrency()` + +To get currency. (`currency`) + + +```php +$currency = $aws->getCurrency(); + +``` +------------------- +`getTimestamp()` + +Get request timestamp. (`getTimestamp`) + + +```php +$timestamp = $aws->getTimestamp(); +``` + +------------------- +`getRawJson()` + +Get the raw JSON response. (original response) + + +```php +$rawJson = $aws->getRawJson(); +``` ## Change log diff --git a/src/AWS/AWS.php b/src/AWS/AWS.php index 32c78de..15c0e89 100644 --- a/src/AWS/AWS.php +++ b/src/AWS/AWS.php @@ -14,6 +14,7 @@ use kamerk22\AmazonGiftCode\Config\Config; use kamerk22\AmazonGiftCode\Exceptions\AmazonErrors; use kamerk22\AmazonGiftCode\Response\CancelResponse; +use kamerk22\AmazonGiftCode\Response\CreateBalanceResponse; use kamerk22\AmazonGiftCode\Response\CreateResponse; class AWS @@ -30,6 +31,7 @@ class AWS public const TERMINATION_STRING = 'aws4_request'; public const CREATE_GIFT_CARD_SERVICE = 'CreateGiftCard'; public const CANCEL_GIFT_CARD_SERVICE = 'CancelGiftCard'; + public const GET_AVAILABLE_FUNDS_SERVICE = 'GetAvailableFunds'; private $_config; @@ -76,6 +78,19 @@ public function cancelCode($creationRequestId, $gcId): CancelResponse return new CancelResponse($result); } + /** + * @return CreateBalanceResponse + */ + public function getBalance(): CreateBalanceResponse + { + $serviceOperation = self::GET_AVAILABLE_FUNDS_SERVICE; + $payload = $this->getAvailableFundsPayload(); + $canonicalRequest = $this->getCanonicalRequest($serviceOperation, $payload); + $dateTimeString = $this->getTimestamp(); + $result = json_decode($this->makeRequest($payload, $canonicalRequest, $serviceOperation, $dateTimeString), true); + return new CreateBalanceResponse($result); + } + /** * @param $payload * @param $canonicalRequest @@ -265,6 +280,17 @@ public function getCancelGiftCardPayload($creationRequestId, $gcId): string return json_encode($payload); } + /** + * @return string + */ + public function getAvailableFundsPayload(): string + { + $payload = [ + 'partnerId' => $this->_config->getPartner(), + ]; + return json_encode($payload); + } + /** * @param $serviceOperation * @param $payload diff --git a/src/AmazonGiftCode.php b/src/AmazonGiftCode.php index e9e3e59..b924c1b 100644 --- a/src/AmazonGiftCode.php +++ b/src/AmazonGiftCode.php @@ -47,6 +47,15 @@ public function cancelGiftCard(string $creationRequestId, string $gcId): Respons return (new AWS($this->_config))->cancelCode($creationRequestId, $gcId); } + /** + * @return Response\CreateBalanceResponse + * + * @throws AmazonErrors + */ + public function getAvailableFunds(): Response\CreateBalanceResponse + { + return (new AWS($this->_config))->getBalance(); + } /** * AmazonGiftCode make own client. diff --git a/src/Response/CreateBalanceResponse.php b/src/Response/CreateBalanceResponse.php new file mode 100644 index 0000000..70c287b --- /dev/null +++ b/src/Response/CreateBalanceResponse.php @@ -0,0 +1,125 @@ + + * + */ + + +namespace kamerk22\AmazonGiftCode\Response; + + +use Illuminate\Support\Facades\Log; + +class CreateBalanceResponse +{ + /** + * Amazon Gift Card Balance Amount + * + * @var string + */ + protected $_amount; + /** + * Amazon Gift Card Balance Currency + * + * @var string + */ + protected $_currency; + /** + * Amazon Gift Card Balance Status + * + * @var string + */ + protected $_status; + /** + * Amazon Gift Card Balance Timestamp + * + * @var string + */ + protected $_timestamp; + /** + * Amazon Gift Card Raw JSON + * + * @var string + */ + protected $_raw_json; + + /** + * Response constructor. + * @param $jsonResponse + */ + public function __construct($jsonResponse) + { + $this->_raw_json = $jsonResponse; + $this->_status = TRUE; + $this->parseJsonResponse($jsonResponse); + } + + /** + * @return string + */ + public function getAmount(): string + { + return $this->_amount; + } + + /** + * @return string + */ + public function getCurrency(): string + { + return $this->_currency; + } + + /** + * @return string + */ + public function getStatus(): string + { + return $this->_status; + } + + /** + * @return string + */ + public function getTimestamp(): string + { + return $this->_timestamp; + } + + /** + * @return string + */ + public function getRawJson(): string + { + return json_encode($this->_raw_json); + } + + /** + * @param $jsonResponse + * @return CreateBalanceResponse + */ + public function parseJsonResponse($jsonResponse): self + { + if (!is_array($jsonResponse)) { + throw new \RuntimeException('Response must be a scalar value'); + } + if (array_key_exists('amount', $jsonResponse['availableFunds'])) { + $this->_amount = $jsonResponse['availableFunds']['amount']; + } + if (array_key_exists('currencyCode', $jsonResponse['availableFunds'])) { + $this->_currency = $jsonResponse['availableFunds']['currencyCode']; + } + if (array_key_exists('status', $jsonResponse)) { + $this->_status = $jsonResponse['status']; + } + if (array_key_exists('timestamp', $jsonResponse)) { + $this->_timestamp = $jsonResponse['timestamp']; + } + + return $this; + + } + +} diff --git a/src/Response/CreateResponse.php b/src/Response/CreateResponse.php index b1f5e87..d3f8d79 100644 --- a/src/Response/CreateResponse.php +++ b/src/Response/CreateResponse.php @@ -53,7 +53,18 @@ class CreateResponse * @var string */ protected $_status; - + /** + * Amazon Gift Card Expiration Date + * + * @var string + */ + protected $_expiration_date; + /** + * Amazon Gift Card Expiration Date + * + * @var string + */ + protected $_card_status; /** * Amazon Gift Card Raw JSON * @@ -122,6 +133,22 @@ public function getStatus(): string return $this->_status; } + /** + * @return string + */ + public function getExpirationDate(): string + { + return $this->_expiration_date; + } + + /** + * @return string + */ + public function getCardStatus(): string + { + return $this->_card_status; + } + /** * @return string @@ -155,6 +182,12 @@ public function parseJsonResponse($jsonResponse): self if (array_key_exists('currencyCode', $jsonResponse['cardInfo']['value'])) { $this->_currency = $jsonResponse['cardInfo']['value']['currencyCode']; } + if (array_key_exists('gcExpirationDate', $jsonResponse)) { + $this->_expiration_date = $jsonResponse['gcExpirationDate']; + } + if (array_key_exists('cardStatus', $jsonResponse['cardInfo'])) { + $this->_card_status = $jsonResponse['cardInfo']['cardStatus']; + } return $this;