我正在开发一个使用PayPal API的电子商务网站。我目前收到以下错误:
Error Sending Payment! exception 'PayPal\Exception\PayPalConnectionException' with message 'Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment.' in C:\xampp\htdocs\leisurelydiversion\vendor\paypal\rest-api-sdk-php\lib\PayPal\Core\PayPalHttpConnection.php:178 Stack trace: #0 C:\xampp\htdocs\leisurelydiversion\vendor\paypal\rest-api-sdk-php\lib\PayPal\Transport\PayPalRestCall.php(74): PayPal\Core\PayPalHttpConnection->execute('{"intent":"sale...') #1 C:\xampp\htdocs\leisurelydiversion\vendor\paypal\rest-api-sdk-php\lib\PayPal\Common\PayPalResourceModel.php(102): PayPal\Transport\PayPalRestCall->execute(Array, '/v1/payments/pa...', 'POST', '{"intent":"sale...', NULL) #2 C:\xampp\htdocs\leisurelydiversion\vendor\paypal\rest-api-sdk-php\lib\PayPal\Api\Payment.php(579): PayPal\Common\PayPalResourceModel::executeCall('/v1/payments/pa...', 'POST', '{"intent":"sale...', NULL, Object(PayPal\Rest\ApiContext), NULL) #3 C:\xampp\htdocs\leisurelydiversion\cart.php(65): PayPal\Api\Payment->create(Object(PayPal\Rest\ApiContext)) #4 {main}
乍一看(无论如何),似乎连接到PayPal网站时出现问题。但是,有两个理由认为我的代码中存在错误。首先,只有当我的“购物车”中有多个商品时才会出现此问题。其次,如果我不在事务对象上调用setItemList()方法,它就可以正常工作。这意味着用于填充$ itemsArray的foreach语句中存在问题;但是,我在数组上调用了var_dump,一切似乎都很好。
array(2) { [0]=> object(PayPal\Api\Item)#6 (1) { ["_propMap":"PayPal\Common\PayPalModel":private]=> array(4) { ["name"]=> string(2) "22" ["currency"]=> string(3) "USD" ["quantity"]=> int(1) ["price"]=> string(5) "19.99" } } [1]=> object(PayPal\Api\Item)#5 (1) { ["_propMap":"PayPal\Common\PayPalModel":private]=> array(4) { ["name"]=> string(2) "21" ["currency"]=> string(3) "USD" ["quantity"]=> int(1) ["price"]=> string(5) "13.99" } } }
问题可能是什么?非常感谢您的帮助。
主页:
`<?php
include("header.php");
require __DIR__ . '/bootstrap.php';
use PayPal\Api\Amount;
use PayPal\Api\Details;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Transaction;
$cart = new cart();
if(isset($_GET['remove'])) {
$id = $_GET['remove'];
$cart->removeItem($id);
}
$subtotal = $cart->getSubtotal();
$total = $cart->getTotal();
$discount = $cart->getDiscount();
$count = $cart->getItemCount();
$merchandiseTotal = $subtotal - $discount;
$total = $merchandiseTotal + SHIPPING;
$payer = new Payer();
$payer->setPaymentMethod("paypal");
$itemsArray = array();
foreach($cart->items as $cartItem) {
$item = new Item();
$item->setName($cartItem->id)->setCurrency("USD")->setQuantity(1)->setPrice($cartItem->price);
$itemsArray[count($itemsArray)] = $item;
}
var_dump($itemsArray);
$itemList = new itemList();
$itemList->setItems($itemsArray);
$details = new Details();
$details->setShipping(SHIPPING)->setSubtotal($merchandiseTotal);
$amount = new Amount();
$amount->setCurrency("USD")->setDetails($details);
$amount->setTotal($total);
$transaction = new Transaction();
$transaction->setAmount($amount)->setItemList($itemList)->setDescription("Purchase from Leisurely Diversion")->setInvoiceNumber(uniqid());
$redirectURLs = new RedirectUrls();
$redirectURLs->setReturnUrl("http://localhost/leisurelydiversion/confirmation.php?success=true")->setCancelUrl("http://localhost/leisurelydiversion/confirmation.php");
$payment = new Payment();
$payment->setIntent("sale")->setPayer($payer)->setTransactions(array($transaction))->setRedirectUrls($redirectURLs);
try {
$payment->create($apiContext);
} catch(Exception $e) {
echo "<h2> Error Sending Payment! $e</h2>";
}
$request = clone $payment;
$url = $payment->getApprovalLink() . "&useraction=commit";
?>
<table class='table table-striped table-responsive'>
<thead>
<tr>
<td>Item</td>
<td>Price</td>
<td></td>
</tr>
<tbody>
<?php if($cart->getItemCount() <= 0) {
echo " <tr>Your cart is empty!</tr>"; } else { foreach($cart->items as $item) {
echo "<tr><td>$item->name <a href='cart.php?remove=$item->id'>Remove from cart</a></td><td>$item->price</td></td>";
}
echo "<tr></tr><tr></tr><tr><td>Subtotal: </td><td>" . $subtotal . "</td> </tr>
<tr> <td>Discount: </td> <td>" . $discount . "</td> </tr>
<tr> <td>Shipping: </td> <td>" . SHIPPING . " </td></tr>
<tr> <td>Total: </td> <td>" . $total . "</td>";
}
?>
</tbody>
</thead>
</table>
<a href="<?php echo $url; ?>"><img src="https://www.paypalobjects.com/webstatic/en_US/btn/btn_checkout_pp_142x27.png"/></a>
<?php
include("footer.php");
?>
购物车类
class cart {
public $items = array();
function __construct() {
$pdo = new pdoAccess("localhost","root","");
$counter = 0;
if (isset($_SESSION['cart'])) { foreach($_SESSION['cart'] as $item) {
$this->items[$counter] = $pdo->getItemDetails($item, "stock");
$counter++;
}
}
}
public function emptyCart() {
session_unset();
session_destroy();
}
public function removeItem($itemID) {
$index = array_search($itemID, $_SESSION['cart']);
unset($_SESSION['cart'][$index]);
$this->__construct();
}
public function getItemCount() {
if(isset($_SESSION['cart'])) {
return count($_SESSION['cart']);
} else {
return 0;
}
}
public function getSubtotal() {
$total = 0;
foreach($this->items as $item) {
$total += $item->price;
}
return $total;
}
public function getDiscount() {
$sub = $this->getSubtotal();
if($this->getItemCount() >= 2) {
$discount = $sub * .1;
} else {
$discount = 0;
}
return round($discount, 2);
}
public function getTotal() {
$sub = $this->getSubtotal();
if($this->getItemCount() >= 2) {
$sub +- $this->getDiscount();
}
$total = $sub + SHIPPING;
return $total;
}
}
?>
是的,我确实有错误报告。我收到的唯一警告如下:
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\leisurelydiversion\vendor\paypal\rest-api-sdk-php\lib\PayPal\Common\PayPalResourceModel.php on line 44
我认为这仅仅是因为付款未成功发送,因为我认为它所指的foreach()负责返回用于完成付款的链接。
答案 0 :(得分:1)
请尝试捕获PayPalConnectionException,如in our wiki所示:
try {
$payment->create($apiContext);
} catch (PayPal\Exception\PayPalConnectionException $ex) {
echo $ex->getCode(); // Prints the Error Code
echo $ex->getData(); // Prints the detailed error message
die($ex);
} catch (Exception $ex) {
die($ex);
}
此外,您正试图获得批准链接,即使它失败了。您可能需要在此之后退出代码,或者在try catch方法中移动批准链接检查。