Php PayPal REST API创建协议:传入的JSON请求未映射到API请求

Php PayPal REST API创建协议:传入的JSON请求未映射到API请求,php,json,rest,curl,paypal,Php,Json,Rest,Curl,Paypal,我正在使用PHP接口[尝试]创建一个计费协议。生成的curl命令如下所示: curl -v -X POST \ https://api.sandbox.paypal.com/v1/payments/billing-agreements/ \ -H "Content-Type: application/json" \ -H "Authorization: Bearer A21...iA" \ -d '{ "name":"sms_from_me_te

我正在使用PHP接口[尝试]创建一个计费协议。生成的
curl
命令如下所示:

curl -v -X POST \
    https://api.sandbox.paypal.com/v1/payments/billing-agreements/ \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer A21...iA" \
    -d '{
         "name":"sms_from_me_test",
         "description":"SMS From Me Monthly Subscription Agreement",
         "start_date":"2017-07-17T08:33:27Z",
         "plan":"P-26R66685UX449822NJ6L2OWY",
         "payer":{"payment_method":"paypal"}}'
响应总是这个JSON:

{
  "name":"MALFORMED_REQUEST",
  "message":"Incoming JSON request does not map to API request",
  "information_link":"https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST",
  "debug_id":"9c2701965bf29"
}
这对我来说是完全不透明的。好吧,JSON不被接受,它没有说明原因

以防万一,与我看到的一些示例相比,我在发送的JSON周围放了方括号

    -d "[{
        ...
        }]"
因为看起来你应该发送一个对象数组,而不是一个对象。这也失败了

我能够创建一个看起来有效的计划,我可以激活它,所以我知道OAuth就像一个符咒。所以这不是问题所在

我还能做什么

下面是使用最新版本PayPal PHP工具包的PHP代码:

    $payer = new \PayPal\Api\Payer();
    $payer->setPaymentMethod('paypal');

    $agreement = new \PayPal\Api\Agreement();

    date_default_timezone_set("UTC");
    $now = time() + 60 * 5;
    $created_on = strftime("%Y-%m-%dT%H:%M:%SZ", $now);

    $agreement
        ->setName($plan_name)
        ->setDescription("SMS From Me Monthly Subscription Agreement")
        ->setStartDate($created_on)
        ->setPlan($plan->getId())
        ->setPayer($payer);

    try
    {
        $agreement->create($api_context);
    }
    catch(\PayPal\Exception\PayPalConnectionException $ex)
    {
        echo "--------------------- exception\n";
        echo "Code: ", $ex->getCode(), "\n";
        echo "Data: ", $ex->getData(), "\n";
        $this->error = "Sorry. Could not create the PayPal Plan.";
        return false;
    }
数据:…
与上面显示的JSON相同

我的代码基于PayPal的github中的示例:


我从贝宝那里得到了答案

计划规范需要是一个对象:

"plan":{"id":"P-26R66685UX449822NJ6L2OWY"}
事实上,我首先将我的
$plan
对象传递给
Agreement::setPlan()
对象。但是当您拥有一个完整的
$plan
对象时,这意味着您违反了
协议
JSON

你不能做的事:

$plan = \PayPal\Api\Plan::get($plan_id, $this->api_context);
$agreement->setPlan($plan);
因为
get()
返回一个完整的计划

你要做的是:

$reduced_plan = new \PayPal\Api\Plan();
$reduced_plan->setId($plan_id);
现在您有了一个计划对象,该对象将在
协议中工作

$agreement->setPlan($reduced_plan);
文件对这一点肯定一点也不清楚


Paypal在github上的回答:

我从Paypal那里得到了答案

计划规范需要是一个对象:

"plan":{"id":"P-26R66685UX449822NJ6L2OWY"}
事实上,我首先将我的
$plan
对象传递给
Agreement::setPlan()
对象。但是当您拥有一个完整的
$plan
对象时,这意味着您违反了
协议
JSON

你不能做的事:

$plan = \PayPal\Api\Plan::get($plan_id, $this->api_context);
$agreement->setPlan($plan);
因为
get()
返回一个完整的计划

你要做的是:

$reduced_plan = new \PayPal\Api\Plan();
$reduced_plan->setId($plan_id);
现在您有了一个计划对象,该对象将在
协议中工作

$agreement->setPlan($reduced_plan);
文件对这一点肯定一点也不清楚

Paypal在github上的回答: