2 Commits
1.0.0 ... 1.0.1

Author SHA1 Message Date
a56f33c81c Debug log updates, error message updates on curl failure
Config setup works with null data if no data is set.

Debugger is now launched in the AamzonIncentive class at the beginning
Debugger id/debug flag set is now one static call
Debugger methods all have proper PHPdoc documentationn added

Fixed Client/Curl error messages to include the original error message
too. Also made sure that a possible null endpoint is not throwing errors
2021-10-20 09:39:26 +09:00
f3b17bbf4c Update tests script with logging, etc 2021-10-19 11:46:52 +09:00
8 changed files with 259 additions and 99 deletions

View File

@@ -34,8 +34,6 @@ class AWS
public function __construct(Config $config) public function __construct(Config $config)
{ {
$this->config = $config; $this->config = $config;
AmazonDebug::setFlag($config->getDebug());
AmazonDebug::setId();
AmazonDebug::writeLog([__METHOD__ => date('Y-m-d H:m:s.u')]); AmazonDebug::writeLog([__METHOD__ => date('Y-m-d H:m:s.u')]);
} }
@@ -154,7 +152,7 @@ class AWS
'Service' => $k_service_hexis, 'Service' => $k_service_hexis,
]]); ]]);
$url = 'https://' . $endpoint . '/' . $service_operation; $url = 'https://' . (string)$endpoint . '/' . $service_operation;
$headers = $this->buildHeaders($payload, $authorization_value, $date_time_string, $service_target); $headers = $this->buildHeaders($payload, $authorization_value, $date_time_string, $service_target);
return (new Client())->request($url, $headers, $payload); return (new Client())->request($url, $headers, $payload);
} }

View File

@@ -39,6 +39,7 @@ namespace Amazon;
use Amazon\AWS\AWS; use Amazon\AWS\AWS;
use Amazon\Config\Config; use Amazon\Config\Config;
use Amazon\Exceptions\AmazonErrors; use Amazon\Exceptions\AmazonErrors;
use Amazon\Debug\AmazonDebug;
class AmazonIncentives class AmazonIncentives
{ {
@@ -65,6 +66,8 @@ class AmazonIncentives
// load AWS settings // load AWS settings
// fail here if settings missing // fail here if settings missing
$this->config = new Config($key, $secret, $partner, $endpoint, $currency, $debug); $this->config = new Config($key, $secret, $partner, $endpoint, $currency, $debug);
// init debug
AmazonDebug::setDebug($this->config->getDebug());
} }
// ********************************************************************* // *********************************************************************

View File

@@ -79,19 +79,19 @@ class Client implements ClientInterface
case CURLE_COULDNT_CONNECT: case CURLE_COULDNT_CONNECT:
case CURLE_COULDNT_RESOLVE_HOST: case CURLE_COULDNT_RESOLVE_HOST:
case CURLE_OPERATION_TIMEOUTED: case CURLE_OPERATION_TIMEOUTED:
$msg = 'Could not connect to AWS (' . $url . '). Please check your ' $message = 'Could not connect to AWS (' . $url . '). Please check your '
. 'internet connection and try again.'; . 'internet connection and try again. [' . $message . ']';
break; break;
case CURLE_SSL_CACERT: case CURLE_SSL_CACERT:
case CURLE_SSL_PEER_CERTIFICATE: case CURLE_SSL_PEER_CERTIFICATE:
$msg = 'Could not verify AWS SSL certificate. Please make sure ' $message = 'Could not verify AWS SSL certificate. Please make sure '
. 'that your network is not intercepting certificates. ' . 'that your network is not intercepting certificates. '
. '(Try going to ' . $url . 'in your browser.) ' . '(Try going to ' . $url . 'in your browser.) '
. 'If this problem persists,'; . '[' . $message . ']';
break; break;
case 0: case 0:
default: default:
$msg = 'Unexpected error communicating with AWS. ' . $message; $message = 'Unexpected error communicating with AWS: ' . $message;
} }
// throw an error like in the normal reqeust, but set to CURL error // throw an error like in the normal reqeust, but set to CURL error

View File

@@ -80,9 +80,9 @@ class Config implements ConfigInterface
} }
/** /**
* @return string * @return string|null
*/ */
public function getEndpoint(): string public function getEndpoint(): ?string
{ {
return $this->endpoint; return $this->endpoint;
} }
@@ -100,9 +100,9 @@ class Config implements ConfigInterface
} }
/** /**
* @return string * @return string|null
*/ */
public function getAccessKey(): string public function getAccessKey(): ?string
{ {
return $this->access_key; return $this->access_key;
} }
@@ -119,9 +119,9 @@ class Config implements ConfigInterface
} }
/** /**
* @return string * @return string|null
*/ */
public function getSecret(): string public function getSecret(): ?string
{ {
return $this->secret_key; return $this->secret_key;
} }
@@ -138,9 +138,9 @@ class Config implements ConfigInterface
} }
/** /**
* @return string * @return string|null
*/ */
public function getCurrency(): string public function getCurrency(): ?string
{ {
return $this->currency; return $this->currency;
} }
@@ -158,9 +158,9 @@ class Config implements ConfigInterface
} }
/** /**
* @return string * @return string|null
*/ */
public function getPartner(): string public function getPartner(): ?string
{ {
return $this->partner_id; return $this->partner_id;
} }
@@ -177,9 +177,9 @@ class Config implements ConfigInterface
} }
/** /**
* @return bool * @return bool|null
*/ */
public function getDebug(): bool public function getDebug(): ?bool
{ {
return $this->debug; return $this->debug;
} }

View File

@@ -5,9 +5,9 @@ namespace Amazon\Config;
interface ConfigInterface interface ConfigInterface
{ {
/** /**
* @return String * @return string|null
*/ */
public function getEndpoint(): string; public function getEndpoint(): ?string;
/** /**
* @param string $endpoint * @param string $endpoint
@@ -16,9 +16,9 @@ interface ConfigInterface
public function setEndpoint(string $endpoint): ConfigInterface; public function setEndpoint(string $endpoint): ConfigInterface;
/** /**
* @return String * @return string|null
*/ */
public function getAccessKey(): string; public function getAccessKey(): ?string;
/** /**
* @param string $key * @param string $key
@@ -27,9 +27,9 @@ interface ConfigInterface
public function setAccessKey(string $key): ConfigInterface; public function setAccessKey(string $key): ConfigInterface;
/** /**
* @return String * @return string|null
*/ */
public function getSecret(): string; public function getSecret(): ?string;
/** /**
* @param string $secret * @param string $secret
@@ -38,9 +38,9 @@ interface ConfigInterface
public function setSecret(string $secret): ConfigInterface; public function setSecret(string $secret): ConfigInterface;
/** /**
* @return String * @return string|null
*/ */
public function getCurrency(): string; public function getCurrency(): ?string;
/** /**
* @param string $currency * @param string $currency
@@ -49,9 +49,9 @@ interface ConfigInterface
public function setCurrency(string $currency): ConfigInterface; public function setCurrency(string $currency): ConfigInterface;
/** /**
* @return String * @return string|null
*/ */
public function getPartner(): string; public function getPartner(): ?string;
/** /**
* @param string $partner * @param string $partner
@@ -60,9 +60,9 @@ interface ConfigInterface
public function setPartner(string $partner): ConfigInterface; public function setPartner(string $partner): ConfigInterface;
/** /**
* @return bool * @return bool|null
*/ */
public function getDebug(): bool; public function getDebug(): ?bool;
/** /**
* @param bool $debug * @param bool $debug

View File

@@ -1,6 +1,7 @@
<?php <?php
// simple write all into an array that we can poll in the return group // simple write all into an array that we can poll in the return group
// to activate AmazonDebug::setDebug(true) must be called once
namespace Amazon\Debug; namespace Amazon\Debug;
@@ -10,11 +11,15 @@ class AmazonDebug
private static $debug = false; private static $debug = false;
private static $id = null; private static $id = null;
public function __construct() /**
{ * set the ID for current run
} * if debug is off, nothing will be set and id is null
* This is run on setFlag, if debug is true
public static function setId(?string $id = null): void *
* @param string|null $id If not set, will default to uniqid() call
* @return void
*/
private static function setId(?string $id = null): void
{ {
if (self::$debug === false) { if (self::$debug === false) {
return; return;
@@ -25,24 +30,69 @@ class AmazonDebug
self::$id = $id; self::$id = $id;
} }
public static function getId(): string /**
* set the debug flag.
* This is automatically run in Amazon\AmazonIncentives::__construct
* No need to run manuall
*
* @param boolean $debug Can only be True or False
* @param string|null $id If not set, will default to uniqid() call
* @return void
*/
public static function setDebug(bool $debug, ?string $id = null): void
{
self::$debug = $debug;
if (self::$debug === false) {
return;
}
self::setId($id);
}
/**
* returns current debug flag status
*
* @return boolean True if debug is on, False if debug is off
*/
public static function getDebug(): bool
{
return self::$debug;
}
/**
* get the current set ID, can return null if debug is off
*
* @return string|null Current set ID for this log run
*/
public static function getId(): ?string
{ {
return self::$id; return self::$id;
} }
public static function setFlag(bool $debug): void /**
{ * write a log entry
self::$debug = $debug; * Data is as array key -> value
} * Will be pushed as new array entry int log
* Main key is the set Id for this run
*
* @param array $data Any array data to store in the log
* @return void
*/
public static function writeLog(array $data): void public static function writeLog(array $data): void
{ {
if (self::$debug === false) { if (self::$debug === false) {
return; return;
} }
self::$log[self::$id][] = $data; self::$log[self::getId()][] = $data;
} }
/**
* get all logs written since first class run
* or get all log entries for given ID
*
* @param string|null $id If set returns only this id logs
* or empty array if not found
* @return array Always array, empty if not data or not found
*/
public static function getLog(?string $id = null): array public static function getLog(?string $id = null): array
{ {
if ($id === null) { if ($id === null) {

View File

@@ -1,6 +1,56 @@
<?php // phpcs:ignore PSR1.Files.SideEffects <?php // phpcs:ignore PSR1.Files.SideEffects
// test for Amazon Gift Card Incentives // Tests for Amazon Gift Card Incentives
/**
* write log as string from array data
* includes timestamp
*
* @param array $data Debug log array data to add to the json string
* @return string
*/
function writeLog(array $data): string
{
return json_encode([
'date' => date('Y-m-d H:i:s'),
'log' => $data
]) . "\n";
}
/**
* translate the UTC amazon date string to Y-m-d H:i:s standard
*
* @param string $date A UTC string date from Amazon
* @return string
*/
function dateTr(string $date): string
{
return date('Y-m-d H:i:s', strtotime($date));
}
/**
* print exception string
*
* @param string $call_request Call request, eg buyGiftCard
* @param integer $error_code $e Exception error code
* @param array $error Array from the Exception message json string
* @param boolean $debug_print If we should show the debug log
* @return void
*/
function printException(
string $call_request,
int $error_code,
array $error,
bool $debug_print
): void {
print "AWS: " . $call_request . ": " . $error['status']
. " [" . $error_code . "]: "
. $error['code'] . " | " . $error['type']
. " | " . $error['message'];
if ($debug_print === true) {
print "<pre>" . print_r($error['log'][$error['log_id'] ?? ''] ?? [], true) . "</pre>";
}
}
// general auto loader // general auto loader
require 'autoloader.php'; require 'autoloader.php';
@@ -61,8 +111,12 @@ AED for UAE
*/ */
$fp = fopen('log/debug.' . date('YmdHis') . '.log', 'w');
// run info test (prints ENV vars)
$run_info_test = false;
// run test to get funds info // run test to get funds info
$run_fund_test = false; $run_fund_test = true;
// run the normal get/cancel gift card tests // run the normal get/cancel gift card tests
$run_gift_tests = true; $run_gift_tests = true;
// run mock error check tests // run mock error check tests
@@ -77,29 +131,31 @@ $mock_debug = false;
// wait in seconds between mock tests // wait in seconds between mock tests
$mock_wait = 2; $mock_wait = 2;
$aws = new Amazon\AmazonIncentives(); if ($run_info_test === true) {
// $aws->createGiftCard(100); $aws = new Amazon\AmazonIncentives();
print "checkMe: <pre>" . print_r($aws->checkMe(), true) . "</pre>"; print "checkMe: <pre>" . print_r($aws->checkMe(), true) . "</pre>";
print "<hr>"; fwrite($fp, writeLog($aws->checkMe()));
print "<hr>";
}
// we should open log file to collect all creationRequestId/gcId
// so we can test and cancel
// check balance // check balance
if ($run_fund_test === true) { if ($run_fund_test === true) {
try { try {
$aws_test = Amazon\AmazonIncentives::make()->getAvailableFunds(); $aws_test = Amazon\AmazonIncentives::make()->getAvailableFunds();
print "AWS: getAvailableFunds: <pre>" . print_r($aws_test, true) . "</pre><br>"; print "AWS: getAvailableFunds: " . $aws_test->getStatus() . ": "
} catch (Exception $e) { . "Amount: " . $aws_test->getAmount() . ", "
print "AWS: getAvailableFunds: " . $error['status'] . "Currency: " . $aws_test->getCurrency() . ", "
. " [" . $e->getCode() . "]: " . "Timestamp: " . $aws_test->getTimestamp();
. $error['code'] . " | " . $error['type']
. " | " . $error['message'] . ": ";
if ($debug_print === true) { if ($debug_print === true) {
print "/<pre>" . print_r($error['log'][$error['log_id'] ?? ''] ?? [], true) . "</pre>"; print "<pre>" . print_r($aws_test, true) . "</pre>";
} }
fwrite($fp, writeLog((array)$aws_test));
} catch (Exception $e) {
$error = Amazon\AmazonIncentives::decodeExceptionMessage($e->getMessage());
printException('getAvailableFunds', $e->getCode(), $error, $debug_print);
fwrite($fp, writeLog($error));
}; };
print "<br>";
sleep($debug_wait); sleep($debug_wait);
// print "LOG: <pre>" . print_r($aws_test->getLog(), true) . "</pre><br>"; // print "LOG: <pre>" . print_r($aws_test->getLog(), true) . "</pre><br>";
print "<hr>"; print "<hr>";
@@ -108,51 +164,97 @@ if ($run_fund_test === true) {
if ($run_gift_tests === true) { if ($run_gift_tests === true) {
// create card // create card
$value = 1000; $value = 1000;
$creation_request_id = '';
$gift_card_id = '';
try {
// we must be sure we pass FLOAT there // we must be sure we pass FLOAT there
$aws_test = Amazon\AmazonIncentives::make()->buyGiftCard((float)$value); $aws_test = Amazon\AmazonIncentives::make()->buyGiftCard((float)$value);
$creation_request_id = $aws_test->getCreationRequestId(); $creation_request_id = $aws_test->getCreationRequestId();
$gift_card_id = $aws_test->getId(); $gift_card_id = $aws_test->getId();
$claim_code = $aws_test->getClaimCode(); $claim_code = $aws_test->getClaimCode();
$expiration_date = $aws_test->getExpirationDate();
$request_status = $aws_test->getStatus(); $request_status = $aws_test->getStatus();
print "AWS: buyGiftCard: " . $request_status . ": " print "AWS: buyGiftCard: " . $request_status . ": "
. "creationRequestId: " . $creation_request_id . ", gcId: " . $gift_card_id . ", " . "creationRequestId: " . $creation_request_id . ", gcId: " . $gift_card_id . ", "
. "CLAIM CODE: <b>" . $claim_code . "</b><br>"; . "EXPIRE DATE: <b>" . dateTr($expiration_date) . "</b>, "
. "CLAIM CODE: <b>" . $claim_code . "</b>";
if ($debug_print === true) { if ($debug_print === true) {
print "<pre>" . print_r($aws_test, true) . "</pre><br>"; print "<pre>" . print_r($aws_test, true) . "</pre>";
} }
fwrite($fp, writeLog((array)$aws_test));
} catch (\Exception $e) {
$error = Amazon\AmazonIncentives::decodeExceptionMessage($e->getMessage());
printException('buyGiftCard', $e->getCode(), $error, $debug_print);
fwrite($fp, writeLog($error));
}
print "<br>";
sleep($debug_wait); sleep($debug_wait);
try {
// cancel above created card card // cancel above created card card
$aws_test = Amazon\AmazonIncentives::make()->cancelGiftCard($creation_request_id, $gift_card_id); $aws_test = Amazon\AmazonIncentives::make()->cancelGiftCard($creation_request_id, $gift_card_id);
$request_status = $aws_test->getStatus(); $request_status = $aws_test->getStatus();
print "AWS: cancelGiftCard: " . $request_status . ": " print "AWS: cancelGiftCard: " . $request_status . ": "
. "creationRequestId: " . $creation_request_id . ", gcId: " . $gift_card_id . "creationRequestId: " . $creation_request_id . ", gcId: " . $gift_card_id;
. "<br>";
if ($debug_print === true) { if ($debug_print === true) {
print "<pre>" . print_r($aws_test, true) . "</pre><br>"; print "<pre>" . print_r($aws_test, true) . "</pre>";
} }
fwrite($fp, writeLog((array)$aws_test));
} catch (\Exception $e) {
$error = Amazon\AmazonIncentives::decodeExceptionMessage($e->getMessage());
print "AWS: cancelGiftCard: " . $error['status']
. " [" . $e->getCode() . "]: "
. $error['code'] . " | " . $error['type']
. " | " . $error['message'];
if ($debug_print === true) {
print "<pre>" . print_r($error['log'][$error['log_id'] ?? ''] ?? [], true) . "</pre>";
}
fwrite($fp, writeLog($error));
}
print "<br>";
sleep($debug_wait); sleep($debug_wait);
// set same request ID twice to get same response test // set same request ID twice to get same response test
try {
$aws_test = Amazon\AmazonIncentives::make()->buyGiftCard((float)$value); $aws_test = Amazon\AmazonIncentives::make()->buyGiftCard((float)$value);
$creation_request_id = $aws_test->getCreationRequestId(); $creation_request_id = $aws_test->getCreationRequestId();
$gift_card_id = $aws_test->getId(); $gift_card_id = $aws_test->getId();
$claim_code = $aws_test->getClaimCode(); $claim_code = $aws_test->getClaimCode();
$expiration_date = $aws_test->getExpirationDate();
$request_status = $aws_test->getStatus(); $request_status = $aws_test->getStatus();
print "AWS: buyGiftCard: CODE A: " . $request_status . ": " print "AWS: buyGiftCard: CODE A: " . $request_status . ": "
. "creationRequestId: " . $creation_request_id . ", gcId: " . $gift_card_id . ", " . "creationRequestId: " . $creation_request_id . ", gcId: " . $gift_card_id . ", "
. "CLAIM CODE: <b>" . $claim_code . "</b><br>"; . "EXPIRE DATE: <b>" . dateTr($expiration_date) . "</b>, "
. "CLAIM CODE: <b>" . $claim_code . "</b>";
if ($debug_print === true) { if ($debug_print === true) {
print "<pre>" . print_r($aws_test, true) . "</pre><br>"; print "<pre>" . print_r($aws_test, true) . "</pre>";
} }
fwrite($fp, writeLog((array)$aws_test));
} catch (\Exception $e) {
$error = Amazon\AmazonIncentives::decodeExceptionMessage($e->getMessage());
printException('cancelGiftCard', $e->getCode(), $error, $debug_print);
fwrite($fp, writeLog($error));
}
print "<br>";
sleep($debug_wait); sleep($debug_wait);
try {
$aws_test = Amazon\AmazonIncentives::make()->buyGiftCard((float)$value, $creation_request_id); $aws_test = Amazon\AmazonIncentives::make()->buyGiftCard((float)$value, $creation_request_id);
$request_status = $aws_test->getStatus(); $request_status = $aws_test->getStatus();
// same?
$expiration_date = $aws_test->getExpirationDate();
print "AWS: buyGiftCard: SAME CODE A AGAIN: " . $request_status . ": " print "AWS: buyGiftCard: SAME CODE A AGAIN: " . $request_status . ": "
. "creationRequestId: " . $creation_request_id . ", gcId: " . $gift_card_id . ", " . "creationRequestId: " . $creation_request_id . ", gcId: " . $gift_card_id . ", "
. "CLAIM CODE: <b>" . $claim_code . "</b><br>"; . "EXPIRE DATE: <b>" . dateTr($expiration_date) . "</b>, "
. "CLAIM CODE: <b>" . $claim_code . "</b>";
if ($debug_print === true) { if ($debug_print === true) {
print "<pre>" . print_r($aws_test, true) . "</pre><br>"; print "<pre>" . print_r($aws_test, true) . "</pre>";
} }
fwrite($fp, writeLog((array)$aws_test));
} catch (\Exception $e) {
$error = Amazon\AmazonIncentives::decodeExceptionMessage($e->getMessage());
printException('buyGiftCard', $e->getCode(), $error, $debug_print);
fwrite($fp, writeLog($error));
}
print "<br>";
print "<hr>"; print "<hr>";
sleep($debug_wait); sleep($debug_wait);
} }
@@ -164,6 +266,7 @@ if ($mock_debug === true) {
$mock_value = 500; $mock_value = 500;
$mock['F0000'] = [ 'ret' => '', 'st' => 'SUCCESS']; // success mock $mock['F0000'] = [ 'ret' => '', 'st' => 'SUCCESS']; // success mock
$mock['F1000'] = [ 'ret' => 'F100', 'st' => 'FAILURE']; // SimpleAmountIsNull, etc
$mock['F2003'] = [ 'ret' => 'F200', 'st' => 'FAILURE']; // InvalidAmountInput $mock['F2003'] = [ 'ret' => 'F200', 'st' => 'FAILURE']; // InvalidAmountInput
$mock['F2004'] = [ 'ret' => 'F200', 'st' => 'FAILURE']; // InvalidAmountValue $mock['F2004'] = [ 'ret' => 'F200', 'st' => 'FAILURE']; // InvalidAmountValue
$mock['F2005'] = [ 'ret' => 'F200', 'st' => 'FAILURE']; // InvalidCurrencyCodeInput $mock['F2005'] = [ 'ret' => 'F200', 'st' => 'FAILURE']; // InvalidCurrencyCodeInput
@@ -186,7 +289,7 @@ if ($mock_debug === true) {
$gift_card_id = $aws_test->getId(); $gift_card_id = $aws_test->getId();
$claim_code = $aws_test->getClaimCode(); $claim_code = $aws_test->getClaimCode();
$request_status = $aws_test->getStatus(); $request_status = $aws_test->getStatus();
print "AWS: MOCK: " . $creation_id . ": buyGiftCard: " . $request_status . ": " print "AWS: MOCK: " . $creation_id . ": buyGiftCard: <b>" . $request_status . "</b>: "
. "creationRequestId: " . $creation_request_id . ", gcId: " . $gift_card_id . ", " . "creationRequestId: " . $creation_request_id . ", gcId: " . $gift_card_id . ", "
. "CLAIM CODE: <b>" . $claim_code . "</b>: "; . "CLAIM CODE: <b>" . $claim_code . "</b>: ";
if ($mock_return['st'] == $request_status) { if ($mock_return['st'] == $request_status) {
@@ -197,6 +300,7 @@ if ($mock_debug === true) {
if ($mock_debug === true) { if ($mock_debug === true) {
print "<pre>" . print_r($aws_test, true) . "</pre>"; print "<pre>" . print_r($aws_test, true) . "</pre>";
} }
fwrite($fp, writeLog((array)$aws_test));
} catch (Exception $e) { } catch (Exception $e) {
$error = Amazon\AmazonIncentives::decodeExceptionMessage($e->getMessage()); $error = Amazon\AmazonIncentives::decodeExceptionMessage($e->getMessage());
print "AWS: MOCK: " . $creation_id . ": buyGiftCard: " . $error['status'] print "AWS: MOCK: " . $creation_id . ": buyGiftCard: " . $error['status']
@@ -212,8 +316,9 @@ if ($mock_debug === true) {
print $mock_failure; print $mock_failure;
} }
if ($mock_debug === true) { if ($mock_debug === true) {
print "/<pre>" . print_r($error['log'][$error['log_id'] ?? ''] ?? [], true) . "</pre>"; print "<pre>" . print_r($error['log'][$error['log_id'] ?? ''] ?? [], true) . "</pre>";
} }
fwrite($fp, writeLog($error));
} }
print "<br>"; print "<br>";
// Waiting a moment, so we don't flood // Waiting a moment, so we don't flood
@@ -222,4 +327,6 @@ if ($mock_debug === true) {
print "<hr>"; print "<hr>";
} }
fclose($fp);
// __END__ // __END__

2
test/log/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
*
!.gitignore