Amazon Gift Card on Demand Incentives stand alone class

This commit is contained in:
2021-10-18 18:24:38 +09:00
commit b00d545a10
14 changed files with 1772 additions and 0 deletions

63
test/autoloader.php Normal file
View File

@@ -0,0 +1,63 @@
<?php // phpcs:ignore PSR1.Files.SideEffects
declare(strict_types=1);
namespace Autoloader;
// shall implement an auto loader
if (class_exists('Autoload', false) === false) {
// define the auto loader class
class Autoload
{
// we do it simple here
// passes on the class to load and we search here in namespace
// to load that class
public static function load($class)
{
// print "(1) Class: $class / DIR: " . __DIR__ . "<br>";
// set directory seperator (we need to replace from namespace)
$DS = DIRECTORY_SEPARATOR;
// base lib
$LIB = defined('LIB') ? LIB : '../src' . $DS;
// if lib is in path at the end, do not add lib again
// note that $LIB can have a directory seperator at the end
// strip that out before we do a match
$_LIB = rtrim($LIB, $DS);
if (!preg_match("|$_LIB$|", __DIR__)) {
$LIB .= $DS;
} else {
$LIB = '';
}
// default path is unset
$path = false;
// set path on full dir
// if we have the namespace in the class, strip it out
$len = 0;
if (strpos($class, __NAMESPACE__) !== false) {
$len = strlen(__NAMESPACE__);
}
// set default extension
$extension = '.php';
// set full include path
$path = __DIR__ . $DS . $LIB . substr($class, $len);
// replace namespace \ with dir sepeator
$path = str_replace('\\', $DS, $path) . $extension;
// print "(2) Class clean: $path<br>";
// if path is set and a valid file
if ($path !== false && is_file($path)) {
// print "<b>(3)</b> Load Path: $path<br>";
// we should sub that
// self::loadFile($path);
include $path;
return true;
}
return false;
}
// end class define
}
spl_autoload_register('Autoloader\Autoload::load', true, true);
} // end check for already defined
// __END__

View File

@@ -0,0 +1,141 @@
<?php // phpcs:ignore PSR1.Files.SideEffects
// test for Amazon Gift Card Incentives
// general auto loader
require 'autoloader.php';
// env file loader
require 'read_env_file.php';
// load env data with dotenv
readEnvFile(__DIR__);
print "<h1>Amazon Gift Card Incentives</h1><br>";
// must have set
// endpoint/region: AWS_GIFT_CARD_ENDPOINT
// aws key: AWS_GIFT_CARD_KEY
// aws secret: AWS_GIFT_CARD_SECRET
// partner id: AWS_GIFT_CARD_PARTNER_ID
// optional
// currency: AWS_ICENTIVE_CURRENCY
// as in .env
// AWS_GIFT_CARD_ENDPOINT.TEST
// AWS_GIFT_CARD_ENDPOINT.LIVE
define('LOCATION', 'test');
foreach (
[
'AWS_GIFT_CARD_KEY', 'AWS_GIFT_CARD_SECRET', 'AWS_GIFT_CARD_PARTNER_ID',
'AWS_GIFT_CARD_ENDPOINT', 'AWS_GIFT_CARD_CURRENCY', 'AWS_DEBUG'
] as $key
) {
//
$_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
JPY for JP
CAD for CA
AUD for AU
TRY for TR
AED for UAE
*/
// run tests
// print "checkMe Static: <pre>" . print_r(Amazon\AmazonIncentives::checkMeStatic(), true) . "</pre>";
$aws = new Amazon\AmazonIncentives();
// $aws->createGiftCard(100);
print "checkMe: <pre>" . print_r($aws->checkMe(), true) . "</pre>";
print "<hr>";
// we should open log file to collect all creationRequestId/gcId
// so we can test and cancel
// check balance
try {
$aws_test = Amazon\AmazonIncentives::make()->getAvailableFunds();
print "AWS: getAvailableFunds: <pre>" . print_r($aws_test, true) . "</pre><br>";
} catch (Exception $e) {
print "AWS: getAvailableFunds FAILURE [" . $e->getCode() . "]: "
. "<pre>" . print_r(Amazon\AmazonIncentives::decodeExceptionMessage($e->getMessage()), true) . "</pre><br>";
exit;
};
// print "LOG: <pre>" . print_r($aws_test->getLog(), true) . "</pre><br>";
print "<hr>";
// skip early for testing
// exit;
/*
// create card
$value = 1000;
// we must be sure we pass FLOAT there
$aws_test = Amazon\AmazonIncentives::make()->buyGiftCard((float)$value);
print "AWS: buyGiftCard: <pre>" . print_r($aws_test, true) . "</pre><br>";
$creation_request_id = $aws_test->getCreationRequestId();
$gift_card_id = $aws_test->getId();
$claim_code = $aws_test->getClaimCode();
print "AWS creationRequestId: " . $creation_request_id . ", gcId: " . $gift_card_id . "<br>";
print "AWS CLAIM CODE: <b>" . $claim_code . "</b><br>";
print "<hr>";
// cancel card
$aws_test = Amazon\AmazonIncentives::make()->cancelGiftCard($creation_request_id, $gift_card_id);
print "AWS: buyGiftCard: <pre>" . print_r($aws_test, true) . "</pre><br>";
print "<hr>";
*/
// MOCK TEST
$value = 500;
$creation_id = 'F0000';
$aws_test = Amazon\AmazonIncentives::make()->buyGiftCard((float)$value, $creation_id);
$creation_request_id = $aws_test->getCreationRequestId();
$gift_card_id = $aws_test->getId();
$claim_code = $aws_test->getClaimCode();
print "AWS: MOCK: " . $creation_id . ": buyGiftCard: <pre>" . print_r($aws_test, true) . "</pre><br>";
print "AWS creationRequestId: " . $creation_request_id . ", gcId: " . $gift_card_id . "<br>";
print "AWS CLAIM CODE: <b>" . $claim_code . "</b><br>";
print "<hr>";
$creation_id = 'F2005';
try {
$aws_test = Amazon\AmazonIncentives::make()->buyGiftCard((float)$value, $creation_id);
$creation_request_id = $aws_test->getCreationRequestId();
$gift_card_id = $aws_test->getId();
$claim_code = $aws_test->getClaimCode();
print "AWS: MOCK: " . $creation_id . ": buyGiftCard: <pre>" . print_r($aws_test, true) . "</pre><br>";
print "AWS creationRequestId: " . $creation_request_id . ", gcId: " . $gift_card_id . "<br>";
print "AWS CLAIM CODE: <b>" . $claim_code . "</b><br>";
} catch (Exception $e) {
print "AWS: MOCK: " . $creation_id . ": buyGiftCard: FAILURE [" . $e->getCode() . "]: "
. "<pre>" . print_r(Amazon\AmazonIncentives::decodeExceptionMessage($e->getMessage()), true) . "</pre><br>";
}
print "<hr>";
// ... should do all possible important mock tests
// failed card (invalid data)
// double card
// __END__

84
test/read_env_file.php Normal file
View File

@@ -0,0 +1,84 @@
<?php
/**
* parses .env file
*
* Rules for .env file
* variable is any alphanumeric string followed by = on the same line
* content starts with the first non space part
* strings can be contained in "
* strings MUST be contained in " if they are multiline
* if string starts with " it will match until another " is found
* anything AFTER " is ignored
* if there are two variables with the same name only the first is used
* variables are case sensitive
*
* @param string $path Folder to file, default is __DIR__
* @param string $env_file What file to load, default is .env
* @return int -1 other error
* 0 for success full load
* 1 for file loadable, but no data inside
* 2 for file not readable
* 3 for file not found
*/
function readEnvFile(string $path = __DIR__, string $env_file = '.env'): int
{
// default -1;
$status = -1;
$env_file_target = $path . DIRECTORY_SEPARATOR . $env_file;
// this is not a file -> abort
if (!is_file($env_file_target)) {
$status = 3;
return $status;
}
// cannot open file -> abort
if (($fp = fopen($env_file_target, 'r')) === false) {
$status = 2;
return $status;
}
// set to readable but not yet any data loaded
$status = 1;
$block = false;
$var = '';
while ($line = fgets($fp)) {
// main match for variable = value part
if (preg_match("/^\s*([\w_.]+)\s*=\s*((\"?).*)/", $line, $matches)) {
$var = $matches[1];
$value = $matches[2];
$quotes = $matches[3];
// wirte only if env is not set yet, and write only the first time
if (empty($_ENV[$var])) {
if (!empty($quotes)) {
// match greedy for first to last so we move any " if there are
if (preg_match('/^"(.*[^\\\])"/U', $value, $matches)) {
$value = $matches[1];
} else {
// this is a multi line
$block = true;
// first " in string remove
// add removed new line back because this is a multi line
$value = ltrim($value, '"') . PHP_EOL;
}
}
// if block is set, we strip line of slashes
$_ENV[$var] = $block === true ? stripslashes($value) : $value;
// set successful load
$status = 0;
}
} elseif ($block === true) {
// read line until there is a unescaped "
// this also strips everything after the last "
if (preg_match("/(.*[^\\\])\"/", $line, $matches)) {
$block = false;
// strip ending " and EVERYTHING that follows after that
$line = $matches[1];
}
// strip line of slashes
$_ENV[$var] .= stripslashes($line);
}
}
fclose($fp);
return $status;
}
// __END__