Rewrite to composer package

composer require gullevek/amazon-incentives
This commit is contained in:
2021-10-21 09:55:32 +09:00
parent 94ea118731
commit 6f9bf781f2
23 changed files with 814 additions and 147 deletions

View File

@@ -52,11 +52,16 @@ function printException(
}
}
// general auto loader
require 'autoloader.php';
// env file loader
// composer auto loader
$loader = require '../vendor/autoload.php';
// need to add this or it will not load here
$loader->addPsr4('gullevek\\', __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'src');
// print "LOADER: <pre>" . print_r($loader, true) . "</pre>";
// env file loader (simple)
require 'read_env_file.php';
use gullevek\AmazonIncentives\AmazonIncentives;
// load env data with dotenv
readEnvFile(__DIR__);
@@ -67,8 +72,9 @@ print "<h1>Amazon Gift Card Incentives</h1><br>";
// aws key: AWS_GIFT_CARD_KEY
// aws secret: AWS_GIFT_CARD_SECRET
// partner id: AWS_GIFT_CARD_PARTNER_ID
// optional
// currency: AWS_ICENTIVE_CURRENCY
// optional
// debug: AWS_DEBUG (if not set: off)
// as in .env
// AWS_GIFT_CARD_ENDPOINT.TEST
@@ -85,33 +91,7 @@ foreach (
$_ENV[$key] = $_ENV[$key . '.' . strtoupper((LOCATION))] ?? $_ENV[$key] ?? '';
}
/*
ENDPOINTS:
- remove '-gamma' for non sandox
WHERE URL REGION
North America https://agcod-v2-gamma.amazon.com us-east-1
https://agcod-v2.amazon.com
(US, CA, MX)
Europe and Asia https://agcod-v2-eu-gamma.amazon.com eu-west-1
https://agcod-v2-eu.amazon.com
(IT, ES, DE, FR, UK, TR, UAE, KSA, PL, NL, SE)
Far East https://agcod-v2-fe-gamma.amazon.com us-west-2
https://agcod-v2-fe.amazon.com
(JP, AU, SG)
CURRENCY
USD for US
EUR for EU (IT, ES, DE, FR, PL, NL, SE)
JPY for JP
CAD for CA
AUD for AU
TRY for TR
AED for UAE
MXN for MX
GBP for UK
*/
// open debug file output
$fp = fopen('log/debug.' . date('YmdHis') . '.log', 'w');
// run info test (prints ENV vars)
@@ -119,7 +99,7 @@ $run_info_test = false;
// run test to get funds info
$run_fund_test = true;
// run the normal get/cancel gift card tests
$run_gift_tests = true;
$run_gift_tests = false;
// run mock error check tests
$run_mocks = false;
@@ -133,7 +113,7 @@ $mock_debug = false;
$mock_wait = 2;
if ($run_info_test === true) {
$aws = new Amazon\AmazonIncentives();
$aws = new AmazonIncentives();
print "checkMe: <pre>" . print_r($aws->checkMe(), true) . "</pre>";
fwrite($fp, writeLog($aws->checkMe()));
print "<hr>";
@@ -142,7 +122,7 @@ if ($run_info_test === true) {
// check balance
if ($run_fund_test === true) {
try {
$aws_test = Amazon\AmazonIncentives::make()->getAvailableFunds();
$aws_test = AmazonIncentives::make()->getAvailableFunds();
print "AWS: getAvailableFunds: " . $aws_test->getStatus() . ": "
. "Amount: " . $aws_test->getAmount() . ", "
. "Currency: " . $aws_test->getCurrency() . ", "
@@ -152,7 +132,7 @@ if ($run_fund_test === true) {
}
fwrite($fp, writeLog((array)$aws_test));
} catch (Exception $e) {
$error = Amazon\AmazonIncentives::decodeExceptionMessage($e->getMessage());
$error = AmazonIncentives::decodeExceptionMessage($e->getMessage());
printException('getAvailableFunds', $e->getCode(), $error, $debug_print);
fwrite($fp, writeLog($error));
};
@@ -169,7 +149,7 @@ if ($run_gift_tests === true) {
$gift_card_id = '';
try {
// we must be sure we pass FLOAT there
$aws_test = Amazon\AmazonIncentives::make()->buyGiftCard((float)$value);
$aws_test = AmazonIncentives::make()->buyGiftCard((float)$value);
$creation_request_id = $aws_test->getCreationRequestId();
$gift_card_id = $aws_test->getId();
$claim_code = $aws_test->getClaimCode();
@@ -184,7 +164,7 @@ if ($run_gift_tests === true) {
}
fwrite($fp, writeLog((array)$aws_test));
} catch (\Exception $e) {
$error = Amazon\AmazonIncentives::decodeExceptionMessage($e->getMessage());
$error = AmazonIncentives::decodeExceptionMessage($e->getMessage());
printException('buyGiftCard', $e->getCode(), $error, $debug_print);
fwrite($fp, writeLog($error));
}
@@ -192,7 +172,7 @@ if ($run_gift_tests === true) {
sleep($debug_wait);
try {
// cancel above created card card
$aws_test = Amazon\AmazonIncentives::make()->cancelGiftCard($creation_request_id, $gift_card_id);
$aws_test = AmazonIncentives::make()->cancelGiftCard($creation_request_id, $gift_card_id);
$request_status = $aws_test->getStatus();
print "AWS: cancelGiftCard: " . $request_status . ": "
. "creationRequestId: " . $creation_request_id . ", gcId: " . $gift_card_id;
@@ -201,7 +181,7 @@ if ($run_gift_tests === true) {
}
fwrite($fp, writeLog((array)$aws_test));
} catch (\Exception $e) {
$error = Amazon\AmazonIncentives::decodeExceptionMessage($e->getMessage());
$error = AmazonIncentives::decodeExceptionMessage($e->getMessage());
print "AWS: cancelGiftCard: " . $error['status']
. " [" . $e->getCode() . "]: "
. $error['code'] . " | " . $error['type']
@@ -216,7 +196,7 @@ if ($run_gift_tests === true) {
// set same request ID twice to get same response test
try {
$aws_test = Amazon\AmazonIncentives::make()->buyGiftCard((float)$value);
$aws_test = AmazonIncentives::make()->buyGiftCard((float)$value);
$creation_request_id = $aws_test->getCreationRequestId();
$gift_card_id = $aws_test->getId();
$claim_code = $aws_test->getClaimCode();
@@ -231,14 +211,14 @@ if ($run_gift_tests === true) {
}
fwrite($fp, writeLog((array)$aws_test));
} catch (\Exception $e) {
$error = Amazon\AmazonIncentives::decodeExceptionMessage($e->getMessage());
$error = AmazonIncentives::decodeExceptionMessage($e->getMessage());
printException('cancelGiftCard', $e->getCode(), $error, $debug_print);
fwrite($fp, writeLog($error));
}
print "<br>";
sleep($debug_wait);
try {
$aws_test = Amazon\AmazonIncentives::make()->buyGiftCard((float)$value, $creation_request_id);
$aws_test = AmazonIncentives::make()->buyGiftCard((float)$value, $creation_request_id);
$request_status = $aws_test->getStatus();
// same?
$expiration_date = $aws_test->getExpirationDate();
@@ -251,7 +231,7 @@ if ($run_gift_tests === true) {
}
fwrite($fp, writeLog((array)$aws_test));
} catch (\Exception $e) {
$error = Amazon\AmazonIncentives::decodeExceptionMessage($e->getMessage());
$error = AmazonIncentives::decodeExceptionMessage($e->getMessage());
printException('buyGiftCard', $e->getCode(), $error, $debug_print);
fwrite($fp, writeLog($error));
}
@@ -285,7 +265,7 @@ if ($mock_debug === true) {
foreach ($mock as $creation_id => $mock_return) {
print "<b>TS: " . microtime() . "</b>: ";
try {
$aws_test = Amazon\AmazonIncentives::make()->buyGiftCard((float)$mock_value, $creation_id);
$aws_test = AmazonIncentives::make()->buyGiftCard((float)$mock_value, $creation_id);
$creation_request_id = $aws_test->getCreationRequestId();
$gift_card_id = $aws_test->getId();
$claim_code = $aws_test->getClaimCode();
@@ -303,7 +283,7 @@ if ($mock_debug === true) {
}
fwrite($fp, writeLog((array)$aws_test));
} catch (Exception $e) {
$error = Amazon\AmazonIncentives::decodeExceptionMessage($e->getMessage());
$error = AmazonIncentives::decodeExceptionMessage($e->getMessage());
print "AWS: MOCK: " . $creation_id . ": buyGiftCard: " . $error['status']
. " [" . $e->getCode() . "]: "
. $error['code'] . " | " . $error['type']