PayPal订单第2版:请求格式不正确,语法上不正确或违反架构

潘卡伊

我正在尝试使用PayPal API。这是Curl用法的参考:https : //developer.paypal.com/docs/api/orders/v2/

在google中搜索后,我发现了一个显示类似错误的链接:但是,没有给出修复它的提示。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客户端错误:POST https://api.sandbox.paypal.com/v2/checkout/orders导致400 Bad Request响应:{“ name”:“ INVALID_REQUEST”,“ message”:“请求格式不正确,语法错误或违反架构。”,“ debug_id(已截断... )

更新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"
                    ]
                ]
            ]
        ]
    ]
);
潘卡伊

下面的代码对我有用。

$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
        ]
    ]            
);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章