Не получается сделать оплату нескольких товаров, не могу найти ошибку.
public function payWithpaypal(Request $request)
{
$payer = new Payer();
$payer->setPaymentMethod('paypal');
$cart = Cart::where('status', 1)->where('token', session()->getId())->get();
$full_price = 0;
$items = [];
for ($i = 0; $i < count($cart); $i++){
$items[$i] = new Item();
$items[$i]->setName(($cart[$i]->product->title)) /** item name **/
->setCurrency('USD')
->setQuantity($cart[$i]->quantity)
->setPrice($cart[$i]->product->price); /** unit price **/
$full_price += $cart[$i]->quantity * $cart[$i]->product->price;
}
$itemList = new ItemList();
$itemList->setItems(array($items));
$amount = new Amount();
$amount->setCurrency('USD')->setTotal($full_price);
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($itemList)
->setDescription('Your transaction description')
->setInvoiceNumber(uniqid());
$redirect_urls = new RedirectUrls();
$redirect_urls->setReturnUrl(URL::to('status')) /** Specify return URL **/
->setCancelUrl(URL::to('status'));
$payment = new Payment();
$payment->setIntent('Sale')
->setPayer($payer)
->setRedirectUrls($redirect_urls)
->setTransactions(array($transaction));
/** dd($payment->create($this->_api_context));exit; **/
try {
$payment->create($this->_api_context);
} catch (PayPalConnectionException $exception) {
dd($exception);
if (\Config::get('app.debug')) {
\session()->put('error', 'Connection timeout');
return Redirect::to('/shop-cart');
} else {
\session()->put('error', 'Some error occur, sorry for inconvenient');
return Redirect::to('/');
}
}
foreach ($payment->getLinks() as $link) {
if ($link->getRel() == 'approval_url') {
$redirect_url = $link->getHref();
break;
}
}
/** add payment ID to session **/
session()->put('paypal_payment_id', $payment->getId());
if (isset($redirect_url)) {
/** redirect to paypal **/
return Redirect::away($redirect_url);
}
session()->put('error', 'Unknown error occurred');
return Redirect::to('/');
}
public function getPaymentStatus()
{
/** Get the payment ID before session clear **/
$payment_id = session()->get('paypal_payment_id');
/** clear the session payment ID **/
session()->forget('paypal_payment_id');
if (empty(\request('PayerID')) || empty(\request('token'))) {
\session()->put('error', 'Payment failed');
return Redirect::to('/');
}
$payment = Payment::get($payment_id, $this->_api_context);
$execution = new PaymentExecution();
$execution->setPayerId(\request('PayerID'));
/**Execute the payment **/
$result = $payment->execute($execution, $this->_api_context);
if ($result->getState() == 'approved') {
\session()->put('success', 'Payment success');
return Redirect::to('/');
}
\session()->put('error', 'Payment failed');
return Redirect::to('/');
}
dd $exception выдаёт такую ошибку: Incoming JSON request does not map to API request.