500 error posting a Sale

bobpassarobobpassaro Member Posts: 9
edited October 22 in Development

So I'm trying to post Sales to LS from a Woocommerce website.

I think I have this right, according to the docs ( https://developers.lightspeedhq.com/retail/tutorials/sales/ ). I am posting to here:

https://api.lightspeedapp.com/API/Account/{account_id}/Sale.json

I'm sending data that looks basically like this:

$sale = array (  
    'employeeID' => 3,
    'registerID' => 1,
    'shopID' => 1,
    'customerID' => 27734,
    'completed' => true,
    'referenceNumber' => '12345678',
    'referenceNumberSource' => 'Website sale',
    'SaleLines' => array(
        'SaleLine' => array(
            array(
                'itemID' => 26190,
                'unitQuantity' => 1,
            ),
        ),
    ),
    'SalePayments' => array(
        'SalePayment' => array(
            'amount' => 10,
            'paymentTypeID' => 3,
        ),
    ),
);

Well, json_encoded, it looks like:

{"employeeID":3,"registerID":1,"shopID":1,"customerID":27734,"completed":true,"referenceNumber":"12345678","referenceNumberSource":"Website sale","SaleLines":{"SaleLine":[{"itemID":26190,"unitQuantity":1}]},"SalePayments":{"SalePayment":{"amount":10,"paymentTypeID":3}}}

The response I get refers to some kind of type error, but I'm not seeing what's up:

{"httpCode":"500","httpMessage":"Internal Server Error","message":"Argument 1 passed to helpers\\XML::arrayToXML() must be of the type array, string given, called in \/web\/cloud\/includes\/helpers\/JSON.class.php on line 57","errorClass":"TypeError"}

Any help appreciated. What am I missing?? Thanks!

Tags:

6 comments

  • gregaricangregarican Member Posts: 336 

    I didn't try running your API call to test, but I'd be careful in how array elements are passed. Here below is the example in their doc page. My guess is that if there is only one SaleLine element then it's not passed in as an array. Same with SalePayment. If there is only a single element it's not passed in as an array either.


    {
        "employeeID": 2,
        "registerID": 5,
        "shopID": 1,
        "customerID": 8,
        "completed": true,
        "SaleLines": {
            "SaleLine": [
            {
                "itemID": 12,
                "unitQuantity": 1
            },
            {
                "itemID": 28,
                "unitQuantity": 1
            }
            ]
        },
        "SalePayments": {
        	"SalePayment": {
        		"amount": 34.48,
        		"paymentTypeID": 1
        	}
        }
    }
    


  • bobpassarobobpassaro Member Posts: 9

    Thanks. I don't know, though. To me, it looks like the JSON is getting parsed correctly. My JSON string looks just like the example -- I mean, without the line breaks. I don't get it, at all. I wish it told me exactly what was supposed to be an array.

  • gregaricangregarican Member Posts: 336 

    Your code example that assigns the $sale variable spells it out for you. Anything that is included in the array() I am assuming will be enclosed in opening and closing characters in your JSON request. Would those characters be braces {} or brackets [] ? That then makes a difference in what your interpreted JSON request will look like.

    Lightspeed Retail is notoriously inconsistent in how it handles arrays. Any array element that only has a single value doesn't have brackets, while any array element that has multiple values will have brackets.

    If you double check how you are assigning your $sale variable then you should find your solution. Can you capture your JSON request in Fiddler or something and paste it here?

  • bobpassarobobpassaro Member Posts: 9

    Here it is formatted a little bit easier to read. Wait, you're saying if there is only one element in SaleLine, it should not be an array, just an object? But if you look at the error message, it seems to say I'm passing a string where it wants and array, which would be the opposite of what I think you are saying.

    {
       "employeeID":3,
       "registerID":1,
       "shopID":1,
       "customerID":27734,
       "completed":true,
       "SaleLines":{
          "SaleLine":[
             {
                "itemID":26190,
                "unitQuantity":1
             }
          ]
       },
       "SalePayments":{
          "SalePayment":{
             "amount":10,
             "paymentTypeID":3
          }
       }
    }
    


  • Adrian SamuelAdrian Samuel Moderator, Lightspeed Staff Posts: 573 moderator

    Hey @bobpassaro,

    Check you're passing the Content-Type header "application/json" and that the API client library is passing your request configuration as you see it here


    I've tested this payload in Postman and found no issue

    Adrian Samuel

    API Integrations Consultant - Strategic Solutions

    Lightspeed HQ

Sign In or Register to comment.