0
我正在使用PayPal API的電子商務網站上工作。我目前收到以下錯誤:PayPal API - PHP錯誤發送付款
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。
非常感謝。事實證明,問題在於總數。我爲多件商品購買添加了折扣,但將其作爲一個整體應用於購買,而不是每件商品。這導致了itemList和細節小計的價格之間的一些不匹配。 – KellyMarchewa