gpt4 book ai didi

php - PayPal API - PHP 发送付款时出错

转载 作者:太空宇宙 更新时间:2023-11-03 16:24:35 24 4
gpt4 key购买 nike

我正在开发一个使用 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() 负责返回用于完成付款的链接。

最佳答案

请 try catch 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 方法中。

关于php - PayPal API - PHP 发送付款时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38033393/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com