Skip to content

Commit

Permalink
Merge pull request #12 from stephangroen/master
Browse files Browse the repository at this point in the history
Authentication update for API version 2015-04-29
  • Loading branch information
Cristian Giordano committed May 1, 2015
2 parents 6b4ba7a + 0e09be8 commit aaa56be
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 26 deletions.
5 changes: 2 additions & 3 deletions config.example.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* This configuration is only needed for running tests.
*/
return [
'version' => '2014-11-03',
'username' => '',
'password' => '',
'version' => '2015-04-29',
'accessToken' => '',
];
2 changes: 1 addition & 1 deletion examples/hello-money.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @link https://manage-sandbox.gocardless.com/organisation
* @link https://developer.gocardless.com/pro/#overview-backwards-compatibility
*/
$api = new Api(new Client, 'API_ID', 'API_KEY', '2014-11-03');
$api = new Api(new Client, 'ACCESS_TOKEN', '2015-04-29');

/** On with the show */

Expand Down
27 changes: 9 additions & 18 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,7 @@ class Api
/**
* @var string
*/
private $username;

/**
* @var string
*/
private $password;
private $accessToken;

/**
* @var string
Expand All @@ -53,11 +48,10 @@ class Api
*/
private $environment;

public function __construct(Client $client, $username, $password, $version, $environment = 'staging')
public function __construct(Client $client, $accessToken, $version, $environment = 'staging')
{
$this->client = $client;
$this->username = $username;
$this->password = $password;
$this->accessToken = $accessToken;
$this->version = $version;
$this->environment = $environment === 'production' ? 'production' : 'staging';
}
Expand Down Expand Up @@ -382,8 +376,7 @@ public function createMandatePdf($options = [])

$response = $this->client->post($this->url($endpoint, $path), [
'headers' => $headers,
'json' => $payload,
'auth' => [$this->username, $this->password]
'json' => $payload
]);
} catch (BadResponseException $ex) {
$this->handleBadResponseException($ex);
Expand Down Expand Up @@ -413,8 +406,7 @@ public function getMandatePdf($id)

try {
$response = $this->client->get($this->url($endpoint, $path), [
'headers' => $headers,
'auth' => [$this->username, $this->password]
'headers' => $headers
]);
} catch (BadResponseException $ex) {
$this->handleBadResponseException($ex);
Expand Down Expand Up @@ -491,8 +483,7 @@ private function get($endpoint, $params = [], $path = null)
try {
$response = $this->client->get($this->url($endpoint, $path), [
'headers' => $this->headers(),
'query' => $params,
'auth' => [$this->username, $this->password]
'query' => $params
])->json();
} catch (BadResponseException $ex) {
$this->handleBadResponseException($ex);
Expand Down Expand Up @@ -542,8 +533,7 @@ private function send($method, $endpoint, $data = [], $path)

$response = $this->client->$method($this->url($endpoint, $path), [
'headers' => $this->headers(),
'json' => $payload,
'auth' => [$this->username, $this->password]
'json' => $payload
])->json();
} catch (BadResponseException $ex) {
$this->handleBadResponseException($ex);
Expand Down Expand Up @@ -580,7 +570,8 @@ private function headers()
{
return [
'GoCardless-Version' => $this->version,
'Content-Type' => 'application/json'
'Content-Type' => 'application/json',
'Authorization' => sprintf('Bearer %s', $this->accessToken)
];
}

Expand Down
7 changes: 3 additions & 4 deletions tests/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@ public function setUp()
{
$config = require __DIR__ . '/../config.php';

$this->api = new Api(new Client, $config['username'], $config['password'], $config['version']);
$this->api = new Api(new Client, $config['accessToken'], $config['version']);
}

/** @test */
function it_can_be_created()
{
$this->assertInstanceOf('GoCardless\Pro\Api', $this->api);
$this->assertAttributeInstanceOf('GuzzleHttp\Client', 'client', $this->api);
$this->assertAttributeNotEmpty('username', $this->api);
$this->assertAttributeNotEmpty('password', $this->api);
$this->assertAttributeNotEmpty('accessToken', $this->api);
$this->assertAttributeEquals('staging', 'environment', $this->api);
}

Expand Down Expand Up @@ -471,7 +470,7 @@ function test_it_throws_version_not_found_exception_if_api_returns_this_error()

$config = require __DIR__ . '/../config.php';

$api = new Api(new Client, $config['username'], $config['password'], '1970-01-01');
$api = new Api(new Client, $config['accessToken'], '1970-01-01');

$api->listCustomers();
}
Expand Down

0 comments on commit aaa56be

Please sign in to comment.