PayPal \ Exception \ PayPalConnectionException:访问https://api.sandbox.paypal.com/v1/payments/payment时获得Http响应代码400
我的<?php
$base = realpath($_SERVER["DOCUMENT_ROOT"]);
require_once $base.'/gsys3/assets/php/paypal.api.php';
use PayPal\Api\Payer;
use PayPal\Api\Details;
use PayPal\Api\Amount;
use PayPal\Api\Transaction;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
if(!isset($_SESSION['account'])){
header('Location: ../../main.php');
} elseif($_SESSION['account']['perms'] != 3){
header('Location: ../../main.php');
}
$payer = new Payer();
$details = new Details();
$amount = new Amount();
$transaction = new Transaction();
$payment = new Payment();
$redirectUrls = new RedirectUrls();
$payer->setPaymentMethod('paypal');
$shippingcost = '1';
$taxcost = '7';
$subtotalcost = '100';
$totalcost = $shippingcost + $taxcost + $subtotalcost;
$details->setShipping($shippingcost)
->setTax($taxcost)
->setSubtotal($subtotalcost);
$amount->setCurrency('USD')
->setTotal($totalcost)
->setDetails($details);
$transaction->setAmount($amount)
->setDescription('Enrollment');
$payment->setIntent('sale')
->setTransactions([$transaction]);
$redirectUrls->setReturnUrl('http://localhost:8080/gsys3/public/dashboard/parent/pay.php?approved=true')
->setCancelUrl('http://localhost:8080/gsys3/public/dashboard/parent/pay.php?approved=false');
$payment->setRedirectUrls($redirectUrls);
try {
$payment->create($paypalapi);
} catch (PayPal\Exception\PPConnectionException $pce){
echo '<pre>';print_r(json_decode($pce->getData()));exit;
}
代码:
paypal.api.php
我的<?php
# Start the session
session_name('_session');
session_start();
# This is the connection credentials to the database! Please edit with caution
$db_host = '127.0.0.1';
$db_database = 'thegrades';
$db_username = 'root';
$db_password = '';
# Initialize the connection to the database
$conn = new PDO("mysql:host={$db_host};dbname={$db_database}", $db_username, $db_password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$base = realpath($_SERVER["DOCUMENT_ROOT"]);
require_once $base.'/gsys3/vendor/autoload.php';
use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;
$paypalapi = new ApiContext(
new OAuthTokenCredential(
'{confidential}',
'{confidential}'
)
);
$paypalapi->setConfig([
'mode' => 'sandbox',
'http.ConnectionTimeOut' => 30,
'log.LogEnabled' => false,
'log.FileName' => '',
'log.LogLevel' => 'FINE',
'validation.level' => 'log'
]);
if(!isset($_SESSION['account'])){
header('Location: ../../main.php');
} elseif($_SESSION['account']['perms'] != 3){
header('Location: ../../main.php');
}
:
return_url
我已经检查过准确的客户端和密钥
答案 0 :(得分:0)
我建议您激活日志以获取有关异常的更多信息。这些是我的开发设置。
$apiContext->setConfig(
array(
'mode' => 'sandbox',
'log.LogEnabled' => true,
'log.FileName' => 'PayPal.log',
'log.LogLevel' => 'DEBUG',
)
);