Code Monkey home page Code Monkey logo

xero's Introduction

xero

A simple node library for Xero Private Applications

Install

  npm install xero

Usage

Request

var express = require("express");
var Xero = require('xero');
var fs = require('fs');

var app = express();

var CONSUMER_KEY="my consumer key";
var CONSUMER_SECRET="my consumer secret";
var RSA_PRIVATE_KEY = fs.readFileSync(process.env.HOME + "/.ssh/id_rsa");
var xero = new Xero(CONSUMER_KEY, CONSUMER_SECRET, RSA_PRIVATE_KEY);

app.get('/', function (req, res) {
  xero.call('GET', '/Users', null, function(err, json) {
    if (err) {
      res.status(err.statusCode);
      res.write(JSON.stringify(err));
      res.end();
    } else {
      res.status(200);
      res.write(JSON.stringify(json));
      res.end();
    }
  });
});

app.listen(3000, function () {
  console.log("Listening...");
});

Response

{
   "Response":{
      "Id":"37286998-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
      "Status":"OK",
      "ProviderName":"My Account",
      "DateTimeUTC":"2013-04-22T17:13:31.2755569Z",
      "Users":{
         "User":{
            "UserID":"613fbf01-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
            "FirstName":"Chadd",
            "LastName":"Sexington",
            "UpdatedDateUTC":"2013-04-12T05:54:50.477",
            "IsSubscriber":"true",
            "OrganisationRole":"STANDARD"
         }
      }
   }
}

Example POST Request

...
// Adding contact(s)
var request;
// Single
request = {
    Name: 'Name1'
};
// Multiple
request = [{
    Name: 'Name1'
}, {
    Name: 'Name2'
}];
xero.call('POST', '/Contacts?SummarizeErrors=false', request, function(err, json) {
        ...
    });

Download PDF

var Xero = require('xero');
var fs = require('fs');

var xero = new Xero(CONSUMER_KEY, CONSUMER_SECRET, RSA_PRIVATE_KEY);
var invoiceId = 'invoice-identifier';
var req = xero.call('GET', '/Invoices/' + invoiceId);

req.setHeader('Accept', 'application/pdf');
req.on('response', function(response) {
  var file = fs.createWriteStream(invoiceId + '.pdf');
  response.pipe(file);
});
req.end();

Docs

http://developer.xero.com/documentation/

Enjoy! - thallium205 https://github.com/thallium205

xero's People

Contributors

andrew-wharton avatar chrisdamba avatar oroce avatar thallium205 avatar trungie avatar umairsiddique avatar wobinb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

xero's Issues

Trouble setting up base on example and testing it.

Hi there,

I'm having trouble setting this up and trying it out. I have created the private and public and uploaded it.
I saw the different issues and change the iff(err) part from one of your issues.

var CONSUMER_KEY="my consumer key";
var CONSUMER_SECRET="my consumer secret";
var RSA_PRIVATE_KEY = require('fs').readFileSync('privatekey.pem');

var Xero = require('xero');

var xero = new Xero(CONSUMER_KEY, CONSUMER_SECRET, RSA_PRIVATE_KEY);
xero.call('GET', '/Users', null, function(err, json) {
        if (err) {
            console.error(err);
        }
        return res.json(200, json);
    });

After the above I just run node myfile.js and it gives me this bunch of errors of log is not defined.

        return res.json(200, json);
               ^

ReferenceError: res is not defined
    at /Users/Stefanus/Desktop/Xero Try Out/xeroTestApi.js:12:16
    at /Users/Stefanus/node_modules/xero/index.js:47:24
    at /Users/Stefanus/node_modules/xero/node_modules/xml2js/lib/xml2js.js:381:22
    at doNTCallback0 (node.js:407:9)
    at process._tickCallback (node.js:336:13)

Can I get some help in regards to this? Many thanks!

Detailed Documentation

Hi

I'm a newbie web developer and I am a bit stuck. Consumer Secret/Key are easily to drop in as I grab those from the xero application. I am lost with the rsa_private_key. I have done some search and found how to generate the rsa key. Do that and uploaded to xero application. But what next? Where is this RSA_PRIVATE_KEY?

Cannot post multiple items in an invoice request

Hi Thallium,

thank you for your library!
I am having issues with posting multiple Line items in an invoice. I have the same issue posting multiple tracking categories within the line item.

Is this a known issue? Would you be able to guide me towards a solution?

Many thanks,
Chiara

Multiple TrackingCategory Not Possible

According to the XERO Developer SDK Regarding TrackingCategory and thier support team. You should be able to submit up to Two Tracking Categories per Line Item in an Invoice. However, it would appear using easyxml actually prevents this API from properly forming the following valid XML:

 <Tracking>
     <TrackingCategory>
         <TrackingCategoryID>5caaf317-e4f1-4dc3-b6fa-c79c437a222c</TrackingCategoryID>
         <Name>Tracking1</Name>
         <Option>option1</Option>
     </TrackingCategory>
     <TrackingCategory>
         <TrackingCategoryID>a8b67858-3713-4c63-8019-132c36c08ba0</TrackingCategoryID>
         <Name>Tracking2</Name>
         <Option>Option2</Option>
     </TrackingCategory>
 </Tracking>

Attempt 1

Testing the following JSON to find it produces the following XML:

Tracking: [
    {
        TrackingCategory: {
            Name: "Melbourne",
            Option: "e44c3446-b741-4717-9710-057c1fc14603",
            TrackingCategoryID: "bf8a9440-37bd-44fd-ab97-f207a2588ac1",
        },
    },{
        TrackingCategory: {
            Name: "Virtual Reality Design",
            Option: "c712bb31-2d5e-470f-90da-70f2cc6fe595",
            TrackingCategoryID: "2d002487-1540-442d-bb8a-b2d7d3ff11c7",
        },
    }
]                 
<Tracking>
    <Tracking>
        <TrackingCategory>
            <Name>Melbourne</Name>
            <Option>e44c3446-b741-4717-9710-057c1fc14603</Option>
            <TrackingCategoryID>bf8a9440-37bd-44fd-ab97-f207a2588ac1</TrackingCategoryID>
        </TrackingCategory>
    </Tracking>
    <Tracking>
        <TrackingCategory>
            <Name>Virtual Reality Design</Name>
            <Option>c712bb31-2d5e-470f-90da-70f2cc6fe595</Option>
            <TrackingCategoryID>2d002487-1540-442d-bb8a-b2d7d3ff11c7</TrackingCategoryID>
        </TrackingCategory>
    </Tracking>
</Tracking>

Attempt 2

Testing the following illegal JSON to find it produces the following XML:

Tracking: {
    TrackingCategory: {
        Name: "Melbourne",
            Option: "e44c3446-b741-4717-9710-057c1fc14603",
            TrackingCategoryID: "bf8a9440-37bd-44fd-ab97-f207a2588ac1",
    },
    TrackingCategory: {
        Name: "Virtual Reality Design",
            Option: "c712bb31-2d5e-470f-90da-70f2cc6fe595",
            TrackingCategoryID: "2d002487-1540-442d-bb8a-b2d7d3ff11c7",
    },
}
<Tracking>
    <Tracking>
        <TrackingCategory>
            <Name>Melbourne</Name>
            <Option>e44c3446-b741-4717-9710-057c1fc14603</Option>
            <TrackingCategoryID>bf8a9440-37bd-44fd-ab97-f207a2588ac1</TrackingCategoryID>
        </TrackingCategory>
    </Tracking>
    <Tracking>
        <TrackingCategory>
            <Name>Virtual Reality Design</Name>
            <Option>c712bb31-2d5e-470f-90da-70f2cc6fe595</Option>
            <TrackingCategoryID>2d002487-1540-442d-bb8a-b2d7d3ff11c7</TrackingCategoryID>
        </TrackingCategory>
    </Tracking>
</Tracking>

Attempt 3

Testing the following JSON to find it produces the following XML:

Tracking: {
    TrackingCategory: [
        {
            Name: "Melbourne",
            Option: "e44c3446-b741-4717-9710-057c1fc14603",
            TrackingCategoryID: "bf8a9440-37bd-44fd-ab97-f207a2588ac1",
        }, {
            Name: "Virtual Reality Design",
            Option: "c712bb31-2d5e-470f-90da-70f2cc6fe595",
            TrackingCategoryID: "2d002487-1540-442d-bb8a-b2d7d3ff11c7",
        },
    ]
}
<Tracking>
    <TrackingCategory>
        <TrackingCategory>
            <Name>Melbourne</Name>
            <Option>e44c3446-b741-4717-9710-057c1fc14603</Option>
            <TrackingCategoryID>bf8a9440-37bd-44fd-ab97-f207a2588ac1</TrackingCategoryID>
        </TrackingCategory>
        <TrackingCategory>
            <Name>Virtual Reality Design</Name>
            <Option>c712bb31-2d5e-470f-90da-70f2cc6fe595</Option>
            <TrackingCategoryID>2d002487-1540-442d-bb8a-b2d7d3ff11c7</TrackingCategoryID>
        </TrackingCategory>
    </TrackingCategory>
</Tracking>

Reviewing the docs at easyxml and the test cases. This api simply can not creat the require xml shape.

Signature invalid - failed to validate signature

I generated private/public key pair using:

openssl genrsa -out privatekey.pem 1024
openssl req -new -x509 -key privatekey.pem -out publickey.cer -days 1825
openssl pkcs12 -export -out public_privatekey.pfx -inkey privatekey.pem -in publickey.cer

and create private app using publickey.cer file and pass private key in configuration :

var Xero = require('xero');

var CONSUMER_KEY = '...';
var CONSUMER_SECRET = '...';
var RSA_PRIVATE_KEY = require('fs').readFileSync('privatekey.pem');

var xero = new Xero(CONSUMER_KEY, CONSUMER_SECRET, RSA_PRIVATE_KEY);

xero.call('GET', '/Users', null, function(err, json) {
    if (err) {
        console.error(err);
    }
    console.log('response:' + JSON.stringify(json));
});

I also tried to to pass RSA_PRIVATE_KEY directly in xero config.
But still i facing
{ statusCode: 401, data: 'oauth_problem=signature_invalid&oauth_problem_advice=Failed%20to%20validate%20signature' }

Please help me.
Thanks.

error messages

When there is an error with the request, then the error should be forewarded to the client. I suggest to paste the complete xml error message that comes from xero.

Otherwise, one never knows the reason WHY something went wrong.

Signature Invalid (signature_invalid)

Getting an OAuth error when trying to run the request example.

Here are my steps:

Created a Xero Private application.
Generated public/private certs using:

openssl genrsa -out privatekey.pem 1024
openssl req -new -x509 -key privatekey.pem -out publickey.cer -days 1825
openssl pkcs12 -export -out public_privatekey.pfx -inkey privatekey.pem -in publickey.cer

Uploaded public key certificate (publickey.cer).
Obtained consumer key/secret.
Tested sample https://github.com/thallium205/xero#request.
Getting following error:

{ statusCode: 401,
  data: 'oauth_problem=signature_invalid&oauth_problem_advice=Failed%20to%20validate%20signature' }

What am I missing here?

Thanks.

no such file or directory

C:\Users\HP\Downloads\MVC\xero-master>node server
fs.js:640
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
^

Error: ENOENT: no such file or directory, open 'C:\Users\HP\Downloads\MVC\xero-m
aster\undefined.ssh\id_rsa'
at Error (native)
at Object.fs.openSync (fs.js:640:18)
at Object.fs.readFileSync (fs.js:508:33)
at Object. (C:\Users\HP\Downloads\MVC\xero-master\server.js:9:26)

at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)

Attachment Support

Hi there,

I'm using your library and it's fantastic.

One thing that I cannot quote work out is how to get attachments to work correctly - the Xero spec is a little vague about the encoding to use (and I am a bit of a node n00b).

I've had a crack at getting the attachment upload thing working per their spec (http://developer.xero.com/documentation/api/attachments/) - https://gist.github.com/zbrba/9364563 (please excuse the re-write - I wanted a one pager to upload to the forum to ask for help)

Any chance you could point me in the direct direction - I am happy to fork and pull.

Why singularize(root)?

Why are we singularizing the root element name? This appears to be a bug?

For example if I try and update a contact number I'd make the request to..

https://api.xero.com/api.xro/2.0/contacts/##contactID##

Payload:

<contacts>
    <Contact>
        <ContactNumber>123456</ContactNumber>
    </Contact>
</contacts>

Notice the root element is contacts and not contact? It won't work if it's singularized.

Access single Employee

Does not seem to support accessing a single employee ie.
xero.call('GET', '/Employees/81axxxxe-5c76-46f7-b136-f01xxxx019e3', null, function(err, json) {

List of all Employees is all OK, but when an id is passed, it returns:
{ statusCode: 404,
data: 'The resource you're looking for cannot be found' }

Am I coding it wrong?

Generate & Download PDF of Invoice

Hey I am trying your code to generate and download PDF of particular invoice ID, but I am facing some problem.
My Code like this:-

var xero = new Xero(CONSUMER_KEY, CONSUMER_SECRET, RSA_PRIVATE_KEY);
var invoiceId = xxxxxxxxxxxxxxxxxxxx;
var req = xero.call('GET', '/Invoices/' + invoiceId);

req.setHeader('Accept', 'application/pdf');
req.on('response', function(response) {
console.log("Response");
var file = fs.createWriteStream(invoiceId + '.pdf');
response.pipe(file);
console.log("Response after cerateing File");
});
req.end();

Both console.log messages printed but doesn't return anything and Browser continuous loading.
Is there any mistake??
How should I Download pdf of invoice??

POST Request to PayItems

Hi,

I've had issues with issuing POST requests for sending new Earnings Rates to Xero.

When creating the JSON for sending with the POST request.

        var newEarnings = {
            EarningsRates : [ {
                Name              : 'Test Earnings',
                EarningsType      : 'ORDINARYTIMEEARNINGS',
                RateType          : 'RATEPERUNIT',
                AccountCode       : '477',
                TypeOfUnits       : 'Hours',
                IsExemptFromTax   : 'false',
                IsExemptFromSuper : 'false'
            } ]
        };

When supplied to
xero.call('POST', 'payroll', '/PayItems', newEarnings, function(...

In the index.js file for the xero module, the newEarnings variable is parsed into XML, this is shown below.

<PayItem>
    <EarningsRates>
        <EarningsRate>
            <Name>Test Earnings</Name>
            <EarningsType>ORDINARYTIMEEARNINGS</EarningsType>
            <RateType>RATEPERUNIT</RateType>
            <AccountCode>477</AccountCode>
            <TypeOfUnits>Hours</TypeOfUnits>
            <IsExemptFromTax>false</IsExemptFromTax>
            <IsExemptFromSuper>false</IsExemptFromSuper>
        </EarningsRate>
    </EarningsRates>
</PayItem>

The response from Xero is

{ statusCode: 400,
  data: '<Response xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><ErrorNumber>0</ErrorNumber><Type>Error</Type><Message>Bad request</Message></Response>' }

With a bit of tinkering I found the issue is that the XML is meant to be PayItems (plural), instead of PayItem (singular).

<PayItems>
...
</PayItems>

It appears the issue is that the PayItems resource is different to all the other Xero resources, in that it is made up of the following:

  • EarningsRates
  • DeductionTypes
  • ReimbursementTypes
  • LeaveTypes

But the XML parser the xero module is expecting the resource is the root element of the XML (this is correct but plural) and that the root array is the singular form of the resource.

I've managed a workaround by editing the following line in the index.js file.

From

post_body = new EasyXml({rootElement: inflect.singularize(root), rootArray: root, manifest: true}).render(body);

To

if (path.search('PayItems') !== -1) {
    post_body = new EasyXml({rootElement: root, rootArray: root, manifest: true}).render(body);
} else {
    post_body = new EasyXml({rootElement: inflect.singularize(root), rootArray: root, manifest: true}).render(body);
}

Please let me know your thoughts on this issue, I'm sure there's a better solution to the problem.
-Gary

[email protected]

You'll want to update your package.json to load a specific version of easyxml (e.g. 0.1.0 instead of *) as the latest version breaks API compatibility.

Example in readme is confusing

var Xero = require('xero');

var xero = new Xero(CONSUMER_KEY, CONSUMER_SECRET, RSA_PRIVATE_KEY);
xero.call('GET', '/Users', null, function(err, json) {
        if (err) {
            log.error(err);
            return res.json(400, {error: 'Unable to contact Xero'});
        }
        return res.json(200, json);
    });

What is res? It's nowhere defined, probably Express's request object.

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.