2021-10-18 18:24:38 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Amazon Incentive Code
|
2021-10-21 13:29:50 +09:00
|
|
|
* Amazon Gift Code on Demand
|
2021-10-18 18:24:38 +09:00
|
|
|
*/
|
|
|
|
|
|
2021-10-21 09:55:32 +09:00
|
|
|
namespace gullevek\AmazonIncentives;
|
2021-10-18 18:24:38 +09:00
|
|
|
|
2021-10-21 09:55:32 +09:00
|
|
|
use gullevek\AmazonIncentives\AWS\AWS;
|
|
|
|
|
use gullevek\AmazonIncentives\Config\Config;
|
|
|
|
|
use gullevek\AmazonIncentives\Exceptions\AmazonErrors;
|
|
|
|
|
use gullevek\AmazonIncentives\Debug\AmazonDebug;
|
2021-10-18 18:24:38 +09:00
|
|
|
|
2021-10-21 13:29:50 +09:00
|
|
|
final class AmazonIncentives
|
2021-10-18 18:24:38 +09:00
|
|
|
{
|
2021-10-21 13:29:50 +09:00
|
|
|
/**
|
|
|
|
|
* @var Config
|
|
|
|
|
*/
|
2021-10-18 18:24:38 +09:00
|
|
|
private $config;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* AmazonGiftCode constructor.
|
|
|
|
|
*
|
2022-06-10 14:37:07 +09:00
|
|
|
* @param string|null $key Account key
|
|
|
|
|
* @param string|null $secret Secret key
|
|
|
|
|
* @param string|null $partner Partner ID
|
|
|
|
|
* @param string|null $endpoint Endpoint URL including https://
|
|
|
|
|
* @param string|null $currency Currency type. Eg USD, JPY, etc
|
|
|
|
|
* @param bool|null $debug Debug flag
|
2021-10-18 18:24:38 +09:00
|
|
|
*/
|
|
|
|
|
public function __construct(
|
|
|
|
|
string $key = null,
|
|
|
|
|
string $secret = null,
|
|
|
|
|
string $partner = null,
|
|
|
|
|
string $endpoint = null,
|
|
|
|
|
string $currency = null,
|
|
|
|
|
bool $debug = null
|
|
|
|
|
) {
|
|
|
|
|
// load AWS settings
|
|
|
|
|
// fail here if settings missing
|
|
|
|
|
$this->config = new Config($key, $secret, $partner, $endpoint, $currency, $debug);
|
2021-10-20 09:36:18 +09:00
|
|
|
// init debug
|
|
|
|
|
AmazonDebug::setDebug($this->config->getDebug());
|
2021-10-18 18:24:38 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// *********************************************************************
|
|
|
|
|
// PRIVATE HELPER METHODS
|
|
|
|
|
// *********************************************************************
|
|
|
|
|
|
|
|
|
|
// *********************************************************************
|
|
|
|
|
// PUBLIC METHODS
|
|
|
|
|
// *********************************************************************
|
|
|
|
|
|
|
|
|
|
/**
|
2022-06-10 14:37:07 +09:00
|
|
|
* Buy a gift card
|
|
|
|
|
*
|
|
|
|
|
* @param float $value Amount to purchase a gift card
|
|
|
|
|
* in currency value
|
|
|
|
|
* @param string|null $creation_request_id Override automatically created request id
|
|
|
|
|
* If not set will create a new one, or
|
|
|
|
|
* return data for created one
|
|
|
|
|
* @return Response\CreateResponse Returns new created response object or
|
|
|
|
|
* previous created if creation_request_id was used
|
2021-10-18 18:24:38 +09:00
|
|
|
*
|
|
|
|
|
* @throws AmazonErrors
|
|
|
|
|
*/
|
|
|
|
|
public function buyGiftCard(float $value, string $creation_request_id = null): Response\CreateResponse
|
|
|
|
|
{
|
2022-06-10 14:50:32 +09:00
|
|
|
return ($this->newAWS())->getCode($value, $creation_request_id);
|
2021-10-18 18:24:38 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-06-10 14:37:07 +09:00
|
|
|
* Cancel a previous created gift card, if within the time frame
|
|
|
|
|
*
|
|
|
|
|
* @param string $creation_request_id Previous created request id from buyGiftCard
|
|
|
|
|
* @param string $gift_card_id Previous gift card id from buyGiftCard (gcId)
|
|
|
|
|
* @return Response\CancelResponse Returns the cancled request object
|
|
|
|
|
*
|
|
|
|
|
* @throws AmazonErrors
|
2021-10-18 18:24:38 +09:00
|
|
|
*/
|
|
|
|
|
public function cancelGiftCard(string $creation_request_id, string $gift_card_id): Response\CancelResponse
|
|
|
|
|
{
|
2022-06-10 14:50:32 +09:00
|
|
|
return ($this->newAWS())->cancelCode($creation_request_id, $gift_card_id);
|
2021-10-18 18:24:38 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-06-10 14:37:07 +09:00
|
|
|
* Gets the current funds in this account
|
|
|
|
|
*
|
|
|
|
|
* @return Response\CreateBalanceResponse Returns the account funds object
|
2021-10-18 18:24:38 +09:00
|
|
|
*
|
|
|
|
|
* @throws AmazonErrors
|
|
|
|
|
*/
|
|
|
|
|
public function getAvailableFunds(): Response\CreateBalanceResponse
|
|
|
|
|
{
|
2022-06-10 14:50:32 +09:00
|
|
|
return ($this->newAWS())->getBalance();
|
2021-10-18 18:24:38 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-06-10 14:37:07 +09:00
|
|
|
* AmazonIncentives creates own client and returns it as static object
|
2021-10-18 18:24:38 +09:00
|
|
|
*
|
2022-06-10 14:37:07 +09:00
|
|
|
* @param string|null $key Account key
|
|
|
|
|
* @param string|null $secret Secret key
|
|
|
|
|
* @param string|null $partner Partner ID
|
|
|
|
|
* @param string|null $endpoint Endpoint URL including https://
|
|
|
|
|
* @param string|null $currency Currency type. Eg USD, JPY, etc
|
|
|
|
|
* @param bool|null $debug Debug flag
|
|
|
|
|
* @return AmazonIncentives self class
|
2021-10-18 18:24:38 +09:00
|
|
|
*/
|
|
|
|
|
public static function make(
|
|
|
|
|
string $key = null,
|
|
|
|
|
string $secret = null,
|
|
|
|
|
string $partner = null,
|
|
|
|
|
string $endpoint = null,
|
|
|
|
|
string $currency = null,
|
|
|
|
|
bool $debug = null
|
|
|
|
|
): AmazonIncentives {
|
|
|
|
|
return new static($key, $secret, $partner, $endpoint, $currency, $debug);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-10 14:50:32 +09:00
|
|
|
/**
|
|
|
|
|
* wrapper to create new AWS class.
|
|
|
|
|
* used in all buy/cancel/get calss
|
|
|
|
|
*
|
|
|
|
|
* @return AWS Main AWS worker class
|
|
|
|
|
*/
|
|
|
|
|
public function newAWS(): AWS
|
|
|
|
|
{
|
|
|
|
|
return new AWS($this->config);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-18 18:24:38 +09:00
|
|
|
/**
|
|
|
|
|
* Decodes the Exception message body
|
|
|
|
|
* Returns an array with code (Amazon error codes), type (Amazon error info)
|
|
|
|
|
* message (Amazon returned error message string)
|
|
|
|
|
*
|
|
|
|
|
* @param string $message Exception message json string
|
2021-10-21 13:29:50 +09:00
|
|
|
* @return array<mixed> Decoded with code, type, message fields
|
2022-06-13 19:51:30 +09:00
|
|
|
*
|
|
|
|
|
* @deprecated use \gullevek\AmazonIncentives\Exceptions\AmazonErrors::decodeExceptionMessage()
|
2021-10-18 18:24:38 +09:00
|
|
|
*/
|
|
|
|
|
public static function decodeExceptionMessage(string $message): array
|
|
|
|
|
{
|
2022-06-13 19:51:30 +09:00
|
|
|
return AmazonErrors::decodeExceptionMessage($message);
|
2021-10-18 18:24:38 +09:00
|
|
|
}
|
2021-10-19 10:52:04 +09:00
|
|
|
|
2021-10-18 18:24:38 +09:00
|
|
|
// *********************************************************************
|
|
|
|
|
// PUBLIC TEST METHODS
|
|
|
|
|
// *********************************************************************
|
|
|
|
|
|
2021-10-21 13:29:50 +09:00
|
|
|
/**
|
2022-06-10 14:37:07 +09:00
|
|
|
* Prints out ENV, CONFIG and KEY data
|
|
|
|
|
* This is for debug only, this will print out secrets.
|
|
|
|
|
* Use with care
|
|
|
|
|
*
|
2021-10-21 13:29:50 +09:00
|
|
|
* @return array<mixed>
|
|
|
|
|
*/
|
2021-10-18 18:24:38 +09:00
|
|
|
public function checkMe(): array
|
|
|
|
|
{
|
|
|
|
|
$data = [];
|
|
|
|
|
|
|
|
|
|
$data['ENV'] = $_ENV;
|
|
|
|
|
$data['CONFIG'] = $this->config;
|
|
|
|
|
$data['KEY'] = $this->config->getAccessKey();
|
|
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// __END__
|