Skip to content

Commit

Permalink
Cobrand login functional.
Browse files Browse the repository at this point in the history
  • Loading branch information
kmanuzon committed May 7, 2016
1 parent 16fb76b commit ecfd69d
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 4 deletions.
42 changes: 42 additions & 0 deletions src/Api/Api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Yodlee\Api;

abstract class Api
{
/**
* Base URL of the Yodlee API.
*
* @var string
*/
protected $baseUrl = 'https://developer.api.yodlee.com/ysl';

/**
* Get the Base URL of Yodlee API.
*
* @return string
*/
protected function getBaseUrl()
{
return trim($this->baseUrl, '/');
}

/**
* Get the full URL to the endpoint.
*
* @param string
* @param string
* @return string
*/
protected function getUrl($cobrandName, $endpoint)
{
$url = sprintf(
'%s/%s/v1/%s',
$this->getBaseUrl(),
trim($cobrandName, '/'),
trim($endpoint, '/')
);

return $url;
}
}
21 changes: 19 additions & 2 deletions src/Api/Endpoints/Cobrand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Yodlee\Api\Endpoints;

use Yodlee\Api\Api;
use Yodlee\Api\Factory;
use Yodlee\RestClient\Curl;

class Cobrand
class Cobrand extends Api
{
/**
* The API factory instance.
Expand All @@ -26,9 +28,24 @@ public function __construct(Factory $factory)
/**
* Authenticate cobrand to get cobrand session token.
*
* @param string
* @param string
* @param string
* @return bool
*/
public function postLogin()
public function postLogin($cobrandName, $cobrandLogin, $cobrandPassword)
{
$endpoint = '/cobrand/login';

$url = $this->getUrl($cobrandName, $endpoint);

$result = Curl::callApi('POST', $url, [
'cobrandName' => $cobrandName,
'cobrandLogin' => $cobrandLogin,
'cobrandPassword' => $cobrandPassword
]);

return true;
}

/**
Expand Down
18 changes: 16 additions & 2 deletions src/Api/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@

class Factory
{
/**
* Cobrand session token.
*
* @var string
*/
protected $cobSession;

/**
* User session token.
*
* @var string
*/
protected $userSession;

/**
* Create a new API factory instance.
*
Expand Down Expand Up @@ -47,8 +61,8 @@ public function user()
*/
public function transactions()
{
$user = new Transactions($this);
$transactions = new Transactions($this);

return $user;
return $transactions;
}
}
74 changes: 74 additions & 0 deletions src/RestClient/Curl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Yodlee\RestClient;

class Curl
{
/**
* Call API via cURL.
*
* Regarding CURLOPT_POSTFIELDS, passing an array will encode the request as
* multipart/form-data while URL encoded string will be in
* application/x-www-form-urlencoded.
* @see http://php.net/manual/en/function.curl-setopt.php#84916
* @see http://stackoverflow.com/a/4073451/4183317
*
* @param string
* @param string
* @param array
* @return array
*/
public static function callApi($method = 'GET', $url = '', array $parameters = [])
{
$return_values = array();

$ch = curl_init();

curl_setopt_array($ch, [
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_URL => $url,
CURLOPT_POSTFIELDS => http_build_query($parameters),
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_TIMEOUT => 360
]);

$response = curl_exec($ch);

if (curl_errno($ch)) {
$return_values['Error'] = "Failed to reach $url.";
} else {
if ($response) {
if (gettype($response) == "string") {
$result = json_decode($response);
if ($result) {
$exitsError = array_key_exists("Error", $result);
if ($exitsError) {
// @todo
//$return_values["Body"] = self::_getErrors($result);
$return_values["Body"] = 'Get errors';
} else {
$return_values["Body"] = $result;
}
} else {
$result = simplexml_load_string($response);
$return_values["Body"] = $response;
}
} else {
$result = json_decode($response);
if ($result === null) {
$return_values['Body'] = "The request does not return any value.";
} else {
$return_values["Body"] = $result;
}
}
} else {
$return_values['Error'] = "Failed to reach $url.";
}
}

curl_close($ch);

return $return_values;
}
}

0 comments on commit ecfd69d

Please sign in to comment.