2017-08-17 56 views
0

对不起这个(不得已,短头;-)拍摄自己的;这显然是我的错,但我花了几天时间在这里和其他地方搜索,阅读文档,尝试各种各样的东西等,但它仍然无法正常工作。PHP REST贝宝支付创建JSON malfromed响应

有人请点我什么是错在这里:

<?php 

$data = array(
    "intent" => "sale", 
    "redirect_urls" => array(
      "return_url" => "http://.../ok.html", 
      "cancel_url" => "http://.../cancel.html" 
     ), 
    "payer" => array(
      "payment_method" => "paypal" 
     ), 
    "transactions" => array(
      "amount" => array(
        "total" => "1.23", 
        "currency" => "GBP" 
       ) 
     ) 
); 

$ch = curl_init("https://api.sandbox.paypal.com/v1/payments/payment"); 

curl_setopt($ch, CURLOPT_VERBOSE, true); 
$logfile = fopen("curl_log.txt", "w"); 
curl_setopt($ch, CURLOPT_STDERR, $logfile); 

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json', 
    'Authorization: Bearer <long token returned from REST call', 
)); 
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 

$res = curl_exec($ch); 

curl_close($ch); 
fclose($logfile); 

?> 

错误:

{"name":"MALFORMED_REQUEST","message":"Incoming JSON request does not map to API request","information_link":"developer.paypal.com/webapps/de‌​veloper/docs/api/…;} 
+0

有什么错误信息或卷曲结果? – smarber

+0

你想达到什么目的?有没有错误?请解释你的问题。 – Roshan

+1

使用[卷曲误差函数(http://php.net/manual/en/function.curl-strerror.php),以获得实际的错误的详细信息 –

回答

0

更新您的$ data数组一样,

$data = array(
    "intent" => "sale", 
    "payer" => array(
      "payment_method" => "paypal" 
     ), 
    "transactions" =>array( 
      array(
       "amount" => array(
         "total" => "1.23", 
         "currency" => "GBP") 
      ) 
     ), 
    "redirect_urls" => array(
      "return_url" => "http://.../ok.html", 
      "cancel_url" => "http://.../cancel.html" 
    ) 
); 
+0

它不是复制粘贴,你需要将事务数组包装在一个数组中 –

+0

非常感谢。 – Nagi

+0

如果它工作,然后upvote答案,所以这将是对其他人也有帮助 –