Compare commits
55 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 33a48f47de | |||
| cf5ece0b5f | |||
| db8df612b4 | |||
| 75f4d0b10a | |||
| 7fed1c2a85 | |||
| 28a9e390cc | |||
| a565d2899b | |||
| 58b126ab83 | |||
| e9a6332ad0 | |||
| a79fd519ed | |||
| 04aa9fa019 | |||
| d86ad8c051 | |||
| 6f5cd028b9 | |||
| 3634c5bdfa | |||
|
|
37b4cbce40 | ||
| 54a4d9e4c3 | |||
| bd2a328956 | |||
| bcbf97b406 | |||
| 422fc66137 | |||
| 462d00cb42 | |||
| 308794488d | |||
| 96b24f9424 | |||
| d69781a709 | |||
| 2bcd42f4a5 | |||
| 05b33ac157 | |||
| 4f072d2226 | |||
| 774e553d90 | |||
| fd5477269b | |||
| afb11a6797 | |||
| d6382960bf | |||
| b26cc82055 | |||
| 6f177986f9 | |||
| e1b5dc1bab | |||
| 7cb5e00a5f | |||
| a51f4df53e | |||
| 42087a64fa | |||
| ab95e1c21b | |||
| 1e836e9a2b | |||
| b1079e1d24 | |||
| 5b99da616b | |||
| f60aa3e1bc | |||
| b041720a18 | |||
| be6b2be55d | |||
|
|
6163d26f50 | ||
|
|
a4c06b9a36 | ||
| 24dc8426de | |||
| 7ef4133537 | |||
| 4f90f2d916 | |||
| 192e4e54e1 | |||
| d0b676f8ce | |||
| c2721cb8d4 | |||
| 18cf3330df | |||
| 3b7f644ae9 | |||
| 6f9bf781f2 | |||
| 94ea118731 |
6
.gitattributes
vendored
Normal file
6
.gitattributes
vendored
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
test/ export-ignore
|
||||||
|
phpstan.neon export-ignore
|
||||||
|
phpunit.xml export-ignore
|
||||||
|
psalm.xml export-ignore
|
||||||
|
.phan/ export-ignore
|
||||||
|
.* export-ignore
|
||||||
49
.github/workflows/ci.yml
vendored
Normal file
49
.github/workflows/ci.yml
vendored
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
name: CI
|
||||||
|
run-name: ${{ github.actor}} runs CI
|
||||||
|
|
||||||
|
on: [push]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
ci-tests:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- uses: php-actions/composer@v6
|
||||||
|
env:
|
||||||
|
COMPOSER_ROOT_VERSION: dev-master
|
||||||
|
- name: "Restore result cache"
|
||||||
|
uses: actions/cache/restore@v4
|
||||||
|
with:
|
||||||
|
path: ./tmp
|
||||||
|
key: "result-cache-v1-${{ matrix.php-version }}-${{ github.run_id }}"
|
||||||
|
restore-keys: |
|
||||||
|
result-cache-v1-${{ matrix.php-version }}-
|
||||||
|
- name: PHPStan Static Analysis
|
||||||
|
uses: php-actions/phpstan@v3
|
||||||
|
with:
|
||||||
|
path: src/
|
||||||
|
- name: "Save result cache"
|
||||||
|
uses: actions/cache/save@v4
|
||||||
|
if: always()
|
||||||
|
with:
|
||||||
|
path: ./tmp
|
||||||
|
key: "result-cache-v1-${{ matrix.php-version }}-${{ github.run_id }}"
|
||||||
|
# We need to use phpunit from the self install to get the class paths
|
||||||
|
# Skip auto tests here, there are too many problems with flooding of the AWS API from github
|
||||||
|
- name: PHPunit Tests Prepare
|
||||||
|
env:
|
||||||
|
AWS_GIFT_CARD_ENDPOINT: "${{ secrets.AWS_GIFT_CARD_ENDPOINT }}"
|
||||||
|
AWS_GIFT_CARD_KEY: "${{ secrets.AWS_GIFT_CARD_KEY }}"
|
||||||
|
AWS_GIFT_CARD_SECRET: "${{ secrets.AWS_GIFT_CARD_SECRET }}"
|
||||||
|
AWS_GIFT_CARD_PARTNER_ID: "${{ secrets.AWS_GIFT_CARD_PARTNER_ID }}"
|
||||||
|
AWS_GIFT_CARD_CURRENCY: "${{ secrets.AWS_GIFT_CARD_CURRENCY }}"
|
||||||
|
run: |
|
||||||
|
echo "AWS_GIFT_CARD_ENDPOINT=${AWS_GIFT_CARD_ENDPOINT}" > test/.env;
|
||||||
|
echo "AWS_GIFT_CARD_KEY=${AWS_GIFT_CARD_KEY}" >> test/.env;
|
||||||
|
echo "AWS_GIFT_CARD_SECRET=${AWS_GIFT_CARD_SECRET}" >> test/.env;
|
||||||
|
echo "AWS_GIFT_CARD_PARTNER_ID=${AWS_GIFT_CARD_PARTNER_ID}" >> test/.env;
|
||||||
|
echo "AWS_GIFT_CARD_CURRENCY=${AWS_GIFT_CARD_CURRENCY}" >> test/.env;
|
||||||
|
echo "AWS_DEBUG=1" >> test/.env;
|
||||||
|
- name: PHPunit Tests
|
||||||
|
run: |
|
||||||
|
vendor/bin/phpunit
|
||||||
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
vendor
|
||||||
|
.phpunit.result.cache
|
||||||
|
composer.lock
|
||||||
99
.phan/config.php
Normal file
99
.phan/config.php
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This configuration will be read and overlaid on top of the
|
||||||
|
* default configuration. Command line arguments will be applied
|
||||||
|
* after this file is read.
|
||||||
|
*
|
||||||
|
* @see src/Phan/Config.php
|
||||||
|
* See Config for all configurable options.
|
||||||
|
*
|
||||||
|
* A Note About Paths
|
||||||
|
* ==================
|
||||||
|
*
|
||||||
|
* Files referenced from this file should be defined as
|
||||||
|
*
|
||||||
|
* ```
|
||||||
|
* Config::projectPath('relative_path/to/file')
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
* where the relative path is relative to the root of the
|
||||||
|
* project which is defined as either the working directory
|
||||||
|
* of the phan executable or a path passed in via the CLI
|
||||||
|
* '-d' flag.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// use Phan\Config;
|
||||||
|
|
||||||
|
return [
|
||||||
|
"minimum_target_php_version" => "7.4",
|
||||||
|
// If true, missing properties will be created when
|
||||||
|
// they are first seen. If false, we'll report an
|
||||||
|
// error message.
|
||||||
|
"allow_missing_properties" => false,
|
||||||
|
|
||||||
|
// Allow null to be cast as any type and for any
|
||||||
|
// type to be cast to null.
|
||||||
|
"null_casts_as_any_type" => false,
|
||||||
|
|
||||||
|
// Backwards Compatibility Checking
|
||||||
|
'backward_compatibility_checks' => true,
|
||||||
|
|
||||||
|
// Run a quick version of checks that takes less
|
||||||
|
// time
|
||||||
|
"quick_mode" => false,
|
||||||
|
|
||||||
|
// Only emit critical issues to start with
|
||||||
|
// (0 is low severity, 5 is normal severity, 10 is critical)
|
||||||
|
"minimum_severity" => 10,
|
||||||
|
|
||||||
|
// default false for include path check
|
||||||
|
"enable_include_path_checks" => true,
|
||||||
|
"include_paths" => [
|
||||||
|
],
|
||||||
|
'ignore_undeclared_variables_in_global_scope' => true,
|
||||||
|
|
||||||
|
"file_list" => [
|
||||||
|
],
|
||||||
|
|
||||||
|
// A list of directories that should be parsed for class and
|
||||||
|
// method information. After excluding the directories
|
||||||
|
// defined in exclude_analysis_directory_list, the remaining
|
||||||
|
// files will be statically analyzed for errors.
|
||||||
|
//
|
||||||
|
// Thus, both first-party and third-party code being used by
|
||||||
|
// your application should be included in this list.
|
||||||
|
'directory_list' => [
|
||||||
|
// Change this to include the folders you wish to analyze
|
||||||
|
// (and the folders of their dependencies)
|
||||||
|
'src/'
|
||||||
|
// 'www',
|
||||||
|
// To speed up analysis, we recommend going back later and
|
||||||
|
// limiting this to only the vendor/ subdirectories your
|
||||||
|
// project depends on.
|
||||||
|
// `phan --init` will generate a list of folders for you
|
||||||
|
//'www/vendor',
|
||||||
|
],
|
||||||
|
|
||||||
|
|
||||||
|
// A list of directories holding code that we want
|
||||||
|
// to parse, but not analyze
|
||||||
|
"exclude_analysis_directory_list" => [
|
||||||
|
'vendor',
|
||||||
|
'test',
|
||||||
|
],
|
||||||
|
'exclude_file_list' => [
|
||||||
|
],
|
||||||
|
|
||||||
|
// what not to show as problem
|
||||||
|
'suppress_issue_types' => [
|
||||||
|
// 'PhanUndeclaredMethod',
|
||||||
|
'PhanEmptyFile',
|
||||||
|
],
|
||||||
|
|
||||||
|
// Override to hardcode existence and types of (non-builtin) globals in the global scope.
|
||||||
|
// Class names should be prefixed with `\`.
|
||||||
|
//
|
||||||
|
// (E.g. `['_FOO' => '\FooClass', 'page' => '\PageClass', 'userId' => 'int']`)
|
||||||
|
'globals_type_map' => [],
|
||||||
|
];
|
||||||
75
Readme.md
75
Readme.md
@@ -1,10 +1,20 @@
|
|||||||
# Amazon Incentives - Gift on Demand standa alone class
|
# Amazon Incentives - Gift Codes on Demand stand alone class
|
||||||
|
|
||||||
This is a abastract from (https://github.com/kamerk22/AmazonGiftCode) to be not dependend on Laravel base code.
|
This is a abastract from [https://github.com/kamerk22/AmazonGiftCod](https://github.com/kamerk22/AmazonGiftCode) to be not dependend on Laravel base code.
|
||||||
|
|
||||||
|
Amazon Gift Codes On Demand (AGCOD). Integration for Amazon Incentive API.
|
||||||
|
|
||||||
|
[General Amazon Incentives Documentation](https://developer.amazon.com/docs/incentives-api/digital-gift-cards.html)
|
||||||
|
|
||||||
|
## How to install
|
||||||
|
|
||||||
|
`composer require gullevek/amazon-incentives`
|
||||||
|
|
||||||
|
## _ENV variables needed
|
||||||
|
|
||||||
Uses .env file to load configuration data
|
Uses .env file to load configuration data
|
||||||
|
|
||||||
## _ENV variables needed
|
The below keys are searched in the _ENV file for loading
|
||||||
|
|
||||||
* AWS_GIFT_CARD_KEY
|
* AWS_GIFT_CARD_KEY
|
||||||
* AWS_GIFT_CARD_SECRET
|
* AWS_GIFT_CARD_SECRET
|
||||||
@@ -15,14 +25,15 @@ Uses .env file to load configuration data
|
|||||||
|
|
||||||
## How to use
|
## How to use
|
||||||
|
|
||||||
The class must be loaded with an autoloader (see test/autoloader.php for example).
|
|
||||||
|
|
||||||
The above _ENV variables must be set (Except AWS_DEBUG, defaults to off).
|
The above _ENV variables must be set (Except AWS_DEBUG, defaults to off).
|
||||||
|
|
||||||
### create gift card
|
### create gift card
|
||||||
|
|
||||||
```php
|
```php
|
||||||
$aws_gc = Amazon\AmazonIncentives::make()->buyGiftCard((float)$value);
|
use gullevek\AmazonIncentives\AmazonIncentives;
|
||||||
|
// buy a gift card with a value
|
||||||
|
$value = 500;
|
||||||
|
$aws_gc = AmazonIncentives::make()->buyGiftCard((float)$value);
|
||||||
// the two below are need if we want to cancel the card
|
// the two below are need if we want to cancel the card
|
||||||
// get gift card id (gcID)
|
// get gift card id (gcID)
|
||||||
$aws_gc->getId();
|
$aws_gc->getId();
|
||||||
@@ -55,14 +66,14 @@ Recommended to pool requests. Or check when last requests where sent and then pr
|
|||||||
|
|
||||||
```php
|
```php
|
||||||
// use getCreationRequestId() and getId() from request
|
// use getCreationRequestId() and getId() from request
|
||||||
$aws_gc = Amazon\AmazonIncentives::make()->cancelGiftCard($creation_request_id, $gift_card_id);
|
$aws_gc = gullevek\AmazonIncentives\AmazonIncentives::make()->cancelGiftCard($creation_request_id, $gift_card_id);
|
||||||
// return is as above
|
// return is as above
|
||||||
```
|
```
|
||||||
|
|
||||||
### check balance
|
### check balance
|
||||||
|
|
||||||
```php
|
```php
|
||||||
$aws_gc = Amazon\AmazonIncentives::make()->getAvailableFunds();
|
$aws_gc = gullevek\AmazonIncentives\AmazonIncentives::make()->getAvailableFunds();
|
||||||
```
|
```
|
||||||
|
|
||||||
## Exceptions
|
## Exceptions
|
||||||
@@ -71,6 +82,15 @@ If the HTTPS request does not return 220 OK it will throw an exception.
|
|||||||
|
|
||||||
The error code is the curl handler error code.
|
The error code is the curl handler error code.
|
||||||
The error message is json encoded array with the layout
|
The error message is json encoded array with the layout
|
||||||
|
|
||||||
|
Use
|
||||||
|
|
||||||
|
```php
|
||||||
|
$exception_array = gullevek\AmazonIncentives\AmazonIncentives::decodeExceptionMessage($exception_message);
|
||||||
|
```
|
||||||
|
|
||||||
|
to extract the below array from the thrown exception
|
||||||
|
|
||||||
```php
|
```php
|
||||||
[
|
[
|
||||||
'status' => 'AWS Status FAILURE or RESEND',
|
'status' => 'AWS Status FAILURE or RESEND',
|
||||||
@@ -84,18 +104,47 @@ The error message is json encoded array with the layout
|
|||||||
|
|
||||||
`status`, `code` and `type` must be checked on a failure.
|
`status`, `code` and `type` must be checked on a failure.
|
||||||
|
|
||||||
**NOTE**: if code is E999 then this is a request flood error:
|
## Other Errors from exceptions
|
||||||
|
|
||||||
|
### T001
|
||||||
|
|
||||||
|
if code is T001 then this is a request flood error:
|
||||||
In this case the request has to be resend after a certain waiting period.
|
In this case the request has to be resend after a certain waiting period.
|
||||||
|
|
||||||
|
### E999
|
||||||
|
|
||||||
|
if code is E999 some other critical error has happened
|
||||||
|
|
||||||
|
### E001
|
||||||
|
|
||||||
|
if code is E001 if the return create/cancel/check calls is not an array
|
||||||
|
|
||||||
|
### C001
|
||||||
|
|
||||||
|
fif code is C001 curl failed to init
|
||||||
|
|
||||||
|
### C002
|
||||||
|
|
||||||
|
if code is C002 a curl error has happened
|
||||||
|
|
||||||
|
### J-number
|
||||||
|
|
||||||
|
if a JSON error was encountered during some encoding this error will be found.
|
||||||
|
The number is the json error code.
|
||||||
|
|
||||||
|
### empty error code
|
||||||
|
|
||||||
|
any other NON amazon error will have only 'message' set if run through decode
|
||||||
|
|
||||||
## Debugging
|
## Debugging
|
||||||
|
|
||||||
If AWS_DEBUG is set to 1 and internal array will be written with debug info.
|
If AWS_DEBUG is set to 1 and internal array will be written with debug info.
|
||||||
|
|
||||||
The Amazon\Debug\AmazonDebug class handles all this.
|
The gulleek\AmazonIncentives\Debug\AmazonDebug class handles all this.
|
||||||
|
|
||||||
In the Amazon\AWS\AWS main class the debugger gets set
|
In the gulleek\AmazonIncentives\AmazonIncentives main class the debugger gets set
|
||||||
* setFlag that turns debugger on/off
|
|
||||||
* setId (to set unique id for each run)
|
* setDebug that turns debugger on/off and if on sets unique id (getId to check)
|
||||||
|
|
||||||
New entries can be written with
|
New entries can be written with
|
||||||
|
|
||||||
|
|||||||
34
composer.json
Normal file
34
composer.json
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
{
|
||||||
|
"name": "gullevek/amazon-incentives",
|
||||||
|
"description": "Amazon Gift Codes, Gift on Demand, Incentives",
|
||||||
|
"keywords": ["AmazonGiftCode", "Amazon", "GiftCard", "AGCOD", "Incentives API", "Amazon Incentives API"],
|
||||||
|
"type": "library",
|
||||||
|
"license": "MIT",
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"gullevek\\AmazonIncentives\\": "src/"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Clemens Schwaighofer",
|
||||||
|
"email": "gullevek@gullevek.org",
|
||||||
|
"homepage": "http://gullevek.org"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"homepage": "https://github.com/gullevek/AmazonIncentives",
|
||||||
|
"minimum-stability": "dev",
|
||||||
|
"require": {
|
||||||
|
"php": ">=7.4.0"
|
||||||
|
},
|
||||||
|
"archive": {
|
||||||
|
"exclude": ["/test/", "/test/*", "/phpstan.neon", "/psalm.xml", "/.phan/", "/.vscode/", "/phpunit.xml"]
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"phpunit/phpunit": "^9",
|
||||||
|
"gullevek/dotenv": "dev-master",
|
||||||
|
"dg/bypass-finals": "dev-master",
|
||||||
|
"phpstan/phpstan": "2.1.x-dev",
|
||||||
|
"phpstan/phpstan-deprecation-rules": "2.0.x-dev"
|
||||||
|
}
|
||||||
|
}
|
||||||
11
phpstan.neon
Normal file
11
phpstan.neon
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
# PHP Stan Config
|
||||||
|
|
||||||
|
parameters:
|
||||||
|
tmpDir: %currentWorkingDirectory%/tmp/phpstan-codeblocks-amazon-incentives
|
||||||
|
level: 9
|
||||||
|
paths:
|
||||||
|
- %currentWorkingDirectory%/src
|
||||||
|
excludePaths:
|
||||||
|
# ignore composer
|
||||||
|
- vendor
|
||||||
|
- test
|
||||||
14
phpunit.xml
Normal file
14
phpunit.xml
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
<phpunit
|
||||||
|
colors="true"
|
||||||
|
verbose="false"
|
||||||
|
>
|
||||||
|
<!-- Below removes final from classes for mock tests -->
|
||||||
|
<extensions>
|
||||||
|
<extension class="test\phpUnit\Hook\BypassFinalHook" file="test/phpUnit/Hook/BypassFinalHook.php" />
|
||||||
|
</extensions>
|
||||||
|
<testsuites>
|
||||||
|
<testsuite name="unit">
|
||||||
|
<directory>test/phpUnit/</directory>
|
||||||
|
</testsuite>
|
||||||
|
</testsuites>
|
||||||
|
</phpunit>
|
||||||
16
psalm.xml
Normal file
16
psalm.xml
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
<?xml version="1.0"?>
|
||||||
|
<psalm
|
||||||
|
errorLevel="8"
|
||||||
|
resolveFromConfigFile="true"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns="https://getpsalm.org/schema/config"
|
||||||
|
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
|
||||||
|
>
|
||||||
|
<projectFiles>
|
||||||
|
<directory name="." />
|
||||||
|
<ignoreFiles>
|
||||||
|
<directory name="vendor" />
|
||||||
|
<directory name="test" />
|
||||||
|
</ignoreFiles>
|
||||||
|
</projectFiles>
|
||||||
|
</psalm>
|
||||||
542
src/AWS/AWS.php
Normal file
542
src/AWS/AWS.php
Normal file
@@ -0,0 +1,542 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace gullevek\AmazonIncentives\AWS;
|
||||||
|
|
||||||
|
use gullevek\AmazonIncentives\Client\Client;
|
||||||
|
use gullevek\AmazonIncentives\Config\Config;
|
||||||
|
use gullevek\AmazonIncentives\Exceptions\AmazonErrors;
|
||||||
|
use gullevek\AmazonIncentives\Debug\AmazonDebug;
|
||||||
|
use gullevek\AmazonIncentives\Response\CancelResponse;
|
||||||
|
use gullevek\AmazonIncentives\Response\CreateBalanceResponse;
|
||||||
|
use gullevek\AmazonIncentives\Response\CreateResponse;
|
||||||
|
use gullevek\AmazonIncentives\Handle\Json;
|
||||||
|
|
||||||
|
class AWS
|
||||||
|
{
|
||||||
|
/** @var string What AWS Service to use: Gift Card on Demand (GCOD) */
|
||||||
|
public const SERVICE_NAME = 'AGCODService';
|
||||||
|
/** @var string */
|
||||||
|
public const ACCEPT_HEADER = 'accept';
|
||||||
|
/** @var string content-type */
|
||||||
|
public const CONTENT_HEADER = 'content-type';
|
||||||
|
/** @var string */
|
||||||
|
public const HOST_HEADER = 'host';
|
||||||
|
/** @var string */
|
||||||
|
public const X_AMZ_DATE_HEADER = 'x-amz-date';
|
||||||
|
/** @var string */
|
||||||
|
public const X_AMZ_TARGET_HEADER = 'x-amz-target';
|
||||||
|
/** @var string */
|
||||||
|
public const AUTHORIZATION_HEADER = 'Authorization';
|
||||||
|
/** @var string type of encryption type */
|
||||||
|
public const AWS_SHA256_ALGORITHM = 'AWS4-HMAC-SHA256';
|
||||||
|
/** @var string key type to use */
|
||||||
|
public const KEY_QUALIFIER = 'AWS4';
|
||||||
|
/** @var string */
|
||||||
|
public const TERMINATION_STRING = 'aws4_request';
|
||||||
|
/** @var string Service to use: Create Gift Card */
|
||||||
|
public const CREATE_GIFT_CARD_SERVICE = 'CreateGiftCard';
|
||||||
|
/** @var string Service to use: Cancle Gift Card */
|
||||||
|
public const CANCEL_GIFT_CARD_SERVICE = 'CancelGiftCard';
|
||||||
|
/** @var string Service to use: Get Available Funds */
|
||||||
|
public const GET_AVAILABLE_FUNDS_SERVICE = 'GetAvailableFunds';
|
||||||
|
|
||||||
|
/** @var Config Configuration class with all settings */
|
||||||
|
private $config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize the main AWS class. This class prepares and sends all the actions
|
||||||
|
* and returns reponses as defined in in the CreateResponse class
|
||||||
|
*
|
||||||
|
* @param Config $config
|
||||||
|
*/
|
||||||
|
public function __construct(Config $config)
|
||||||
|
{
|
||||||
|
$this->config = $config;
|
||||||
|
AmazonDebug::writeLog([__METHOD__ => date('Y-m-d H:m:s.u')]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bug a gift card
|
||||||
|
*
|
||||||
|
* @param float $amount Amount to buy a gifr card in set currencty
|
||||||
|
* @param string|null $creation_id Override creation id, if not set will
|
||||||
|
* be created automatically. If not valid error
|
||||||
|
* will be thrown
|
||||||
|
* @return CreateResponse Object with AWS response data
|
||||||
|
*
|
||||||
|
* @throws AmazonErrors
|
||||||
|
*/
|
||||||
|
public function getCode(float $amount, ?string $creation_id = null): CreateResponse
|
||||||
|
{
|
||||||
|
$service_operation = self::CREATE_GIFT_CARD_SERVICE;
|
||||||
|
$payload = $this->getGiftCardPayload($amount, $creation_id);
|
||||||
|
$canonical_request = $this->getCanonicalRequest($service_operation, $payload);
|
||||||
|
$date_time_string = $this->getTimestamp();
|
||||||
|
AmazonDebug::writeLog(['call' => __METHOD__]);
|
||||||
|
$result = Json::jsonDecode($this->makeRequest(
|
||||||
|
$payload,
|
||||||
|
$canonical_request,
|
||||||
|
$service_operation,
|
||||||
|
$date_time_string
|
||||||
|
));
|
||||||
|
return new CreateResponse($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cancle an ordered gift card, only possible within the the time limit
|
||||||
|
*
|
||||||
|
* @param string $creation_request_id Previously created creation request id
|
||||||
|
* @param string $gift_card_id Previously created gift card id
|
||||||
|
* @return CancelResponse Object with AWS response data
|
||||||
|
*
|
||||||
|
* @throws AmazonErrors
|
||||||
|
*/
|
||||||
|
public function cancelCode(string $creation_request_id, string $gift_card_id): CancelResponse
|
||||||
|
{
|
||||||
|
$service_operation = self::CANCEL_GIFT_CARD_SERVICE;
|
||||||
|
$payload = $this->getCancelGiftCardPayload($creation_request_id, $gift_card_id);
|
||||||
|
$canonical_request = $this->getCanonicalRequest($service_operation, $payload);
|
||||||
|
$date_time_string = $this->getTimestamp();
|
||||||
|
AmazonDebug::writeLog(['call' => __METHOD__]);
|
||||||
|
$result = Json::jsonDecode($this->makeRequest(
|
||||||
|
$payload,
|
||||||
|
$canonical_request,
|
||||||
|
$service_operation,
|
||||||
|
$date_time_string
|
||||||
|
));
|
||||||
|
return new CancelResponse($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get current account funds
|
||||||
|
*
|
||||||
|
* @return CreateBalanceResponse Object with AWS response data
|
||||||
|
*
|
||||||
|
* @throws AmazonErrors
|
||||||
|
*/
|
||||||
|
public function getBalance(): CreateBalanceResponse
|
||||||
|
{
|
||||||
|
$service_operation = self::GET_AVAILABLE_FUNDS_SERVICE;
|
||||||
|
$payload = $this->getAvailableFundsPayload();
|
||||||
|
$canonical_request = $this->getCanonicalRequest($service_operation, $payload);
|
||||||
|
$date_time_string = $this->getTimestamp();
|
||||||
|
AmazonDebug::writeLog(['call' => __METHOD__]);
|
||||||
|
$result = Json::jsonDecode($this->makeRequest(
|
||||||
|
$payload,
|
||||||
|
$canonical_request,
|
||||||
|
$service_operation,
|
||||||
|
$date_time_string
|
||||||
|
));
|
||||||
|
return new CreateBalanceResponse($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* return a new curl connection client class
|
||||||
|
*
|
||||||
|
* @return Client Curl connection Client
|
||||||
|
*/
|
||||||
|
public function newClient(): Client
|
||||||
|
{
|
||||||
|
return new Client();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* General request method for all actions
|
||||||
|
* Calls the Client class that actually runs the json request
|
||||||
|
* For service_operation valid data see AWS GCOD documentation
|
||||||
|
*
|
||||||
|
* @param string $payload The data needed for this request
|
||||||
|
* @param string $canonical_request Header data to send for this request
|
||||||
|
* @param string $service_operation Service operation. CREATE_GIFT_CARD_SERVICE,
|
||||||
|
* CANCEL_GIFT_CARD_SERVICE or
|
||||||
|
* GET_AVAILABLE_FUNDS_SERVICE constant values
|
||||||
|
* @param string $date_time_string Ymd\THis\Z encoded timestamp, getTimestamp()
|
||||||
|
* @return string Request result as string, json data
|
||||||
|
*/
|
||||||
|
public function makeRequest(
|
||||||
|
string $payload,
|
||||||
|
string $canonical_request,
|
||||||
|
string $service_operation,
|
||||||
|
string $date_time_string
|
||||||
|
): string {
|
||||||
|
// debug
|
||||||
|
AmazonDebug::writeLog([__METHOD__ => [
|
||||||
|
'Operation' => $service_operation,
|
||||||
|
'Payload' => $payload,
|
||||||
|
'Cannonical Request' => $canonical_request,
|
||||||
|
'Date Time String' => $date_time_string
|
||||||
|
|
||||||
|
]]);
|
||||||
|
$KEY_QUALIFIER = self::KEY_QUALIFIER;
|
||||||
|
$canonical_request_hash = $this->buildHash($canonical_request);
|
||||||
|
$string_to_sign = $this->buildStringToSign($canonical_request_hash);
|
||||||
|
$authorization_value = $this->buildAuthSignature($string_to_sign);
|
||||||
|
|
||||||
|
$secret_key = $this->config->getSecret();
|
||||||
|
$endpoint = $this->config->getEndpoint();
|
||||||
|
$region_name = $this->getRegion();
|
||||||
|
|
||||||
|
$SERVICE_NAME = 'AGCODService';
|
||||||
|
$service_target = 'com.amazonaws.agcod.' . $SERVICE_NAME . '.' . $service_operation;
|
||||||
|
$date_string = $this->getDateString();
|
||||||
|
|
||||||
|
$signature_aws_key = $KEY_QUALIFIER . $secret_key;
|
||||||
|
|
||||||
|
$k_date = $this->hmac($date_string, $signature_aws_key);
|
||||||
|
$k_date_hexis = $this->hmac($date_string, $signature_aws_key, false);
|
||||||
|
$k_region = $this->hmac($region_name, $k_date);
|
||||||
|
$k_region_hexis = $this->hmac($region_name, $k_date, false);
|
||||||
|
$k_service_hexis = $this->hmac($SERVICE_NAME, $k_region, false);
|
||||||
|
|
||||||
|
AmazonDebug::writeLog([__METHOD__ => [
|
||||||
|
'Date' => $k_date_hexis,
|
||||||
|
'Region' => $k_region_hexis,
|
||||||
|
'Service' => $k_service_hexis,
|
||||||
|
]]);
|
||||||
|
|
||||||
|
$url = 'https://' . $endpoint . '/' . $service_operation;
|
||||||
|
$headers = $this->buildHeaders(
|
||||||
|
$payload,
|
||||||
|
$authorization_value,
|
||||||
|
$date_time_string,
|
||||||
|
$service_target
|
||||||
|
);
|
||||||
|
return ($this->newClient())->request($url, $headers, $payload);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build the headers used in the makeRequest method.
|
||||||
|
* These are the HTML headers used with curl
|
||||||
|
*
|
||||||
|
* @param string $payload Paylout to create this header for
|
||||||
|
* @param string $authorization_value Auth string
|
||||||
|
* @param string $date_time_string Ymd\THis\Z encoded timestamp, getTimestamp()
|
||||||
|
* @param string $service_target Target service in the agcod string:
|
||||||
|
* Value like com.amazonaws.agcod.<sn>.<so>
|
||||||
|
* @return array<int,string> Header data as array for curl request
|
||||||
|
*/
|
||||||
|
public function buildHeaders(
|
||||||
|
string $payload,
|
||||||
|
string $authorization_value,
|
||||||
|
string $date_time_string,
|
||||||
|
string $service_target
|
||||||
|
): array {
|
||||||
|
$ACCEPT_HEADER = self::ACCEPT_HEADER;
|
||||||
|
$X_AMZ_DATE_HEADER = self::X_AMZ_DATE_HEADER;
|
||||||
|
$X_AMZ_TARGET_HEADER = self::X_AMZ_TARGET_HEADER;
|
||||||
|
$AUTHORIZATION_HEADER = self::AUTHORIZATION_HEADER;
|
||||||
|
return [
|
||||||
|
'Content-Type:' . $this->getContentType(),
|
||||||
|
'Content-Length: ' . strlen($payload),
|
||||||
|
$AUTHORIZATION_HEADER . ':' . $authorization_value,
|
||||||
|
$X_AMZ_DATE_HEADER . ':' . $date_time_string,
|
||||||
|
$X_AMZ_TARGET_HEADER . ':' . $service_target,
|
||||||
|
$ACCEPT_HEADER . ':' . $this->getContentType()
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The request string build with the actauly request data created by
|
||||||
|
* getCanonicalRequest(). This string is used in the auth signature call
|
||||||
|
*
|
||||||
|
* @param string $canonical_request_hash sha256 hash to build from
|
||||||
|
* @return string String to send to buildAuthSignature()
|
||||||
|
*/
|
||||||
|
public function buildStringToSign($canonical_request_hash): string
|
||||||
|
{
|
||||||
|
$AWS_SHA256_ALGORITHM = self::AWS_SHA256_ALGORITHM;
|
||||||
|
$TERMINATION_STRING = self::TERMINATION_STRING;
|
||||||
|
$SERVICE_NAME = self::SERVICE_NAME;
|
||||||
|
$region_name = $this->getRegion();
|
||||||
|
$date_time_string = $this->getTimestamp();
|
||||||
|
$date_string = $this->getDateString();
|
||||||
|
$string_to_sign = "$AWS_SHA256_ALGORITHM\n"
|
||||||
|
. "$date_time_string\n"
|
||||||
|
. "$date_string/$region_name/$SERVICE_NAME/$TERMINATION_STRING\n"
|
||||||
|
. "$canonical_request_hash";
|
||||||
|
|
||||||
|
return $string_to_sign;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build the authentication signature used in the buildHeaders method
|
||||||
|
*
|
||||||
|
* @param string $string_to_sign Data to sign, buildStringToSign()
|
||||||
|
* @return string Authorized value as string
|
||||||
|
*/
|
||||||
|
public function buildAuthSignature(string $string_to_sign): string
|
||||||
|
{
|
||||||
|
$AWS_SHA256_ALGORITHM = self::AWS_SHA256_ALGORITHM;
|
||||||
|
$SERVICE_NAME = self::SERVICE_NAME;
|
||||||
|
$TERMINATION_STRING = self::TERMINATION_STRING;
|
||||||
|
$ACCEPT_HEADER = self::ACCEPT_HEADER;
|
||||||
|
$HOST_HEADER = self::HOST_HEADER;
|
||||||
|
$X_AMZ_DATE_HEADER = self::X_AMZ_DATE_HEADER;
|
||||||
|
$X_AMZ_TARGET_HEADER = self::X_AMZ_TARGET_HEADER;
|
||||||
|
|
||||||
|
$aws_key_id = $this->config->getAccessKey();
|
||||||
|
$region_name = $this->getRegion();
|
||||||
|
|
||||||
|
$date_string = $this->getDateString();
|
||||||
|
$derived_key = $this->buildDerivedKey();
|
||||||
|
// Calculate signature per http://docs.aws.amazon.com/general/latest/gr/sigv4-calculate-signature.html
|
||||||
|
$final_signature = $this->hmac($string_to_sign, $derived_key, false);
|
||||||
|
|
||||||
|
// Assemble Authorization Header with signing information
|
||||||
|
// per http://docs.aws.amazon.com/general/latest/gr/sigv4-add-signature-to-request.html
|
||||||
|
$authorization_value =
|
||||||
|
$AWS_SHA256_ALGORITHM
|
||||||
|
. ' Credential=' . $aws_key_id
|
||||||
|
. '/' . $date_string . '/' . $region_name . '/' . $SERVICE_NAME . '/' . $TERMINATION_STRING . ','
|
||||||
|
. ' SignedHeaders='
|
||||||
|
. $ACCEPT_HEADER . ';' . $HOST_HEADER . ';' . $X_AMZ_DATE_HEADER . ';' . $X_AMZ_TARGET_HEADER . ','
|
||||||
|
. ' Signature='
|
||||||
|
. $final_signature;
|
||||||
|
|
||||||
|
return $authorization_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build the derived key to build the final hmac signature string
|
||||||
|
*
|
||||||
|
* @param bool $rawOutput Set to true to create the hash based message
|
||||||
|
* authenticator string as normal text string or
|
||||||
|
* lowercase hexbits
|
||||||
|
* @return string Derived key (hmac type)
|
||||||
|
*/
|
||||||
|
public function buildDerivedKey(bool $rawOutput = true): string
|
||||||
|
{
|
||||||
|
$KEY_QUALIFIER = self::KEY_QUALIFIER;
|
||||||
|
$TERMINATION_STRING = self::TERMINATION_STRING;
|
||||||
|
$SERVICE_NAME = self::SERVICE_NAME;
|
||||||
|
|
||||||
|
$aws_secret_key = $this->config->getSecret();
|
||||||
|
// Append Key Qualifier, "AWS4", to secret key per
|
||||||
|
// shttp://docs.aws.amazon.com/general/latest/gr/signature-v4-examples.html
|
||||||
|
$signature_aws_key = $KEY_QUALIFIER . $aws_secret_key;
|
||||||
|
$region_name = $this->getRegion();
|
||||||
|
$date_string = $this->getDateString();
|
||||||
|
|
||||||
|
$k_date = $this->hmac($date_string, $signature_aws_key);
|
||||||
|
$k_region = $this->hmac($region_name, $k_date);
|
||||||
|
$k_service = $this->hmac($SERVICE_NAME, $k_region);
|
||||||
|
|
||||||
|
// Derived the Signing key (derivedKey aka kSigning)
|
||||||
|
return $this->hmac($TERMINATION_STRING, $k_service, $rawOutput);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get the region based on endpoint
|
||||||
|
* list as of 2021/10/20
|
||||||
|
* 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
|
||||||
|
*
|
||||||
|
* @return string Region string depending on given endpoint url
|
||||||
|
*/
|
||||||
|
public function getRegion(): string
|
||||||
|
{
|
||||||
|
$endpoint = $this->config->getEndpoint();
|
||||||
|
// default region
|
||||||
|
$region_name = 'us-east-1';
|
||||||
|
|
||||||
|
switch ($endpoint) {
|
||||||
|
case 'agcod-v2.amazon.com':
|
||||||
|
case 'agcod-v2-gamma.amazon.com':
|
||||||
|
$region_name = 'us-east-1';
|
||||||
|
break;
|
||||||
|
case 'agcod-v2-eu.amazon.com':
|
||||||
|
case 'agcod-v2-eu-gamma.amazon.com':
|
||||||
|
$region_name = 'eu-west-1';
|
||||||
|
break;
|
||||||
|
case 'agcod-v2-fe.amazon.com':
|
||||||
|
case 'agcod-v2-fe-gamma.amazon.com':
|
||||||
|
$region_name = 'us-west-2';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return $region_name;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The actual data to send as json encoded string for creating a gift card.
|
||||||
|
* The creation request id must be in the format:
|
||||||
|
* <partner_id>_<unique id 13 characters>
|
||||||
|
*
|
||||||
|
* @param float $amount Amount of currencty to create the gift card
|
||||||
|
* request for
|
||||||
|
* @param string|null $creation_id The creation id, if not set will be created here
|
||||||
|
* @return string JSON encoded array to be used as payload
|
||||||
|
* in get gift card call
|
||||||
|
*/
|
||||||
|
public function getGiftCardPayload(float $amount, ?string $creation_id = null): string
|
||||||
|
{
|
||||||
|
$payload = [
|
||||||
|
'creationRequestId' => $creation_id ?: uniqid($this->config->getPartner() . '_'),
|
||||||
|
'partnerId' => $this->config->getPartner(),
|
||||||
|
'value' =>
|
||||||
|
[
|
||||||
|
'currencyCode' => $this->config->getCurrency(),
|
||||||
|
'amount' => $amount
|
||||||
|
]
|
||||||
|
];
|
||||||
|
return (json_encode($payload)) ?: '';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The actual data to send as json encoded string to cancel a created gift card
|
||||||
|
*
|
||||||
|
* @param string $creation_request_id Creation request id from previous get gift card
|
||||||
|
* @param string $gift_card_id Gift card id from previous get gift card
|
||||||
|
* @return string JSON encoded array to be used as payload
|
||||||
|
* in cancle gift card call
|
||||||
|
*/
|
||||||
|
public function getCancelGiftCardPayload(string $creation_request_id, string $gift_card_id): string
|
||||||
|
{
|
||||||
|
$payload = [
|
||||||
|
'creationRequestId' => $creation_request_id,
|
||||||
|
'partnerId' => $this->config->getPartner(),
|
||||||
|
'gcId' => $gift_card_id
|
||||||
|
];
|
||||||
|
return (json_encode($payload)) ?: '';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The actualy data to send as json encoded string for getting the current
|
||||||
|
* account funds
|
||||||
|
*
|
||||||
|
* @return string JSON encoded array to be used as payload in funds call
|
||||||
|
*/
|
||||||
|
public function getAvailableFundsPayload(): string
|
||||||
|
{
|
||||||
|
$payload = [
|
||||||
|
'partnerId' => $this->config->getPartner(),
|
||||||
|
];
|
||||||
|
return (json_encode($payload)) ?: '';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Heeders used in the getCanonicalRequest()
|
||||||
|
*
|
||||||
|
* @param string $service_operation Service operation code in the service string request
|
||||||
|
* Value is: com.amazonaws.agcod.AGCODService.<so>
|
||||||
|
* @return string Header string to be used
|
||||||
|
*/
|
||||||
|
public function buildCanonicalHeaders(string $service_operation): string
|
||||||
|
{
|
||||||
|
$ACCEPT_HEADER = self::ACCEPT_HEADER;
|
||||||
|
$HOST_HEADER = self::HOST_HEADER;
|
||||||
|
$X_AMZ_DATE_HEADER = self::X_AMZ_DATE_HEADER;
|
||||||
|
$X_AMZ_TARGET_HEADER = self::X_AMZ_TARGET_HEADER;
|
||||||
|
$date_time_string = $this->getTimestamp();
|
||||||
|
$endpoint = $this->config->getEndpoint();
|
||||||
|
$content_type = $this->getContentType();
|
||||||
|
return "$ACCEPT_HEADER:$content_type\n"
|
||||||
|
. "$HOST_HEADER:$endpoint\n"
|
||||||
|
. "$X_AMZ_DATE_HEADER:$date_time_string\n"
|
||||||
|
. "$X_AMZ_TARGET_HEADER:com.amazonaws.agcod.AGCODService.$service_operation";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Headers used in the get/cancel/funds requests
|
||||||
|
*
|
||||||
|
* @param string $service_operation Service operation code to be used in header request
|
||||||
|
* and main request call
|
||||||
|
* @param string $payload Payload from get/cancle Code or funds call
|
||||||
|
* @return string Full POST service request code
|
||||||
|
*/
|
||||||
|
public function getCanonicalRequest(string $service_operation, string $payload): string
|
||||||
|
{
|
||||||
|
$HOST_HEADER = self::HOST_HEADER;
|
||||||
|
$X_AMZ_DATE_HEADER = self::X_AMZ_DATE_HEADER;
|
||||||
|
$X_AMZ_TARGET_HEADER = self::X_AMZ_TARGET_HEADER;
|
||||||
|
$ACCEPT_HEADER = self::ACCEPT_HEADER;
|
||||||
|
$payload_hash = $this->buildHash($payload);
|
||||||
|
$canonical_headers = $this->buildCanonicalHeaders($service_operation);
|
||||||
|
$canonical_request = "POST\n"
|
||||||
|
. "/$service_operation\n\n"
|
||||||
|
. "$canonical_headers\n\n"
|
||||||
|
. "$ACCEPT_HEADER;$HOST_HEADER;$X_AMZ_DATE_HEADER;$X_AMZ_TARGET_HEADER\n"
|
||||||
|
. "$payload_hash";
|
||||||
|
return $canonical_request;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build sha256 hash from given data
|
||||||
|
*
|
||||||
|
* @param string $data Data to be hashed with sha256
|
||||||
|
* @return string sha256 hash
|
||||||
|
*/
|
||||||
|
public function buildHash(string $data): string
|
||||||
|
{
|
||||||
|
return hash('sha256', $data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a sha256 based Hash-Based Message Authentication Code
|
||||||
|
* with the given key and data
|
||||||
|
*
|
||||||
|
* @param string $data Data to be hashed with key below
|
||||||
|
* @param string $key Key to be used for creating the hash
|
||||||
|
* @param bool $raw Returning data as ascii string or hexibits
|
||||||
|
* @return string Hash-Based Message Authentication Code
|
||||||
|
*/
|
||||||
|
public function hmac(string $data, string $key, bool $raw = true): string
|
||||||
|
{
|
||||||
|
return hash_hmac('sha256', $data, $key, $raw);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build timestamp in the format used by AWS services
|
||||||
|
* eg 20211009\T102030\Z
|
||||||
|
*
|
||||||
|
* @return string date string based on current time. Ymd\THis\Z
|
||||||
|
*/
|
||||||
|
public function getTimestamp()
|
||||||
|
{
|
||||||
|
return gmdate('Ymd\THis\Z');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get only the date string from the getTimestamp
|
||||||
|
* eg 20211009
|
||||||
|
*
|
||||||
|
* @return string Date string YYYYmmdd extracted from getTimestamp()
|
||||||
|
*/
|
||||||
|
public function getDateString()
|
||||||
|
{
|
||||||
|
return substr($this->getTimestamp(), 0, 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fixed content type for submission, is json
|
||||||
|
*
|
||||||
|
* @return string 'application/json' string
|
||||||
|
*/
|
||||||
|
public function getContentType(): string
|
||||||
|
{
|
||||||
|
return 'application/json';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// __END__
|
||||||
@@ -1,427 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Amazon\AWS;
|
|
||||||
|
|
||||||
use Amazon\Client\Client;
|
|
||||||
use Amazon\Config\Config;
|
|
||||||
use Amazon\Exceptions\AmazonErrors;
|
|
||||||
use Amazon\Debug\AmazonDebug;
|
|
||||||
use Amazon\Response\CancelResponse;
|
|
||||||
use Amazon\Response\CreateBalanceResponse;
|
|
||||||
use Amazon\Response\CreateResponse;
|
|
||||||
|
|
||||||
class AWS
|
|
||||||
{
|
|
||||||
public const SERVICE_NAME = 'AGCODService';
|
|
||||||
public const ACCEPT_HEADER = 'accept';
|
|
||||||
public const CONTENT_HEADER = 'content-type';
|
|
||||||
public const HOST_HEADER = 'host';
|
|
||||||
public const X_AMZ_DATE_HEADER = 'x-amz-date';
|
|
||||||
public const X_AMZ_TARGET_HEADER = 'x-amz-target';
|
|
||||||
public const AUTHORIZATION_HEADER = 'Authorization';
|
|
||||||
public const AWS_SHA256_ALGORITHM = 'AWS4-HMAC-SHA256';
|
|
||||||
public const KEY_QUALIFIER = 'AWS4';
|
|
||||||
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;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Config $config
|
|
||||||
*/
|
|
||||||
public function __construct(Config $config)
|
|
||||||
{
|
|
||||||
$this->config = $config;
|
|
||||||
AmazonDebug::writeLog([__METHOD__ => date('Y-m-d H:m:s.u')]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param float $amount
|
|
||||||
* @param string|null $creation_id
|
|
||||||
* @return CreateResponse
|
|
||||||
*
|
|
||||||
* @throws AmazonErrors
|
|
||||||
*/
|
|
||||||
public function getCode(float $amount, ?string $creation_id = null): CreateResponse
|
|
||||||
{
|
|
||||||
$service_operation = self::CREATE_GIFT_CARD_SERVICE;
|
|
||||||
$payload = $this->getGiftCardPayload($amount, $creation_id);
|
|
||||||
$canonical_request = $this->getCanonicalRequest($service_operation, $payload);
|
|
||||||
$date_time_string = $this->getTimestamp();
|
|
||||||
AmazonDebug::writeLog(['call' => __METHOD__]);
|
|
||||||
$result = json_decode($this->makeRequest(
|
|
||||||
$payload,
|
|
||||||
$canonical_request,
|
|
||||||
$service_operation,
|
|
||||||
$date_time_string
|
|
||||||
), true);
|
|
||||||
return new CreateResponse($result);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $creation_request_id
|
|
||||||
* @param string $gift_card_id
|
|
||||||
* @return CancelResponse
|
|
||||||
*
|
|
||||||
* @throws AmazonErrors
|
|
||||||
*/
|
|
||||||
public function cancelCode(string $creation_request_id, string $gift_card_id): CancelResponse
|
|
||||||
{
|
|
||||||
$service_operation = self::CANCEL_GIFT_CARD_SERVICE;
|
|
||||||
$payload = $this->getCancelGiftCardPayload($creation_request_id, $gift_card_id);
|
|
||||||
$canonical_request = $this->getCanonicalRequest($service_operation, $payload);
|
|
||||||
$date_time_string = $this->getTimestamp();
|
|
||||||
AmazonDebug::writeLog(['call' => __METHOD__]);
|
|
||||||
$result = json_decode($this->makeRequest(
|
|
||||||
$payload,
|
|
||||||
$canonical_request,
|
|
||||||
$service_operation,
|
|
||||||
$date_time_string
|
|
||||||
), true);
|
|
||||||
return new CancelResponse($result);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return CreateBalanceResponse
|
|
||||||
*
|
|
||||||
* @throws AmazonErrors
|
|
||||||
*/
|
|
||||||
public function getBalance(): CreateBalanceResponse
|
|
||||||
{
|
|
||||||
$service_operation = self::GET_AVAILABLE_FUNDS_SERVICE;
|
|
||||||
$payload = $this->getAvailableFundsPayload();
|
|
||||||
$canonical_request = $this->getCanonicalRequest($service_operation, $payload);
|
|
||||||
$date_time_string = $this->getTimestamp();
|
|
||||||
AmazonDebug::writeLog(['call' => __METHOD__]);
|
|
||||||
$result = json_decode($this->makeRequest(
|
|
||||||
$payload,
|
|
||||||
$canonical_request,
|
|
||||||
$service_operation,
|
|
||||||
$date_time_string
|
|
||||||
), true);
|
|
||||||
return new CreateBalanceResponse($result);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $payload
|
|
||||||
* @param string $canonical_request
|
|
||||||
* @param string $service_operation
|
|
||||||
* @param string $date_time_string
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function makeRequest(
|
|
||||||
string $payload,
|
|
||||||
string $canonical_request,
|
|
||||||
string $service_operation,
|
|
||||||
string $date_time_string
|
|
||||||
): string {
|
|
||||||
// debug
|
|
||||||
AmazonDebug::writeLog([__METHOD__ => [
|
|
||||||
'Operation' => $service_operation,
|
|
||||||
'Payload' => $payload,
|
|
||||||
'Cannonical Request' => $canonical_request,
|
|
||||||
'Date Time String' => $date_time_string
|
|
||||||
|
|
||||||
]]);
|
|
||||||
$KEY_QUALIFIER = self::KEY_QUALIFIER;
|
|
||||||
$canonical_request_hash = $this->buildHash($canonical_request);
|
|
||||||
$string_to_sign = $this->buildStringToSign($canonical_request_hash);
|
|
||||||
$authorization_value = $this->buildAuthSignature($string_to_sign);
|
|
||||||
|
|
||||||
$secret_key = $this->config->getSecret();
|
|
||||||
$endpoint = $this->config->getEndpoint();
|
|
||||||
$region_name = $this->getRegion();
|
|
||||||
|
|
||||||
$SERVICE_NAME = 'AGCODService';
|
|
||||||
$service_target = 'com.amazonaws.agcod.' . $SERVICE_NAME . '.' . $service_operation;
|
|
||||||
$date_string = $this->getDateString();
|
|
||||||
|
|
||||||
$signature_aws_key = $KEY_QUALIFIER . $secret_key;
|
|
||||||
|
|
||||||
$k_date = $this->hmac($date_string, $signature_aws_key);
|
|
||||||
$k_date_hexis = $this->hmac($date_string, $signature_aws_key, false);
|
|
||||||
$k_region = $this->hmac($region_name, $k_date);
|
|
||||||
$k_region_hexis = $this->hmac($region_name, $k_date, false);
|
|
||||||
$k_service_hexis = $this->hmac($SERVICE_NAME, $k_region, false);
|
|
||||||
|
|
||||||
AmazonDebug::writeLog([__METHOD__ => [
|
|
||||||
'Date' => $k_date_hexis,
|
|
||||||
'Region' => $k_region_hexis,
|
|
||||||
'Service' => $k_service_hexis,
|
|
||||||
]]);
|
|
||||||
|
|
||||||
$url = 'https://' . (string)$endpoint . '/' . $service_operation;
|
|
||||||
$headers = $this->buildHeaders($payload, $authorization_value, $date_time_string, $service_target);
|
|
||||||
return (new Client())->request($url, $headers, $payload);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $payload
|
|
||||||
* @param string $authorization_value
|
|
||||||
* @param string $date_time_string
|
|
||||||
* @param string $service_target
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function buildHeaders(
|
|
||||||
string $payload,
|
|
||||||
string $authorization_value,
|
|
||||||
string $date_time_string,
|
|
||||||
string $service_target
|
|
||||||
): array {
|
|
||||||
$ACCEPT_HEADER = self::ACCEPT_HEADER;
|
|
||||||
$X_AMZ_DATE_HEADER = self::X_AMZ_DATE_HEADER;
|
|
||||||
$X_AMZ_TARGET_HEADER = self::X_AMZ_TARGET_HEADER;
|
|
||||||
$AUTHORIZATION_HEADER = self::AUTHORIZATION_HEADER;
|
|
||||||
return [
|
|
||||||
'Content-Type:' . $this->getContentType(),
|
|
||||||
'Content-Length: ' . strlen($payload),
|
|
||||||
$AUTHORIZATION_HEADER . ':' . $authorization_value,
|
|
||||||
$X_AMZ_DATE_HEADER . ':' . $date_time_string,
|
|
||||||
$X_AMZ_TARGET_HEADER . ':' . $service_target,
|
|
||||||
$ACCEPT_HEADER . ':' . $this->getContentType()
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $string_to_sign
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function buildAuthSignature(string $string_to_sign): string
|
|
||||||
{
|
|
||||||
$AWS_SHA256_ALGORITHM = self::AWS_SHA256_ALGORITHM;
|
|
||||||
$SERVICE_NAME = self::SERVICE_NAME;
|
|
||||||
$TERMINATION_STRING = self::TERMINATION_STRING;
|
|
||||||
$ACCEPT_HEADER = self::ACCEPT_HEADER;
|
|
||||||
$HOST_HEADER = self::HOST_HEADER;
|
|
||||||
$X_AMZ_DATE_HEADER = self::X_AMZ_DATE_HEADER;
|
|
||||||
$X_AMZ_TARGET_HEADER = self::X_AMZ_TARGET_HEADER;
|
|
||||||
|
|
||||||
$aws_key_id = $this->config->getAccessKey();
|
|
||||||
$region_name = $this->getRegion();
|
|
||||||
|
|
||||||
$date_string = $this->getDateString();
|
|
||||||
$derived_key = $this->buildDerivedKey();
|
|
||||||
// Calculate signature per http://docs.aws.amazon.com/general/latest/gr/sigv4-calculate-signature.html
|
|
||||||
$final_signature = $this->hmac($string_to_sign, $derived_key, false);
|
|
||||||
|
|
||||||
// Assemble Authorization Header with signing information
|
|
||||||
// per http://docs.aws.amazon.com/general/latest/gr/sigv4-add-signature-to-request.html
|
|
||||||
$authorization_value =
|
|
||||||
$AWS_SHA256_ALGORITHM
|
|
||||||
. ' Credential=' . $aws_key_id
|
|
||||||
. '/' . $date_string . '/' . $region_name . '/' . $SERVICE_NAME . '/' . $TERMINATION_STRING . ','
|
|
||||||
. ' SignedHeaders='
|
|
||||||
. $ACCEPT_HEADER . ';' . $HOST_HEADER . ';' . $X_AMZ_DATE_HEADER . ';' . $X_AMZ_TARGET_HEADER . ','
|
|
||||||
. ' Signature='
|
|
||||||
. $final_signature;
|
|
||||||
|
|
||||||
return $authorization_value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $canonical_request_hash
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function buildStringToSign($canonical_request_hash): string
|
|
||||||
{
|
|
||||||
$AWS_SHA256_ALGORITHM = self::AWS_SHA256_ALGORITHM;
|
|
||||||
$TERMINATION_STRING = self::TERMINATION_STRING;
|
|
||||||
$SERVICE_NAME = self::SERVICE_NAME;
|
|
||||||
$region_name = $this->getRegion();
|
|
||||||
$date_time_string = $this->getTimestamp();
|
|
||||||
$date_string = $this->getDateString();
|
|
||||||
$string_to_sign = "$AWS_SHA256_ALGORITHM\n"
|
|
||||||
. "$date_time_string\n"
|
|
||||||
. "$date_string/$region_name/$SERVICE_NAME/$TERMINATION_STRING\n"
|
|
||||||
. "$canonical_request_hash";
|
|
||||||
|
|
||||||
return $string_to_sign;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param bool $rawOutput
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function buildDerivedKey(bool $rawOutput = true): string
|
|
||||||
{
|
|
||||||
$KEY_QUALIFIER = self::KEY_QUALIFIER;
|
|
||||||
$TERMINATION_STRING = self::TERMINATION_STRING;
|
|
||||||
$SERVICE_NAME = self::SERVICE_NAME;
|
|
||||||
|
|
||||||
$aws_secret_key = $this->config->getSecret();
|
|
||||||
// Append Key Qualifier, "AWS4", to secret key per
|
|
||||||
// shttp://docs.aws.amazon.com/general/latest/gr/signature-v4-examples.html
|
|
||||||
$signature_aws_key = $KEY_QUALIFIER . $aws_secret_key;
|
|
||||||
$region_name = $this->getRegion();
|
|
||||||
$date_string = $this->getDateString();
|
|
||||||
|
|
||||||
$k_date = $this->hmac($date_string, $signature_aws_key);
|
|
||||||
$k_region = $this->hmac($region_name, $k_date);
|
|
||||||
$k_service = $this->hmac($SERVICE_NAME, $k_region);
|
|
||||||
|
|
||||||
// Derived the Signing key (derivedKey aka kSigning)
|
|
||||||
return $this->hmac($TERMINATION_STRING, $k_service, $rawOutput);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getRegion(): string
|
|
||||||
{
|
|
||||||
$endpoint = $this->config->getEndpoint();
|
|
||||||
// default region
|
|
||||||
$region_name = 'us-east-1';
|
|
||||||
|
|
||||||
switch ($endpoint) {
|
|
||||||
case 'agcod-v2.amazon.com':
|
|
||||||
case 'agcod-v2-gamma.amazon.com':
|
|
||||||
$region_name = 'us-east-1';
|
|
||||||
break;
|
|
||||||
case 'agcod-v2-eu.amazon.com':
|
|
||||||
case 'agcod-v2-eu-gamma.amazon.com':
|
|
||||||
$region_name = 'us-west-1';
|
|
||||||
break;
|
|
||||||
case 'agcod-v2-fe.amazon.com':
|
|
||||||
case 'agcod-v2-fe-gamma.amazon.com':
|
|
||||||
$region_name = 'us-west-2';
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return $region_name;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param float $amount
|
|
||||||
* @param string $creation_id
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getGiftCardPayload(float $amount, ?string $creation_id = null): string
|
|
||||||
{
|
|
||||||
$amount = trim($amount);
|
|
||||||
$payload = [
|
|
||||||
'creationRequestId' => $creation_id ?: uniqid($this->config->getPartner() . '_'),
|
|
||||||
'partnerId' => $this->config->getPartner(),
|
|
||||||
'value' =>
|
|
||||||
[
|
|
||||||
'currencyCode' => $this->config->getCurrency(),
|
|
||||||
'amount' => (float)$amount
|
|
||||||
]
|
|
||||||
];
|
|
||||||
return json_encode($payload);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $creation_request_id
|
|
||||||
* @param string $gift_card_id
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getCancelGiftCardPayload(string $creation_request_id, string $gift_card_id): string
|
|
||||||
{
|
|
||||||
$gift_card_response_id = trim($gift_card_id);
|
|
||||||
$payload = [
|
|
||||||
'creationRequestId' => $creation_request_id,
|
|
||||||
'partnerId' => $this->config->getPartner(),
|
|
||||||
'gcId' => $gift_card_response_id
|
|
||||||
];
|
|
||||||
return json_encode($payload);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getAvailableFundsPayload(): string
|
|
||||||
{
|
|
||||||
$payload = [
|
|
||||||
'partnerId' => $this->config->getPartner(),
|
|
||||||
];
|
|
||||||
return json_encode($payload);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $service_operation
|
|
||||||
* @param string $payload
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getCanonicalRequest(string $service_operation, string $payload): string
|
|
||||||
{
|
|
||||||
$HOST_HEADER = self::HOST_HEADER;
|
|
||||||
$X_AMZ_DATE_HEADER = self::X_AMZ_DATE_HEADER;
|
|
||||||
$X_AMZ_TARGET_HEADER = self::X_AMZ_TARGET_HEADER;
|
|
||||||
$ACCEPT_HEADER = self::ACCEPT_HEADER;
|
|
||||||
$payload_hash = $this->buildHash($payload);
|
|
||||||
$canonical_headers = $this->buildCanonicalHeaders($service_operation);
|
|
||||||
$canonical_request = "POST\n"
|
|
||||||
. "/$service_operation\n\n"
|
|
||||||
. "$canonical_headers\n\n"
|
|
||||||
. "$ACCEPT_HEADER;$HOST_HEADER;$X_AMZ_DATE_HEADER;$X_AMZ_TARGET_HEADER\n"
|
|
||||||
. "$payload_hash";
|
|
||||||
return $canonical_request;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $data
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function buildHash(string $data): string
|
|
||||||
{
|
|
||||||
return hash('sha256', $data);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return false|string
|
|
||||||
*/
|
|
||||||
public function getTimestamp()
|
|
||||||
{
|
|
||||||
return gmdate('Ymd\THis\Z');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $data
|
|
||||||
* @param string $key
|
|
||||||
* @param bool $raw
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function hmac(string $data, string $key, bool $raw = true): string
|
|
||||||
{
|
|
||||||
return hash_hmac('sha256', $data, $key, $raw);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool|string
|
|
||||||
*/
|
|
||||||
public function getDateString()
|
|
||||||
{
|
|
||||||
return substr($this->getTimestamp(), 0, 8);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getContentType(): string
|
|
||||||
{
|
|
||||||
return 'application/json';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $service_operation
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function buildCanonicalHeaders(string $service_operation): string
|
|
||||||
{
|
|
||||||
$ACCEPT_HEADER = self::ACCEPT_HEADER;
|
|
||||||
$HOST_HEADER = self::HOST_HEADER;
|
|
||||||
$X_AMZ_DATE_HEADER = self::X_AMZ_DATE_HEADER;
|
|
||||||
$X_AMZ_TARGET_HEADER = self::X_AMZ_TARGET_HEADER;
|
|
||||||
$date_time_string = $this->getTimestamp();
|
|
||||||
$endpoint = $this->config->getEndpoint();
|
|
||||||
$content_type = $this->getContentType();
|
|
||||||
return "$ACCEPT_HEADER:$content_type\n"
|
|
||||||
. "$HOST_HEADER:$endpoint\n"
|
|
||||||
. "$X_AMZ_DATE_HEADER:$date_time_string\n"
|
|
||||||
. "$X_AMZ_TARGET_HEADER:com.amazonaws.agcod.AGCODService.$service_operation";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// __END__
|
|
||||||
@@ -1,183 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Amazon Incentive Code
|
|
||||||
*
|
|
||||||
* # settings:
|
|
||||||
* aws endpoint (also sets region)
|
|
||||||
* aws key
|
|
||||||
* aws secret
|
|
||||||
* aws partner id
|
|
||||||
* money type (set as default, override in call)
|
|
||||||
* money value (set as default, override in call)
|
|
||||||
*
|
|
||||||
* # checks
|
|
||||||
* endpoint + region must match
|
|
||||||
*
|
|
||||||
* # calls:
|
|
||||||
* create gift card: CreateGiftCard
|
|
||||||
* cancel gift card: CancelGiftCard
|
|
||||||
*
|
|
||||||
* activate gift card: ActivateGiftCard
|
|
||||||
* deactivate gift card: DeactivateGiftCard
|
|
||||||
*
|
|
||||||
* gift card status: ActivationStatusCheck
|
|
||||||
*
|
|
||||||
* check available funds: GetAvailablefunds
|
|
||||||
*
|
|
||||||
* api server health check
|
|
||||||
*
|
|
||||||
* # sub classes
|
|
||||||
* config reader/checker
|
|
||||||
* API v4 encrypter
|
|
||||||
* submitter/data getter
|
|
||||||
* error handler/retry
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace Amazon;
|
|
||||||
|
|
||||||
use Amazon\AWS\AWS;
|
|
||||||
use Amazon\Config\Config;
|
|
||||||
use Amazon\Exceptions\AmazonErrors;
|
|
||||||
use Amazon\Debug\AmazonDebug;
|
|
||||||
|
|
||||||
class AmazonIncentives
|
|
||||||
{
|
|
||||||
private $config;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* AmazonGiftCode constructor.
|
|
||||||
*
|
|
||||||
* @param string|null $key
|
|
||||||
* @param string|null $secret
|
|
||||||
* @param string|null $partner
|
|
||||||
* @param string|null $endpoint
|
|
||||||
* @param string|null $currency
|
|
||||||
* @param bool|null $debug
|
|
||||||
*/
|
|
||||||
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);
|
|
||||||
// init debug
|
|
||||||
AmazonDebug::setDebug($this->config->getDebug());
|
|
||||||
}
|
|
||||||
|
|
||||||
// *********************************************************************
|
|
||||||
// PRIVATE HELPER METHODS
|
|
||||||
// *********************************************************************
|
|
||||||
|
|
||||||
// *********************************************************************
|
|
||||||
// PUBLIC DEBUG METHODS
|
|
||||||
// *********************************************************************
|
|
||||||
|
|
||||||
// like log collector (array) and returner
|
|
||||||
|
|
||||||
// *********************************************************************
|
|
||||||
// PUBLIC METHODS
|
|
||||||
// *********************************************************************
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param float $value
|
|
||||||
* @param string $creation_request_id AWS creationRequestId
|
|
||||||
* @return Response\CreateResponse
|
|
||||||
*
|
|
||||||
* @throws AmazonErrors
|
|
||||||
*/
|
|
||||||
public function buyGiftCard(float $value, string $creation_request_id = null): Response\CreateResponse
|
|
||||||
{
|
|
||||||
return (new AWS($this->config))->getCode($value, $creation_request_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $creation_request_id AWS creationRequestId
|
|
||||||
* @param string $gift_card_id AWS gcId
|
|
||||||
* @return Response\CancelResponse
|
|
||||||
*/
|
|
||||||
public function cancelGiftCard(string $creation_request_id, string $gift_card_id): Response\CancelResponse
|
|
||||||
{
|
|
||||||
return (new AWS($this->config))->cancelCode($creation_request_id, $gift_card_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return Response\CreateBalanceResponse
|
|
||||||
*
|
|
||||||
* @throws AmazonErrors
|
|
||||||
*/
|
|
||||||
public function getAvailableFunds(): Response\CreateBalanceResponse
|
|
||||||
{
|
|
||||||
return (new AWS($this->config))->getBalance();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* AmazonGiftCode make own client.
|
|
||||||
*
|
|
||||||
* @param string|null $key
|
|
||||||
* @param string|null $secret
|
|
||||||
* @param string|null $partner
|
|
||||||
* @param string|null $endpoint
|
|
||||||
* @param string|null $currency
|
|
||||||
* @param bool|null $debug
|
|
||||||
* @return AmazonGiftCode
|
|
||||||
*/
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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
|
|
||||||
* @return array Decoded with code, type, message fields
|
|
||||||
*/
|
|
||||||
public static function decodeExceptionMessage(string $message): array
|
|
||||||
{
|
|
||||||
$message_ar = json_decode($message, true);
|
|
||||||
// if we have an error, build empty block and only fill message
|
|
||||||
if (json_last_error()) {
|
|
||||||
$message_ar = [
|
|
||||||
'status' => '',
|
|
||||||
'code' => '',
|
|
||||||
'type' => '',
|
|
||||||
'message' => $message,
|
|
||||||
'log_id' => '',
|
|
||||||
'log' => []
|
|
||||||
];
|
|
||||||
}
|
|
||||||
return $message_ar;
|
|
||||||
}
|
|
||||||
|
|
||||||
// *********************************************************************
|
|
||||||
// PUBLIC TEST METHODS
|
|
||||||
// *********************************************************************
|
|
||||||
|
|
||||||
public function checkMe(): array
|
|
||||||
{
|
|
||||||
$data = [];
|
|
||||||
|
|
||||||
$data['ENV'] = $_ENV;
|
|
||||||
$data['CONFIG'] = $this->config;
|
|
||||||
$data['KEY'] = $this->config->getAccessKey();
|
|
||||||
|
|
||||||
return $data;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// __END__
|
|
||||||
@@ -1,108 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Amazon\Client;
|
|
||||||
|
|
||||||
use Amazon\Exceptions\AmazonErrors;
|
|
||||||
use Amazon\Debug\AmazonDebug;
|
|
||||||
|
|
||||||
class Client implements ClientInterface
|
|
||||||
{
|
|
||||||
// instead of JsonResponse::HTTP_OK
|
|
||||||
private const HTTP_OK = 200;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param string $url The URL being requested, including domain and protocol
|
|
||||||
* @param array $headers Headers to be used in the request
|
|
||||||
* @param array|string $params Can be nested for arrays and hashes
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @return String
|
|
||||||
*/
|
|
||||||
public function request(string $url, array $headers, $params): string
|
|
||||||
{
|
|
||||||
$handle = curl_init($url);
|
|
||||||
curl_setopt($handle, CURLOPT_POST, true);
|
|
||||||
curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
|
|
||||||
// curl_setopt($handle, CURLOPT_FAILONERROR, true);
|
|
||||||
curl_setopt($handle, CURLOPT_POSTFIELDS, $params);
|
|
||||||
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
|
|
||||||
$result = curl_exec($handle);
|
|
||||||
|
|
||||||
if ($result === false) {
|
|
||||||
$err = curl_errno($handle);
|
|
||||||
$message = curl_error($handle);
|
|
||||||
$this->handleCurlError($url, $err, $message);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (curl_getinfo($handle, CURLINFO_HTTP_CODE) !== self::HTTP_OK) {
|
|
||||||
$err = curl_errno($handle);
|
|
||||||
AmazonDebug::writeLog(['CURL_REQUEST_RESULT' => $result]);
|
|
||||||
// extract all the error codes from Amazon
|
|
||||||
$result_ar = json_decode($result, true);
|
|
||||||
// if message is 'Rate exceeded', set different error
|
|
||||||
if (($result_ar['message'] ?? '') == 'Rate exceeded') {
|
|
||||||
$error_status = 'RESEND';
|
|
||||||
$error_code = 'T001';
|
|
||||||
$error_type = 'RateExceeded';
|
|
||||||
$message = $result_ar['message'];
|
|
||||||
} else {
|
|
||||||
// for all other error messages
|
|
||||||
$error_status = $result_ar['agcodResponse']['status'] ?? 'FAILURE';
|
|
||||||
$error_code = $result_ar['errorCode'] ?? 'E999';
|
|
||||||
$error_type = $result_ar['errorType'] ?? 'OtherUnknownError';
|
|
||||||
$message = $result_ar['message'] ?? 'Unknown error occured';
|
|
||||||
}
|
|
||||||
// throw Error here with all codes
|
|
||||||
throw AmazonErrors::getError(
|
|
||||||
$error_status,
|
|
||||||
$error_code,
|
|
||||||
$error_type,
|
|
||||||
$message,
|
|
||||||
$err
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Undocumented function
|
|
||||||
*
|
|
||||||
* @param string $url
|
|
||||||
* @param string $errno
|
|
||||||
* @param string $message
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
private function handleCurlError(string $url, int $errno, string $message): void
|
|
||||||
{
|
|
||||||
switch ($errno) {
|
|
||||||
case CURLE_COULDNT_CONNECT:
|
|
||||||
case CURLE_COULDNT_RESOLVE_HOST:
|
|
||||||
case CURLE_OPERATION_TIMEOUTED:
|
|
||||||
$message = 'Could not connect to AWS (' . $url . '). Please check your '
|
|
||||||
. 'internet connection and try again. [' . $message . ']';
|
|
||||||
break;
|
|
||||||
case CURLE_SSL_CACERT:
|
|
||||||
case CURLE_SSL_PEER_CERTIFICATE:
|
|
||||||
$message = 'Could not verify AWS SSL certificate. Please make sure '
|
|
||||||
. 'that your network is not intercepting certificates. '
|
|
||||||
. '(Try going to ' . $url . 'in your browser.) '
|
|
||||||
. '[' . $message . ']';
|
|
||||||
break;
|
|
||||||
case 0:
|
|
||||||
default:
|
|
||||||
$message = 'Unexpected error communicating with AWS: ' . $message;
|
|
||||||
}
|
|
||||||
|
|
||||||
// throw an error like in the normal reqeust, but set to CURL error
|
|
||||||
throw AmazonErrors::getError(
|
|
||||||
'FAILURE',
|
|
||||||
'C001',
|
|
||||||
'CurlError',
|
|
||||||
$message,
|
|
||||||
$errno
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// __END__
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Amazon\Client;
|
|
||||||
|
|
||||||
interface ClientInterface
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @param string $url The URL being requested, including domain and protocol
|
|
||||||
* @param array $headers Headers to be used in the request
|
|
||||||
* @param array|string $params Can be nested for arrays and hashes
|
|
||||||
*
|
|
||||||
* @return String
|
|
||||||
*/
|
|
||||||
|
|
||||||
public function request(string $url, array $headers, $params): string;
|
|
||||||
}
|
|
||||||
|
|
||||||
// __END__
|
|
||||||
@@ -1,199 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Amazon\Config;
|
|
||||||
|
|
||||||
class Config implements ConfigInterface
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
private $endpoint;
|
|
||||||
/**
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
private $access_key;
|
|
||||||
/**
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
private $secret_key;
|
|
||||||
/**
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
private $partner_id;
|
|
||||||
/**
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
private $currency;
|
|
||||||
/**
|
|
||||||
* @var bool
|
|
||||||
*/
|
|
||||||
private $debug;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string|null $key
|
|
||||||
* @param string|null $secret
|
|
||||||
* @param string|null $partner
|
|
||||||
* @param string|null $endpoint
|
|
||||||
* @param string|null $currency
|
|
||||||
*/
|
|
||||||
public function __construct(
|
|
||||||
?string $key,
|
|
||||||
?string $secret,
|
|
||||||
?string $partner,
|
|
||||||
?string $endpoint,
|
|
||||||
?string $currency,
|
|
||||||
?bool $debug,
|
|
||||||
) {
|
|
||||||
$this->setAccessKey($key ?: $this->parseEnv('AWS_GIFT_CARD_KEY'));
|
|
||||||
$this->setSecret($secret ?: $this->parseEnv('AWS_GIFT_CARD_SECRET'));
|
|
||||||
$this->setPartner($partner ?: $this->parseEnv('AWS_GIFT_CARD_PARTNER_ID'));
|
|
||||||
$this->setEndpoint($endpoint ?: $this->parseEnv('AWS_GIFT_CARD_ENDPOINT'));
|
|
||||||
$this->setCurrency($currency ?: $this->parseEnv('AWS_GIFT_CARD_CURRENCY'));
|
|
||||||
$this->setDebug($debug ?: $this->parseEnv('AWS_DEBUG'));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* string key to search, returns entry from _ENV
|
|
||||||
* if not matchin key, returns empty
|
|
||||||
*
|
|
||||||
* @param string $key To search in _ENV array
|
|
||||||
* @return string|bool Returns either string or true/false (DEBUG flag)
|
|
||||||
*/
|
|
||||||
private function parseEnv(string $key)
|
|
||||||
{
|
|
||||||
$return = '';
|
|
||||||
switch ($key) {
|
|
||||||
case 'AWS_DEBUG':
|
|
||||||
$return = !empty($_ENV['AWS_DEBUG']) ? true : false;
|
|
||||||
break;
|
|
||||||
case 'AWS_GIFT_CARD_KEY':
|
|
||||||
case 'AWS_GIFT_CARD_SECRET':
|
|
||||||
case 'AWS_GIFT_CARD_PARTNER_ID':
|
|
||||||
case 'AWS_GIFT_CARD_ENDPOINT':
|
|
||||||
case 'AWS_GIFT_CARD_CURRENCY':
|
|
||||||
$return = $_ENV[$key] ?? '';
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return $return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string|null
|
|
||||||
*/
|
|
||||||
public function getEndpoint(): ?string
|
|
||||||
{
|
|
||||||
return $this->endpoint;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $endpoint
|
|
||||||
* @return ConfigInterface
|
|
||||||
*/
|
|
||||||
public function setEndpoint(string $endpoint): ConfigInterface
|
|
||||||
{
|
|
||||||
// TODO: check valid endpoint + set region
|
|
||||||
$this->endpoint = parse_url($endpoint, PHP_URL_HOST);
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string|null
|
|
||||||
*/
|
|
||||||
public function getAccessKey(): ?string
|
|
||||||
{
|
|
||||||
return $this->access_key;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $key
|
|
||||||
* @return ConfigInterface
|
|
||||||
*/
|
|
||||||
public function setAccessKey(string $key): ConfigInterface
|
|
||||||
{
|
|
||||||
$this->access_key = $key;
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string|null
|
|
||||||
*/
|
|
||||||
public function getSecret(): ?string
|
|
||||||
{
|
|
||||||
return $this->secret_key;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $secret
|
|
||||||
* @return ConfigInterface
|
|
||||||
*/
|
|
||||||
public function setSecret(string $secret): ConfigInterface
|
|
||||||
{
|
|
||||||
$this->secret_key = $secret;
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string|null
|
|
||||||
*/
|
|
||||||
public function getCurrency(): ?string
|
|
||||||
{
|
|
||||||
return $this->currency;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $currency
|
|
||||||
* @return ConfigInterface
|
|
||||||
*/
|
|
||||||
public function setCurrency(string $currency): ConfigInterface
|
|
||||||
{
|
|
||||||
// TODO: check currency valid + currenc valid for region
|
|
||||||
$this->currency = $currency;
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string|null
|
|
||||||
*/
|
|
||||||
public function getPartner(): ?string
|
|
||||||
{
|
|
||||||
return $this->partner_id;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $partner
|
|
||||||
* @return ConfigInterface
|
|
||||||
*/
|
|
||||||
public function setPartner(string $partner): ConfigInterface
|
|
||||||
{
|
|
||||||
$this->partner_id = $partner;
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool|null
|
|
||||||
*/
|
|
||||||
public function getDebug(): ?bool
|
|
||||||
{
|
|
||||||
return $this->debug;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param bool $debug
|
|
||||||
* @return ConfigInterface
|
|
||||||
*/
|
|
||||||
public function setDebug(bool $debug): ConfigInterface
|
|
||||||
{
|
|
||||||
$this->debug = $debug;
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// __END__
|
|
||||||
@@ -1,74 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Amazon\Config;
|
|
||||||
|
|
||||||
interface ConfigInterface
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @return string|null
|
|
||||||
*/
|
|
||||||
public function getEndpoint(): ?string;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $endpoint
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function setEndpoint(string $endpoint): ConfigInterface;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string|null
|
|
||||||
*/
|
|
||||||
public function getAccessKey(): ?string;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $key
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function setAccessKey(string $key): ConfigInterface;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string|null
|
|
||||||
*/
|
|
||||||
public function getSecret(): ?string;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $secret
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function setSecret(string $secret): ConfigInterface;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string|null
|
|
||||||
*/
|
|
||||||
public function getCurrency(): ?string;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $currency
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function setCurrency(string $currency): ConfigInterface;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string|null
|
|
||||||
*/
|
|
||||||
public function getPartner(): ?string;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $partner
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function setPartner(string $partner): ConfigInterface;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool|null
|
|
||||||
*/
|
|
||||||
public function getDebug(): ?bool;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param bool $debug
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function setDebug(bool $debug): ConfigInterface;
|
|
||||||
}
|
|
||||||
|
|
||||||
// __END__
|
|
||||||
@@ -1,106 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
|
|
||||||
class AmazonDebug
|
|
||||||
{
|
|
||||||
private static $log = [];
|
|
||||||
private static $debug = false;
|
|
||||||
private static $id = null;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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
|
|
||||||
*
|
|
||||||
* @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) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if ($id === null) {
|
|
||||||
$id = uniqid();
|
|
||||||
}
|
|
||||||
self::$id = $id;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* write a log entry
|
|
||||||
* 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
|
|
||||||
{
|
|
||||||
if (self::$debug === false) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
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
|
|
||||||
{
|
|
||||||
if ($id === null) {
|
|
||||||
return self::$log;
|
|
||||||
} else {
|
|
||||||
return self::$log[$id] ?? [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// __END__
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Amazon\Exceptions;
|
|
||||||
|
|
||||||
use RuntimeException;
|
|
||||||
|
|
||||||
class AmazonErrors extends RuntimeException
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @param string $error_status agcodResponse->status from Amazon
|
|
||||||
* @param string $error_code errorCode from Amazon
|
|
||||||
* @param string $error_type errorType from Amazon
|
|
||||||
* @param string $message
|
|
||||||
* @param string $_error_code
|
|
||||||
* @return AmazonErrors
|
|
||||||
*/
|
|
||||||
public static function getError(
|
|
||||||
string $error_status,
|
|
||||||
string $error_code,
|
|
||||||
string $error_type,
|
|
||||||
string $message,
|
|
||||||
string $_error_code
|
|
||||||
): self {
|
|
||||||
// NOTE: if xdebug.show_exception_trace is set to 1 this will print ERRORS
|
|
||||||
return new static(
|
|
||||||
json_encode([
|
|
||||||
'status' => $error_status,
|
|
||||||
'code' => $error_code,
|
|
||||||
'type' => $error_type,
|
|
||||||
'message' => $message,
|
|
||||||
// atach log data if exists
|
|
||||||
'log_id' => \Amazon\Debug\AmazonDebug::getId(),
|
|
||||||
'log' => \Amazon\Debug\AmazonDebug::getLog(),
|
|
||||||
]),
|
|
||||||
$_error_code
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// __END__
|
|
||||||
@@ -1,111 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Amazon\Response;
|
|
||||||
|
|
||||||
class CancelResponse
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Amazon Gift Card gcId.
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected $id;
|
|
||||||
/**
|
|
||||||
* Amazon Gift Card creationRequestId
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected $creation_request_id;
|
|
||||||
/**
|
|
||||||
* Amazon Gift Card status
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected $status;
|
|
||||||
/**
|
|
||||||
* Amazon Gift Card Raw JSON
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected $raw_json;
|
|
||||||
/**
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected $log;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Response constructor.
|
|
||||||
* @param array $json_response
|
|
||||||
*/
|
|
||||||
public function __construct(array $json_response)
|
|
||||||
{
|
|
||||||
$this->raw_json = $json_response;
|
|
||||||
$this->log = \Amazon\Debug\AmazonDebug::getLog(\Amazon\Debug\AmazonDebug::getId());
|
|
||||||
$this->parseJsonResponse($json_response);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getLog(): array
|
|
||||||
{
|
|
||||||
return $this->log;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getId(): string
|
|
||||||
{
|
|
||||||
return $this->id;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getCreationRequestId(): string
|
|
||||||
{
|
|
||||||
return $this->creation_request_id;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getStatus(): string
|
|
||||||
{
|
|
||||||
return $this->status;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getRawJson(): string
|
|
||||||
{
|
|
||||||
return json_encode($this->raw_json);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $json_response
|
|
||||||
* @return CancelResponse
|
|
||||||
*/
|
|
||||||
public function parseJsonResponse(array $json_response): self
|
|
||||||
{
|
|
||||||
if (!is_array($json_response)) {
|
|
||||||
throw new \RuntimeException('Response must be a scalar value');
|
|
||||||
}
|
|
||||||
if (array_key_exists('gcId', $json_response)) {
|
|
||||||
$this->id = $json_response['gcId'];
|
|
||||||
}
|
|
||||||
if (array_key_exists('creationRequestId', $json_response)) {
|
|
||||||
$this->creation_request_id = $json_response['creationRequestId'];
|
|
||||||
}
|
|
||||||
// SUCCESS, FAILURE, RESEND
|
|
||||||
if (array_key_exists('status', $json_response)) {
|
|
||||||
$this->status = $json_response['status'];
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// __END__
|
|
||||||
@@ -1,129 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Amazon\Response;
|
|
||||||
|
|
||||||
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;
|
|
||||||
/**
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected $log;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Response constructor.
|
|
||||||
*
|
|
||||||
* @param array $json_response
|
|
||||||
*/
|
|
||||||
public function __construct(array $json_response)
|
|
||||||
{
|
|
||||||
$this->raw_json = $json_response;
|
|
||||||
$this->log = \Amazon\Debug\AmazonDebug::getLog(\Amazon\Debug\AmazonDebug::getId());
|
|
||||||
$this->parseJsonResponse($json_response);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getLog(): array
|
|
||||||
{
|
|
||||||
return $this->log;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @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);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Undocumented function
|
|
||||||
*
|
|
||||||
* @param array $json_response
|
|
||||||
* @return CreateBalanceResponse
|
|
||||||
*/
|
|
||||||
public function parseJsonResponse(array $json_response): self
|
|
||||||
{
|
|
||||||
if (!is_array($json_response)) {
|
|
||||||
throw new \RuntimeException('Response must be a scalar value');
|
|
||||||
}
|
|
||||||
if (array_key_exists('amount', $json_response['availableFunds'])) {
|
|
||||||
$this->amount = $json_response['availableFunds']['amount'];
|
|
||||||
}
|
|
||||||
if (array_key_exists('currencyCode', $json_response['availableFunds'])) {
|
|
||||||
$this->currency = $json_response['availableFunds']['currencyCode'];
|
|
||||||
}
|
|
||||||
// SUCCESS, FAILURE, RESEND
|
|
||||||
if (array_key_exists('status', $json_response)) {
|
|
||||||
$this->status = $json_response['status'];
|
|
||||||
}
|
|
||||||
if (array_key_exists('timestamp', $json_response)) {
|
|
||||||
$this->timestamp = $json_response['timestamp'];
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,201 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Amazon\Response;
|
|
||||||
|
|
||||||
class CreateResponse
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Amazon Gift Card gcId.
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected $id;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Amazon Gift Card creationRequestId
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected $creation_request_id;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Amazon Gift Card gcClaimCode
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected $claim_code;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Amazon Gift Card amount
|
|
||||||
*
|
|
||||||
* @var float
|
|
||||||
*/
|
|
||||||
protected $value;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Amazon Gift Card currency
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected $currency;
|
|
||||||
/**
|
|
||||||
* Amazon Gift Card status
|
|
||||||
*
|
|
||||||
* @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
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected $raw_json;
|
|
||||||
/**
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected $log;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Response constructor.
|
|
||||||
* @param array $json_response
|
|
||||||
*/
|
|
||||||
public function __construct(array $json_response)
|
|
||||||
{
|
|
||||||
$this->raw_json = $json_response;
|
|
||||||
$this->log = \Amazon\Debug\AmazonDebug::getLog(\Amazon\Debug\AmazonDebug::getId());
|
|
||||||
$this->parseJsonResponse($json_response);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getLog(): array
|
|
||||||
{
|
|
||||||
return $this->log;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getId(): string
|
|
||||||
{
|
|
||||||
return $this->id;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getCreationRequestId(): string
|
|
||||||
{
|
|
||||||
return $this->creation_request_id;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getClaimCode(): string
|
|
||||||
{
|
|
||||||
return $this->claim_code;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getValue(): string
|
|
||||||
{
|
|
||||||
return $this->value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getCurrency(): string
|
|
||||||
{
|
|
||||||
return $this->currency;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
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
|
|
||||||
*/
|
|
||||||
public function getRawJson(): string
|
|
||||||
{
|
|
||||||
return json_encode($this->raw_json);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $json_response
|
|
||||||
* @return CreateResponse
|
|
||||||
*/
|
|
||||||
public function parseJsonResponse(array $json_response): self
|
|
||||||
{
|
|
||||||
if (!is_array($json_response)) {
|
|
||||||
throw new \RuntimeException('Response must be a scalar value');
|
|
||||||
}
|
|
||||||
if (array_key_exists('gcId', $json_response)) {
|
|
||||||
$this->id = $json_response['gcId'];
|
|
||||||
}
|
|
||||||
if (array_key_exists('creationRequestId', $json_response)) {
|
|
||||||
$this->creation_request_id = $json_response['creationRequestId'];
|
|
||||||
}
|
|
||||||
if (array_key_exists('gcClaimCode', $json_response)) {
|
|
||||||
$this->claim_code = $json_response['gcClaimCode'];
|
|
||||||
}
|
|
||||||
if (array_key_exists('amount', $json_response['cardInfo']['value'])) {
|
|
||||||
$this->value = $json_response['cardInfo']['value']['amount'];
|
|
||||||
}
|
|
||||||
if (array_key_exists('currencyCode', $json_response['cardInfo']['value'])) {
|
|
||||||
$this->currency = $json_response['cardInfo']['value']['currencyCode'];
|
|
||||||
}
|
|
||||||
if (array_key_exists('gcExpirationDate', $json_response)) {
|
|
||||||
$this->expiration_date = $json_response['gcExpirationDate'];
|
|
||||||
}
|
|
||||||
if (array_key_exists('cardStatus', $json_response['cardInfo'])) {
|
|
||||||
$this->card_status = $json_response['cardInfo']['cardStatus'];
|
|
||||||
}
|
|
||||||
// SUCCESS, FAILURE, RESEND
|
|
||||||
if (array_key_exists('status', $json_response)) {
|
|
||||||
$this->status = $json_response['status'];
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// __END__
|
|
||||||
171
src/AmazonIncentives.php
Normal file
171
src/AmazonIncentives.php
Normal file
@@ -0,0 +1,171 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Amazon Incentive Code
|
||||||
|
* Amazon Gift Code on Demand
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace gullevek\AmazonIncentives;
|
||||||
|
|
||||||
|
use gullevek\AmazonIncentives\AWS\AWS;
|
||||||
|
use gullevek\AmazonIncentives\Config\Config;
|
||||||
|
use gullevek\AmazonIncentives\Exceptions\AmazonErrors;
|
||||||
|
use gullevek\AmazonIncentives\Debug\AmazonDebug;
|
||||||
|
|
||||||
|
final class AmazonIncentives
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var Config
|
||||||
|
*/
|
||||||
|
private $config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AmazonGiftCode constructor.
|
||||||
|
*
|
||||||
|
* @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
|
||||||
|
*/
|
||||||
|
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);
|
||||||
|
// init debug
|
||||||
|
AmazonDebug::setDebug($this->config->getDebug());
|
||||||
|
}
|
||||||
|
|
||||||
|
// *********************************************************************
|
||||||
|
// PRIVATE HELPER METHODS
|
||||||
|
// *********************************************************************
|
||||||
|
|
||||||
|
// *********************************************************************
|
||||||
|
// PUBLIC METHODS
|
||||||
|
// *********************************************************************
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
*
|
||||||
|
* @throws AmazonErrors
|
||||||
|
*/
|
||||||
|
public function buyGiftCard(float $value, ?string $creation_request_id = null): Response\CreateResponse
|
||||||
|
{
|
||||||
|
return ($this->newAWS())->getCode($value, $creation_request_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
public function cancelGiftCard(string $creation_request_id, string $gift_card_id): Response\CancelResponse
|
||||||
|
{
|
||||||
|
return ($this->newAWS())->cancelCode($creation_request_id, $gift_card_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the current funds in this account
|
||||||
|
*
|
||||||
|
* @return Response\CreateBalanceResponse Returns the account funds object
|
||||||
|
*
|
||||||
|
* @throws AmazonErrors
|
||||||
|
*/
|
||||||
|
public function getAvailableFunds(): Response\CreateBalanceResponse
|
||||||
|
{
|
||||||
|
return ($this->newAWS())->getBalance();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AmazonIncentives creates own client and returns it as static object
|
||||||
|
*
|
||||||
|
* @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
|
||||||
|
*/
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
* @return array<mixed> Decoded with code, type, message fields
|
||||||
|
*
|
||||||
|
* @deprecated use \gullevek\AmazonIncentives\Exceptions\AmazonErrors::decodeExceptionMessage()
|
||||||
|
*/
|
||||||
|
public static function decodeExceptionMessage(string $message): array
|
||||||
|
{
|
||||||
|
return AmazonErrors::decodeExceptionMessage($message);
|
||||||
|
}
|
||||||
|
|
||||||
|
// *********************************************************************
|
||||||
|
// PUBLIC TEST METHODS
|
||||||
|
// *********************************************************************
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prints out ENV, CONFIG and KEY data
|
||||||
|
* This is for debug only, this will print out secrets.
|
||||||
|
* Use with care
|
||||||
|
*
|
||||||
|
* @return array<mixed>
|
||||||
|
*/
|
||||||
|
public function checkMe(): array
|
||||||
|
{
|
||||||
|
$data = [];
|
||||||
|
|
||||||
|
$data['ENV'] = $_ENV;
|
||||||
|
$data['CONFIG'] = $this->config;
|
||||||
|
$data['KEY'] = $this->config->getAccessKey();
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// __END__
|
||||||
127
src/Client/Client.php
Normal file
127
src/Client/Client.php
Normal file
@@ -0,0 +1,127 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace gullevek\AmazonIncentives\Client;
|
||||||
|
|
||||||
|
use gullevek\AmazonIncentives\Exceptions\AmazonErrors;
|
||||||
|
use gullevek\AmazonIncentives\Debug\AmazonDebug;
|
||||||
|
use gullevek\AmazonIncentives\Handle\Json;
|
||||||
|
|
||||||
|
class Client implements ClientInterface
|
||||||
|
{
|
||||||
|
/** @var int instead of JsonResponse::HTTP_OK */
|
||||||
|
private const HTTP_OK = 200;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Makes an request to the target url via curl
|
||||||
|
* Returns result as string (json)
|
||||||
|
*
|
||||||
|
* @param string $url The URL being requested,
|
||||||
|
* including domain and protocol
|
||||||
|
* @param array<int,string> $headers Headers to be used in the request
|
||||||
|
* @param array<mixed>|string $params Can be nested for arrays and hashes
|
||||||
|
* @return string Result as json string
|
||||||
|
*/
|
||||||
|
public function request(string $url, array $headers, $params): string
|
||||||
|
{
|
||||||
|
$handle = curl_init($url);
|
||||||
|
if ($handle === false) {
|
||||||
|
// throw Error here with all codes
|
||||||
|
throw AmazonErrors::getError(
|
||||||
|
'FAILURE',
|
||||||
|
'C001',
|
||||||
|
'CurlInitError',
|
||||||
|
'Failed to init curl with url: ' . $url,
|
||||||
|
0
|
||||||
|
);
|
||||||
|
}
|
||||||
|
curl_setopt($handle, CURLOPT_POST, true);
|
||||||
|
curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
|
||||||
|
// curl_setopt($handle, CURLOPT_FAILONERROR, true);
|
||||||
|
curl_setopt($handle, CURLOPT_POSTFIELDS, $params);
|
||||||
|
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
|
||||||
|
$result = curl_exec($handle);
|
||||||
|
|
||||||
|
if ($result === false) {
|
||||||
|
$err = curl_errno($handle);
|
||||||
|
$message = curl_error($handle);
|
||||||
|
$this->handleCurlError($url, $err, $message);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (curl_getinfo($handle, CURLINFO_HTTP_CODE) !== self::HTTP_OK) {
|
||||||
|
$err = curl_errno($handle);
|
||||||
|
AmazonDebug::writeLog(['CURL_REQUEST_RESULT' => $result]);
|
||||||
|
// extract all the error codes from Amazon
|
||||||
|
// note we do not care about result errors here, if json decode fails, just set to empty
|
||||||
|
try {
|
||||||
|
$result_ar = Json::jsonDecode((string)$result);
|
||||||
|
} catch (AmazonErrors $e) {
|
||||||
|
$result_ar = [];
|
||||||
|
}
|
||||||
|
// if message is 'Rate exceeded', set different error
|
||||||
|
if (($result_ar['message'] ?? '') == 'Rate exceeded') {
|
||||||
|
$error_status = 'RESEND';
|
||||||
|
$error_code = 'T001';
|
||||||
|
$error_type = 'RateExceeded';
|
||||||
|
$message = $result_ar['message'] ?? 'Rate exceeded';
|
||||||
|
} else {
|
||||||
|
// for all other error messages
|
||||||
|
$error_status = $result_ar['agcodResponse']['status'] ?? 'FAILURE';
|
||||||
|
$error_code = $result_ar['errorCode'] ?? 'E999';
|
||||||
|
$error_type = $result_ar['errorType'] ?? 'OtherUnknownError';
|
||||||
|
$message = $result_ar['message'] ?? 'Unknown error occured';
|
||||||
|
}
|
||||||
|
// throw Error here with all codes
|
||||||
|
throw AmazonErrors::getError(
|
||||||
|
$error_status,
|
||||||
|
$error_code,
|
||||||
|
$error_type,
|
||||||
|
$message,
|
||||||
|
$err
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return (string)$result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* handles any CURL errors and throws an error with the correct
|
||||||
|
* error message
|
||||||
|
*
|
||||||
|
* @param string $url The url that was originaly used
|
||||||
|
* @param int $errno Error number from curl handler
|
||||||
|
* @param string $message The error message string from curl
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
private function handleCurlError(string $url, int $errno, string $message): void
|
||||||
|
{
|
||||||
|
switch ($errno) {
|
||||||
|
case CURLE_COULDNT_CONNECT:
|
||||||
|
case CURLE_COULDNT_RESOLVE_HOST:
|
||||||
|
case CURLE_OPERATION_TIMEOUTED:
|
||||||
|
$message = 'Could not connect to AWS (' . $url . '). Please check your '
|
||||||
|
. 'internet connection and try again. [' . $message . ']';
|
||||||
|
break;
|
||||||
|
case CURLE_SSL_PEER_CERTIFICATE:
|
||||||
|
$message = 'Could not verify AWS SSL certificate. Please make sure '
|
||||||
|
. 'that your network is not intercepting certificates. '
|
||||||
|
. '(Try going to ' . $url . 'in your browser.) '
|
||||||
|
. '[' . $message . ']';
|
||||||
|
break;
|
||||||
|
case 0:
|
||||||
|
default:
|
||||||
|
$message = 'Unexpected error communicating with AWS: ' . $message;
|
||||||
|
}
|
||||||
|
|
||||||
|
// throw an error like in the normal reqeust, but set to CURL error
|
||||||
|
throw AmazonErrors::getError(
|
||||||
|
'FAILURE',
|
||||||
|
'C002',
|
||||||
|
'CurlError',
|
||||||
|
$message,
|
||||||
|
$errno
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// __END__
|
||||||
20
src/Client/ClientInterface.php
Normal file
20
src/Client/ClientInterface.php
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace gullevek\AmazonIncentives\Client;
|
||||||
|
|
||||||
|
interface ClientInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param string $url The URL being requested,
|
||||||
|
* including domain and protocol
|
||||||
|
* @param array<mixed> $headers Headers to be used in the request
|
||||||
|
* @param array<mixed>|string $params Can be nested for arrays and hashes
|
||||||
|
*
|
||||||
|
* @return String
|
||||||
|
*/
|
||||||
|
public function request(string $url, array $headers, $params): string;
|
||||||
|
}
|
||||||
|
|
||||||
|
// __END__
|
||||||
216
src/Config/Config.php
Normal file
216
src/Config/Config.php
Normal file
@@ -0,0 +1,216 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace gullevek\AmazonIncentives\Config;
|
||||||
|
|
||||||
|
class Config implements ConfigInterface
|
||||||
|
{
|
||||||
|
/** @var string Endpoint URL without https:// */
|
||||||
|
private $endpoint = '';
|
||||||
|
/** @var string Access Key */
|
||||||
|
private $access_key = '';
|
||||||
|
/** @var string Secret Key */
|
||||||
|
private $secret_key = '';
|
||||||
|
/** @var string Partner ID */
|
||||||
|
private $partner_id = '';
|
||||||
|
/** @var string Currency type as USD, JPY, etc */
|
||||||
|
private $currency = '';
|
||||||
|
/** @var bool Debug flag on or off */
|
||||||
|
private $debug = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string|null $key Access key
|
||||||
|
* @param string|null $secret Secret Key
|
||||||
|
* @param string|null $partner Partner ID
|
||||||
|
* @param string|null $endpoint Endpoing URL including https://
|
||||||
|
* @param string|null $currency Currency to use, see valid list on AWS documentation.
|
||||||
|
* valid names are like USD, JPY, etc
|
||||||
|
* @param bool|null $debug Debug flag
|
||||||
|
*/
|
||||||
|
public function __construct(
|
||||||
|
?string $key,
|
||||||
|
?string $secret,
|
||||||
|
?string $partner,
|
||||||
|
?string $endpoint,
|
||||||
|
?string $currency,
|
||||||
|
?bool $debug
|
||||||
|
) {
|
||||||
|
/**
|
||||||
|
* @psalm-suppress InvalidScalarArgument
|
||||||
|
* @phpstan-ignore-next-line
|
||||||
|
*/
|
||||||
|
$this->setAccessKey(($key) ?: $this->parseEnv('AWS_GIFT_CARD_KEY'));
|
||||||
|
/**
|
||||||
|
* @psalm-suppress InvalidScalarArgument
|
||||||
|
* @phpstan-ignore-next-line
|
||||||
|
*/
|
||||||
|
$this->setSecret(($secret) ?: $this->parseEnv('AWS_GIFT_CARD_SECRET'));
|
||||||
|
/**
|
||||||
|
* @psalm-suppress InvalidScalarArgument
|
||||||
|
* @phpstan-ignore-next-line
|
||||||
|
*/
|
||||||
|
$this->setPartner(($partner) ?: $this->parseEnv('AWS_GIFT_CARD_PARTNER_ID'));
|
||||||
|
/**
|
||||||
|
* @psalm-suppress InvalidScalarArgument
|
||||||
|
* @phpstan-ignore-next-line
|
||||||
|
*/
|
||||||
|
$this->setEndpoint(($endpoint) ?: $this->parseEnv('AWS_GIFT_CARD_ENDPOINT'));
|
||||||
|
/**
|
||||||
|
* @psalm-suppress InvalidScalarArgument
|
||||||
|
* @phpstan-ignore-next-line
|
||||||
|
*/
|
||||||
|
$this->setCurrency(($currency) ?: $this->parseEnv('AWS_GIFT_CARD_CURRENCY'));
|
||||||
|
/**
|
||||||
|
* @psalm-suppress InvalidScalarArgument
|
||||||
|
* @phpstan-ignore-next-line
|
||||||
|
*/
|
||||||
|
$this->setDebug(($debug) ?: $this->parseEnv('AWS_DEBUG'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* string key to search, returns entry from _ENV
|
||||||
|
* if not matchin key, returns empty
|
||||||
|
*
|
||||||
|
* @param string $key To search in _ENV array
|
||||||
|
* @return string|bool Returns either string or true/false (DEBUG flag)
|
||||||
|
*/
|
||||||
|
private function parseEnv(string $key)
|
||||||
|
{
|
||||||
|
$return = '';
|
||||||
|
switch ($key) {
|
||||||
|
case 'AWS_DEBUG':
|
||||||
|
$return = !empty($_ENV['AWS_DEBUG']) ? true : false;
|
||||||
|
break;
|
||||||
|
case 'AWS_GIFT_CARD_KEY':
|
||||||
|
case 'AWS_GIFT_CARD_SECRET':
|
||||||
|
case 'AWS_GIFT_CARD_PARTNER_ID':
|
||||||
|
case 'AWS_GIFT_CARD_ENDPOINT':
|
||||||
|
case 'AWS_GIFT_CARD_CURRENCY':
|
||||||
|
$return = !empty($_ENV[$key]) && is_string($_ENV[$key]) ?
|
||||||
|
$_ENV[$key] : '';
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return $return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string Returns current set endpoint, without https://
|
||||||
|
*/
|
||||||
|
public function getEndpoint(): string
|
||||||
|
{
|
||||||
|
return $this->endpoint;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $endpoint Full endpoint url with https://
|
||||||
|
* @return ConfigInterface Class interface (self)
|
||||||
|
*/
|
||||||
|
public function setEndpoint(string $endpoint): ConfigInterface
|
||||||
|
{
|
||||||
|
// TODO: check valid endpoint + set region
|
||||||
|
$this->endpoint = (parse_url($endpoint, PHP_URL_HOST)) ?: '';
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string Current access key
|
||||||
|
*/
|
||||||
|
public function getAccessKey(): string
|
||||||
|
{
|
||||||
|
return $this->access_key;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $key Access Key to set
|
||||||
|
* @return ConfigInterface Class interface (self)
|
||||||
|
*/
|
||||||
|
public function setAccessKey(string $key): ConfigInterface
|
||||||
|
{
|
||||||
|
$this->access_key = $key;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string Current secret key
|
||||||
|
*/
|
||||||
|
public function getSecret(): string
|
||||||
|
{
|
||||||
|
return $this->secret_key;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $secret Secret key to set
|
||||||
|
* @return ConfigInterface Class interface (self)
|
||||||
|
*/
|
||||||
|
public function setSecret(string $secret): ConfigInterface
|
||||||
|
{
|
||||||
|
$this->secret_key = $secret;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string Current set currency
|
||||||
|
*/
|
||||||
|
public function getCurrency(): string
|
||||||
|
{
|
||||||
|
return $this->currency;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $currency Currency to set (eg USD, JPY, etc)
|
||||||
|
* @return ConfigInterface Class interface (self)
|
||||||
|
*/
|
||||||
|
public function setCurrency(string $currency): ConfigInterface
|
||||||
|
{
|
||||||
|
// TODO: check currency valid + currenc valid for region
|
||||||
|
$this->currency = $currency;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string Current set partner id
|
||||||
|
*/
|
||||||
|
public function getPartner(): string
|
||||||
|
{
|
||||||
|
return $this->partner_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $partner Partner id to set
|
||||||
|
* @return ConfigInterface Class interface (self)
|
||||||
|
*/
|
||||||
|
public function setPartner(string $partner): ConfigInterface
|
||||||
|
{
|
||||||
|
$this->partner_id = $partner;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool Current set debug flag as bool
|
||||||
|
*/
|
||||||
|
public function getDebug(): bool
|
||||||
|
{
|
||||||
|
return $this->debug;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param bool $debug Set debug flag as bool
|
||||||
|
* @return ConfigInterface Class interface (self)
|
||||||
|
*/
|
||||||
|
public function setDebug(bool $debug): ConfigInterface
|
||||||
|
{
|
||||||
|
$this->debug = $debug;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// __END__
|
||||||
76
src/Config/ConfigInterface.php
Normal file
76
src/Config/ConfigInterface.php
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace gullevek\AmazonIncentives\Config;
|
||||||
|
|
||||||
|
interface ConfigInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @return string Returns current set endpoint, without https://
|
||||||
|
*/
|
||||||
|
public function getEndpoint(): string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $endpoint Full endpoint url with https://
|
||||||
|
* @return ConfigInterface Class interface (self)
|
||||||
|
*/
|
||||||
|
public function setEndpoint(string $endpoint): ConfigInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string Current access key
|
||||||
|
*/
|
||||||
|
public function getAccessKey(): string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $key Access Key to set
|
||||||
|
* @return ConfigInterface Class interface (self)
|
||||||
|
*/
|
||||||
|
public function setAccessKey(string $key): ConfigInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string Current secret key
|
||||||
|
*/
|
||||||
|
public function getSecret(): string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $secret Secret key to set
|
||||||
|
* @return ConfigInterface Class interface (self)
|
||||||
|
*/
|
||||||
|
public function setSecret(string $secret): ConfigInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string Current set currency
|
||||||
|
*/
|
||||||
|
public function getCurrency(): string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $currency Currency to set (eg USD, JPY, etc)
|
||||||
|
* @return ConfigInterface Class interface (self)
|
||||||
|
*/
|
||||||
|
public function setCurrency(string $currency): ConfigInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string Current set partner id
|
||||||
|
*/
|
||||||
|
public function getPartner(): string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $partner Partner id to set
|
||||||
|
* @return ConfigInterface Class interface (self)
|
||||||
|
*/
|
||||||
|
public function setPartner(string $partner): ConfigInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool Current set debug flag as bool
|
||||||
|
*/
|
||||||
|
public function getDebug(): bool;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param bool $debug Set debug flag as bool
|
||||||
|
* @return ConfigInterface Class interface (self)
|
||||||
|
*/
|
||||||
|
public function setDebug(bool $debug): ConfigInterface;
|
||||||
|
}
|
||||||
|
|
||||||
|
// __END__
|
||||||
111
src/Debug/AmazonDebug.php
Normal file
111
src/Debug/AmazonDebug.php
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
// simple write all into an array that we can poll in the return group
|
||||||
|
// to activate AmazonDebug::setDebug(true) must be called once
|
||||||
|
|
||||||
|
namespace gullevek\AmazonIncentives\Debug;
|
||||||
|
|
||||||
|
class AmazonDebug
|
||||||
|
{
|
||||||
|
/** @var array<string,array<array<mixed>>> Log data array log id -> array of log entries */
|
||||||
|
private static $log = [];
|
||||||
|
/** @var bool debug flag */
|
||||||
|
private static $debug = false;
|
||||||
|
/** @var string|null Last set internal log array id */
|
||||||
|
private static $id = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
*
|
||||||
|
* @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) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if ($id === null) {
|
||||||
|
$id = uniqid();
|
||||||
|
}
|
||||||
|
self::$id = $id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set the debug flag.
|
||||||
|
* This is automatically run in gullevek\AmazonIncentives\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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* write a log entry
|
||||||
|
* 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<mixed> $data Any array data to store in the log
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public static function writeLog(array $data): void
|
||||||
|
{
|
||||||
|
if (self::$debug === false) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
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<mixed> Always array, empty if not data or not found
|
||||||
|
*/
|
||||||
|
public static function getLog(?string $id = null): array
|
||||||
|
{
|
||||||
|
if ($id === null) {
|
||||||
|
return self::$log;
|
||||||
|
} else {
|
||||||
|
return self::$log[$id] ?? [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// __END__
|
||||||
71
src/Exceptions/AmazonErrors.php
Normal file
71
src/Exceptions/AmazonErrors.php
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace gullevek\AmazonIncentives\Exceptions;
|
||||||
|
|
||||||
|
use RuntimeException;
|
||||||
|
use gullevek\AmazonIncentives\Debug\AmazonDebug;
|
||||||
|
|
||||||
|
final class AmazonErrors extends RuntimeException
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Returns an Runtime exception including a json encoded string with all
|
||||||
|
* parameters including last log id and log
|
||||||
|
*
|
||||||
|
* @param string $error_status agcodResponse->status from Amazon
|
||||||
|
* @param string $error_code errorCode from Amazon
|
||||||
|
* @param string $error_type errorType from Amazon
|
||||||
|
* @param string $message Message string to ad
|
||||||
|
* @param int $_error_code Error code to set
|
||||||
|
* @return AmazonErrors Exception Class
|
||||||
|
*/
|
||||||
|
public static function getError(
|
||||||
|
string $error_status,
|
||||||
|
string $error_code,
|
||||||
|
string $error_type,
|
||||||
|
string $message,
|
||||||
|
int $_error_code
|
||||||
|
): self {
|
||||||
|
// NOTE: if xdebug.show_exception_trace is set to 1 this will print ERRORS
|
||||||
|
return new static(
|
||||||
|
(json_encode([
|
||||||
|
'status' => $error_status,
|
||||||
|
'code' => $error_code,
|
||||||
|
'type' => $error_type,
|
||||||
|
'message' => $message,
|
||||||
|
// attach log data if exists
|
||||||
|
'log_id' => AmazonDebug::getId(),
|
||||||
|
'log' => AmazonDebug::getLog(),
|
||||||
|
])) ?: 'AmazonErrors: json encode problem: ' . $message,
|
||||||
|
$_error_code
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
* @return array<mixed> Decoded with code, type, message fields
|
||||||
|
*/
|
||||||
|
public static function decodeExceptionMessage(string $message): array
|
||||||
|
{
|
||||||
|
$message_ar = json_decode($message, true);
|
||||||
|
// if we have an error, build empty block and only fill message
|
||||||
|
if (json_last_error() || $message_ar === null || !is_array($message_ar)) {
|
||||||
|
$message_ar = [
|
||||||
|
'status' => '',
|
||||||
|
'code' => '',
|
||||||
|
'type' => '',
|
||||||
|
'message' => $message,
|
||||||
|
'log_id' => '',
|
||||||
|
'log' => []
|
||||||
|
];
|
||||||
|
}
|
||||||
|
return $message_ar;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// __END__
|
||||||
42
src/Handle/Json.php
Normal file
42
src/Handle/Json.php
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle json conversions
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace gullevek\AmazonIncentives\Handle;
|
||||||
|
|
||||||
|
use gullevek\AmazonIncentives\Exceptions\AmazonErrors;
|
||||||
|
|
||||||
|
class Json
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* decode json string
|
||||||
|
*
|
||||||
|
* @param string $json
|
||||||
|
* @return array<mixed>
|
||||||
|
* @throws AmazonErrors
|
||||||
|
*/
|
||||||
|
public static function jsonDecode(string $json): array
|
||||||
|
{
|
||||||
|
$result = json_decode($json, true);
|
||||||
|
if (
|
||||||
|
($json_last_error = json_last_error()) ||
|
||||||
|
$result === null ||
|
||||||
|
!is_array($result)
|
||||||
|
) {
|
||||||
|
throw AmazonErrors::getError(
|
||||||
|
'FAILURE',
|
||||||
|
'J-' . $json_last_error,
|
||||||
|
'jsonDecoreError',
|
||||||
|
'Failed to decode json data',
|
||||||
|
0
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// __END__
|
||||||
105
src/Response/CancelResponse.php
Normal file
105
src/Response/CancelResponse.php
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace gullevek\AmazonIncentives\Response;
|
||||||
|
|
||||||
|
use gullevek\AmazonIncentives\Debug\AmazonDebug;
|
||||||
|
|
||||||
|
class CancelResponse
|
||||||
|
{
|
||||||
|
/** @var string Amazon Gift Card gcId (gift card id). */
|
||||||
|
protected $id = '';
|
||||||
|
/** @var string Amazon Gift Card creationRequestId (creation request id) */
|
||||||
|
protected $creation_request_id = '';
|
||||||
|
/** @var string Amazon Gift Card status */
|
||||||
|
protected $status = '';
|
||||||
|
/** @var array<mixed> Amazon Gift Card Raw JSON */
|
||||||
|
protected $raw_json = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Response constructor for canceling gitf cards
|
||||||
|
*
|
||||||
|
* @param array<mixed> $json_response JSON response from web request to AWS
|
||||||
|
*/
|
||||||
|
public function __construct(array $json_response)
|
||||||
|
{
|
||||||
|
$this->raw_json = $json_response;
|
||||||
|
$this->parseJsonResponse($json_response);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get log entry with current set log id
|
||||||
|
*
|
||||||
|
* @return array<mixed> Log array
|
||||||
|
*/
|
||||||
|
public function getLog(): array
|
||||||
|
{
|
||||||
|
return AmazonDebug::getLog(AmazonDebug::getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The gift card id as created by the previous get code call
|
||||||
|
*
|
||||||
|
* @return string Gift card id
|
||||||
|
*/
|
||||||
|
public function getId(): string
|
||||||
|
{
|
||||||
|
return $this->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creation Request id from original get code call
|
||||||
|
*
|
||||||
|
* @return string Creation request id
|
||||||
|
*/
|
||||||
|
public function getCreationRequestId(): string
|
||||||
|
{
|
||||||
|
return $this->creation_request_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Request status
|
||||||
|
*
|
||||||
|
* @return string Request status as string: SUCCESS, FAILURE, RESEND
|
||||||
|
*/
|
||||||
|
public function getStatus(): string
|
||||||
|
{
|
||||||
|
return $this->status;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the request data as json string. This is a re-encode from decoded
|
||||||
|
* makeRequest call
|
||||||
|
*
|
||||||
|
* @return string JSON encoded string from the return values
|
||||||
|
*/
|
||||||
|
public function getRawJson(): string
|
||||||
|
{
|
||||||
|
return (json_encode($this->raw_json)) ?: '';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set class variables with response data from makeRequest and return self
|
||||||
|
*
|
||||||
|
* @param array<mixed> $json_response JSON response as array
|
||||||
|
* @return CancelResponse Return self object
|
||||||
|
*/
|
||||||
|
public function parseJsonResponse(array $json_response): self
|
||||||
|
{
|
||||||
|
if (array_key_exists('gcId', $json_response)) {
|
||||||
|
$this->id = $json_response['gcId'];
|
||||||
|
}
|
||||||
|
if (array_key_exists('creationRequestId', $json_response)) {
|
||||||
|
$this->creation_request_id = $json_response['creationRequestId'];
|
||||||
|
}
|
||||||
|
// SUCCESS, FAILURE, RESEND
|
||||||
|
if (array_key_exists('status', $json_response)) {
|
||||||
|
$this->status = $json_response['status'];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// __END__
|
||||||
127
src/Response/CreateBalanceResponse.php
Normal file
127
src/Response/CreateBalanceResponse.php
Normal file
@@ -0,0 +1,127 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace gullevek\AmazonIncentives\Response;
|
||||||
|
|
||||||
|
use gullevek\AmazonIncentives\Debug\AmazonDebug;
|
||||||
|
|
||||||
|
class CreateBalanceResponse
|
||||||
|
{
|
||||||
|
/** @var float Amazon Gift Card Balance Amount */
|
||||||
|
protected $amount = 0;
|
||||||
|
/** @var string Amazon Gift Card Balance Currency */
|
||||||
|
protected $currency = '';
|
||||||
|
/** @var string Amazon Gift Card Balance Status */
|
||||||
|
protected $status = '';
|
||||||
|
/** @var string Amazon Gift Card Balance Timestamp */
|
||||||
|
protected $timestamp = '';
|
||||||
|
/** @var array<mixed> Amazon Gift Card Raw JSON */
|
||||||
|
protected $raw_json = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Response constructor for requesting account funds status
|
||||||
|
*
|
||||||
|
* @param array<mixed> $json_response JSON response from web request to AWS
|
||||||
|
*/
|
||||||
|
public function __construct(array $json_response)
|
||||||
|
{
|
||||||
|
$this->raw_json = $json_response;
|
||||||
|
$this->parseJsonResponse($json_response);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get log entry with current set log id
|
||||||
|
*
|
||||||
|
* @return array<mixed> Log array
|
||||||
|
*/
|
||||||
|
public function getLog(): array
|
||||||
|
{
|
||||||
|
return AmazonDebug::getLog(AmazonDebug::getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the current available funds amount
|
||||||
|
*
|
||||||
|
* @return float Funds amount in set currency
|
||||||
|
*/
|
||||||
|
public function getAmount(): float
|
||||||
|
{
|
||||||
|
return $this->amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the set currency type
|
||||||
|
*
|
||||||
|
* @return string Currency type. Eg USD, JPY, etc
|
||||||
|
*/
|
||||||
|
public function getCurrency(): string
|
||||||
|
{
|
||||||
|
return $this->currency;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get timestamp as set.
|
||||||
|
* eg 20220609T061446Z
|
||||||
|
*
|
||||||
|
* @return string Timestamp string. Ymd\THis\Z
|
||||||
|
*/
|
||||||
|
public function getTimestamp(): string
|
||||||
|
{
|
||||||
|
return $this->timestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Request status
|
||||||
|
*
|
||||||
|
* @return string Request status as string: SUCCESS, FAILURE, RESEND
|
||||||
|
*/
|
||||||
|
public function getStatus(): string
|
||||||
|
{
|
||||||
|
return $this->status;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the request data as json string. This is a re-encode from decoded
|
||||||
|
* makeRequest call
|
||||||
|
*
|
||||||
|
* @return string JSON encoded string from the return values
|
||||||
|
*/
|
||||||
|
public function getRawJson(): string
|
||||||
|
{
|
||||||
|
return (json_encode($this->raw_json)) ?: '';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set class variables with response data from makeRequest and return self
|
||||||
|
*
|
||||||
|
* @param array<mixed> $json_response JSON response as array
|
||||||
|
* @return CreateBalanceResponse Return self object
|
||||||
|
*/
|
||||||
|
public function parseJsonResponse(array $json_response): self
|
||||||
|
{
|
||||||
|
if (
|
||||||
|
is_array($json_response['availableFunds']) &&
|
||||||
|
array_key_exists('amount', $json_response['availableFunds']) &&
|
||||||
|
is_numeric($json_response['availableFunds']['amount'])
|
||||||
|
) {
|
||||||
|
$this->amount = (float)$json_response['availableFunds']['amount'];
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
is_array($json_response['availableFunds']) &&
|
||||||
|
array_key_exists('currencyCode', $json_response['availableFunds']) &&
|
||||||
|
is_string($json_response['availableFunds']['currencyCode'])
|
||||||
|
) {
|
||||||
|
$this->currency = $json_response['availableFunds']['currencyCode'];
|
||||||
|
}
|
||||||
|
// SUCCESS, FAILURE, RESEND
|
||||||
|
if (array_key_exists('status', $json_response)) {
|
||||||
|
$this->status = $json_response['status'];
|
||||||
|
}
|
||||||
|
if (array_key_exists('timestamp', $json_response)) {
|
||||||
|
$this->timestamp = $json_response['timestamp'];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
}
|
||||||
184
src/Response/CreateResponse.php
Normal file
184
src/Response/CreateResponse.php
Normal file
@@ -0,0 +1,184 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace gullevek\AmazonIncentives\Response;
|
||||||
|
|
||||||
|
use gullevek\AmazonIncentives\Debug\AmazonDebug;
|
||||||
|
|
||||||
|
class CreateResponse
|
||||||
|
{
|
||||||
|
/** @var string Amazon Gift Card gcId */
|
||||||
|
protected $id = '';
|
||||||
|
/** @var string Amazon Gift Card creationRequestId */
|
||||||
|
protected $creation_request_id = '';
|
||||||
|
/** @var string Amazon Gift Card gcClaimCode */
|
||||||
|
protected $claim_code = '';
|
||||||
|
/** @var float Amazon Gift Card amount */
|
||||||
|
protected $value = 0;
|
||||||
|
/** @var string Amazon Gift Card currency */
|
||||||
|
protected $currency = '';
|
||||||
|
/** @var string Amazon Gift Card status */
|
||||||
|
protected $status = '';
|
||||||
|
/** @var string Amazon Gift Card Expiration Date */
|
||||||
|
protected $expiration_date = '';
|
||||||
|
/** @var string Amazon Gift Card Expiration Date */
|
||||||
|
protected $card_status = '';
|
||||||
|
/** @var array<mixed> Amazon Gift Card Raw JSON as array */
|
||||||
|
protected $raw_json = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Response constructor for creating gift cards
|
||||||
|
*
|
||||||
|
* @param array<mixed> $json_response JSON response from web request to AWS
|
||||||
|
*/
|
||||||
|
public function __construct(array $json_response)
|
||||||
|
{
|
||||||
|
$this->raw_json = $json_response;
|
||||||
|
$this->parseJsonResponse($json_response);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get log entry with current set log id
|
||||||
|
*
|
||||||
|
* @return array<mixed> Log array
|
||||||
|
*/
|
||||||
|
public function getLog(): array
|
||||||
|
{
|
||||||
|
return AmazonDebug::getLog(AmazonDebug::getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gift Card ID returned from AWS. Can be used in the cancel request
|
||||||
|
*
|
||||||
|
* @return string Gift card id
|
||||||
|
*/
|
||||||
|
public function getId(): string
|
||||||
|
{
|
||||||
|
return $this->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Either the one set with the method parameter, or automatically created
|
||||||
|
* during get code request
|
||||||
|
*
|
||||||
|
* @return string Creation request id
|
||||||
|
*/
|
||||||
|
public function getCreationRequestId(): string
|
||||||
|
{
|
||||||
|
return $this->creation_request_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The actual gift code, recommended not to be stored anywhere and only shown
|
||||||
|
* to user
|
||||||
|
*
|
||||||
|
* @return string Gift order claim code on AWS
|
||||||
|
*/
|
||||||
|
public function getClaimCode(): string
|
||||||
|
{
|
||||||
|
return $this->claim_code;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The ordered gift code value in given currency
|
||||||
|
*
|
||||||
|
* @return float Gift order value in currency
|
||||||
|
*/
|
||||||
|
public function getValue(): float
|
||||||
|
{
|
||||||
|
return $this->value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The currently set currency
|
||||||
|
*
|
||||||
|
* @return string Currency type. Eg USD, JPY, etc
|
||||||
|
*/
|
||||||
|
public function getCurrency(): string
|
||||||
|
{
|
||||||
|
return $this->currency;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Expiration date for the ordered gift code.
|
||||||
|
* eg 20220609T061446Z
|
||||||
|
*
|
||||||
|
* @return string Timestamp until when the gift code is valid. Ymd\THis\Z
|
||||||
|
*/
|
||||||
|
public function getExpirationDate(): string
|
||||||
|
{
|
||||||
|
return $this->expiration_date;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gift card status. If the same creation request id is sent again and the
|
||||||
|
* gift card got cancled, this is reflected here
|
||||||
|
*
|
||||||
|
* @return string Gift card status
|
||||||
|
*/
|
||||||
|
public function getCardStatus(): string
|
||||||
|
{
|
||||||
|
return $this->card_status;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Request status
|
||||||
|
*
|
||||||
|
* @return string Request status as string: SUCCESS, FAILURE, RESEND
|
||||||
|
*/
|
||||||
|
public function getStatus(): string
|
||||||
|
{
|
||||||
|
return $this->status;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Returns the request data as json string. This is a re-encode from decoded
|
||||||
|
* makeRequest call
|
||||||
|
*
|
||||||
|
* @return string JSON encoded string from the return values
|
||||||
|
*/
|
||||||
|
public function getRawJson(): string
|
||||||
|
{
|
||||||
|
return (json_encode($this->raw_json)) ?: '';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set class variables with response data from makeRequest and return self
|
||||||
|
*
|
||||||
|
* @param array<mixed> $json_response JSON response as array
|
||||||
|
* @return CreateResponse Return self object
|
||||||
|
*/
|
||||||
|
public function parseJsonResponse(array $json_response): self
|
||||||
|
{
|
||||||
|
if (array_key_exists('gcId', $json_response)) {
|
||||||
|
$this->id = $json_response['gcId'];
|
||||||
|
}
|
||||||
|
if (array_key_exists('creationRequestId', $json_response)) {
|
||||||
|
$this->creation_request_id = $json_response['creationRequestId'];
|
||||||
|
}
|
||||||
|
if (array_key_exists('gcClaimCode', $json_response)) {
|
||||||
|
$this->claim_code = $json_response['gcClaimCode'];
|
||||||
|
}
|
||||||
|
if (array_key_exists('amount', $json_response['cardInfo']['value'])) {
|
||||||
|
$this->value = $json_response['cardInfo']['value']['amount'];
|
||||||
|
}
|
||||||
|
if (array_key_exists('currencyCode', $json_response['cardInfo']['value'])) {
|
||||||
|
$this->currency = $json_response['cardInfo']['value']['currencyCode'];
|
||||||
|
}
|
||||||
|
if (array_key_exists('gcExpirationDate', $json_response)) {
|
||||||
|
$this->expiration_date = $json_response['gcExpirationDate'];
|
||||||
|
}
|
||||||
|
if (array_key_exists('cardStatus', $json_response['cardInfo'])) {
|
||||||
|
$this->card_status = $json_response['cardInfo']['cardStatus'];
|
||||||
|
}
|
||||||
|
// SUCCESS, FAILURE, RESEND
|
||||||
|
if (array_key_exists('status', $json_response)) {
|
||||||
|
$this->status = $json_response['status'];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// __END__
|
||||||
7
test/.env.example
Normal file
7
test/.env.example
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# for AWS gift card testing
|
||||||
|
AWS_GIFT_CARD_ENDPOINT=
|
||||||
|
AWS_GIFT_CARD_KEY=
|
||||||
|
AWS_GIFT_CARD_SECRET=
|
||||||
|
AWS_GIFT_CARD_PARTNER_ID=
|
||||||
|
AWS_GIFT_CARD_CURRENCY=
|
||||||
|
AWS_DEBUG=
|
||||||
140
test/agcod_http_return_samples.md
Normal file
140
test/agcod_http_return_samples.md
Normal file
@@ -0,0 +1,140 @@
|
|||||||
|
# Return data from AWS as json string
|
||||||
|
|
||||||
|
## Mock tests self
|
||||||
|
|
||||||
|
Tests run in local mock tests without connection to AWS
|
||||||
|
|
||||||
|
### funds
|
||||||
|
|
||||||
|
```json
|
||||||
|
{"availableFunds":{"amount":0.0,"currencyCode":"JPY"},"status":"SUCCESS","timestamp":"20220610T085450Z"}
|
||||||
|
```
|
||||||
|
|
||||||
|
### buy
|
||||||
|
|
||||||
|
```json
|
||||||
|
{"cardInfo":{"cardNumber":null,"cardStatus":"Fulfilled","expirationDate":null,"value":{"amount":1000.0,"currencyCode":"JPY"}},"creationRequestId":"PartnerId_62a309167e7a4","gcClaimCode":"LJ49-AKDUV6-UYCP","gcExpirationDate":"Thu Jun 10 14:59:59 UTC 2032","gcId":"5535125272070255","status":"SUCCESS"}
|
||||||
|
```
|
||||||
|
|
||||||
|
### cancel
|
||||||
|
|
||||||
|
```json
|
||||||
|
{"creationRequestId":"EG3bd_62a309167e7a4","gcId":"5535125272070255","status":"SUCCESS"}
|
||||||
|
```
|
||||||
|
|
||||||
|
### buy other 1
|
||||||
|
|
||||||
|
```json
|
||||||
|
{"cardInfo":{"cardNumber":null,"cardStatus":"RefundedToPurchaser","expirationDate":null,"value":{"amount":1000.0,"currencyCode":"JPY"}},"creationRequestId":"PartnerId_62a309167e7a4","gcClaimCode":"LJ49-AKDUV6-UYCP","gcExpirationDate":"Thu Jun 10 14:59:59 UTC 2032","gcId":"5535125272070255","status":"SUCCESS"}
|
||||||
|
```
|
||||||
|
|
||||||
|
### buy aother 2
|
||||||
|
|
||||||
|
```json
|
||||||
|
{"cardInfo":{"cardNumber":null,"cardStatus":"Fulfilled","expirationDate":null,"value":{"amount":1000.0,"currencyCode":"JPY"}},"creationRequestId":"PartnerId_62a30923e9705","gcClaimCode":"UM97-FD5QKK-WKCT","gcExpirationDate":"Thu Jun 10 14:59:59 UTC 2032","gcId":"5540334324324221","status":"SUCCESS"}
|
||||||
|
```
|
||||||
|
|
||||||
|
### buy other 2 (same create request id)
|
||||||
|
|
||||||
|
```json
|
||||||
|
{"cardInfo":{"cardNumber":null,"cardStatus":"Fulfilled","expirationDate":null,"value":{"amount":1000.0,"currencyCode":"JPY"}},"creationRequestId":"PartnerId_62a30923e9705","gcClaimCode":"UM97-FD5QKK-WKCT","gcExpirationDate":"Thu Jun 10 14:59:59 UTC 2032","gcId":"5540334324324221","status":"SUCCESS"}
|
||||||
|
```
|
||||||
|
|
||||||
|
## AWS GCOD Mocks
|
||||||
|
|
||||||
|
Mocking on AWS side, these will be returned if given request ID codes are sent.
|
||||||
|
A working test account must exist for this
|
||||||
|
|
||||||
|
## success
|
||||||
|
|
||||||
|
### buy gift card F0000
|
||||||
|
|
||||||
|
```json
|
||||||
|
{"cardInfo":{"cardNumber":null,"cardStatus":"Fulfilled","expirationDate":null,"value":{"amount":500.0,"currencyCode":"JPY"}},"creationRequestId":"F0000","gcClaimCode":"ZYXW-VUTS-RQPO","gcExpirationDate":null,"gcId":"ABC123ZYX987","status":"SUCCESS"}
|
||||||
|
```
|
||||||
|
|
||||||
|
## errors
|
||||||
|
|
||||||
|
### F1000 -> F100
|
||||||
|
|
||||||
|
```json
|
||||||
|
{"agcodResponse":{"__type":"CreateGiftCardResponse:http://internal.amazon.com/coral/com.amazonaws.agcod/","cardInfo":null,"creationRequestId":null,"gcClaimCode":null,"gcExpirationDate":null,"gcId":null,"status":"FAILURE"},"errorCode":"F100","errorType":"GeneralError","message":"General Error"}
|
||||||
|
```
|
||||||
|
|
||||||
|
### F2003 -> F200
|
||||||
|
|
||||||
|
```json
|
||||||
|
{"agcodResponse":{"__type":"CreateGiftCardResponse:http://internal.amazon.com/coral/com.amazonaws.agcod/","cardInfo":null,"creationRequestId":null,"gcClaimCode":null,"gcExpirationDate":null,"gcId":null,"status":"FAILURE"},"errorCode":"F200","errorType":"InvalidAmountInput","message":"Amount can't be null"}
|
||||||
|
```
|
||||||
|
|
||||||
|
### F2004 -> F200
|
||||||
|
|
||||||
|
```json
|
||||||
|
{"agcodResponse":{"__type":"CreateGiftCardResponse:http://internal.amazon.com/coral/com.amazonaws.agcod/","cardInfo":null,"creationRequestId":null,"gcClaimCode":null,"gcExpirationDate":null,"gcId":null,"status":"FAILURE"},"errorCode":"F200","errorType":"InvalidAmountValue","message":"Amount must be larger than 0"}
|
||||||
|
```
|
||||||
|
|
||||||
|
### F2005 -> F200
|
||||||
|
|
||||||
|
```json
|
||||||
|
{"agcodResponse":{"__type":"CreateGiftCardResponse:http://internal.amazon.com/coral/com.amazonaws.agcod/","cardInfo":null,"creationRequestId":null,"gcClaimCode":null,"gcExpirationDate":null,"gcId":null,"status":"FAILURE"},"errorCode":"F200","errorType":"InvalidCurrencyCodeInput","message":"Currency Code can't be null or empty"}
|
||||||
|
```
|
||||||
|
|
||||||
|
### F2010 -> F200
|
||||||
|
|
||||||
|
```json
|
||||||
|
{"agcodResponse":{"__type":"CreateGiftCardResponse:http://internal.amazon.com/coral/com.amazonaws.agcod/","cardInfo":null,"creationRequestId":null,"gcClaimCode":null,"gcExpirationDate":null,"gcId":null,"status":"FAILURE"},"errorCode":"F200","errorType":"CardAlreadyActivatedWithDifferentRequestId","message":"The card was already activated with a different request id"}
|
||||||
|
```
|
||||||
|
|
||||||
|
### F2015 -> F200
|
||||||
|
|
||||||
|
```json
|
||||||
|
{"agcodResponse":{"__type":"CreateGiftCardResponse:http://internal.amazon.com/coral/com.amazonaws.agcod/","cardInfo":null,"creationRequestId":null,"gcClaimCode":null,"gcExpirationDate":null,"gcId":null,"status":"FAILURE"},"errorCode":"F200","errorType":"MaxAmountExceeded","message":"Max Amount Exceeded"}
|
||||||
|
```
|
||||||
|
|
||||||
|
### F2016 -> F200
|
||||||
|
|
||||||
|
```json
|
||||||
|
{"agcodResponse":{"__type":"CreateGiftCardResponse:http://internal.amazon.com/coral/com.amazonaws.agcod/","cardInfo":null,"creationRequestId":null,"gcClaimCode":null,"gcExpirationDate":null,"gcId":null,"status":"FAILURE"},"errorCode":"F200","errorType":"CurrencyCodeMismatch","message":"Currency Code Mismatch"}
|
||||||
|
```
|
||||||
|
|
||||||
|
### F2017 -> F200
|
||||||
|
|
||||||
|
```json
|
||||||
|
{"agcodResponse":{"__type":"CreateGiftCardResponse:http://internal.amazon.com/coral/com.amazonaws.agcod/","cardInfo":null,"creationRequestId":null,"gcClaimCode":null,"gcExpirationDate":null,"gcId":null,"status":"FAILURE"},"errorCode":"F200","errorType":"FractionalAmountNotAllowed","message":"Fractional Amount Not Allowed"}
|
||||||
|
```
|
||||||
|
|
||||||
|
### F2047 -> F200
|
||||||
|
|
||||||
|
```json
|
||||||
|
{"agcodResponse":{"__type":"CreateGiftCardResponse:http://internal.amazon.com/coral/com.amazonaws.agcod/","cardInfo":null,"creationRequestId":null,"gcClaimCode":null,"gcExpirationDate":null,"gcId":null,"status":"FAILURE"},"errorCode":"F200","errorType":"CancelRequestArrivedAfterTimeLimit","message":"Cancellation cannot be processed as too much time has elapsed since creation"}
|
||||||
|
```
|
||||||
|
|
||||||
|
### F3003 -> F300
|
||||||
|
|
||||||
|
```json
|
||||||
|
{"agcodResponse":{"__type":"CreateGiftCardResponse:http://internal.amazon.com/coral/com.amazonaws.agcod/","cardInfo":null,"creationRequestId":null,"gcClaimCode":null,"gcExpirationDate":null,"gcId":null,"status":"FAILURE"},"errorCode":"F300","errorType":"InsufficientFunds","message":"Insufficient Funds"}
|
||||||
|
```
|
||||||
|
|
||||||
|
### F3005 -> F300
|
||||||
|
|
||||||
|
```json
|
||||||
|
{"agcodResponse":{"__type":"CreateGiftCardResponse:http://internal.amazon.com/coral/com.amazonaws.agcod/","cardInfo":null,"creationRequestId":null,"gcClaimCode":null,"gcExpirationDate":null,"gcId":null,"status":"FAILURE"},"errorCode":"F300","errorType":"GeneralError","message":"General Error"}
|
||||||
|
```
|
||||||
|
|
||||||
|
### F3010 -> F300
|
||||||
|
|
||||||
|
```json
|
||||||
|
{"agcodResponse":{"__type":"CreateGiftCardResponse:http://internal.amazon.com/coral/com.amazonaws.agcod/","cardInfo":null,"creationRequestId":null,"gcClaimCode":null,"gcExpirationDate":null,"gcId":null,"status":"FAILURE"},"errorCode":"F300","errorType":"CustomerSurpassedDailyVelocityLimit","message":"Customer has exceeded daily velocity limit"}
|
||||||
|
```
|
||||||
|
|
||||||
|
### F4000 -> F400
|
||||||
|
|
||||||
|
```json
|
||||||
|
{"agcodResponse":{"__type":"CreateGiftCardResponse:http://internal.amazon.com/coral/com.amazonaws.agcod/","cardInfo":null,"creationRequestId":null,"gcClaimCode":null,"gcExpirationDate":null,"gcId":null,"status":"RESEND"},"errorCode":"F400","errorType":"SystemTemporarilyUnavailable","message":"System Temporarily Unavailable"}
|
||||||
|
```
|
||||||
|
|
||||||
|
### F5000 -> F500
|
||||||
|
|
||||||
|
```json
|
||||||
|
{"agcodResponse":{"__type":"CreateGiftCardResponse:http://internal.amazon.com/coral/com.amazonaws.agcod/","cardInfo":null,"creationRequestId":null,"gcClaimCode":null,"gcExpirationDate":null,"gcId":null,"status":"FAILURE"},"errorCode":"F500","errorType":"GeneralError","message":"General Error"}
|
||||||
|
```
|
||||||
@@ -1,63 +0,0 @@
|
|||||||
<?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__
|
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
* write log as string from array data
|
* write log as string from array data
|
||||||
* includes timestamp
|
* includes timestamp
|
||||||
*
|
*
|
||||||
* @param array $data Debug log array data to add to the json string
|
* @param array<mixed> $data Debug log array data to add to the json string
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
function writeLog(array $data): string
|
function writeLog(array $data): string
|
||||||
@@ -25,7 +25,7 @@ function writeLog(array $data): string
|
|||||||
*/
|
*/
|
||||||
function dateTr(string $date): string
|
function dateTr(string $date): string
|
||||||
{
|
{
|
||||||
return date('Y-m-d H:i:s', strtotime($date));
|
return date('Y-m-d H:i:s', (strtotime($date)) ?: null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -33,7 +33,7 @@ function dateTr(string $date): string
|
|||||||
*
|
*
|
||||||
* @param string $call_request Call request, eg buyGiftCard
|
* @param string $call_request Call request, eg buyGiftCard
|
||||||
* @param integer $error_code $e Exception error code
|
* @param integer $error_code $e Exception error code
|
||||||
* @param array $error Array from the Exception message json string
|
* @param array<mixed> $error Array from the Exception message json string
|
||||||
* @param boolean $debug_print If we should show the debug log
|
* @param boolean $debug_print If we should show the debug log
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
@@ -52,13 +52,18 @@ function printException(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// general auto loader
|
// composer auto loader
|
||||||
require 'autoloader.php';
|
$loader = require '../vendor/autoload.php';
|
||||||
// env file loader
|
// need to add this or it will not load here
|
||||||
require 'read_env_file.php';
|
$loader->addPsr4('gullevek\\', __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'src');
|
||||||
|
// print "LOADER: <pre>" . print_r($loader, true) . "</pre>";
|
||||||
|
|
||||||
|
use gullevek\AmazonIncentives\AmazonIncentives;
|
||||||
|
use gullevek\AmazonIncentives\Exceptions\AmazonErrors;
|
||||||
|
use gullevek\dotEnv\DotEnv;
|
||||||
|
|
||||||
// load env data with dotenv
|
// load env data with dotenv
|
||||||
readEnvFile(__DIR__);
|
DotEnv::readEnvFile(__DIR__);
|
||||||
|
|
||||||
print "<h1>Amazon Gift Card Incentives</h1><br>";
|
print "<h1>Amazon Gift Card Incentives</h1><br>";
|
||||||
|
|
||||||
@@ -67,81 +72,50 @@ print "<h1>Amazon Gift Card Incentives</h1><br>";
|
|||||||
// aws key: AWS_GIFT_CARD_KEY
|
// aws key: AWS_GIFT_CARD_KEY
|
||||||
// aws secret: AWS_GIFT_CARD_SECRET
|
// aws secret: AWS_GIFT_CARD_SECRET
|
||||||
// partner id: AWS_GIFT_CARD_PARTNER_ID
|
// partner id: AWS_GIFT_CARD_PARTNER_ID
|
||||||
// optional
|
|
||||||
// currency: AWS_ICENTIVE_CURRENCY
|
// currency: AWS_ICENTIVE_CURRENCY
|
||||||
|
// optional
|
||||||
// as in .env
|
// debug: AWS_DEBUG (if not set: off)
|
||||||
// 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
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
$fp = fopen('log/debug.' . date('YmdHis') . '.log', 'w');
|
|
||||||
|
|
||||||
// run info test (prints ENV vars)
|
// run info test (prints ENV vars)
|
||||||
$run_info_test = false;
|
$run_info_test = !empty($_GET['info']) ? true : false;
|
||||||
// run test to get funds info
|
// run test to get funds info
|
||||||
$run_fund_test = true;
|
$run_fund_test = !empty($_GET['fund']) ? true : false;
|
||||||
// run the normal get/cancel gift card tests
|
// run the normal get/cancel gift card tests
|
||||||
$run_gift_tests = true;
|
$run_gift_tests = !empty($_GET['gift']) ? true : false;
|
||||||
// run mock error check tests
|
// run mock error check tests
|
||||||
$run_mocks = false;
|
$run_mocks = !empty($_GET['mocks']) ? true : false;
|
||||||
|
|
||||||
// should we print debug info
|
// should we print debug info
|
||||||
$debug_print = false;
|
$debug_print = !empty($_GET['debug']) ? true : false;
|
||||||
// how long to wait between each call
|
// how long to wait between each call
|
||||||
$debug_wait = 2;
|
$debug_wait = 2;
|
||||||
// if set to true will print all the debug logs too
|
// if set to true will print all the debug logs too
|
||||||
$mock_debug = false;
|
$mock_debug = !empty($_GET['debug_mock']) ? true : false;
|
||||||
// wait in seconds between mock tests
|
// wait in seconds between mock tests
|
||||||
$mock_wait = 2;
|
$mock_wait = 2;
|
||||||
|
|
||||||
|
if (empty($_GET)) {
|
||||||
|
print "<b>Use _GET parameters to start tests</b>";
|
||||||
|
}
|
||||||
|
|
||||||
|
// open debug file output
|
||||||
|
$fp = fopen('log/debug.' . date('YmdHis') . '.log', 'w');
|
||||||
|
if (!is_resource($fp)) {
|
||||||
|
die("Cannot open log debug file");
|
||||||
|
}
|
||||||
|
|
||||||
if ($run_info_test === true) {
|
if ($run_info_test === true) {
|
||||||
$aws = new Amazon\AmazonIncentives();
|
$aws = new AmazonIncentives();
|
||||||
print "checkMe: <pre>" . print_r($aws->checkMe(), true) . "</pre>";
|
$aws_check_me = $aws->checkMe();
|
||||||
fwrite($fp, writeLog($aws->checkMe()));
|
print "checkMe: <pre>" . print_r($aws_check_me, true) . "</pre>";
|
||||||
|
fwrite($fp, writeLog($aws_check_me));
|
||||||
print "<hr>";
|
print "<hr>";
|
||||||
}
|
}
|
||||||
|
|
||||||
// check balance
|
// check balance
|
||||||
if ($run_fund_test === true) {
|
if ($run_fund_test === true) {
|
||||||
try {
|
try {
|
||||||
$aws_test = Amazon\AmazonIncentives::make()->getAvailableFunds();
|
$aws_test = AmazonIncentives::make()->getAvailableFunds();
|
||||||
print "AWS: getAvailableFunds: " . $aws_test->getStatus() . ": "
|
print "AWS: getAvailableFunds: " . $aws_test->getStatus() . ": "
|
||||||
. "Amount: " . $aws_test->getAmount() . ", "
|
. "Amount: " . $aws_test->getAmount() . ", "
|
||||||
. "Currency: " . $aws_test->getCurrency() . ", "
|
. "Currency: " . $aws_test->getCurrency() . ", "
|
||||||
@@ -151,7 +125,7 @@ if ($run_fund_test === true) {
|
|||||||
}
|
}
|
||||||
fwrite($fp, writeLog((array)$aws_test));
|
fwrite($fp, writeLog((array)$aws_test));
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
$error = Amazon\AmazonIncentives::decodeExceptionMessage($e->getMessage());
|
$error = AmazonErrors::decodeExceptionMessage($e->getMessage());
|
||||||
printException('getAvailableFunds', $e->getCode(), $error, $debug_print);
|
printException('getAvailableFunds', $e->getCode(), $error, $debug_print);
|
||||||
fwrite($fp, writeLog($error));
|
fwrite($fp, writeLog($error));
|
||||||
};
|
};
|
||||||
@@ -168,7 +142,7 @@ if ($run_gift_tests === true) {
|
|||||||
$gift_card_id = '';
|
$gift_card_id = '';
|
||||||
try {
|
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 = 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();
|
||||||
@@ -183,7 +157,7 @@ if ($run_gift_tests === true) {
|
|||||||
}
|
}
|
||||||
fwrite($fp, writeLog((array)$aws_test));
|
fwrite($fp, writeLog((array)$aws_test));
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
$error = Amazon\AmazonIncentives::decodeExceptionMessage($e->getMessage());
|
$error = AmazonErrors::decodeExceptionMessage($e->getMessage());
|
||||||
printException('buyGiftCard', $e->getCode(), $error, $debug_print);
|
printException('buyGiftCard', $e->getCode(), $error, $debug_print);
|
||||||
fwrite($fp, writeLog($error));
|
fwrite($fp, writeLog($error));
|
||||||
}
|
}
|
||||||
@@ -191,7 +165,7 @@ if ($run_gift_tests === true) {
|
|||||||
sleep($debug_wait);
|
sleep($debug_wait);
|
||||||
try {
|
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 = 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;
|
||||||
@@ -200,7 +174,7 @@ if ($run_gift_tests === true) {
|
|||||||
}
|
}
|
||||||
fwrite($fp, writeLog((array)$aws_test));
|
fwrite($fp, writeLog((array)$aws_test));
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
$error = Amazon\AmazonIncentives::decodeExceptionMessage($e->getMessage());
|
$error = AmazonErrors::decodeExceptionMessage($e->getMessage());
|
||||||
print "AWS: cancelGiftCard: " . $error['status']
|
print "AWS: cancelGiftCard: " . $error['status']
|
||||||
. " [" . $e->getCode() . "]: "
|
. " [" . $e->getCode() . "]: "
|
||||||
. $error['code'] . " | " . $error['type']
|
. $error['code'] . " | " . $error['type']
|
||||||
@@ -212,10 +186,32 @@ if ($run_gift_tests === true) {
|
|||||||
}
|
}
|
||||||
print "<br>";
|
print "<br>";
|
||||||
sleep($debug_wait);
|
sleep($debug_wait);
|
||||||
|
// request same card again and get error
|
||||||
|
try {
|
||||||
|
$aws_test = AmazonIncentives::make()->buyGiftCard((float)$value, $creation_request_id);
|
||||||
|
$request_status = $aws_test->getStatus();
|
||||||
|
// same?
|
||||||
|
$claim_code = $aws_test->getClaimCode();
|
||||||
|
$expiration_date = $aws_test->getExpirationDate();
|
||||||
|
print "AWS: buyGiftCard: CANCLED SAME CODE AGAIN: " . $request_status . ": "
|
||||||
|
. "creationRequestId: " . $creation_request_id . ", gcId: " . $gift_card_id . ", "
|
||||||
|
. "EXPIRE DATE: <b>" . dateTr($expiration_date) . "</b>, "
|
||||||
|
. "CLAIM CODE: <b>" . $claim_code . "</b>";
|
||||||
|
if ($debug_print === true) {
|
||||||
|
print "<pre>" . print_r($aws_test, true) . "</pre>";
|
||||||
|
}
|
||||||
|
fwrite($fp, writeLog((array)$aws_test));
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$error = AmazonErrors::decodeExceptionMessage($e->getMessage());
|
||||||
|
printException('buyGiftCard', $e->getCode(), $error, $debug_print);
|
||||||
|
fwrite($fp, writeLog($error));
|
||||||
|
}
|
||||||
|
print "<br>";
|
||||||
|
sleep($debug_wait);
|
||||||
|
|
||||||
// set same request ID twice to get same response test
|
// set same request ID twice to get same response test
|
||||||
try {
|
try {
|
||||||
$aws_test = Amazon\AmazonIncentives::make()->buyGiftCard((float)$value);
|
$aws_test = 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();
|
||||||
@@ -230,16 +226,17 @@ if ($run_gift_tests === true) {
|
|||||||
}
|
}
|
||||||
fwrite($fp, writeLog((array)$aws_test));
|
fwrite($fp, writeLog((array)$aws_test));
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
$error = Amazon\AmazonIncentives::decodeExceptionMessage($e->getMessage());
|
$error = AmazonErrors::decodeExceptionMessage($e->getMessage());
|
||||||
printException('cancelGiftCard', $e->getCode(), $error, $debug_print);
|
printException('cancelGiftCard', $e->getCode(), $error, $debug_print);
|
||||||
fwrite($fp, writeLog($error));
|
fwrite($fp, writeLog($error));
|
||||||
}
|
}
|
||||||
print "<br>";
|
print "<br>";
|
||||||
sleep($debug_wait);
|
sleep($debug_wait);
|
||||||
try {
|
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();
|
$request_status = $aws_test->getStatus();
|
||||||
// same?
|
// same?
|
||||||
|
$claim_code = $aws_test->getClaimCode();
|
||||||
$expiration_date = $aws_test->getExpirationDate();
|
$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 . ", "
|
||||||
@@ -250,7 +247,7 @@ if ($run_gift_tests === true) {
|
|||||||
}
|
}
|
||||||
fwrite($fp, writeLog((array)$aws_test));
|
fwrite($fp, writeLog((array)$aws_test));
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
$error = Amazon\AmazonIncentives::decodeExceptionMessage($e->getMessage());
|
$error = AmazonErrors::decodeExceptionMessage($e->getMessage());
|
||||||
printException('buyGiftCard', $e->getCode(), $error, $debug_print);
|
printException('buyGiftCard', $e->getCode(), $error, $debug_print);
|
||||||
fwrite($fp, writeLog($error));
|
fwrite($fp, writeLog($error));
|
||||||
}
|
}
|
||||||
@@ -260,10 +257,11 @@ if ($run_gift_tests === true) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// MOCK TEST
|
// MOCK TEST
|
||||||
if ($mock_debug === true) {
|
if ($run_mocks === true) {
|
||||||
$mock_ok = '<span style="color:green;">MOCK OK</span>';
|
$mock_ok = '<span style="color:green;">MOCK OK</span>';
|
||||||
$mock_failure = '<span style="color:red;">MOCK FAILURE</span>';
|
$mock_failure = '<span style="color:red;">MOCK FAILURE</span>';
|
||||||
$mock_value = 500;
|
$mock_value = 500;
|
||||||
|
$mock = [];
|
||||||
|
|
||||||
$mock['F0000'] = [ 'ret' => '', 'st' => 'SUCCESS']; // success mock
|
$mock['F0000'] = [ 'ret' => '', 'st' => 'SUCCESS']; // success mock
|
||||||
$mock['F1000'] = [ 'ret' => 'F100', 'st' => 'FAILURE']; // SimpleAmountIsNull, etc
|
$mock['F1000'] = [ 'ret' => 'F100', 'st' => 'FAILURE']; // SimpleAmountIsNull, etc
|
||||||
@@ -284,7 +282,7 @@ if ($mock_debug === true) {
|
|||||||
foreach ($mock as $creation_id => $mock_return) {
|
foreach ($mock as $creation_id => $mock_return) {
|
||||||
print "<b>TS: " . microtime() . "</b>: ";
|
print "<b>TS: " . microtime() . "</b>: ";
|
||||||
try {
|
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();
|
$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();
|
||||||
@@ -302,7 +300,7 @@ if ($mock_debug === true) {
|
|||||||
}
|
}
|
||||||
fwrite($fp, writeLog((array)$aws_test));
|
fwrite($fp, writeLog((array)$aws_test));
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
$error = Amazon\AmazonIncentives::decodeExceptionMessage($e->getMessage());
|
$error = AmazonErrors::decodeExceptionMessage($e->getMessage());
|
||||||
print "AWS: MOCK: " . $creation_id . ": buyGiftCard: " . $error['status']
|
print "AWS: MOCK: " . $creation_id . ": buyGiftCard: " . $error['status']
|
||||||
. " [" . $e->getCode() . "]: "
|
. " [" . $e->getCode() . "]: "
|
||||||
. $error['code'] . " | " . $error['type']
|
. $error['code'] . " | " . $error['type']
|
||||||
|
|||||||
23
test/aws_read_env_tests.php
Normal file
23
test/aws_read_env_tests.php
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
// just print out env data nd connect data
|
||||||
|
// checkMe from AmazonIntentives call is requal to
|
||||||
|
// run_info_test === true in aws_gift_card_tests.php
|
||||||
|
|
||||||
|
$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>";
|
||||||
|
|
||||||
|
use gullevek\AmazonIncentives\AmazonIncentives;
|
||||||
|
use gullevek\dotEnv\DotEnv;
|
||||||
|
|
||||||
|
// load env data with dotenv
|
||||||
|
DotEnv::readEnvFile(__DIR__);
|
||||||
|
|
||||||
|
print "_ENV: <pre>" . print_r($_ENV, true) . "</pre>";
|
||||||
|
|
||||||
|
$aws = new AmazonIncentives();
|
||||||
|
print "checkMe: <pre>" . print_r($aws->checkMe(), true) . "</pre>";
|
||||||
|
|
||||||
|
// __END__
|
||||||
1102
test/phpUnit/AmazonIncentivesTest.php
Normal file
1102
test/phpUnit/AmazonIncentivesTest.php
Normal file
File diff suppressed because it is too large
Load Diff
21
test/phpUnit/Hook/BypassFinalHook.php
Normal file
21
test/phpUnit/Hook/BypassFinalHook.php
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
// strip the final name from a to be mocked class
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace test\phpUnit\Hook;
|
||||||
|
|
||||||
|
use DG\BypassFinals;
|
||||||
|
use PHPUnit\Runner\BeforeFirstTestHook;
|
||||||
|
|
||||||
|
// only works if it is the FIRST load and not before EACH test
|
||||||
|
final class BypassFinalHook implements BeforeFirstTestHook
|
||||||
|
{
|
||||||
|
public function executeBeforeFirstTest(): void
|
||||||
|
{
|
||||||
|
BypassFinals::enable();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// __END__
|
||||||
@@ -1,84 +0,0 @@
|
|||||||
<?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__
|
|
||||||
2
tmp/.gitignore
vendored
Normal file
2
tmp/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
*
|
||||||
|
!.gitignore
|
||||||
Reference in New Issue
Block a user