Skip to content

Commit

Permalink
Adds refresh-token-and-retry support on getting a 401 back from Intui…
Browse files Browse the repository at this point in the history
…t Payments.
  • Loading branch information
keith-chargeover committed Jan 24, 2020
1 parent 0a192d1 commit 10a839b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 27 deletions.
13 changes: 1 addition & 12 deletions QuickBooks/HTTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -600,20 +600,9 @@ function($curl, $header) use (&$response_headers)
return $len;
}
);
$this->_last_responseheaders = $response_headers;

$response = curl_exec($ch);

/*
print("\n\n\n" . '---------------------' . "\n");
print('[[request ' . $request . ']]' . "\n\n\n");
print('[[resonse ' . $response . ']]' . "\n\n\n\n\n");
print_r($params);
print_r(curl_getinfo($ch));
print_r($headers);
print("\n" . '---------------------' . "\n\n\n\n");
*/
$this->_last_responseheaders = $response_headers;

$this->_last_response = $response;
$this->_log('HTTP response: ' . substr($response, 0, 500) . '...', QUICKBOOKS_LOG_VERBOSE);
Expand Down
49 changes: 34 additions & 15 deletions QuickBooks/Payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ public function log($message, $level = QUICKBOOKS_LOG_NORMAL)
*/
protected function _http($Context, $url_path, $raw_body = null, $operation = null)
{
if($operation !== null)
if ($operation !== null)
{
$method = $operation;
}
Expand All @@ -727,16 +727,7 @@ protected function _http($Context, $url_path, $raw_body = null, $operation = nul
$authmode = $Context->authmode();

$auth_str = '';
if ($authmode == QuickBooks_IPP::AUTHMODE_OAUTHV1)
{
$params = array();

$OAuth = new QuickBooks_IPP_OAuthv1($this->_oauth_consumer_key, $this->_oauth_consumer_secret);
$signed = $OAuth->sign($method, $url, $authcreds['oauth_access_token'], $authcreds['oauth_access_token_secret'], $params);

$auth_str = $signed[3];
}
else if ($authmode == QuickBooks_IPP::AUTHMODE_OAUTHV2)
if ($authmode == QuickBooks_IPP::AUTHMODE_OAUTHV2)
{
// Do we need to renew OAuth tokens?
$Context->IPP()->handleRenewal();
Expand Down Expand Up @@ -781,22 +772,50 @@ protected function _http($Context, $url_path, $raw_body = null, $operation = nul
$return = null; // ERROR
}

$info = $HTTP->lastInfo();
$this->_last_httpinfo = $info;

if ($info['http_code'] == 401)
{
// Auth tokens expired; automatically refresh and retry
$Context->IPP()->forceRenewal();
$authcreds = $Context->IPP()->authcreds();

// Set the new header
$headers['Authorization'] = 'Bearer ' . $authcreds['oauth_access_token'];
$HTTP->setHeaders($headers);

// Retry the request
if ($method == 'POST')
{
$return = $HTTP->POST();
}
else if ($method == 'GET')
{
$return = $HTTP->GET();
}
else if ($method == 'DELETE')
{
$return = $HTTP->DELETE();
}

$info = $HTTP->lastInfo();
$this->_last_httpinfo = $info;
}

$this->_last_request = $HTTP->lastRequest();
$this->_last_response = $HTTP->lastResponse();

//
$this->log($HTTP->getLog(), QUICKBOOKS_LOG_DEBUG);

$info = $HTTP->lastInfo();
$this->_last_httpinfo = $info;

$this->_last_intuittid = '';
$response_headers = $HTTP->lastResponseHeaders();
if (!empty($response_headers['intuit_tid']))
{
$this->_last_intuittid = $response_headers['intuit_tid'];
}

$errnum = $HTTP->errorNumber();
$errmsg = $HTTP->errorMessage();

Expand Down

0 comments on commit 10a839b

Please sign in to comment.