PHP PayPal Error - MALFORMED_REQUEST - Incoming JSON request does not map to API request
我使用PayPal REST API时出错,但在任何地方都找不到解决方案。我正在尝试将用户发送到PayPal,以为会话数组中的指定项目付款。我尝试更改付款方式,意图和URL。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | $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(); } } |
完整错误消息:
exception 'PayPal\\Exception\\PayPalConnectionException' with message 'Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment.' in /var/www/html/vendor/paypal/rest-api-sdk-php/lib/PayPal/Core/PayPalHttpConnection.php:177 Stack trace: #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\
est\\ApiContext), NULL) #3 /var/www/html/classes/PayPal.php(168): PayPal\\Api\\Payment->create(Object(PayPal\
est\\ApiContext)) #4 /var/www/html/classes/PayPal.php(50): PayPal->pay(Array) #5 /var/www/html/checkout.php(39): PayPal->__construct() #6 {main}
我建议通过两件事来解决此问题:
您可以通过执行以下操作来做到这一点:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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
- Wiki页面介绍了如何开始使用我们的API:https://github.com/paypal/PayPal-PHP-SDK/wiki
尤其是,您应该查看该示例,该示例演示了您正在尝试做的事情:http://paypal.github.io/PayPal-PHP-SDK/sample/doc/payments/CreatePaymentUsingPayPal.html 让我知道这是否有助于您解决所面临的问题。