Skip to content

Commit

Permalink
Fix #58, #55, #52, #25
Browse files Browse the repository at this point in the history
Update Refresh Token part for OAuth 2
Fix bugs that cause cURL is not closed correctly
  • Loading branch information
hlu2 committed Aug 17, 2017
1 parent 60efee4 commit ae806ee
Show file tree
Hide file tree
Showing 20 changed files with 4,813 additions and 90 deletions.
11 changes: 6 additions & 5 deletions src/Core/Configuration/LocalConfigReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,12 @@ public static function initializeOAuthSettings($xmlObj, $ippConfig, $OAuthOption
// Set SSL check status to be true
$ippConfig->SSLCheckStatus = true;
$currentOAuth2AccessTokenKey = $xmlObj->intuit->ipp->security->oauth2->attributes()['accessTokenKey'];
$currentOAuth2RefreshTokenKey = $xmlObj->intuit->ipp->security->oauth1->attributes()['refreshTokenKey'];
$currentConsumerKey = $xmlObj->intuit->ipp->security->oauth1->attributes()['ClientID'];
$currentConsumerSecret = $xmlObj->intuit->ipp->security->oauth1->attributes()['ClientSecret'];
$ippConfig->RealmID = $xmlObj->intuit->ipp->security->oauth1->attributes()['QBORealmID'];
$OAuth2AccessToken = new OAuth2AccessToken($currentOAuth2AccessTokenKey, $currentOAuth2RefreshTokenKey, $currentConsumerKey, $currentConsumerSecret);
$currentOAuth2RefreshTokenKey = $xmlObj->intuit->ipp->security->oauth2->attributes()['refreshTokenKey'];
$clientID = $xmlObj->intuit->ipp->security->oauth2->attributes()['ClientID'];
$clientSecret = $xmlObj->intuit->ipp->security->oauth2->attributes()['ClientSecret'];
$ippConfig->RealmID = $xmlObj->intuit->ipp->security->oauth2->attributes()['QBORealmID'];
$OAuth2AccessToken = new OAuth2AccessToken($clientID, $clientSecret, $currentOAuth2AccessTokenKey, $currentOAuth2RefreshTokenKey);

$ippConfig->Security = $OAuth2AccessToken;
$ippConfig->OAuthMode = CoreConstants::OAUTH2;
} else {
Expand Down
23 changes: 22 additions & 1 deletion src/Core/CoreConstants.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ class CoreConstants
*/
const RESOURCE = "resource";

/**
* The Content Type String.
* @var string CONTENT_TYPE
*/
const CONTENT_TYPE = "Content-Type";

/**
* Content type: text/xml.
* @var string CONTENTTYPE_TEXTXML
Expand Down Expand Up @@ -170,9 +176,14 @@ class CoreConstants
* @var string QBO_BASEURL
*/
const QBO_BASEURL = "https://quickbooks.api.intuit.com/";
const PRODUCTION_QBO = "Production";


const IPP_BASEURL = "https://appcenter.intuit.com/api/";

const DEVELOPMENT_SANDBOX = "Development";
const SANDBOX_DEVELOPMENT = "https://sandbox-quickbooks.api.intuit.com";

/**
* Id Parameter Name.
* @var string Id
Expand Down Expand Up @@ -255,7 +266,7 @@ class CoreConstants
* The Request source header value.
* @var string REQUESTSOURCEHEADER
*/
const USERAGENT = "V3PHPSDK3.3.0";
const USERAGENT = "V3PHPSDK3.4.0";

public static function getType($string, $return=1)
{
Expand Down Expand Up @@ -285,4 +296,14 @@ public static function getQuickBooksOnlineAPIEntityRules()
"IPPEstimate" => array( "DownloadPDF" => true, "SendEmail" => true ),
);
}

//--------------------------------------------------------------------------------------------------
//OAuth 2
const OAUTH2_TOKEN_ENDPOINT_URL = "https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer";
const OAUTH2_REFRESH_GRANTYPE = "refresh_token";
const OAUTH2_AUTHORIZATION_TYPE = "Basic ";
const EXPIRES_IN = "expires_in";
const X_REFRESH_TOKEN_EXPIRES_IN = "x_refresh_token_expires_in";
const ACCESS_TOKEN = "access_token";

}
2 changes: 2 additions & 0 deletions src/Core/HttpClients/BaseCurl.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@ public function getInfo($type)

/**
* Close the resource connection to curl
* Remove the pointer after closed
*/
public function close()
{
curl_close($this->curl);
$this->curl = null;
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/Core/HttpClients/CurlHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ private function getIntuitResponse(){
}

private function intializeCurl(){

if($this->basecURL->isCurlSet()){ return; }
else {$this->basecURL->init();}
}
Expand All @@ -104,7 +103,7 @@ private function setSSL(&$curl_opt, $verifySSL){
if($verifySSL){
$curl_opt[CURLOPT_SSL_VERIFYPEER] = true;
$curl_opt[CURLOPT_SSL_VERIFYHOST] = 2;
$curl_opt[CURLOPT_CAINFO] = dirname(dirname(__FILE__)) . "/OAuth/OAuth2/certs/apiintuitcom.pem"; //Pem certification Key Path
$curl_opt[CURLOPT_CAINFO] = dirname(dirname(__FILE__)) . "/OAuth/OAuth2/certs/cacert.pem"; //Pem certification Key Path
}
}

Expand Down
24 changes: 21 additions & 3 deletions src/Core/HttpClients/FaultHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,19 @@ public function getIntuitErrorMessage(){
public function getIntuitErrorDetail(){
return $this->intuitErrorDetail;
}
public function parseResponse($message){

/**
* Only parse XML format to string values now
*/
public function parseResponse($message, $contentType){
if(!$this->isValidXml($message)){
return;
}

$xmlObj = simplexml_load_string($message);

if(!$this->isStandardFormat($xmlObj)){
return;
}
$type = (string)$xmlObj->Fault->attributes()['type'];
if(isset($type) && !empty($type)){
$this->intuitErrorType = $type;
Expand All @@ -138,8 +145,19 @@ public function parseResponse($message){

}

/**
* Check if the format is standard Intuit Response format
*/
private function isStandardFormat($xmlObj)
{
if(!isset($xmlObj->Fault) || !isset($xmlObj->Fault->Error)){
return false;
}
return true;
}

private function isValidXml($content)
{
{
$content = trim($content);
if (empty($content)) {
return false;
Expand Down
20 changes: 19 additions & 1 deletion src/Core/HttpClients/IntuitResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
namespace QuickBooksOnline\API\Core\HttpClients;

use QuickBooksOnline\API\Exception\SdkException;
use QuickBooksOnline\API\Core\CoreConstants;



class IntuitResponse{

Expand All @@ -13,6 +16,8 @@ class IntuitResponse{

private $faultHandler;

private $contentType;

public function __construct($passedHeaders, $passedBody, $passedHttpResponseCode){
if(isset($passedHeaders)){
$this->setHeaders($passedHeaders);
Expand All @@ -32,7 +37,7 @@ public function __construct($passedHeaders, $passedBody, $passedHttpResponseCode
$this->faultHandler = new FaultHandler();
$this->faultHandler->setHttpStatusCode($this->httpResponseCode);
$this->faultHandler->setResponseBody($this->body);
$this->faultHandler->parseResponse($this->body);
$this->faultHandler->parseResponse($this->body, $this->contentType);
//Manually set the error message
$this->faultHandler->setOAuthHelperError("Invalid auth/bad request (got a " .$passedHttpResponseCode . ", expected HTTP/1.1 20X or a redirect)");
}
Expand All @@ -50,11 +55,20 @@ public function setHeaders($rawHeaders){
}else {
list($key, $value) = explode(': ', $line);
$this->headers[$key] = $value;
//set response content type
$this->setContentType($key, $value);
}
}

}

private function setContentType($key, $val){
$trimedKey = trim($key);
if(strcasecmp($trimedKey, CoreConstants::CONTENT_TYPE) == 0){
$this->contentType = trim($val);
}
}

public function getHeaders(){
return $this->headers;
}
Expand All @@ -71,4 +85,8 @@ public function getFaultHandler(){
return $this->faultHandler;
}

public function getResponseContentType(){
return $this->contentType;
}

}
6 changes: 6 additions & 0 deletions src/Core/HttpClients/RestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ class RestHandler
*/
protected $RequestSerializer;

/**
* Get the Logging component for the REST service
* @var RequestLogging
*/
protected $RequestLogging;

/**
*
* Initializes a new instance of the RestHandler class.
Expand Down
8 changes: 8 additions & 0 deletions src/Core/HttpClients/SyncRestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ public function __construct($context)
return $this;
}

public function updateContext($newServiceContext){
if($newServiceContext instanceof ServiceContext){
$this->context = $newServiceContext;
}else{
throw new SdkException("Cannot Update Service Context.");
}
}

//----------------New Added Method to get Last error
/**
* Return an representation of an error returned by the last request, or null
Expand Down
Loading

0 comments on commit ae806ee

Please sign in to comment.