intuit / QuickBooks-V3-PHP-SDK

Official PHP SDK for QuickBooks REST API v3.0: https://developer.intuit.com/
Apache License 2.0
241 stars 242 forks source link

content-type of "text/xml" in 401 results in response not being parsed #351

Open bch36 opened 4 years ago

bch36 commented 4 years ago

In IntuitResponse.php, a fault response isn't parsed unless the content-type is "application/xml". However, for a 401 response, the content type is "text/xml." This results in the response not being parsed.

It seems like either the check should be updated in IntuitResponse.php::147, or responses should be sent using the content-type of "application/xml".

Current code: if($this->getResponseContentType() != null && strcasecmp($this->getResponseContentType(), CoreConstants::CONTENTTYPE_APPLICATIONXML) == 0)

Example response with the unexpected content-type of text/xml:

RESPONSE URI FOR SEQUENCE ID 46707
==================================
https://quickbooks.api.intuit.com/v3/company/XXXXXXXXXXX/customer/1462?minorversion=41

RESPONSE HEADERS
================
date: Thu, 20 Aug 2020 00:31:26 GMT
**content-type: text/xml**
content-length: 358
connection: close
server: nginx
intuit_tid: 1-5f3dc45e-932b6d5f713b50aa802eaed3
www-authenticate: Bearer realm="Intuit", error="invalid_token"

RESPONSE BODY
=============
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<IntuitResponse time="2020-08-19T17:31:26.785-07:00" xmlns="http://schema.intuit.com/finance/v3">
    <Fault type="AUTHENTICATION">
        <Error code="3200">
            <Message>message=AuthenticationFailed; errorCode=003200; statusCode=401</Message>
        </Error>
    </Fault>
</IntuitResponse>

My own workaround:

$error = $dataService->getLastError();
if (empty($error->getIntuitErrorCode()))
    $error->parseResponse($error->getResponseBody()); // Uses same method as IntuitResponse.php::148
abisalehalliprasan commented 4 years ago

@bch36 : Thanks for code suggestions. We will test this change and provide an update.

abisalehalliprasan commented 4 years ago

@bch36 : We can tweak the setFaultHandler() under IntuitResponse.php class to check if the response content type is text/xml:

if($this->getResponseContentType() != null && (strcasecmp($this->getResponseContentType(), CoreConstants::CONTENTTYPE_APPLICATIONXML) == 0 ||
                strcasecmp($this->getResponseContentType(), CoreConstants::CONTENTTYPE_TEXTXML) == 0))

I am not sure how this fix will help you. If your program wants to differentiate the error calls, you might need to pass throwonExcpetion as false so that the latest error is captured in the DateService object as shown here

Ref : Customer Query

bch36 commented 4 years ago

That fix will work great. The problem was that 401 responses from QuickBooks weren't being parsed because they used a different response content type than other errors, and this will fix that. I don't care about differentiating error calls, I just want all error responses to be consistently handled. Your fix will satisfy that. Thanks for looking into this!

On Tue, Sep 1, 2020 at 1:14 AM abisalehalliprasan notifications@github.com wrote:

@bch36 https://github.com/bch36 : We can tweak the setFaultHandler() under IntuitResponse.php class to check if the response content type is text/xml:

if($this->getResponseContentType() != null && (strcasecmp($this->getResponseContentType(), CoreConstants::CONTENTTYPE_APPLICATIONXML) == 0 || strcasecmp($this->getResponseContentType(), CoreConstants::CONTENTTYPE_TEXTXML) == 0))

I am not sure how this fix will help you. If your program wants to differentiate the error calls, you might need to pass throwonExcpetion as false so that the latest error is captured in the DateService object as shown here https://intuit.github.io/QuickBooks-V3-PHP-SDK/handle-error.html

Ref : Customer Query https://github.com/intuit/QuickBooks-V3-PHP-SDK/blob/master/src/_Samples/CustomerQuery.php

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/intuit/QuickBooks-V3-PHP-SDK/issues/351#issuecomment-684417978, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHSN7I4HOBNGI6CVMLOFSLTSDSGKXANCNFSM4QMJACXA .

abisalehalliprasan commented 3 years ago

Opening up our SDK for hacktoberfest contributions.

What is it? Hacktoberfest is underway right now and we would love for you to contribute 🚀 . More information on #hacktoberfest is here

Develop-with-Intuit for Hacktoberfest