0

I have copied the sample code from the Xero developers site but keep getting an error with the DateOfBirth here is the code

<?php
require_once(__DIR__ . '/vendor/autoload.php');

// Configure OAuth2 access token for authorization: OAuth2
$config = XeroAPI\XeroPHP\Configuration::getDefaultConfiguration()->setAccessToken( 'YOUR_ACCESS_TOKEN' );       

$apiInstance = new XeroAPI\XeroPHP\Api\PayrollAuApi(
    new GuzzleHttp\Client(),
    $config
);
$xeroTenantId = "xeroTenantId_example";
$dateOfBirth = new DateTime('2000-10-28');

$homeAddress = new XeroAPI\XeroPHP\Models\PayrollAu\HomeAddress;
$homeAddress->setAddressLine1('123 Test st');
$homeAddress->setRegion('VIC');
$homeAddress->setPostalCode(3000);
$homeAddress->setCity('Melbourne');

$employee = new XeroAPI\XeroPHP\Models\PayrollAu\Employee;
$employee->setFirstName('Adam');
$employee->setLastName('Adamson');
$employee->setDateOfBirth($dateOfBirth);
$employee->setHomeAddress($homeAddress);

try {
  $result = $apiInstance->createEmployee($xeroTenantId, $employee);
} catch (Exception $e) {
  echo 'Exception when calling PayrollAuApi->createEmployee: ', $e->getMessage(), PHP_EOL;
}
?>

The error I get is Bad requestError occurred during JSON de/serialization. Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'Paycycle.API.DTO.AU.Employee.UpdateEmployeeRequest' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'DateOfBirth', line 1, position 15.

This is what is being posted from the XeroAPI\XeroPHP\Models\PayrollAu\Employee Object

[date_of_birth] => DateTime Object ( [date] => 2000-10-28 00:00:00.000000 [timezone_type] => 3 [timezone] => Australia/NSW )

DeanO
  • 93
  • 1
  • 10

3 Answers3

0

The documentation doesn't look much different.

Maybe pass an array of employees instead?

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • nope, I can add an employee via the accounting api rather than the payroll AU api, but I then can't find the employee, which is no use at all. You would expect that their demo code would atleast work but it doesn't – DeanO May 15 '22 at 07:05
  • I've indeed confused the [documentation](https://xeroapi.github.io/xero-php-oauth2/docs/v2/payroll_au/index.html#api-PayrollAu-createEmployee); maybe pass an array `[ $employee ]`? The update call would already need to have an `$employeeID`. One can only guess, while `UpdateEmployeeRequest` is unknown. – Martin Zeitler May 15 '22 at 07:23
0

Passing and array of employees like this worked for me.

[{
      "FirstName": "Joe",
      "LastName": "Baba",
      "DateOfBirth": "1998-09-01",
      "HomeAddress": {
        "AddressLine1": "123 Main St",
        "AddressLine2": "St. Kilda",
        "City": "Waiwhetu",
        "Region": "ACT",
        "PostalCode": "3182",
        "Country": "AUSTRALIA"
      }
    }
]

Here is the screenshot of the post method that executed successfully.

1

vimuth
  • 5,064
  • 33
  • 79
  • 116
0

yep, got it sorted, I just needed to put the employee into an array

$newEmployees = [];     
array_push($newEmployees, $employee);

$result = $apiInstance->createEmployee($xeroTenantId, $newEmployees);
DeanO
  • 93
  • 1
  • 10