调用创建付款时MALFORMED_REQUEST

时间:2017-12-10 01:25:36

标签: php paypal paypal-rest-sdk

我正在尝试调用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

标题 - 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);

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

2 个答案:

答案 0 :(得分:1)

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"
    }
  ]
}

答案 1 :(得分:0)

你没有一个有效的json(没有语法明智但是Paypal的预期)。我假设您正在逃避并创建字符串而不是实际对象。以下是您应该尝试的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"
    }
  ]
}

您也可以尝试添加

curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data))                                                                       
);                                                                                                                   

这将确保您发送有关数据格式的标题