-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9e86cdd
Showing
20 changed files
with
1,568 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/nbproject | ||
/composer.lock | ||
/vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015 Jason Varga | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# Active Ants API client | ||
|
||
|
||
This package provides a wrapper for the ActiveAnts ShopApi. This package was developed | ||
by Afosto to make a reliable connection between Afosto (Retail Software) and Active Ants | ||
and provides all the basic functionality. | ||
|
||
|
||
## Installation | ||
|
||
To install, use composer: | ||
|
||
``` | ||
composer require afosto/active-ants | ||
``` | ||
|
||
## Usage | ||
|
||
First get an account at ActiveAnts and obtain a username and password for the | ||
ShopApi. | ||
|
||
Start the app with the following code. The application will obtain an authorization-token, | ||
retreive settings and cache these in the cache folder. | ||
|
||
```php | ||
App::start($endpoint, $username, $password, $cacheDirectory); | ||
``` | ||
|
||
Below you'll find a subset of the available methods. | ||
|
||
|
||
### Create a product | ||
|
||
```php | ||
$product = Product::model() | ||
->setName('testProduct') | ||
->setSku('testSku'); | ||
|
||
if ($product->save()) { | ||
echo "Product was saved"; | ||
} | ||
``` | ||
|
||
|
||
### Create an order | ||
|
||
```php | ||
$item = OrderItem::model() | ||
->setSku('testSku') | ||
->setGrossPrice(1.21) | ||
->setName('Test Product') | ||
->setTaxRate(21); | ||
|
||
$address = Address::model() | ||
->setName('Afosto SaaS BV') | ||
->setAddress('Protonstraat', 9, 'a') | ||
->setCity('Groningen') | ||
->setCountry('NL') | ||
->setPostalcode('9743AL'); | ||
|
||
$order = Order::model() | ||
->setEmail('[email protected]') | ||
->setOrderId('#1') | ||
->setPhoneNumber('0507119519') | ||
->addOrderItem($item) | ||
->setBillingAddress($address) | ||
->setShippingAddress($address); | ||
|
||
if ($order->save()) { | ||
echo "Order was saved"; | ||
} | ||
``` | ||
|
||
|
||
### Get stock for all products | ||
|
||
```php | ||
foreach (Stock::model()->findAll() as $stock) { | ||
echo $stock->sku . ': ' . $stock->stock . "\n"; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"name": "afosto/active-ants", | ||
"description": "Client for ActiveAnts API", | ||
"type": "package", | ||
"require": { | ||
"guzzlehttp/guzzle": "^6.1" | ||
}, | ||
"keywords": [ | ||
"active", | ||
"ants", | ||
"client", | ||
"afosto", | ||
"api" | ||
], | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Afosto Development Team", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"Afosto\\ActiveAnts\\": ["src", "src/models", "src/exception", "src/app"] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace Afosto\ActiveAnts; | ||
|
||
require_once(dirname(__FILE__) . '/vendor/autoload.php'); | ||
|
||
//Make sure this directory is writable | ||
$cacheDirectory = dirname(__FILE__) . '/../cache/'; | ||
|
||
App::start('', '', '', $cacheDirectory); | ||
|
||
$product = Product::model() | ||
->setName('testProduct') | ||
->setSku('testSku'); | ||
|
||
if (!$product->save()) { | ||
echo $product->getMessage(); | ||
} | ||
|
||
$item = OrderItem::model() | ||
->setSku('testSku', false) | ||
->setGrossPrice(1.21) | ||
->setName('testProduct') | ||
->setTaxRate(21); | ||
|
||
$address = Address::model() | ||
->setName('Afosto SaaS BV') | ||
->setAddress('Protonstraat', 9, 'a') | ||
->setCity('Groningen') | ||
->setCountry('NL') | ||
->setPostalcode('9743AL'); | ||
|
||
$order = Order::model() | ||
->setEmail('[email protected]') | ||
->setOrderId('#' . rand(100,999)) | ||
->setPhoneNumber('test') | ||
->addOrderItem($item) | ||
->setBillingAddress($address) | ||
->setShippingAddress($address); | ||
|
||
|
||
if (!$order->save()) { | ||
echo $order->getMessage(); | ||
} | ||
|
||
$purchase = PurchaseOrder::model() | ||
->addItem('testSku', 1) | ||
->addReference('testPurchaseOrder'); | ||
|
||
if (!$purchase->save()) { | ||
echo $purchase->getMessage(); | ||
} | ||
|
||
foreach (Stock::model()->findAll() as $stock) { | ||
echo $stock->sku . ': ' . $stock->stock . "\n"; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
namespace Afosto\ActiveAnts; | ||
|
||
class App { | ||
|
||
/** | ||
* Contains the client | ||
* @var Client | ||
*/ | ||
public $client; | ||
|
||
/** | ||
* The settings | ||
* @var Settings | ||
*/ | ||
public $settings; | ||
|
||
/** | ||
* The cache component | ||
* @var Cache | ||
*/ | ||
public $cache; | ||
|
||
/** | ||
* The session hash (user connected) | ||
* @var string | ||
*/ | ||
private $_hash; | ||
|
||
/** | ||
* Contains the app | ||
* @var App | ||
*/ | ||
private static $app; | ||
|
||
/** | ||
* Build an api client | ||
* @param sring $endpoint | ||
* @param string $username | ||
* @param string $password | ||
* @param string $cacheDirectory | ||
*/ | ||
public static function start($endpoint, $username, $password, $cacheDirectory) { | ||
if (self::$app == null) { | ||
self::$app = new self(); | ||
self::$app->setAuth($username, $password)->setEndpoint($endpoint)->setCacheDirectory($cacheDirectory); | ||
self::$app->client->authorize(); | ||
} | ||
return self::$app; | ||
} | ||
|
||
/** | ||
* Return the ready instance | ||
* @param boolean $debug | ||
* @return App | ||
*/ | ||
public static function getInstance() { | ||
if (self::$app == null) { | ||
throw new ApiException('Not connected'); | ||
} | ||
return self::$app; | ||
} | ||
/** | ||
* Set the authorization parameters | ||
* @param string $username | ||
* @param string $password | ||
* @return App | ||
*/ | ||
public function setAuth($username, $password) { | ||
$this->client = new Client(); | ||
$this->client->setUsername($username); | ||
$this->client->setPassword($password); | ||
$this->_hash = sha1($username . $password); | ||
return $this; | ||
} | ||
|
||
|
||
/** | ||
* Set the authorization parameters | ||
* @param string $endpoint | ||
* @return App | ||
*/ | ||
public function setEndpoint($endpoint) { | ||
$this->endpoint = $endpoint; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Prepare the cache component | ||
* @param string $cacheDirectory | ||
* @return \Afosto\ActiveAnts\App | ||
*/ | ||
public function setCacheDirectory($cacheDirectory) { | ||
$this->cache = new Cache($this->_hash, $cacheDirectory); | ||
return $this; | ||
} | ||
|
||
/** | ||
* Return the active ants settings | ||
* @return Settings | ||
*/ | ||
public function getSettings() { | ||
if (is_null($this->settings)) { | ||
$this->settings = Settings::load(); | ||
} | ||
return $this->settings; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
namespace Afosto\ActiveAnts; | ||
|
||
class Cache { | ||
|
||
/** | ||
* Set the user session hash | ||
* @var string | ||
*/ | ||
private $_hash; | ||
|
||
/** | ||
* The path to the cache directory | ||
* @var string | ||
*/ | ||
private $_cacheDirectory; | ||
|
||
/** | ||
* Set the directory used for caching | ||
* @param string $hash | ||
* @param string $cacheDirectory | ||
* @throws ApiException | ||
*/ | ||
public function __construct($hash, $cacheDirectory) { | ||
$this->_hash = $hash; | ||
$this->_cacheDirectory = $cacheDirectory; | ||
if (!is_writable($cacheDirectory)) { | ||
throw new ApiException('Cache directory is not writable'); | ||
} | ||
return $this; | ||
} | ||
|
||
/** | ||
* Set the cache key value | ||
* @param string $key | ||
* @param string $contents | ||
* @param integer $expiry Seconds untill expiration | ||
*/ | ||
public function setCache($key, $contents, $expiry) { | ||
if (file_put_contents($this->_cacheDirectory. '/' . $this->_getCacheKey($key), serialize($contents)) === FALSE) { | ||
throw new ApiException('Cache storage failed'); | ||
} | ||
touch($this->_cacheDirectory . '/' . $this->_getCacheKey($key), (time() + (int) $expiry)); | ||
} | ||
|
||
/** | ||
* Returns a cache key | ||
* @param string $key | ||
* @return string|boolean | ||
*/ | ||
public function getCache($key) { | ||
if (file_exists($this->_cacheDirectory . '/' . $this->_getCacheKey($key))) { | ||
if (filemtime($this->_cacheDirectory . '/' . $this->_getCacheKey($key)) < time()) { | ||
unlink($this->_cacheDirectory . '/' . $this->_getCacheKey($key)); | ||
return false; | ||
} | ||
return unserialize(file_get_contents($this->_cacheDirectory . '/' . $this->_getCacheKey($key))); | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Returns the cache key, based on client specific data | ||
* @param string $key | ||
* @return string | ||
*/ | ||
private function _getCacheKey($key) { | ||
return $this->_hash . '-' . $key . '.bin'; | ||
} | ||
|
||
} |
Oops, something went wrong.