gpt4 book ai didi

php - 调用创建付款时出现 MALFORMED_REQUEST

转载 作者:太空宇宙 更新时间:2023-11-03 15:54:22 24 4
gpt4 key购买 nike

我正在尝试调用 PayPal 的 CreatePayment API (REST API) 函数但出现此错误:

[name] => MALFORMED_REQUEST  
[message] => Incoming JSON request does not map to API request

API 网址:https://api.sandbox.paypal.com/v1/payments/payment

header - PHP 数组:

(
[0] => Content-Type: application/json
[1] => Authorization: Bearer A21AAH....SQ
)

json 数据(我验证它是有效的 json):

{  
"intent":"sale",
"redirect_urls":"{\"return_url\": \"http:\/\/localhost\/ProgrammingProjects\/TestAppRain\/web\/return_placeholder.php\", \"cancel_url\": \"http:\/\/localhost\/ProgrammingProjects\/TestAppRain\/web\/cancel_placeholder.php\"}",
"payer":"{\"payment_method\": \"paypal\"}",
"transactions":"[ {\"amount\":{ \"total\": \"1.00\", \"currency\": \"USD\"},\"item_list\":{ \"items\": [ {\"quantity\": \"1\",\"name\": \"TestAppRain Ticket\",\"price\": \"1\",\"currency\": \"USD\",\"description\": \"TestAppRain Ticket\",\"tax\": \"0\" } ]},\"description\": \"TestAppRain Ticket\",\"invoice_number\": \"123456\",\"custom\": \"ENCRYPTED_USERID_HERE\" }]"
}

我不清楚如何将数组传递给 API,但我相信 Paypal 需要 json 格式的数据。这是我正在做的:

function TalkToPayPal($paypalURL, $data, $headers){
global $ppClientID, $ppSecret;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $paypalURL);

if($headers){
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}else{
curl_setopt($ch, CURLOPT_HEADER, false);
}

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $ppClientID . ":" . $ppSecret);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$result = curl_exec($ch);
curl_close($ch);

if(empty($result)){
return null;
}
else
{
$jsonObject = json_decode($result);

if(json_last_error() == JSON_ERROR_NONE){
return $jsonObject;
}else{
return null;
}
}
}

function CreatePayPalPayment($data, $headers){
global $ppDNS;
$ppURL = "https://$ppDNS/v1/payments/payment";
$jsonObj = TalkToPayPal($ppURL, $data, $headers);

if($jsonObj){
$errorString = "";
if($jsonObj->error){
$errorString .= $jsonObj->error . " | ";
}

if($jsonObj->error_description){
$errorString .= $jsonObj->error_description;
}

if($errorString){
return "ERROR: " . $errorString;
}else{
return $jsonObj;
}
}else{
return "ERROR: PayPal call to Create Payment failed.";
}
}

$response = CreatePayPalPayment($json, $headers);

我做错了什么?提前致谢。

最佳答案

enter image description here 您的不是有效的 JSON,因为您出于某种原因正在转义 ":

{  
"intent":"sale",
"redirect_urls":"{\"return_url\": \"http:\/\/localhost\/ProgrammingProjects\/TestAppRain\/web\/return_placeholder.php\", \"cancel_url\": \"http:\/\/localhost\/ProgrammingProjects\/TestAppRain\/web\/cancel_placeholder.php\"}",
"payer":"{\"payment_method\": \"paypal\"}",
"transactions":"[ {\"amount\":{ \"total\": \"1.00\", \"currency\": \"USD\"},\"item_list\":{ \"items\": [ {\"quantity\": \"1\",\"name\": \"TestAppRain Ticket\",\"price\": \"1\",\"currency\": \"USD\",\"description\": \"TestAppRain Ticket\",\"tax\": \"0\" } ]},\"description\": \"TestAppRain Ticket\",\"invoice_number\": \"123456\",\"custom\": \"ENCRYPTED_USERID_HERE\" }]"
}

去掉转义应该没问题

这是一个有效的 JSON(取自 https://developer.paypal.com/docs/api/overview/ )

"intent": "sale",
"redirect_urls": {
"return_url": "https://example.com/your_redirect_url.html",
"cancel_url": "https://example.com/your_cancel_url.html"
},
"payer": {
"payment_method": "paypal"
},
"transactions": [{
"amount": {
"total": "7.47",
"currency": "USD"
}
}]

这里是你应该使用的 JSON:

{
"intent": "sale",
"redirect_urls": {
"return_url": "http://localhost/ProgrammingProjects/TestAppRain/web/return_placeholder.php",
"cancel_url": "http://localhost/ProgrammingProjects/TestAppRain/web/cancel_placeholder.php"
},
"payer": {
"payment_method": "paypal"
},
"transactions": [
{
"amount": {
"total": "1.00",
"currency": "USD"
},
"item_list": {
"items": [
{
"quantity": "1",
"name": "TestAppRainTicket",
"price": "1",
"currency": "USD",
"description": "TestAppRainTicket",
"tax": "0"
}
]
},
"description": "TestAppRainTicket",
"invoice_number": "123456",
"custom": "ENCRYPTED_USERID_HERE"
}
]
}

关于php - 调用创建付款时出现 MALFORMED_REQUEST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47734995/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com