AOS_Invoices integration via API

Hello
For the past few days i 've been trying to integrate an accounting web service. I want to be able to just create an invoice in SuiteCRM and it would get sent via API to the Web service.

So my first step was to standardize an ID from the web service, because the web service uses its own ID system to pair clients to other documents.I created a field inside Accounts module “idokladid_c” that would store this ID for API request. Nevertheless, the Accounts(Suitecrm) - Clients(WebService) integration is working pretty good. i used logic_hooks and made an authentication function in another file so that it would always have access_token.

The problem i have, is with AOS_Invoices integration. This is what i did until now: I again used logic_hooks (after_save) to execute a function in custom/modules/AOS_Invoices/Ext/example.php . I reused the Auth script for access_token (i am aware that i should’t do that but i’m not very skilled in PHP)
So the next step is to write a method which will take the ID of Client(WebService) from the field in CRM and takes the product name and then composes a JSON Array. This is where my first problem is, when i try to add an Array into the Array it errors out:

My Input:

$data = array("PurchaserId" => $bean->idokladid_c,
              "IssuedInvoiceItems" => ["name" => "TestProdukt"],
);
$data = json_encode($data);

Output:

"Cannot deserialize the current JSON object (e.g. {\"name\":\"value\"}) into type 'System.Collections.Generic.IEnumerable`1[Doklad.Api.ApiModels.IssuedInvoiceItemApiModelWrite]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.\r\nTo 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.

As this is a problem, there is also one more with this i have no idea how to properly use [ $v = load_relationship() ] and use it to retrieve product names. To top it off i don’t think its gonna be able to retrieve more than 1 name, however that is not that big of a problem as we only add 1 product per invoice.

I’m using: Suitecrm Version 7.11.8
on: Ubuntu Server OS

Thank you for responses.

Sam

You’re mixing PHP array syntax with JSON array syntax.

When writing PHP nested arrays, don’t use [ to start the nested array, use “Array(”

Search online for correct PHP array syntax and validators (like https://phpcodechecker.com/ )

Hi. Thank you for your response.

So even tho i changed the PHP Array syntax as you said to :

$data = Array("PurchaserId" => 99199,
              "IssuedInvoiceItems" => Array("name" => "TestProdukt")
);

I’m still getting the same error

Thanks

Sam

I just tried it here

http://php.fnlist.com/php/json_encode

pasting this into the “value” input box:

Array("PurchaserId" => 99199,
              "IssuedInvoiceItems" => Array("name" => "TestProdukt")
)

and it encodes correctly…