【问题标题】:Paypal Rest Api Error贝宝休息 API 错误
【发布时间】:2013-10-18 06:07:46
【问题描述】:

当我尝试通过贝宝沙盒提交测试付款时,我收到了这个新响应:

array(2) { ["header"]=> string(287) "HTTP/1.1 400 Bad Request Server: Apache-Coyote/1.1 PayPal-Debug-Id: b235ca4e7c1ed Content-Type: application/json Content-Length: 334 DC: origin1-api.sandbox.paypal.com Date: Thu, 10 Oct 2013 17:23:54 GMT Connection: close Set-Cookie: DC=origin1-api.sandbox.paypal.com; secure " ["body"]=> object(stdClass)#4 (5) { ["name"]=> string(16) "VALIDATION_ERROR" ["details"]=> array(1) { [0]=> object(stdClass)#5 (2) { ["field"]=> string(12) "transactions" ["issue"]=> string(95) "Item amount must add up to specified amount subtotal (or total if amount details not specified)" } } ["message"]=> string(29) "Invalid request - see details" ["information_link"]=> string(73) "https://developer.paypal.com/webapps/developer/docs/api/#VALIDATION_ERROR" ["debug_id"]=> string(13) "b235ca4e7c1ed" } }

这个错误表明我的项目总数没有加到我的小计中,但很明显,正如您从下面的 json 中看到的那样:

{
"intent": "sale",
"redirect_urls": {
    "return_url": "http://test.example.com/",
    "cancel_url": "http://www.example.com/cart"
},
"payer": {
    "payment_method": "paypal"
},
"transactions": [
    {
        "amount": {
            "total": "904.75",
            "currency": "USD",
            "details": {
                "subtotal": "889.80",
                "shipping": "14.95"
            }
        },
        "description": "Order ID #9",
        "item_list": {
            "items": [
                {
                    "quantity": "1",
                    "name": "40 Steps and a View-20\" x 24\" - 1.5\" thick gallery wrap canvas",
                    "price": "204.95",
                    "currency": "USD",
                    "sku": "7 - 10"
                },
                {
                    "quantity": "1",
                    "name": "Emerald Bay-24\" x 36\" - 0.75\" thin gallery wrap canvas",
                    "price": "219.95",
                    "currency": "USD",
                    "sku": "8 - 5"
                },
                {
                    "quantity": "1",
                    "name": "Life on the Beach-36\" x 48\" - 0.75\" thin gallery wrap canvas",
                    "price": "299.95",
                    "currency": "USD",
                    "sku": "2 - 6"
                },
                {
                    "quantity": "1",
                    "name": "View from the Mountain Tops-16\" x 20\" - 0.75\" thin gallery wrap canvas",
                    "price": "167.95",
                    "currency": "USD",
                    "sku": "5 - 3"
                }
            ]
        }
    }
]

}

这是我的支付宝代码:

class paypal {
    private $access_token;
    private $token_type;

    /**
    * Constructor
    *
    * Handles oauth 2 bearer token fetch
    * @link https://developer.paypal.com/webapps/developer/docs/api/#authentication--headers
    */
    public function __construct(){
        $postvals = "grant_type=client_credentials";
        $uri = PAYMENT_URI . "v1/oauth2/token";

        $auth_response = self::curl($uri, 'POST', $postvals, true);
        $this->access_token = $auth_response['body']->access_token;
        $this->token_type = $auth_response['body']->token_type;
    }

    /**
    * cURL
    *
    * Handles GET / POST requests for auth requests
    * @link http://php.net/manual/en/book.curl.php
    */
    private function curl($url, $method = 'GET', $postvals = null, $auth = false){
        $ch = curl_init($url);

        //if we are sending request to obtain bearer token
        if ($auth){
            $headers = array("Accept: application/json", "Accept-Language: en_US");
            curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
            curl_setopt($ch, CURLOPT_USERPWD, CLIENT_ID . ":" .CLIENT_SECRET);
            curl_setopt($ch, CURLOPT_SSLVERSION, 3);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        //if we are sending request with the bearer token for protected resources
        } else {
            $headers = array("Content-Type:application/json", "Authorization:{$this->token_type} {$this->access_token}");
        }

        $options = array(
            CURLOPT_HEADER => true,
            CURLINFO_HEADER_OUT => true,
            CURLOPT_HTTPHEADER => $headers,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_VERBOSE => true,
            CURLOPT_TIMEOUT => 10
        );

        if ($method == 'POST'){
            $options[CURLOPT_POSTFIELDS] = $postvals;
            $options[CURLOPT_CUSTOMREQUEST] = $method;
        }

        curl_setopt_array($ch, $options);

        $response = curl_exec($ch);
        $header = substr($response, 0, curl_getinfo($ch,CURLINFO_HEADER_SIZE));
        $body = json_decode(substr($response, curl_getinfo($ch,CURLINFO_HEADER_SIZE)));
        curl_close($ch);

        return array('header' => $header, 'body' => $body);
    }

    // Function for Processing Payment
    function process_payment($request) {
        $postvals = $request;
        $uri = PAYMENT_URI . "v1/payments/payment";
        return $this->curl($uri, 'POST', $postvals);
    }
}

【问题讨论】:

  • 发现任何错误? @user2864113
  • 我在顶部发布了响应,但没有收到 php 错误
  • 显示您的请求代码
  • 我用贝宝代码更新了
  • 我查看了您的来源,但 Paypal IPN 与 Paypal REST Api 不同。我使用的贝宝代码直接来自他们的开发者博客。这是链接:[链接]devblog.paypal.com/authenticating-with-oauth-2

标签: php json paypal


【解决方案1】:

Transactions 是一个数组,但您传入的是一个对象。你应该发送

“交易”:[ { “数量”:{ ... } ]

改为

更多信息请见https://developer.paypal.com/webapps/developer/docs/api/#create-a-payment

【讨论】:

  • 我编辑了我的问题以寻求帮助,因为我无法弄清楚如何让 json_encode 将括号放入事务中。请参阅上面的编辑信息。
【解决方案2】:
  'transactions' => array(
        0 => array(
          'amount' => array(
              'total' =>''.number_format($order_total,2).'',
              .....
           ),               
           'description' =>'Mike and Maureen Photography - Order ID #'.$order_id.''
        )
    ),

【讨论】:

  • 谢谢,我发现那部分现在出现了一个新错误,因为我已经针对新错误编辑了我的帖子。
  • 这很容易从错误消息中找出。您的商品价格加到 892.80,总价格设置为 889.80
  • 是的,我只是将价格与数据库中的销售混为一谈,现在一切都很好。现在我只需要处理我得到的数据。
【解决方案3】:

你是尝试自己打开socket,所以需要自己添加HTTP Host header

$header .= "Host: www.sandbox.paypal.com\r\n";

来源:PayPal IPN Bad Request 400 Error

【讨论】:

    猜你喜欢
    • 2014-02-15
    • 2016-04-25
    • 2015-10-29
    • 2017-02-01
    • 2018-10-05
    • 2014-11-02
    • 2015-10-16
    • 2020-04-09
    • 1970-01-01
    相关资源
    最近更新 更多