1

I am trying to create an invoice in xero and then get a pdf version uploaded onto mongoDB through my parse server. I am authenicating xero in an express app in the main.js of my application.
When I save the invoice Pdf to parse it is rejected as 'schema mismatch, expecting file but getting object', what am I missing in my code to create the PDF version?

let oauth_verifier = req.query.oauth_verifier;
let accessToken = await xeroClient.oauth1Client.swapRequestTokenforAccessToken(lastRequestToken, oauth_verifier)
    .then(async() => {
        var invoice = xeroClient.invoices.create(data)
            .then(async(invoice) => {
                var inv = invoice["Invoices"][0];
                var invId = inv["InvoiceID"];
                await xeroClient.invoices.get({ InvoiceID: invId}, "application/pdf")
                    .then((invPdf) => {

                        Parse.initialize("--------------------");    
                        Parse.serverURL = 'http://--.---.---.--:--/parse';
                        var Invoices = Parse.Object.extend("Invoices");
                        var invoice = new Invoices;
                        invoice.set('invoicePdf', invPdf);
                        invoice.save(); 

                        event.returnValue = true;
                        win.close();
                    })
            })
twl2009
  • 79
  • 1
  • 11
  • First thing I'd do* is to see if the return is actually coming back as a PDF file, narrow down the problem - don't you need to specify somewhere that your string "application/pdf" is the "Accept" header, or is that implied in the function? (* - I'm not familiar with the language you're using here, never mind much of the rest of it). – droopsnoot Jul 02 '19 at 10:36
  • If it's Node.js, the code in github suggests that the only header-args the function supports is "If-modified-since" - https://github.com/XeroAPI/xero-node/blob/ce69368/src/AccountingAPIClient.ts#L62 - which would suggest that even if you specify "Accept: application/pdf", the `get` call will ignore it. – droopsnoot Jul 02 '19 at 10:49
  • i have seen this suggested, but not sure exactly how to use it. `var invoice = xero.getInvoice(invoiceId, modifiedAfter, where, "application/pdf"); var content = invoice.PdfContentRaw; var filename = invoice.FileName;` – twl2009 Jul 02 '19 at 11:27
  • Can you please console.log(invPdf) and share what you have in this var? – Davi Macêdo Jul 02 '19 at 16:59
  • Hi Davi, it gives an object:- `{ Id: '30afd09c-2125-4c76-815e-dbf72be5fe7a', Status: 'OK', ProviderName: 'bibwine-mds', DateTimeUTC: '/Date(1562067286613)/', Invoices: [ { Type: 'ACCREC', InvoiceID: 'ad2892fd-df50-43c7-b026-c9a57295ebea', InvoiceNumber: 'INV-0137', Reference: '', Status: 'DRAFT', LineAmountTypes: 'Exclusive', LineItems: [Array], SubTotal: 400, TotalTax: 0, Total: 400, UpdatedDateUTC: '/Date(1562067285793+0000)/', CurrencyCode: 'GBP' } ] }` – twl2009 Jul 02 '19 at 18:26
  • OK, so that has ignored the request to send the invoice as a PDF. If you try your original request but replace your string "application/pdf" with "Accept: application/pdf", does that make any difference? – droopsnoot Jul 03 '19 at 17:33
  • Same result unfortunately `Error: schema mismatch for Invoices.invoicePdf; expected File but got Object` – twl2009 Jul 03 '19 at 18:00
  • If you have a look at that link to GitHub for the Xero Node API, there is the "savePDF" function as well - have you tried that? https://github.com/XeroAPI/xero-node/blob/36ab8a513263426a173633691f5308237f473b99/src/AccountingAPIClient.ts#L469 – droopsnoot Jul 03 '19 at 18:31
  • Well spotted. Below code is working now. Cheers Droopsnoot `await xeroClient.invoices.savePDF({ InvoiceID: invId, savePath: path.join(__dirname, invNumber + '.pdf',)})` – twl2009 Jul 03 '19 at 21:57
  • I'll add it as an answer then, in case it makes it more noticeable for anyone else searching. – droopsnoot Jul 04 '19 at 08:31

1 Answers1

1

In the GitHub source for the Node.JS, there is a separate function called savePDF which seems to do the trick, as you noted in the comments above. https://github.com/XeroAPI/xero-node/blob/36ab8a513263426a173633691f5308237f473b99/src/AccountingAPIClient.ts#L469

droopsnoot
  • 931
  • 1
  • 7
  • 11