Giter Club home page Giter Club logo

Comments (10)

ronanq avatar ronanq commented on August 15, 2024 1

I don't think there is a bug or anything to fix there. Creating an attachment works fine for me and so does passing in the parameter to include with online invoicing:

$XeroOAuth->request
('PUT', $XeroOAuth->url('Invoice/'.$invoices->Invoices[0]->Invoice->InvoiceID.'/Attachments/image.png', 'core'), 
array('IncludeOnline' => 'True'), 
$attachmentFile, 
'file');

Let me know if I am missing something, but I don't think anything needs to be changed with the existing code.

from xerooauth-php.

iqinit avatar iqinit commented on August 15, 2024

Hello,
I just had the same problem. I found a bug in lib/XeroOAuth.php where it will always set the mime type to be application/x-www-form-urlencoded instead of application/pdf so Xero rejects it. To fix it find:

if ($this->format == "file") {
                                        $put_body = $this->xml;
                                } else {
                                        $put_body = $this->safe_encode ( $this->xml );
                                        $this->headers ['Content-Type'] = 'application/x-www-form-urlencoded';
                                }

And replace it with:

switch ($this->format) {
                                case "file" :
                                        $put_body = $this->xml;
                                        break;
                                case "pdf" :
                                        $put_body = $this->xml;
                                        $this->headers ['Content-Type'] = 'application/pdf';
                                        break;
                                case "json" :
                                        $put_body = $this->xml;
                                        $this->headers ['Content-Type'] = 'application/json';
                                        break;
                                default :
                                        $put_body = $this->safe_encode ( $this->xml );
                                        $this->headers ['Content-Type'] = 'application/x-www-form-urlencoded';
                                        break;
                        }

Then make sure you set pdf as the format when you call the put request:

$response = $XeroOAuth->request('PUT', $XeroOAuth->url('Invoice/'.$invoices->Invoices[0]->Invoice->InvoiceID.'/Attachments/image.png', 'core'), array(), $attachmentFile, 'pdf');

from xerooauth-php.

samNIe avatar samNIe commented on August 15, 2024

Hello,
Thanks for replying , and thanks for the great method, not sure the Parameters $attachmentFile need tp be raw data in string or in some kind of xml structure

from xerooauth-php.

iqinit avatar iqinit commented on August 15, 2024

That was just from the tests/tests.php file so the full section is:

if ($_REQUEST['invoice']=="attachment") {
                                $attachmentFile = file_get_contents('http://i.imgur.com/mkDFLf2.png');

                            $response = $XeroOAuth->request('PUT', $XeroOAuth->url('Invoice/'.$invoices->Invoices[0]->Invoice->InvoiceID.'/Attachments/image.png', 'core'), array(), $attachmentFile, 'file');
                                        if ($XeroOAuth->response['code'] == 200) {
                                                        echo "Attachment successfully created against this invoice.";
                                            } else {
                                                outputError($XeroOAuth);
                                            }
                        }

So $attachmentFile is just the raw data of PDF

I am using it by just doing a put request with the contents of the pdf, then reading that into the variable:

$attachmentFile = file_get_contents('php://input');

from xerooauth-php.

samNIe avatar samNIe commented on August 15, 2024

Thanks a lot, that works!

from xerooauth-php.

samNIe avatar samNIe commented on August 15, 2024

Hi
I found another problem, when i try to include the attachment online, by using this method it get a 500 respond,
so are there any way to change the post method as well?
i did this: but it seem not working

case 'POST' :
$fh = tmpfile();
switch ($this->format) {
case "xml":
curl_setopt ( $c, CURLOPT_POST, TRUE );
$post_body = $this->safe_encode ( $this->xml );
curl_setopt ( $c, CURLOPT_POSTFIELDS, $post_body );
$this->request_params ['xml'] = $post_body;
$contentLength = strlen ( $post_body );
$this->headers ['Content-Type'] = 'application/x-www-form-urlencoded';
break;
case "pdf":
$put_body = $this->xml;
$this->headers ['Content-Type'] = 'application/pdf';
fwrite ( $fh, $put_body );
rewind ( $fh );
curl_setopt ( $c, CURLOPT_POST, true );
curl_setopt ( $c, CURLOPT_POSTFIELDS, $fh );
curl_setopt ( $c, CURLOPT_INFILESIZE, strlen ( $put_body ) );
$contentLength = strlen ( $put_body );
break;

            }
            break;

reference for the include attachment online : http://developer.xero.com/documentation/api/attachments/
search key word : Include with Online Invoice
🙇

from xerooauth-php.

iqinit avatar iqinit commented on August 15, 2024

I don't think it is going make a difference with that for put/post. From the document:

"The PUT method is identical to the POST method."

When I add ?IncludeOnline=true to the URL I get:

oauth_problem=consumer_key_unknown&oauth_problem_advice=Consumer%20key%20was%20not%20recognised

So I am guessing there is a bug somewhere with parsing the URL, then causing a problem with oAuth.

from xerooauth-php.

iqinit avatar iqinit commented on August 15, 2024

I've found the first bug which is that it is adding a second ? to the URL.
I fixed that by altering the sign function in OAuthSimple.php to be

$this->_parameters['oauth_signature'] = $this->_generateSignature($normParams);

    if (strpos($this->_path,'?') !== false) {
    $returnURL = $this->_path . '&' . $this->_normalizedParameters('true');
    } else {
    $returnURL = $this->_path . '?' . $this->_normalizedParameters('true');
    }

        return Array(
            'parameters' => $this->_parameters,
            'signature' => $this->_oauthEscape($this->_parameters['oauth_signature']),
            'signed_url' => $returnURL,
            'header' => $this->getHeaderString(),
            'sbs'=> $this->sbs
            );
    }

So that will now look for a ? in the url and if it is there use an & instead of a ? so the oauth parameters get passed as well.

However I am now getting:

oauth_problem=signature_invalid&oauth_problem_advice=Failed%20to%20validate%20signature

So somewhere else the ? must be messing up the signing!
I don't understand why Xero didn't just set this as another variable you can pass in with the XML as it would make it a million times easier!

I'm still getting my head around how oAuth works so not sure if I will be able to find why the signing is failing now.

from xerooauth-php.

samNIe avatar samNIe commented on August 15, 2024

I have done some test, it seems no matter where we add IncludeOnline=true it will cause the oauth problem, it seems there is nothing we can do at this stage.
Thanks for the bug fix

from xerooauth-php.

samNIe avatar samNIe commented on August 15, 2024

Hi Ronanq
Thanks for reply, that works for me as well.
Just come with another question
Are there any way to sent the invoice through API?

from xerooauth-php.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.