So, the response I'm getting back from PayPal is that my totals don't match. But if you look at the json response, they do...
{
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"redirect_urls": {
"return_url": "edited_out",
"cancel_url": "edited_out"
},
"transactions": [
{
"amount": {
"currency": "USD",
"total": "18.28"
},
"item_list": {
"items": [
{
"quantity": 2,
"name": "This item is a test for shipping calculation",
"price": "18.28",
"currency": "USD"
}
]
},
"description": "Payment description",
"invoice_number": "554261363c260"
}
]}{"name":"VALIDATION_ERROR","details":[{"field":"transactions","issue":"Item amount must add up to specified amount subtotal (or total if amount details not specified)"}]
My Code:
$payer = new Payer();
$payer->setPaymentMethod("paypal");
// ### Itemized information
// (Optional) Lets you specify item wise
// information
$total_shipping=0;
foreach(Cart::content() as $row)
{
foreach(Inventory::where('id', '=', $row->id)->get() as $product)
{
foreach(Inventory::where( 'id', '=', $row->id)->get() as $product)
{
$total_shipping += $product->shipping;
//echo $total_shipping; } echo $total_shipping;
}
}
}
$i = 0;
$myshipping = 0;
foreach(Cart::content() as $row)
{
foreach(Inventory::where('id', '=', $row->id)->get() as $product)
{
$myshipping += $product->shipping;
$items[$i] = new Item();
$items[$i]->setQuantity($row->qty)
->setName($row->name)
->setPrice(($row->price * $row->qty) + (($row->price * $row->qty) * 0.07) + $product->shipping)
->setCurrency('USD');
$i++;
}
}
$itemList = new ItemList();
$itemList->setItems($items);
$amount = new Amount();
$amount->setCurrency("USD")
->setTotal(Cart::total() + (Cart::total() * 0.07) + $total_shipping);
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($itemList)
->setDescription("Payment description")
->setInvoiceNumber(uniqid());
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl("edited_out")
->setCancelUrl("edited_out");
$payment = new Payment();
$payment->setIntent("sale")
->setPayer($payer)
->setRedirectUrls($redirectUrls)
->setTransactions(array($transaction));
$request = clone $payment;
try {
$payment->create($apiContext);
} catch (Exception $ex) {
//ResultPrinter::printError("Created Payment Using PayPal. Please visit the URL to Approve.", "Payment", null, $request, $ex);
//var_dump($items);
echo "<pre>" . $payment;
echo $ex->getData();
exit(1);
}
$approvalUrl = $payment->getApprovalLink();
return Redirect::to($approvalUrl);
Any ideas on how I can fix this so my payment is created? If it helps, the miscalculation doesn't happen when the quantity of an item is 1. Or when their are multiple DIFFERENT items. The error only occurs when the quantity is greater than 1.