使用 PayPal-PHP-SDK 的异常 [Http 响应代码 400]



我正在遵循有关如何使用PayPal-PHP-SDK的教程,我在控制台中遇到了此错误:

[Sun Mar 08 16:11:21.729977 2015] [:error] [pid 4484:tid 1760] [client ::1:4308] PHP Fatal error:  
Uncaught exception 'PayPal\Exception\PayPalConnectionException' with message 
'Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment.' 
in C:\UniServerZ\www\PayPal\vendor\paypal\rest-api-sdk-php\lib\PayPal\Core\PayPalHttpConnection.php:176nStack trace:n#0 
C:\UniServerZ\www\PayPal\vendor\paypal\rest-api-sdk-php\lib\PayPal\Transport\PayPalRestCall.php(74): 
PayPal\Core\PayPalHttpConnection->execute('{"intent":"sale...')n#1 
C:\UniServerZ\www\PayPal\vendor\paypal\rest-api-sdk-php\lib\PayPal\Common\PayPalResourceModel.php(103): 
PayPal\Transport\PayPalRestCall->execute(Array, '/v1/payments/pa...', 'POST', '{"intent":"sale...', NULL)n#2 
C:\UniServerZ\www\PayPal\vendor\paypal\rest-api-sdk-php\lib\PayPal\Api\Payment.php(424): 
PayPal\Common\PayPalResourceModel::executeCall('/v1/payments/pa...', 'POST', '{"intent":"sale...', NULL, Object(PayPal\Rest\ApiContext), NULL)n#3 
C:\UniServerZ\www\PayPal\member\payment.php(48): PayPal\Api\Payment->create(Object(PayPal\Rest\ApiContext))n#4 {mai in 
C:\UniServerZ\www\PayPal\vendor\paypal\rest-api-sdk-php\lib\PayPal\Core\PayPalHttpConnection.php on line 176, referer: http://localhost:1313/PayPal/

更新

尝试更新函数名称后,日志中的新消息是:

[Mon Mar 09 23:28:26.612147 2015] [:error] [pid 4344:tid 1772] [client ::1:2189] PHP Fatal error:  Uncaught exception 'PayPal\Exception\PayPalConnectionException' with message 'Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment.' in C:\UniServerZ\www\PayPal\vendor\paypal\rest-api-sdk-php\lib\PayPal\Core\PayPalHttpConnection.php:176nStack trace:n#0 C:\UniServerZ\www\PayPal\vendor\paypal\rest-api-sdk-php\lib\PayPal\Transport\PayPalRestCall.php(74): PayPal\Core\PayPalHttpConnection->execute('{"intent":"sale...')n#1 C:\UniServerZ\www\PayPal\vendor\paypal\rest-api-sdk-php\lib\PayPal\Common\PayPalResourceModel.php(103): PayPal\Transport\PayPalRestCall->execute(Array, '/v1/payments/pa...', 'POST', '{"intent":"sale...', NULL)n#2 C:\UniServerZ\www\PayPal\vendor\paypal\rest-api-sdk-php\lib\PayPal\Api\Payment.php(424): PayPal\Common\PayPalResourceModel::executeCall('/v1/payments/pa...', 'POST', '{"intent":"sale...', NULL, Object(PayPal\Rest\ApiContext), NULL)n#3 C:\UniServerZ\www\PayPal\member\payment.php(48): PayPal\Api\Payment->create(Object(PayPal\Rest\ApiContext))n#4 {mai in C:\UniServerZ\www\PayPal\vendor\paypal\rest-api-sdk-php\lib\PayPal\Core\PayPalHttpConnection.php on line 176, referer: http://localhost:1313/PayPal/

这是代码:

<?php
use PayPalApiPayer;
use PayPalApiDetails;
use PayPalApiAmount;
use PayPalApiTransaction;
use PayPalApiPayment;
use PayPalApiRedirectUrls;
require '../scr/start.php';
$payer = new Payer();
$details = new Details();
$amount = new Amount();
$transaction = new Transaction();
$payment = new Payment();
$redirectUrls = new RedirectUrls();
// Payer
$payer->setPaymentMethod('paypal');
// Details
$details->setShipping('2.00')
    ->setTax('0.00')
    ->setSubtotal('20.00'); //feed for membership
// Amount
$amount->setCurrency('CLP')
    ->setTotal('22.00') // Shipping + Tax + Subtotal + Everything else you need to charge
    ->setDetails($details);
// Transaction
$transaction->setAmount($amount)
    ->setDescription('Membership');
$payment->setIntent('sale')
    ->setPayer($payer)
    ->setTransactions([$transaction]);
// Redirect URLs
$redirectUrls->setReturnUrl('http://localhost:1313/PayPal/PayPall/Pay.php?approved=true')
    ->setCancelUrl('http://localhost:1313/PayPal/PayPall/Pay.php?approved=false');
$payment->setRedirectUrls($redirectUrls);
try {
    $payment->create($api);
    // Generate and store hash
    // Prepare and execute transaction storage

} catch (PPConnectionException $e) {
    // Perhaps log an error
    header('Location: ../PayPall/error.php');
}
var_dump($payment->getLinks());
?>

任何方向,改进同一问题的问题,评论,建议或澄清请求/更多信息等,而不是帮助解决问题将不胜感激。

更改此代码。 PPConnectionException更名为PayPalConnectionException。您还可以执行$e->getData()来检索详细的异常消息。

} catch (PayPalConnectionException $e) {
    echo $e->getData();
    // Perhaps log an error
    header('Location: ../PayPall/error.php');
}

最新更新