calcinai / xero-php

A php library for the Xero API, with a cleaner OAuth interface and ORM-like abstraction.
MIT License
359 stars 262 forks source link

PayItems API getting Bad Request #665

Open cproetti opened 4 years ago

cproetti commented 4 years ago

I'm having issues with the PayItems API. Whats happening is I'm trying to create a new EarningsRate however I'm getting a an exception from XeroPHP when ->save(). The exception only says "Bad Request". When I go to the Xero API Call Details page to see the response message I'm also seeing an error that only says "Bad Request".

Attached you will find a test.txt which contains my test code. I've also attached the Request Message and Response Message from Xero API Call Details web page.

I have also tried making a PayItems request that contains EarningsRates, DeductionTypes, ReimbursementTypes, and LeaveTypes all together as seen on the POST example https://developer.xero.com/documentation/payroll-api/payitems but this too had no success.

Do you have any idea where I'm going wrong or why its not working?

request_message.txt response_message.txt test.txt

Healyhatman commented 4 years ago

You can't create a single earnings rate - you have to retrieve ALL of the Earnings Rates, and add your new rate to the PayItems collection, and save that. Or at least that's my understanding.

From the docs:

Note: Deleting payroll PayItems works as follows. If you include a PayItem type (example: ) you must include each

, otherwise it will be deleted. If you omit the PayItem type from your request all PayItems of that type will *not* be modified or deleted. There are two exceptions. PayItems that have been used within a POSTED Payrun will become 'locked' and can not be deleted. EarningsRates with the default Name 'ETP Leave Earnings' or 'Redundancy' cannot be deleted.

I guess what's happening is it thinks you're trying to delete all the other rates you haven't included, and because some have been used it's not happy.

cproetti commented 4 years ago

@Healyhatman Yes I'm aware of that. If you look at the attached file test.txt you can see that I'm firstly retrieving all from the API and then adding the new item called "Testearnings".

The request thats made (attached file request_message.txt) contains all the existing EarningsTypes and the new item "Testearnings".

I looked at the POST example on Xero and saw that they were including all EarningsRates, DeductionTypes, ReimbursementTypes, and LeaveTypes in the same request, so I also tried making a request that also includes DeductionTypes, ReimbursementTypes, and LeaveTypes in addition to EarningsRates however I still get the same response Bad Request.

Healyhatman commented 4 years ago

Sorry for not reading your post properly. I'm having this issue too, it seems :)

cproetti commented 4 years ago

@calcinai Alright guys, heres the issue. On the file PayItem.php the function getRootNodeName() returns 'PayItem' when it should be 'PayItems'.

I have modified this, and re-tested with the same code, the request now goes through successfully and I can see the newly created item in Xero. I also re-tested with EarningsRate, DeductionType, LeaveType, and ReimbursementType all in the same request and it too went through successfully now.

Please locate the two attached files, one containing the original Request Message (also attached in my initial post), and the other containing the modified Request Message. The one with the opening xml tag "PayItems" is the successful request.

fixed_request_message.txt request_message.txt

Healyhatman commented 4 years ago

Can confirm: Changes the getRootNodeName to return "PayItems" and am able to save the new rates.