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

时间:2015-05-08 00:22:30

标签: php json api rest paypal

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

    $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->执行(' {"意图":" 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 ...& #39;,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(数组)#5 /var/www/html/checkout.php(39):PayPal-> __construct()#6 {main}

1 个答案:

答案 0 :(得分:0)

我会建议两件事来解决这个问题:

  1. 从错误消息中您看到您看到的错误属于PayPal\Exception\PayPalConnectionException类型。我们有一个特殊的方法getData()来检索有关错误的确切详细信息。
  2. 你可以通过这样做来做到这一点:

    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));
    }
    
    1. 如果您希望获得一些有关我们SDK的帮助,那么有很多文档可以帮助您。以下是您应该关注的最有趣的内容:

    2. 具体而言,您应该查看此示例,准确演示您要执行的操作:http://paypal.github.io/PayPal-PHP-SDK/sample/doc/payments/CreatePaymentUsingPayPal.html

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