Skip to content

Commit 0fdcaa1

Browse files
committed
Working version.
1 parent 5375107 commit 0fdcaa1

5 files changed

Lines changed: 144 additions & 1 deletion

File tree

README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,31 @@
1-
# avangate-rest-api-client
1+
This is a simple client built on top of Guzzle.
2+
3+
### Usage
4+
5+
#### Initialize by calling:
6+
7+
```php
8+
$client = new Client([
9+
'code' => MERCHANT_CODE,
10+
'key' => MERCHANT_APIKEY,
11+
'base_uri' => 'https://api.avangate.com/3.0/'
12+
]);
13+
```
14+
15+
#### Use as a Guzzle client
16+
17+
```php
18+
try {
19+
$response = $client->get('orders/?StartDate=2015-01-01');
20+
$orderListing = json_decode($response->getBody()->getContents());
21+
22+
print_r($orderListing);
23+
24+
} catch (\GuzzleHttp\Exception\ClientException $e) {
25+
$contents = json_decode($e->getResponse()->getBody()->getContents());
26+
var_dump($contents->message);
27+
28+
} catch (\Exception $e) {
29+
var_dump($e->getMessage());
30+
}
31+
```

composer.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "bogdananton/avangate-rest-api-client",
3+
"license": "MIT",
4+
"require": {
5+
"php": "^5.6",
6+
"guzzlehttp/guzzle": "6.1.0"
7+
},
8+
"autoload": {
9+
"psr-4": {
10+
"AvangateClient\\" : "src/"
11+
}
12+
}
13+
}

phpunit.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<phpunit bootstrap="vendor/autoload.php" colors="true">
3+
<testsuites>
4+
<testsuite name="all">
5+
<directory suffix=".php">tests/</directory>
6+
</testsuite>
7+
</testsuites>
8+
<php>
9+
<env name="no_proxy" value="*" />
10+
<const name="MERCHANT_CODE" value="*****" />
11+
<const name="MERCHANT_APIKEY" value="********************" />
12+
</php>
13+
</phpunit>

src/Client.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
namespace AvangateClient;
3+
4+
class Client extends \GuzzleHttp\Client
5+
{
6+
public function __construct(array $setup)
7+
{
8+
$date = date('Y-m-d H:i:s');
9+
$accept = (array_key_exists('headers.accept', $setup) ? $setup['headers.accept'] : 'application/json');
10+
11+
$code = $setup['code'];
12+
$key = $setup['key'];
13+
$hash = hash_hmac('md5', strlen($code) . $code . strlen($date) . $date, $key);
14+
15+
$headers = [
16+
'X-Avangate-Authentication' => 'code="' . $code . '" date="' . $date . '" hash="' . $hash . '"',
17+
'Accept' => $accept
18+
];
19+
20+
$setup['headers'] = array_key_exists('headers', $setup) ? array_merge($setup['headers'], $headers) : $headers;
21+
unset($setup['code'], $setup['key'], $setup['version']);
22+
23+
parent::__construct($setup);
24+
}
25+
}

tests/ConnectTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
namespace tests;
3+
4+
use AvangateClient\Client;
5+
use GuzzleHttp\Exception\ClientException;
6+
7+
class ConnectTest extends \PHPUnit_Framework_TestCase
8+
{
9+
public function setUp()
10+
{
11+
if (MERCHANT_CODE === '*****') {
12+
$message = 'Set valid credentials (MERCHANT_CODE / MERCHANT_APIKEY) in phpunit.xml file.';
13+
static::fail($message . ' Check https://secure.avangate.com/cpanel/account_settings.php for details.');
14+
}
15+
}
16+
17+
/**
18+
* When invalid credentials are given then throw exception.
19+
*/
20+
public function testWhenInvalidCredentialsAreGivenThenThrowException()
21+
{
22+
$client = new Client([
23+
'code' => 'INVALID-MERCHANT-CODE',
24+
'key' => 'INVALID-MERCHANT-APIKEY',
25+
'base_uri' => 'https://api.avangate.com/3.0/'
26+
]);
27+
28+
try {
29+
$client->get('currencies/');
30+
static::fail('When you see it, an expected exception was not thrown.');
31+
32+
} catch (ClientException $e) {
33+
static::assertEquals(401, $e->getResponse()->getStatusCode());
34+
$sentDetails = json_decode($e->getResponse()->getBody()->getContents());
35+
36+
static::assertEquals('AUTHENTICATION_ERROR', $sentDetails->error_code);
37+
static::assertEquals('Authentication needed for this resource', $sentDetails->message);
38+
}
39+
}
40+
41+
/**
42+
* When valid credentials are set then return valid details.
43+
*/
44+
public function testWhenValidCredentialsAreSetThenReturnValidDetails()
45+
{
46+
$client = new Client([
47+
'code' => MERCHANT_CODE,
48+
'key' => MERCHANT_APIKEY,
49+
'base_uri' => 'https://api.avangate.com/3.0/'
50+
]);
51+
52+
$allowedCurrencies = json_decode($client->get('currencies/')->getBody()->getContents(), true);
53+
54+
static::assertTrue(is_array($allowedCurrencies));
55+
static::assertTrue(count($allowedCurrencies) > 0);
56+
57+
foreach ($allowedCurrencies as $currency) {
58+
static::assertEquals(strtolower($currency), $currency);
59+
static::assertEquals(3, strlen($currency));
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)