php - 未捕获的 PPConnectionException Http 响应代码 400

标签 php paypal

基本上我可以看到的是我无法创建 apiContext 对象。谁能看到错误在哪里吗?请告诉我如何改进这个问题。

错误如下:

Fatal error: Uncaught exception 'PayPal\Exception\PPConnectionException'
 with message 'Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment.' in /vendor/paypal
/sdk-core-php/lib/PayPal/Core/PPHttpConnection.php:104 Stack trace: #0 
/vendor/paypal/sdk-core-php/lib/PayPal/Transport/PPRestCall.php(44): 
PayPal\Core\PPHttpConnection->execute('{"intent":"sale...') #1 /vendor/paypal
/rest-api-sdk-php/lib/PayPal/Api/Payment.php(246): PayPal\Transport
\PPRestCall->execute(Array, '/v1/payments/pa...', 'POST', 
'{"intent":"sale...') #2 /pymt.php(38): PayPal\Api
\Payment->create(Object(PayPal\Rest\ApiContext)) #3 {main} thrown in /vendor
/paypal/sdk-core-php/lib/PayPal/Core/PPHttpConnection.php on line 104 

这是我正在使用的 PHP 示例。我正在尝试从我的服务器上运行它。如果客户点击 pay with paypal 按钮,它将运行以下示例脚本:

<?php

require __DIR__ . '/../bootstrap.php';
use PayPal\Api\Address;
use PayPal\Api\Amount;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\FundingInstrument;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Transaction;
session_start();



$payer = new Payer();
$payer->setPayment_method('paypal');

$amount = new Amount();
$amount->setCurrency('USD');
$amount->setTotal('1.00');

$transaction = new Transaction();
$transaction->setAmount($amount);
$transaction->setDescription('This is the payment description.');

$baseUrl = getBaseUrl();
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturn_url('baseUrl/sale.php');
$redirectUrls->setCancel_url('baseUrl/saleFail.php');

$payment = new Payment();
$payment->setIntent('sale');
$payment->setPayer($payer);
$payment->setRedirect_urls($redirectUrls);
$payment->setTransactions(array($transaction));

try {
  $payment->create($apiContext);
} catch (\PPConnectionException $ex) {
  echo 'Exception: ' . $ex->getMessage() . PHP_EOL;
  var_dump($ex->getData());
  exit(1);
}

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

$_SESSION['paymentId'] = $payment->getId();
if(isset($redirectUrl)) {
  header('Location: $redirectUrl');
  exit;
}

这是经过编辑的 bootstrap.php:

<?php

/*
 * Sample bootstrap file.
 */

// Include the composer autoloader
if(!file_exists(__DIR__ .'/vendor/autoload.php')) {
    echo "The 'vendor' folder is missing. You must run 'composer update --no-dev' to     resolve application dependencies.\nPlease see the README for more information.\n";
    exit(1);
}


require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/common.php';

use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;

$apiContext = getApiContext();

/**
 Helper method for getting an APIContext for all calls
 *
 * @return PayPal\Rest\ApiContext
 */
function getApiContext() {

    // ### Api context
// Use an ApiContext object to authenticate 
// API calls. The clientId and clientSecret for the 
// OAuthTokenCredential class can be retrieved from 
// developer.paypal.com

$apiContext = new ApiContext(
    new OAuthTokenCredential(
        'MY CLIENT ID',
        'MY SECRET'
    )
);


// Register the sdk_config.ini file in current directory
// as the configuration source.
if(!defined("PP_CONFIG_PATH")) {
    define("PP_CONFIG_PATH", __DIR__);
}

return $apiContext;
}

有人可以帮忙吗?如果您需要更多信息,请告诉我。先谢谢了。

最佳答案

我也遇到了同样的问题,我发现如果任何参数错误(例如:无效的卡号或无效的货币格式),php SDK会抛出连接异常,而不是返回某些Json消息(当我组织POST时)自己数据并用curl发送,就可以了)!我想知道这样的设计是否合理,或者我们只是错过了一些东西!...

关于php - 未捕获的 PPConnectionException Http 响应代码 400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20038211/

相关文章:

php - Javascript:点击更改文本框 'Type'

grails - 使用 Paypal 汇款

php - PHP 和 MySQL 中的实时搜索(不适用于数组?)

php - 加密 - 解密 AES PHP

php - 如何在 Symfony 4 中使用简单的函数?

php - Paypal 自适应支付 - 描述字段

PHP 无法加载供应商文件夹 : How to install composer on my server?

php - 我想使用 dompdf 在循环中创建多个 pdf?

Django、PayPal IPN 和用户名——概念问题

php - 我这样做对吗? - Paypal IPN 工作