@@ -654,9 +654,14 @@ public function forceRenewal()
654654 */
655655 protected function _handleRenewal ($ force_renewal = false )
656656 {
657+ $ this ->_log ('_handleRenewal( ' . var_export ($ force_renewal , true ) . ') ' , QUICKBOOKS_LOG_DEBUG );
658+
657659 static $ was_renewed_during_this_session = false ;
658660 static $ renewal_attempts = 0 ;
659661
662+ $ this ->_log (' Was renewed already? ' . var_export ($ was_renewed_during_this_session , true ), QUICKBOOKS_LOG_DEBUG );
663+ $ this ->_log (' Renewal attemps so far: ' . var_export ($ renewal_attempts , true ), QUICKBOOKS_LOG_DEBUG );
664+
660665 $ renewal_attempts ++;
661666
662667 $ needs_renewal = false ;
@@ -665,21 +670,29 @@ protected function _handleRenewal($force_renewal = false)
665670 $ this ->_authmode == QuickBooks_IPP::AUTHMODE_OAUTHV2 and
666671 $ force_renewal )
667672 {
673+ $ this ->_log ('Forced renewal, so renewal is needed! ' , QUICKBOOKS_LOG_DEBUG );
668674 $ needs_renewal = true ;
669675 }
670676 else if (!$ was_renewed_during_this_session and
671677 is_object ($ this ->_driver ) and
672678 $ this ->_authmode == QuickBooks_IPP::AUTHMODE_OAUTHV2 and
673679 strtotime ($ this ->_authcred ['oauth_access_expiry ' ]) - 60 < time ())
674680 {
681+ $ this ->_log ('Expired token, so renewal is needed! ' , QUICKBOOKS_LOG_DEBUG );
675682 $ needs_renewal = true ;
676683 }
677684
685+ $ this ->_log (' Do we need to renew? ' . var_export ($ needs_renewal , true ), QUICKBOOKS_LOG_DEBUG );
686+
678687 if ($ needs_renewal )
679688 {
689+ $ this ->_log ('Attempting discover... ' , QUICKBOOKS_LOG_DEBUG );
690+
680691 if ($ discover = QuickBooks_IPP_IntuitAnywhere::discover ($ this ->_sandbox ) and
681692 !empty ($ this ->_authcred ['oauth_client_id ' ]))
682693 {
694+ $ this ->_log ('Attempting renewal... ' , QUICKBOOKS_LOG_DEBUG );
695+
683696 $ ch = curl_init ($ discover ['token_endpoint ' ]);
684697 curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
685698 curl_setopt ($ ch , CURLOPT_FOLLOWLOCATION , false ); // Do not follow; security risk here
@@ -711,11 +724,18 @@ protected function _handleRenewal($force_renewal = false)
711724 // Replace our auth creds with the new ones
712725 $ this ->_authcred = array_merge ($ this ->_authcred , $ this ->_driver ->oauthLoadV2 ($ this ->_key , $ this ->_authcred ['app_tenant ' ]));
713726
727+ $ this ->_log (' Renewal success! New token: ' . $ json ['access_token ' ], QUICKBOOKS_LOG_DEBUG );
728+
714729 // Successfully renewed!
715730 return true ;
716731 }
732+ else
733+ {
734+ $ this ->_log (' Renewal failed: ' . $ info ['http_code ' ] . ': ' . $ retr , QUICKBOOKS_LOG_DEBUG );
735+ }
717736 }
718737
738+ $ this ->_log (' Discover failed! ' , QUICKBOOKS_LOG_DEBUG );
719739 return false ;
720740 }
721741
@@ -1007,8 +1027,7 @@ public function log($message, $level = QUICKBOOKS_LOG_NORMAL)
10071027
10081028 protected function _request ($ Context , $ type , $ url , $ action , $ data , $ post = true )
10091029 {
1010- $ headers = array (
1011- );
1030+ $ headers = array ();
10121031
10131032 if ($ action == QuickBooks_IPP_IDS::OPTYPE_ADD or
10141033 $ action == QuickBooks_IPP_IDS::OPTYPE_MOD or
@@ -1029,8 +1048,7 @@ protected function _request($Context, $type, $url, $action, $data, $post = true)
10291048 {
10301049 $ headers ['Authorization ' ] = 'Bearer ' . $ this ->_authcred ['oauth_access_token ' ];
10311050 }
1032- }
1033- else if ($ this ->_authmode == QuickBooks_IPP::AUTHMODE_OAUTHV1 )
1051+ } else if ($ this ->_authmode == QuickBooks_IPP::AUTHMODE_OAUTHV1 )
10341052 {
10351053 // If we have credentials, sign the request
10361054 if ($ this ->_authcred ['oauth_access_token ' ] and
@@ -1081,13 +1099,11 @@ protected function _request($Context, $type, $url, $action, $data, $post = true)
10811099 if ($ data and $ data [0 ] == '< ' )
10821100 {
10831101 // Do nothing
1084- }
1085- else
1102+ } else
10861103 {
10871104 $ data = http_build_query ($ signdata );
10881105 }
1089- }
1090- else
1106+ } else
10911107 {
10921108 ;
10931109 }
@@ -1121,16 +1137,29 @@ protected function _request($Context, $type, $url, $action, $data, $post = true)
11211137
11221138 // If we got back a 401, indicating an expired token, we can renew and retry!
11231139 $ info = $ HTTP ->lastInfo ();
1140+ $ this ->_log ('HTTP response code: ' . $ info ['http_code ' ], QUICKBOOKS_LOG_DEBUG );
1141+
1142+ if ($ info ['http_code ' ] == QuickBooks_HTTP::HTTP_401 )
1143+ {
1144+ $ this ->_log ('Caught HTTP 401 on response on token ' . $ this ->_authcred ['oauth_access_token ' ] . ', will attempt renewal: ' . $ return , QUICKBOOKS_LOG_DEBUG );
1145+ }
1146+
11241147 if ($ info ['http_code ' ] == QuickBooks_HTTP::HTTP_401 and
11251148 ( false !== stripos ($ return , 'expired ' ) or false !== stripos ($ return , 'AuthenticationFailed ' ) ) and // Expired OAuth token
11261149 $ this ->_authmode == QuickBooks_IPP::AUTHMODE_OAUTHV2 and
11271150 $ this ->_authcred ['oauth_access_token ' ])
11281151 {
1152+ $ this ->_log ('Forcing renewal... ' , QUICKBOOKS_LOG_DEBUG );
1153+
11291154 // Force renewal of the token _right now_
11301155 $ renewed = $ this ->forceRenewal ();
11311156
1157+ $ this ->_log ('Attempted renewal...: ' . var_export ($ renewed , true ), QUICKBOOKS_LOG_DEBUG );
1158+
11321159 if ($ renewed )
11331160 {
1161+ $ this ->_log ('Renewal success! Setting new token and re-attempting: ' . $ this ->_authcred ['oauth_access_token ' ], QUICKBOOKS_LOG_DEBUG );
1162+
11341163 // Set the new token
11351164 $ headers ['Authorization ' ] = 'Bearer ' . $ this ->_authcred ['oauth_access_token ' ];
11361165
0 commit comments