Skip to content

Commit

Permalink
Merge pull request #74 from hyperwallet/DEV-v3
Browse files Browse the repository at this point in the history
Dev v3
  • Loading branch information
akalichety-hw authored Nov 21, 2020
2 parents a58a39a + 210fb25 commit 8a47421
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
ChangeLog
=========
1.6.1
-------------------
Added custom headers

1.6.0
-------------------
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Hyperwallet REST SDK (Beta)
===========================

A library to manage users, transfer methods and payments through the Hyperwallet Rest V3 API
For Rest V4 APIs, please use SDK v2.1.0
For Rest V4 APIs, please use SDK v2.x.x

Prerequisites
------------
Expand Down
4 changes: 2 additions & 2 deletions src/Hyperwallet/Hyperwallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ class Hyperwallet {
*
* @throws HyperwalletArgumentException
*/
public function __construct($username, $password, $programToken = null, $server = 'https://api.sandbox.hyperwallet.com', $encryptionData = array()) {
public function __construct($username, $password, $programToken = null, $server = 'https://api.sandbox.hyperwallet.com', $encryptionData = array(), $clientOptions = array()) {
if (empty($username) || empty($password)) {
throw new HyperwalletArgumentException('You need to specify your API username and password!');
}

$this->programToken = $programToken;
$this->client = new ApiClient($username, $password, $server, array(), $encryptionData);
$this->client = new ApiClient($username, $password, $server, $clientOptions, $encryptionData);
}

//--------------------------------------
Expand Down
17 changes: 14 additions & 3 deletions src/Hyperwallet/Util/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Hyperwallet\Model\BaseModel;
use Hyperwallet\Response\ErrorResponse;
use Hyperwallet\Util\HyperwalletEncryption;
use Hyperwallet\Util\HyperwalletUUID;

/**
* The internal API client
Expand All @@ -22,7 +23,7 @@ class ApiClient {
*
* @var string
*/
const VERSION = '1.6.0';
const VERSION = '1.6.1';

/**
* The Guzzle http client
Expand All @@ -38,6 +39,13 @@ class ApiClient {
*/
private $encryption;

/**
* The UUID generator for http request/response
*
* @var HyperwalletUUID
*/
private $uuid;

/**
* Boolean flag that checks if ApiClient is constructed with encryption enabled or not
*
Expand All @@ -55,14 +63,17 @@ class ApiClient {
* @param array $encryptionData Encryption data to initialize ApiClient with encryption enabled
*/
public function __construct($username, $password, $server, $clientOptions = array(), $encryptionData = array()) {
$this->uuid = HyperwalletUUID::v4();
// Setup http client if not specified
$this->client = new Client(array_merge_recursive(array(
'base_uri' => $server,
'auth' => array($username, $password),
'headers' => array(
'User-Agent' => 'Hyperwallet PHP SDK v' . self::VERSION,
'Accept' => 'application/json'
)
'Accept' => 'application/json',
'x-sdk-version' => self::VERSION,
'x-sdk-type' => 'PHP',
'x-sdk-contextId' => $this->uuid)
), $clientOptions));
if (!empty($encryptionData) && isset($encryptionData['clientPrivateKeySetLocation']) &&
isset($encryptionData['hyperwalletKeySetLocation'])) {
Expand Down
57 changes: 57 additions & 0 deletions src/Hyperwallet/Util/HyperwalletUUID.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
namespace Hyperwallet\Util;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\UriTemplate;
use Hyperwallet\Exception\HyperwalletApiException;
use Hyperwallet\Exception\HyperwalletException;
use Hyperwallet\Model\BaseModel;
use Hyperwallet\Response\ErrorResponse;
use Composer\Autoload\ClassLoader;
use phpseclib\Crypt\RSA;
use phpseclib\Math\BigInteger;
use phpseclib\Crypt\Hash;
use JOSE_URLSafeBase64;
use JOSE_JWS;
use JOSE_JWE;
use JOSE_JWK;
use JOSE_JWT;

/**
* The encryption service for Hyperwallet client's requests/responses
*
* @package Hyperwallet\Util
*/
class HyperwalletUUID {

/**
* Generates UUID
*
* @return string
*
* @throws HyperwalletException
*/
public static function v4() {
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',

// 32 bits for "time_low"
mt_rand(0, 0xffff), mt_rand(0, 0xffff),

// 16 bits for "time_mid"
mt_rand(0, 0xffff),

// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand(0, 0x0fff) | 0x4000,

// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand(0, 0x3fff) | 0x8000,

// 48 bits for "node"
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
);
}
}
2 changes: 0 additions & 2 deletions tests/Hyperwallet/Tests/HyperwalletTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@

class HyperwalletTest extends \PHPUnit_Framework_TestCase {

private $includeIntegrationTest = false; //change this value to true if integration tests have to be run

public function testConstructor_throwErrorIfUsernameIsEmpty() {
try {
new Hyperwallet('', 'test-password');
Expand Down
2 changes: 1 addition & 1 deletion tests/Hyperwallet/Tests/Util/ApiClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,7 @@ private function validateRequest($method, $path, $query, array $body, $hasConten
$request = $this->container[0]['request'];
$this->assertEquals($method, $request->getMethod());

$this->assertCount(($hasContentType ? 6 : 4) + count($headers), $request->getHeaders());
$this->assertCount(($hasContentType ? 9 : 7) + count($headers), $request->getHeaders());
$this->assertArrayHasKeyAndValue('Accept', $isEncrypted ? 'application/jose+json' : 'application/json', $request->getHeaders());
if ($hasContentType) {
$this->assertArrayHasKeyAndValue('Content-Type', $isEncrypted ? 'application/jose+json' : 'application/json', $request->getHeaders());
Expand Down
2 changes: 1 addition & 1 deletion tests/Hyperwallet/Tests/Util/HyperwalletEncryptionTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Hyperwallet\Tests;
namespace Hyperwallet\Tests\Util;

use Hyperwallet\Util\HyperwalletEncryption;
use Hyperwallet\Exception\HyperwalletException;
Expand Down
23 changes: 23 additions & 0 deletions tests/Hyperwallet/Tests/Util/HyperwalletUUIDTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
namespace Hyperwallet\Tests\Util;

use Hyperwallet\Exception\HyperwalletException;
use Hyperwallet\Util\HyperwalletUUID;

class HyperwalletUUIDTest extends \PHPUnit_Framework_TestCase {

public function testShouldSuccessfullyGenerateRandomUUIDs() {
// Setup data
$UUIDUtility = new HyperwalletUUID();

// Execute test
$uuid1 = $UUIDUtility->v4();
$uuid2 = $UUIDUtility->v4();
$uuid3 = $UUIDUtility->v4();

// Validate result
$this->assertNotEquals($uuid1, $uuid2);
$this->assertNotEquals($uuid2, $uuid3);
$this->assertNotEquals($uuid3, $uuid1);
}
}

0 comments on commit 8a47421

Please sign in to comment.