Skip to content

Commit ae806ee

Browse files
committed
Fix #58, #55, #52, #25
Update Refresh Token part for OAuth 2 Fix bugs that cause cURL is not closed correctly
1 parent 60efee4 commit ae806ee

20 files changed

+4813
-90
lines changed

src/Core/Configuration/LocalConfigReader.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,12 @@ public static function initializeOAuthSettings($xmlObj, $ippConfig, $OAuthOption
246246
// Set SSL check status to be true
247247
$ippConfig->SSLCheckStatus = true;
248248
$currentOAuth2AccessTokenKey = $xmlObj->intuit->ipp->security->oauth2->attributes()['accessTokenKey'];
249-
$currentOAuth2RefreshTokenKey = $xmlObj->intuit->ipp->security->oauth1->attributes()['refreshTokenKey'];
250-
$currentConsumerKey = $xmlObj->intuit->ipp->security->oauth1->attributes()['ClientID'];
251-
$currentConsumerSecret = $xmlObj->intuit->ipp->security->oauth1->attributes()['ClientSecret'];
252-
$ippConfig->RealmID = $xmlObj->intuit->ipp->security->oauth1->attributes()['QBORealmID'];
253-
$OAuth2AccessToken = new OAuth2AccessToken($currentOAuth2AccessTokenKey, $currentOAuth2RefreshTokenKey, $currentConsumerKey, $currentConsumerSecret);
249+
$currentOAuth2RefreshTokenKey = $xmlObj->intuit->ipp->security->oauth2->attributes()['refreshTokenKey'];
250+
$clientID = $xmlObj->intuit->ipp->security->oauth2->attributes()['ClientID'];
251+
$clientSecret = $xmlObj->intuit->ipp->security->oauth2->attributes()['ClientSecret'];
252+
$ippConfig->RealmID = $xmlObj->intuit->ipp->security->oauth2->attributes()['QBORealmID'];
253+
$OAuth2AccessToken = new OAuth2AccessToken($clientID, $clientSecret, $currentOAuth2AccessTokenKey, $currentOAuth2RefreshTokenKey);
254+
254255
$ippConfig->Security = $OAuth2AccessToken;
255256
$ippConfig->OAuthMode = CoreConstants::OAUTH2;
256257
} else {

src/Core/CoreConstants.php

+22-1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ class CoreConstants
117117
*/
118118
const RESOURCE = "resource";
119119

120+
/**
121+
* The Content Type String.
122+
* @var string CONTENT_TYPE
123+
*/
124+
const CONTENT_TYPE = "Content-Type";
125+
120126
/**
121127
* Content type: text/xml.
122128
* @var string CONTENTTYPE_TEXTXML
@@ -170,9 +176,14 @@ class CoreConstants
170176
* @var string QBO_BASEURL
171177
*/
172178
const QBO_BASEURL = "https://quickbooks.api.intuit.com/";
179+
const PRODUCTION_QBO = "Production";
180+
173181

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

184+
const DEVELOPMENT_SANDBOX = "Development";
185+
const SANDBOX_DEVELOPMENT = "https://sandbox-quickbooks.api.intuit.com";
186+
176187
/**
177188
* Id Parameter Name.
178189
* @var string Id
@@ -255,7 +266,7 @@ class CoreConstants
255266
* The Request source header value.
256267
* @var string REQUESTSOURCEHEADER
257268
*/
258-
const USERAGENT = "V3PHPSDK3.3.0";
269+
const USERAGENT = "V3PHPSDK3.4.0";
259270

260271
public static function getType($string, $return=1)
261272
{
@@ -285,4 +296,14 @@ public static function getQuickBooksOnlineAPIEntityRules()
285296
"IPPEstimate" => array( "DownloadPDF" => true, "SendEmail" => true ),
286297
);
287298
}
299+
300+
//--------------------------------------------------------------------------------------------------
301+
//OAuth 2
302+
const OAUTH2_TOKEN_ENDPOINT_URL = "https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer";
303+
const OAUTH2_REFRESH_GRANTYPE = "refresh_token";
304+
const OAUTH2_AUTHORIZATION_TYPE = "Basic ";
305+
const EXPIRES_IN = "expires_in";
306+
const X_REFRESH_TOKEN_EXPIRES_IN = "x_refresh_token_expires_in";
307+
const ACCESS_TOKEN = "access_token";
308+
288309
}

src/Core/HttpClients/BaseCurl.php

+2
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,12 @@ public function getInfo($type)
8383

8484
/**
8585
* Close the resource connection to curl
86+
* Remove the pointer after closed
8687
*/
8788
public function close()
8889
{
8990
curl_close($this->curl);
91+
$this->curl = null;
9092
}
9193
}
9294

src/Core/HttpClients/CurlHttpClient.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ private function getIntuitResponse(){
8686
}
8787

8888
private function intializeCurl(){
89-
9089
if($this->basecURL->isCurlSet()){ return; }
9190
else {$this->basecURL->init();}
9291
}
@@ -104,7 +103,7 @@ private function setSSL(&$curl_opt, $verifySSL){
104103
if($verifySSL){
105104
$curl_opt[CURLOPT_SSL_VERIFYPEER] = true;
106105
$curl_opt[CURLOPT_SSL_VERIFYHOST] = 2;
107-
$curl_opt[CURLOPT_CAINFO] = dirname(dirname(__FILE__)) . "/OAuth/OAuth2/certs/apiintuitcom.pem"; //Pem certification Key Path
106+
$curl_opt[CURLOPT_CAINFO] = dirname(dirname(__FILE__)) . "/OAuth/OAuth2/certs/cacert.pem"; //Pem certification Key Path
108107
}
109108
}
110109

src/Core/HttpClients/FaultHandler.php

+21-3
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,19 @@ public function getIntuitErrorMessage(){
110110
public function getIntuitErrorDetail(){
111111
return $this->intuitErrorDetail;
112112
}
113-
public function parseResponse($message){
113+
114+
/**
115+
* Only parse XML format to string values now
116+
*/
117+
public function parseResponse($message, $contentType){
114118
if(!$this->isValidXml($message)){
115119
return;
116120
}
117-
118121
$xmlObj = simplexml_load_string($message);
122+
123+
if(!$this->isStandardFormat($xmlObj)){
124+
return;
125+
}
119126
$type = (string)$xmlObj->Fault->attributes()['type'];
120127
if(isset($type) && !empty($type)){
121128
$this->intuitErrorType = $type;
@@ -138,8 +145,19 @@ public function parseResponse($message){
138145

139146
}
140147

148+
/**
149+
* Check if the format is standard Intuit Response format
150+
*/
151+
private function isStandardFormat($xmlObj)
152+
{
153+
if(!isset($xmlObj->Fault) || !isset($xmlObj->Fault->Error)){
154+
return false;
155+
}
156+
return true;
157+
}
158+
141159
private function isValidXml($content)
142-
{
160+
{
143161
$content = trim($content);
144162
if (empty($content)) {
145163
return false;

src/Core/HttpClients/IntuitResponse.php

+19-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
namespace QuickBooksOnline\API\Core\HttpClients;
33

44
use QuickBooksOnline\API\Exception\SdkException;
5+
use QuickBooksOnline\API\Core\CoreConstants;
6+
7+
58

69
class IntuitResponse{
710

@@ -13,6 +16,8 @@ class IntuitResponse{
1316

1417
private $faultHandler;
1518

19+
private $contentType;
20+
1621
public function __construct($passedHeaders, $passedBody, $passedHttpResponseCode){
1722
if(isset($passedHeaders)){
1823
$this->setHeaders($passedHeaders);
@@ -32,7 +37,7 @@ public function __construct($passedHeaders, $passedBody, $passedHttpResponseCode
3237
$this->faultHandler = new FaultHandler();
3338
$this->faultHandler->setHttpStatusCode($this->httpResponseCode);
3439
$this->faultHandler->setResponseBody($this->body);
35-
$this->faultHandler->parseResponse($this->body);
40+
$this->faultHandler->parseResponse($this->body, $this->contentType);
3641
//Manually set the error message
3742
$this->faultHandler->setOAuthHelperError("Invalid auth/bad request (got a " .$passedHttpResponseCode . ", expected HTTP/1.1 20X or a redirect)");
3843
}
@@ -50,11 +55,20 @@ public function setHeaders($rawHeaders){
5055
}else {
5156
list($key, $value) = explode(': ', $line);
5257
$this->headers[$key] = $value;
58+
//set response content type
59+
$this->setContentType($key, $value);
5360
}
5461
}
5562

5663
}
5764

65+
private function setContentType($key, $val){
66+
$trimedKey = trim($key);
67+
if(strcasecmp($trimedKey, CoreConstants::CONTENT_TYPE) == 0){
68+
$this->contentType = trim($val);
69+
}
70+
}
71+
5872
public function getHeaders(){
5973
return $this->headers;
6074
}
@@ -71,4 +85,8 @@ public function getFaultHandler(){
7185
return $this->faultHandler;
7286
}
7387

88+
public function getResponseContentType(){
89+
return $this->contentType;
90+
}
91+
7492
}

src/Core/HttpClients/RestHandler.php

+6
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ class RestHandler
4141
*/
4242
protected $RequestSerializer;
4343

44+
/**
45+
* Get the Logging component for the REST service
46+
* @var RequestLogging
47+
*/
48+
protected $RequestLogging;
49+
4450
/**
4551
*
4652
* Initializes a new instance of the RestHandler class.

src/Core/HttpClients/SyncRestHandler.php

+8
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ public function __construct($context)
4646
return $this;
4747
}
4848

49+
public function updateContext($newServiceContext){
50+
if($newServiceContext instanceof ServiceContext){
51+
$this->context = $newServiceContext;
52+
}else{
53+
throw new SdkException("Cannot Update Service Context.");
54+
}
55+
}
56+
4957
//----------------New Added Method to get Last error
5058
/**
5159
* Return an representation of an error returned by the last request, or null

0 commit comments

Comments
 (0)