Square:值不应为字符串

Square: Value was not expected to be a string

我遇到 "Value was not expected to be a string" 错误。我已经使用验证器验证了 JSON 请求,并确保所有值都包含在双引号内。但是,当它告诉我长度(在本例中为 500)时,它大于 JSON 本身的长度。

[category] => INVALID_REQUEST_ERROR
[code] => BAD_REQUEST
[detail] => Value was not expected to be a string (line 1, character 500)

使用:

{
"idempotency_key": "1",
"billing_address": {
"address_line_1": "123 Main Street",
"address_line_2": "",
"locality": "New York City",
"administrative_district_level_1": "NY",
"postal_code": "12345",
"country": "US"
},
"amount_money": {
"amount": "10",
"currency": "USD"
},
"delay_capture": "false",
"buyer_email_address": "someone@example.com",
"card_nonce": "C257wsh4fggd1OWEQLTwU0MIdnA"
}

这个错误信息太含糊了。如何将未知变量从字符串转换为未知的所需数据类型?

这里的问题是您试图将 amount_money 中的 amount 指定为字符串而不是数字,delay_capture 也是如此。尝试

"amount": 10,
"delay_capture": false,

我同意,错误信息有点混乱,我看看能不能改进一下。

次要问题:如果数据已经格式化为JSON 确保JSON没有被第二次编码!

//$encodedData = json_encode($json);//double-encoded code issue here!
$encodedData = $json;
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $encodedData);
$request_headers[] = 'Content-Length: '.strlen($encodedData);