Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POST Request to PayItems #26

Open
gazzenger opened this issue Dec 1, 2016 · 0 comments
Open

POST Request to PayItems #26

gazzenger opened this issue Dec 1, 2016 · 0 comments

Comments

@gazzenger
Copy link

gazzenger commented Dec 1, 2016

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant