当前位置: 首页 > 知识库问答 >
问题:

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

朱承载
2023-03-14
    $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();
        }
    }

带有消息“访问https://api.sandbox.paypal.com/v1/payments/payments/payment时获得Http响应代码400”的异常“PayPal\exception\PaypalConnectionException”。“在/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/ww/html/vendor/paypal/rest-api-sdk-php/lib/paypal/common/paypalresourceModel.php(103):paypal\transport\paypalrestcall->execute(数组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))www.html/classes/paypal.php(50):paypal->pay(数组)#5/var/www.html/checkout.php(39):paypal->__construct()#6{main}

共有1个答案

慎俊雄
2023-03-14

我建议使用两种方法来解决此问题:

  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));
}
    null
 类似资料:
  • 我用的是PHP Paypal SDK:https://github.com/paypal/PayPal-PHP-SDK 通过PHP SDK引用PayPal REST API返回“传入JSON请求未映射到API请求” 继续https://stackoverflow.com/questions/39827032/paypal-how-to-get-payment-id-on-basis-of-tran

  • 我试图通过PHP SDK(沙箱环境)创建并执行一个与PayPal REST API的支付,如下所示。支付创建()工作良好,但支付执行()返回。JSON请求是由SDK创建的,那么会有什么问题呢?提前道谢。

  • 我正在使用Spring实现一个RESTful web服务。其中一个endpoint接收JSON字符串作为请求体,我希望将其映射到POJO。然而,现在看来传入的JSON字符串并没有映射到POJO的属性。 这里是@RestController界面 数据模型 最后是我的发帖请求: 与内容类型指定为应用程序/json 但是,当我打印出对象值时,打印出的是默认值(“first”和“last”),而不是我传入

  • 这是我的控制器,它将请求映射到以下url

  • Web 容器需要本章描述的映射技术去映射客户端请求到 Servlet(该规范2.5以前的版本,使用这些映射技术是作为一个建议而不是要求,允许servlet 容器各有其不同的策略用于映射客户端请求到 servlet)。

  • Postman成功调用Firebase API,但Apache Camel-Spring Boot应用程序返回错误。 API:https://fcm.googleapis.com/fcm/notification?notification_key_name=expectedvalue Http方法:GET 标题: 授权:预期值 Content-Type:应用程序/json project_id:预