XeroAPI / xero-php-oauth2

Xero PHP SDK for oAuth 2 generated from Xero API OpenAPI Spec 3.0
MIT License
91 stars 65 forks source link

JSON request body could not be read #205

Closed deemonic closed 3 years ago

deemonic commented 3 years ago

Describe the bug A clear and concise description of what the bug is.

I have been trying to create an invoice using the following package https://github.com/webfox/laravel-xero-oauth2. I have raised this as an issue on that repo too. I keep getting the following error

XeroAPI\XeroPHP\ApiException: [400] Client error: PUT https://api.xero.com/api.xro/2.0/Invoices?summarizeErrors=false resulted in a 400 Bad Request response: { "ErrorNumber": 14, "Type": "PostDataInvalidException", "Message": "JSON request body could not be read, Error (truncated...) in file C:\inetpub\sites\webhooks.crystallegal.com\vendor\xeroapi\xero-php-oauth2\lib\Api\AccountingApi.php on line 7453

I can see in the AccountingApi.php file that the request is serialize as JSON

To Reproduce Steps to reproduce the behavior:

I have successfully done the following

Created an oAuth2.0 instance (This is working) Added the required scopes for Contacts and Invoices in the Xero.php config file and re-connected my app using oAuth 2.0 I have succussfulyl created a contact in Xero using the API and return the contact ID which is used for the Invoice request Create a LineItems object as at least 1 Line item is required to create an invoice Here is my method

public function createInvoice(Request $request, OauthCredentialManager $xeroCredentials) {

$arr_invoices = [];

$lineitems = [];
array_push($lineitems, $this->getLineItem());

$invoice = new Invoice();
$invoice->setReference('Ref-' . $request->matter_id)
    ->setDueDate(Carbon::now())
    ->setContact($this->getContact($xeroCredentials))
    ->setLineItems($lineitems)
    ->setStatus(Invoice::STATUS_AUTHORISED)
    ->setType(Invoice::TYPE_ACCPAY)
    ->setLineAmountTypes(\XeroAPI\XeroPHP\Models\Accounting\LineAmountTypes::EXCLUSIVE);

array_push($arr_invoices, $invoice);

$invoices = new Invoices();
$invoices->setInvoices($arr_invoices);

//dd($invoices);

$result = $this->apiInstance->createInvoices($xeroCredentials->getTenantId(), $invoices);

dd($result);

} Expected behavior

I would expect this to create an Invoice as I believe I have followed instructions form this package as well as the Xero documentation

SidneyAllen commented 3 years ago

@deemonic - a 400 error indicates invalid data.

Looking at your code example, there are several pieces of data you are getting from somewhere outside the code block.

Two places I would look - the "getLineItem" may not be using a valid Account Code. Also, getContact() method is unclear what you are returning.

We have additional SDK documentation you may want to look at https://xeroapi.github.io/xero-php-oauth2/docs/v2/accounting/index.html#api-Accounting-createInvoices

SidneyAllen commented 3 years ago

@deemonic - if you have any further question, let us know. Otherwise, we will go ahead and close this issue.