【问题标题】:PayPal REST API creating an agreement: Incoming JSON request does not map to API requestPayPal REST API 创建协议:传入 JSON 请求未映射到 API 请求
【发布时间】:2017-12-21 18:27:05
【问题描述】:

我正在使用 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;
    }

Data: ... 与我上面显示的 JSON 相同...

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

https://github.com/paypal/PayPal-PHP-SDK/blob/master/sample/billing/CreateBillingAgreementWithPayPal.php

【问题讨论】:

    标签: php json rest curl paypal


    【解决方案1】:

    我从 PayPal 得到了答复。

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

    "plan":{"id":"P-26R66685UX449822NJ6L2OWY"}
    

    事实上,我首先将我的$plan 对象传递给了Agreement::setPlan() 对象。但是当你有一个完整的$plan 对象时,这意味着你破坏了Agreement 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 中工作的计划对象:

    $agreement->setPlan($reduced_plan);
    

    文档对那一点肯定不清楚。

    Paypal 在 github 上的回答:https://github.com/paypal/PayPal-PHP-SDK/issues/891

    【讨论】:

      猜你喜欢
      • 2020-10-17
      • 2018-09-07
      • 2017-12-31
      • 2015-03-11
      • 2017-12-19
      • 2017-04-17
      • 2015-07-18
      • 2017-02-13
      • 2014-02-08
      相关资源
      最近更新 更多