Server : nginx/1.18.0 System : Linux localhost 6.14.3-x86_64-linode168 #1 SMP PREEMPT_DYNAMIC Mon Apr 21 19:47:55 EDT 2025 x86_64 User : www-data ( 33) PHP Version : 8.0.16 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, Directory : /var/www/ecommerce/app/Http/Controllers/Shop/ |
<?php
namespace App\Http\Controllers\Shop;
use App\Models\Cart;
use App\Models\Product;
use Illuminate\Http\Request;
use Srmklive\PayPal\Services\ExpressCheckout;
class PaypalController extends Controller
{
public function payment()
{
$cart = Cart::where('user_id', auth()->id())->where('order_id', null)->get()->toArray();
$data = [];
// return $cart;
$data['items'] = array_map(function ($item) use ($cart) {
$name = Product::where('id', $item['product_id'])->pluck('title');
return [
'name' => $name,
'price' => $item['price'],
'desc' => 'Thank you for using paypal',
'qty' => $item['quantity'],
];
}, $cart);
$data['invoice_id'] = 'ORD-' . strtoupper(uniqid());
$data['invoice_description'] = "Order #{$data['invoice_id']} Invoice";
$data['return_url'] = route('payment.success');
$data['cancel_url'] = route('payment.cancel');
$total = 0;
foreach ($data['items'] as $item) {
$total += $item['price'] * $item['qty'];
}
$data['total'] = $total;
if (session('coupon')) {
$data['shipping_discount'] = session('coupon')['value'];
}
Cart::where('user_id', auth()->id())->where('order_id', null)->update(['order_id' => session()->get('id')]);
// return session()->get('id');
$provider = new ExpressCheckout;
$response = $provider->setExpressCheckout($data);
return redirect($response['paypal_link']);
}
/**
* Responds with a welcome message with instructions
*
* @return \Illuminate\Http\Response
*/
public function cancel()
{
dd('Your payment is canceled. You can create cancel page here.');
}
/**
* Responds with a welcome message with instructions
*
* @param Request $request
*
* @return \Illuminate\Http\Response
*/
public function success(Request $request)
{
$provider = new ExpressCheckout;
$response = $provider->getExpressCheckoutDetails($request->token);
// return $response;
if (in_array(strtoupper($response['ACK']), ['SUCCESS', 'SUCCESSWITHWARNING'])) {
request()->session()->flash('success', 'You successfully pay from Paypal! Thank You');
session()->forget('cart');
session()->forget('coupon');
return redirect()->route('home');
}
request()->session()->flash('error', 'Something went wrong please try again!!!');
return redirect()->back();
}
}