douniani679741
douniani679741
2017-09-19 15:13
浏览 50
已采纳

Square Connect - 当字段作为数组发送时,OrdersApi错误EXPECTED_ARRAY

I have been working with the square-connect api, and I am having issues trying to create an order. I have followed the docs for creating an order. Below is the code in question.

$api = new \SquareConnect\Api\OrdersApi();
    $location = $this->get_location();
    if (!empty($items)) {
        if (is_string($items)) {
            $items = json_decode($items);
        }
        // print_r($items);
        if (!is_array($items)) {
            // echo "Items is not an array -- exiting now";
            return false;
        }
        $order_data = [];
        $order_data['idempotency_key'] = uniqid();
        $order_data['line_items'] = [];
        foreach ($items as $key => $li) {
            $order_data['line_items'][] = [
                'name' => $li->name,
                'base_price_money' => ['amount' => ($li->price * 100)],
                'quantity' => $li->qty
            ];
        }
        $order_data['taxes'] = [
            'type' => 'ADDITIVE',
            'name' => 'State Sales Tax',
            'percentage' => '7'
        ];
        print_r($order_data);
        // exit;
        $apiResponse = $api->createOrder($location, new  \SquareConnect\Model\CreateOrderRequest($order_data));
        $order = $apiResponse->getOrder();
        print_r($apiResponse);
        print_r($order);
    } 

When I make the request I get the following error

Message: [HTTP/1.1 400 Bad Request] {"errors":[{"category":"INVALID_REQUEST_ERROR","code":"EXPECTED_ARRAY","detail":"Expected an array.","field":"taxes"}]}

I have checked that the taxes field is an array

["taxes"]=>
  array(3) {
    ["type"]=>
    string(8) "ADDITIVE"
    ["name"]=>
    string(15) "State Sales Tax"
    ["percentage"]=>
    string(1) "7"
  }

Any assistance would be greatly appreciated

EDIT: print_r($items) output

Array
(
    [0] => stdClass Object
        (
            [id] => 24
            [square_id] => L7PCKMIEDQFDR34IZ3E3VIDO
            [variation_id] => SUJCAPZJQLM7VGAILP66NMMM
            [variation_name] => Regular
            [qty] => 1
            [price] => 3.99
            [name] => Ham & Cheese
        )

    [1] => stdClass Object
        (
            [id] => 4
            [square_id] => 37RNDFXRVJPLR4UT7UXS5QOO
            [variation_id] => 27Z5H3V6ZRHW2X5LVNGXOARY
            [variation_name] => Starbucks DOUBLESHOT Expresso
            [qty] => 1
            [price] => 2.5
            [name] => Starbucks DOUBLESHOT Expresso
        )

)

var_dump(order_data)...

array(3) {
  ["idempotency_key"]=>
  string(13) "59c138b7a67e2"
  ["line_items"]=>
  array(2) {
    [0]=>
    array(3) {
      ["name"]=>
      string(12) "Ham & Cheese"
      ["base_price_money"]=>
      array(1) {
        ["amount"]=>
        float(399)
      }
      ["quantity"]=>
      string(1) "1"
    }
    [1]=>
    array(3) {
      ["name"]=>
      string(29) "Starbucks DOUBLESHOT Expresso"
      ["base_price_money"]=>
      array(1) {
        ["amount"]=>
        float(250)
      }
      ["quantity"]=>
      string(1) "1"
    }
  }
  ["taxes"]=>
  array(3) {
    ["type"]=>
    string(8) "ADDITIVE"
    ["name"]=>
    string(15) "State Sales Tax"
    ["percentage"]=>
    string(1) "7"
  }
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongtu0088
    dongtu0088 2017-09-19 15:46
    已采纳

    The issue here is that you are expected to give an array of tax objects, and it is confusing because you are using the implicit object creation capability of the SDK with PHP arrays. in JSON, your taxes would look like:

    "taxes":[
      {
        "type":"additive"
        ...
      }
    ]
    

    (note that is is an array of objects), you are providing:

    "taxes":{
      "type":"additive"
      ...
    }
    

    So if you want to just make a quick change to your code, you should do something like:

        $order_data['taxes'] =array(array(
            'type' => 'ADDITIVE',
            'name' => 'State Sales Tax',
            'percentage' => '7'
        ));
    

    If you wanted to be more verbose in your code you could do something like:

    $order_data = new \SquareConnect\Model\CreateOrderRequest();
    $taxes = \SquareConnect\Model\CreateOrderRequestTax();
    $taxes->setType('ADDITIVE');
    ...
    $order_data->setTaxes($taxes);
    

    Clearly the implicit array way is a bit easier, but it can be confusing in PHP. Does that help?

    点赞 评论

相关推荐