sdk-php icon indicating copy to clipboard operation
sdk-php copied to clipboard

transactionRequest errors on array of lineItems

Open Critter opened this issue 6 years ago • 5 comments

I didn't know where else to ask this.

The schema says that transactionRequestType takes <xs:element name="lineItems" type="anet:ArrayOfLineItem" minOccurs="0"/> an array of lineItem.

The example on the documention site only shows 1 item being passed in.

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

Wouldn't an array of lineitems be like this:

"lineItems": [
        {
          "lineItem": {
            "itemId": "10077",
            "name": "Fake name for something",
            "description": "dog.com",
            "quantity": 1,
            "unitPrice": "399.00"
          }
        },
        {
          "lineItem": {
            "itemId": "10077",
            "name": "fake name for something",
            "description": "cat.com",
            "quantity": 1,
            "unitPrice": "399.00"
          }
        }
      ]

This throws back an error saying that lineItems is an invalid element.

I'm creating my lineItems like this:

public function getLineItems()
{
	$result = [];
	foreach ($this->lineItems as $lineitem) {
		$arr = array(
			"lineItem" => array(
				"itemId" => $lineitem->itemId,
				"name" => mb_strimwidth($lineitem->name,0,31,''),
				"description" => $lineitem->description,
				"quantity" => $lineitem->quantity,
				"unitPrice" => $lineitem->unitPrice
			)
		);
		$result[] = $arr;
	}
	return $result;
}

and using them in my request like this:

public function generatePaymentRequest()
{
  $result = array(
    "createTransactionRequest" => array(
      "merchantAuthentication" => array(
        "name" => $this->authnet->name,
        "transactionKey" => $this->authnet->transactionkey
      ),
      "refId" => $this->refId,
      "transactionRequest" => array(
        "transactionType" => $this->transactionType,
        "amount" => $this->amount,
        "profile" => array(
          "customerProfileId" => $this->customerProfileId,
          "paymentProfile" => array(
            "paymentProfileId" => $this->paymentProfileId
          ),
        ),
        "lineItems" => $this->getLineItems() <-----<
      ),
    )
  );
  return json_encode($result);
}

Critter avatar Aug 26 '18 17:08 Critter

Hi @Critter

Can you refer to this issue #116 ? It might have the answer you are looking for.

gnongsie avatar Aug 27 '18 07:08 gnongsie

I was hopeful, but I'm still having issues.

This is a valid request according to the docs. I've run this with my data, and it's created a 2-lined order

{
  "createTransactionRequest": {
    "merchantAuthentication": {
      "name": "zzzzzzz",
      "transactionKey": "zzzzzzz"
    },
    "refId": "xnumber-ref",
    "transactionRequest": {
      "transactionType": "authCaptureTransaction",
      "amount": 798,
      "profile": {
        "customerProfileId": "8675309",
        "paymentProfile": {
          "paymentProfileId": "48595683"
        }
      },
      "lineItems": {
        "lineItem" : {
          "itemId": "10077",
          "name": "Yearly Subscription",
          "description": "dog.com",
          "quantity": 1,
          "unitPrice": "399.00"
        },
        "lineItem" : {
          "itemId": "10077",
          "name": "Yearly Subscription",
          "description": "cat.com",
          "quantity": 1,
          "unitPrice": "399.00"
        }
      }
    }
  }
}

The problem is, is that it's not valid JSON, if you try to validate it you get a SyntaxError: Duplicate key 'lineItem' on line 25

validated JSON would have the lineitems like this:

{
  "createTransactionRequest": {
    "merchantAuthentication": {
      "name": "zzzzzzz",
      "transactionKey": "zzzzzzz"
    },
    "refId": "xnumber-ref",
    "transactionRequest": {
      "transactionType": "authCaptureTransaction",
      "amount": 798,
      "profile": {
        "customerProfileId": "8675309",
        "paymentProfile": {
          "paymentProfileId": "48595683"
        }
      },
      "lineItems": [
        {
          "itemId": "10077",
          "name": "Yearly Subscription",
          "description": "dog.com",
          "quantity": 1,
          "unitPrice": "399.00"
        },
        {
          "itemId": "10077",
          "name": "Yearly Subscription",
          "description": "cat.com",
          "quantity": 1,
          "unitPrice": "399.00"
        }
      ]
    }
  }
}

Critter avatar Aug 27 '18 18:08 Critter

I agree that this is not valid JSON, depending on the validator you used. We have taken certain liberties with the JSON validation to ensure that this works in our context.

gnongsie avatar Aug 28 '18 05:08 gnongsie

Something should reflect this somewhere, though, no? Or at least the JSON example should show more than 1 lineitem since it doesn't match with the schema.

Critter avatar Aug 29 '18 14:08 Critter

Thank you for the suggestion. I'll relay that information to the relevant team to have it documented in some way so that developers are not inconvenienced.

gnongsie avatar Aug 29 '18 18:08 gnongsie