xero icon indicating copy to clipboard operation
xero copied to clipboard

POST Request to PayItems

Open gazzenger opened this issue 8 years ago • 0 comments

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

gazzenger avatar Dec 01 '16 06:12 gazzenger