academe / authorizenet-objects

Value objects for construction Authorize.Net messages
7 stars 6 forks source link

CHECKME: JSON arrays #1

Closed judgej closed 7 years ago

judgej commented 7 years ago

The documentation gives example XML arrays like this:

    <lineItems>
      <lineItem>
        <itemId>1</itemId>
        <name>vase</name>
        <description>Cannes logo </description>
        <quantity>18</quantity>
        <unitPrice>45.00</unitPrice>
      </lineItem>
    </lineItems>

then the JSON form looks like this in some places (with no arrays or unique properties, so could never work if there were more than one lineItem):

            "lineItems": {
                "lineItem": {
                    "itemId": "1",
                    "name": "vase",
                    "description": "Cannes logo",
                    "quantity": "18",
                    "unitPrice": "45.00"
                }
            },

and in other places it looks like this:

            "userFields": {
                "userField": [
                    {
                        "name": "MerchantDefinedFieldName1",
                        "value": "MerchantDefinedFieldValue1"
                    },
                    {
                        "name": "favorite_color",
                        "value": "blue"
                    }
                ]
            }

Now, this may be how it works, but looks wrong. It could be that a single lineItem is sent as an object, and multiple lineItems are sent as an array of objects, but it makes more sense to me if the same datatype is used in all cases (instead of a lineItem being sometimes an object and sometimes an array), i.e. an array of objects, even if only one. If always sending an array, then it it makes sense to drop the userField object in the structure. This is how I have assumed it works, for now:

            "userFields": [
                    {
                        "name": "MerchantDefinedFieldName1",
                        "value": "MerchantDefinedFieldValue1"
                    },
                    {
                        "name": "favorite_color",
                        "value": "blue"
                    }
            ]

This ticket will remain open until I know which is correct.

judgej commented 7 years ago

The answer is that each array of userField or lineItem fields needs to be wrapped in a single userField or lineItem element. That seems a bit bizarre, but is the way the translation from XML to JSON has been implemented.

However, unlike as shown in the docs, an array of is permitted, so we can just stick to an array and not have to drop down to an object when there is just one.