PayPal Orders v2:请求格式不正确、语法不正确或违反架构
PayPal Orders v2: Request is not well-formed, syntactically incorrect, or violates schema
我正在尝试使用 PayPal API。以下是 Curl 用法的参考:https://developer.paypal.com/docs/api/orders/v2/
在 google 中搜索后,我发现 link 显示了类似的错误:但是,没有给出修复它的提示。 https://www.paypal-community.com/t5/REST-APIs/Request-is-not-well-formed-syntactically-incorrect-or-violates/td-p/2090480
我在 PHP 和 Laravel
中有以下代码
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', $uri, [
'headers' => [
'Accept' => 'application/json',
'Accept-Language' => 'en_US',
'Content-Type' => 'application/json',
'Authorization' => "Bearer " . $this->token
],
'form_params' => [
"intent" => "CAPTURE",
"purchase_units" => [
"amount" => [
"currency_code" => "USD",
"value" => "100.00"
]
]
]
]
);
当我运行代码时,出现以下错误
GuzzleHttp\Exception\ClientException Client error: POST https://api.sandbox.paypal.com/v2/checkout/orders
resulted in a 400 Bad Request
response: {"name":"INVALID_REQUEST","message":"Request is
not well-formed, syntactically incorrect, or violates
schema.","debug_id (truncated...)
更新 1 - 修改了答案中的代码,但仍然是相同的错误消息
$response = $client->request('POST', $uri, [
'headers' => [
'Accept' => 'application/json',
'Accept-Language' => 'en_US',
'Content-Type' => 'application/json',
'Authorization' => "Bearer " . $this->token
],
'form_params' => [
"intent" => "CAPTURE",
"purchase_units" => [
[
"amount" => [
"currency_code" => "USD",
"value" => "100.00"
]
]
]
]
]
);
文档说:
purchase_units: array (contains the purchase_unit object)
An array of purchase units. Each purchase unit establishes a contract between a customer and merchant. Each purchase unit represents either a full or partial order that the customer intends to purchase from the merchant.
你现在的 purchase_units 是 purchase_unit,不是 purchase_unit 的数组,试试把你的 form_params 改成
[
"intent" => "CAPTURE",
"purchase_units" => [
[
"amount" => [
"currency_code" => "USD",
"value" => "100.00"
]
]
]
]
'form_params' => [
PayPal REST API 不使用表单参数。
您需要发送一个 'json' 数据对象。见 GuzzleHttp client documentation.
下面的代码适合我。
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', $uri, [
'json' => [
"intent" => "CAPTURE",
"purchase_units" => [
[
"amount" => [
"currency_code" => "USD",
"value" => "100.00"
]
]
]
],
'headers' => [
'Accept' => 'application/json',
'Accept-Language' => 'en_US',
'Content-Type' => 'application/json',
'Authorization' => "Bearer " . $this->token
]
]
);
我正在尝试使用 PayPal API。以下是 Curl 用法的参考:https://developer.paypal.com/docs/api/orders/v2/
在 google 中搜索后,我发现 link 显示了类似的错误:但是,没有给出修复它的提示。 https://www.paypal-community.com/t5/REST-APIs/Request-is-not-well-formed-syntactically-incorrect-or-violates/td-p/2090480
我在 PHP 和 Laravel
中有以下代码$client = new \GuzzleHttp\Client();
$response = $client->request('POST', $uri, [
'headers' => [
'Accept' => 'application/json',
'Accept-Language' => 'en_US',
'Content-Type' => 'application/json',
'Authorization' => "Bearer " . $this->token
],
'form_params' => [
"intent" => "CAPTURE",
"purchase_units" => [
"amount" => [
"currency_code" => "USD",
"value" => "100.00"
]
]
]
]
);
当我运行代码时,出现以下错误
GuzzleHttp\Exception\ClientException Client error:
POST https://api.sandbox.paypal.com/v2/checkout/orders
resulted in a400 Bad Request
response: {"name":"INVALID_REQUEST","message":"Request is not well-formed, syntactically incorrect, or violates schema.","debug_id (truncated...)
更新 1 - 修改了答案中的代码,但仍然是相同的错误消息
$response = $client->request('POST', $uri, [
'headers' => [
'Accept' => 'application/json',
'Accept-Language' => 'en_US',
'Content-Type' => 'application/json',
'Authorization' => "Bearer " . $this->token
],
'form_params' => [
"intent" => "CAPTURE",
"purchase_units" => [
[
"amount" => [
"currency_code" => "USD",
"value" => "100.00"
]
]
]
]
]
);
文档说:
purchase_units: array (contains the purchase_unit object) An array of purchase units. Each purchase unit establishes a contract between a customer and merchant. Each purchase unit represents either a full or partial order that the customer intends to purchase from the merchant.
你现在的 purchase_units 是 purchase_unit,不是 purchase_unit 的数组,试试把你的 form_params 改成
[
"intent" => "CAPTURE",
"purchase_units" => [
[
"amount" => [
"currency_code" => "USD",
"value" => "100.00"
]
]
]
]
'form_params' => [
PayPal REST API 不使用表单参数。
您需要发送一个 'json' 数据对象。见 GuzzleHttp client documentation.
下面的代码适合我。
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', $uri, [
'json' => [
"intent" => "CAPTURE",
"purchase_units" => [
[
"amount" => [
"currency_code" => "USD",
"value" => "100.00"
]
]
]
],
'headers' => [
'Accept' => 'application/json',
'Accept-Language' => 'en_US',
'Content-Type' => 'application/json',
'Authorization' => "Bearer " . $this->token
]
]
);