Skip to content

Instantly share code, notes, and snippets.

@mwoodbri
Created March 6, 2016 23:56
Show Gist options
  • Save mwoodbri/ebf933d32455bca6c86d to your computer and use it in GitHub Desktop.
Save mwoodbri/ebf933d32455bca6c86d to your computer and use it in GitHub Desktop.
var request = require('request');
var CONSUMER_KEY = 'YOUR_XERO_CONSUMER_KEY';
var accountCodes = {
eating_out: 420
};
export default async function(hook) {
var transaction = hook.params;
if (/#expenses(?: |$)/.test(transaction.data.notes)) {
hook.res.end();
}
var body = `
<Receipts>
<Receipt>
<User>
<UserID>YOUR_XERO_USER_ID</UserID>
</User>
<Contact>
<Name>${transaction.data.merchant.name}</Name>
</Contact>
<Date>${transaction.data.created}</Date>
<LineItems>
<LineItem>
<Description>${transaction.data.description}</Description>
<UnitAmount>${-transaction.data.amount / 100}</UnitAmount>
<AccountCode>${accountCodes[transaction.data.category]}</AccountCode>
</LineItem>
</LineItems>
</Receipt>
</Receipts>
`;
hook.datastore.get('RSA_PRIVATE_KEY', function(err, RSA_PRIVATE_KEY) {
if (err) { return res.end(err.message); }
var oauth = {
consumer_key: CONSUMER_KEY,
token: CONSUMER_KEY,
signature_method : 'RSA-SHA1',
private_key: RSA_PRIVATE_KEY
};
request.put({url: 'https://api.xero.com/api.xro/2.0/Receipts', oauth: oauth, body: body}, function(err, res) {
if (err) { return res.end(err.message); }
hook.res.end(res.body);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment