2016-06-25 96 views
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。

回答

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方法内移动批准链接检查。

+0

非常感谢。事实证明,问题在于总数。我为多件商品购买添加了折扣,但将其作为一个整体应用于购买,而不是每件商品。这导致了itemList和细节小计的价格之间的一些不匹配。 – KellyMarchewa