PHP PayPal 错误 - MALFORMED_REQUEST - 传入的 JSON 请求未映射到 API 请求

Posted

技术标签:

【中文标题】PHP PayPal 错误 - MALFORMED_REQUEST - 传入的 JSON 请求未映射到 API 请求【英文标题】:PHP PayPal Error - MALFORMED_REQUEST - Incoming JSON request does not map to API request 【发布时间】:2015-07-18 18:40:24 【问题描述】:

我在使用 PayPal REST API 时出错,但我无法在任何地方找到解决方案。我正在尝试将用户发送到 PayPal 以支付会话数组中的指定项目。我已尝试更改付款方式、意图和 URL。

    $this->setupCurl();
    $this->setupPayPal();

    $this->payer = new Payer();
    $this->details = array();
    $this->amount = array();
    $this->transaction = array();
    $this->redirectUrls = new RedirectUrls();
    $this->payment = new Payment();

    $this->payer->setPaymentMethod('paypal'); // TODO paypal . credit_card

    foreach ($cart as $id => $item) 
        $this->details[$id] = new Details();
        $this->amount[$id] = new Amount();
        $this->transaction[$id] = new Transaction();

        $this->details[$id]->setShipping('0.00')
                      ->setTax('0.00')
                      ->setSubtotal(formatMoney($this->products[$id]['price'] * $item['quantity'], '', 2)); // TODO

        $this->amount[$id]->setCurrency('AUD')
                     ->setTotal(formatMoney($this->products[$id]['price'] * $item['quantity'], '', 2))
                     ->setDetails($this->details[$id]);

        $this->transaction[$id]->setAmount($this->amount[$id])
                          ->setDescription($this->products[$id]['name']); // TODO
    

    $this->redirectUrls->setReturnUrl('https://warsentech.com/pay?approved=true')
                       ->setCancelUrl('https://warsentech.com/pay?approved=false');

    $this->payment->setIntent('authorize') // TODO sale . buy
                  ->setPayer($this->payer)
                  ->setRedirectUrls($this->redirectUrls)
                  ->setTransactions($this->transaction); // TODO - add transactions into array from the cart items

    try 
        $this->payment->create($this->paypal);

        $hash = md5($this->payment->getId());
        $_SESSION['paypal_hash'] = $hash;
        if ($this->databaseConnection()) 
            $result = $this->db_connection->prepare('INSERT INTO transactions_paypal (user_id, payment_id, hash, complete) VALUES (:user_id, :payment_id, :hash, 0)');
            $result->execute([
                'user_id' => $_SESSION['user_id'],
                'payment_id' => $this->payment->getId(),
                'hash' => $hash
            ]);
        
     catch (Exception $e) 
        die(paypalError($e));
    

    foreach ($this->payment->getLinks() as $link) 
        if ($link->getRel() == 'approval_url') 
            $redirectUrl = $link->getHref();
        
    

完整的错误信息:

异常 'PayPal\Exception\PayPalConnectionException' 并带有消息“访问 https://api.sandbox.paypal.com/v1/payments/payment 时获得 Http 响应代码 400。”在 /var/www/html/vendor/paypal/rest-api-sdk-php/lib/PayPal/Core/PayPalHttpConnection.php:177 堆栈跟踪:#0 /var/www/html/vendor/paypal/rest-api -sdk-php/lib/PayPal/Transport/PayPalRestCall.php(74): PayPal\Core\PayPalHttpConnection->execute('"intent":"auth...') #1 /var/www/html/vendor /paypal/rest-api-sdk-php/lib/PayPal/Common/PayPalResourceModel.php(103): PayPal\Transport\PayPalRestCall->execute(Array, '/v1/payments/pa...', 'POST' , '"intent":"auth...', NULL) #2 /var/www/html/vendor/paypal/rest-api-sdk-php/lib/PayPal/Api/Payment.php(424): PayPal\Common\PayPalResourceModel::executeCall('/v1/payments/pa...', 'POST', '"intent":"auth...', NULL, Object(PayPal\Rest\ApiContext), NULL ) #3 /var/www/html/classes/PayPal.php(168): PayPal\Api\Payment->create(Object(PayPal\Rest\ApiContext)) #4 /var/www/html/classes/PayPal. php(50): PayPal->pay(Array) #5 /var/www/html/checkout.php(39): PayPal->__construct() #6 main

【问题讨论】:

【参考方案1】:

我会推荐两件事来解决这个问题:

    从您看到的错误消息中,您看到的错误属于PayPal\Exception\PayPalConnectionException 类型。我们有一个特殊的方法getData() 来检索有关错误的确切详细信息。

你可以这样做:

try 
    $this->payment->create($this->paypal);

    $hash = md5($this->payment->getId());
    $_SESSION['paypal_hash'] = $hash;
    if ($this->databaseConnection()) 
        $result = $this->db_connection->prepare('INSERT INTO transactions_paypal (user_id, payment_id, hash, complete) VALUES (:user_id, :payment_id, :hash, 0)');
        $result->execute([
            'user_id' => $_SESSION['user_id'],
            'payment_id' => $this->payment->getId(),
            'hash' => $hash
        ]);
    
 catch (PayPal\Exception\PayPalConnectionException $e) 
    echo $e->getData(); // This will print a JSON which has specific details about the error.
    die(paypalError($e));

    如果您希望在集成我们的 SDK 时获得帮助,有很多文档可以帮助您。以下是您应该关注的最有趣的一个:

    演示每个 API 操作的示例:http://paypal.github.io/PayPal-PHP-SDK/sample/ 如何通过一个命令在本地机器上运行这些示例:https://github.com/paypal/PayPal-PHP-SDK/wiki/Samples 解释如何开始使用我们的 API 的 Wiki 页面:https://github.com/paypal/PayPal-PHP-SDK/wiki

具体来说,您应该查看这个示例,该示例准确地展示了您正在尝试做的事情:http://paypal.github.io/PayPal-PHP-SDK/sample/doc/payments/CreatePaymentUsingPayPal.html

如果这有助于找出您面临的问题,请告诉我。

【讨论】:

以上是关于PHP PayPal 错误 - MALFORMED_REQUEST - 传入的 JSON 请求未映射到 API 请求的主要内容,如果未能解决你的问题,请参考以下文章

PayPal:MALFORMED_REQUEST,没有其他活动

PayPal 请求不断收到 MALFORMED_REQUEST

支付宝错误 400

创建用户时 XMPP jid-malformed(400) 错误 Android

404 curl_error当我在CURLOPT_HTTPHEADER中添加PayPal-Mock-Response时

Elasticsearches 写入 忽略 错误字段 存储正确字段 ignore_malformed