-
composer.json
Open in GitHub{ // "require": { "php": "^7.2.5", // "srmklive/paypal": "~1.0" }, // }
-
routes/web.php
Open in GitHubuse Illuminate\Support\Facades\Route; // Route::get('paypal-checkout/{order}', 'PaypalController@paypalCheckout')->name('paypal.checkout'); Route::get('paypal-success', 'PaypalController@paypalSuccess')->name('paypal.success'); Route::get('paypal-cancel', 'PaypalController@paypalCancel')->name('paypal.cancel'); //
-
app/Http/Controllers/PaypalController.php
Open in GitHubuse Illuminate\Http\Request; use Gloudemans\Shoppingcart\Facades\Cart; use Srmklive\PayPal\Services\ExpressCheckout; class PaypalController extends Controller { private function checkoutData($orderId) { $discount = session()->get('coupon')['discount'] ?? 0; $newSubtotal = (Cart::subtotal() - $discount); $newTotal = $newSubtotal; $cartItems = Cart::Content()->map(function ($item) { return [ 'name' => $item->name, 'price' => $item->price, 'qty' => $item->qty ]; })->toArray(); $checkoutData = [ 'items' => $cartItems, 'return_url' => route('paypal.success', $orderId), 'cancel_url' => route('paypal.cancel'), 'invoice_id' => uniqid(), 'invoice_description' => "Order description", 'total' => $newTotal ]; return $checkoutData; } public function paypalCheckout($orderId) { $checkoutData = $this->checkoutData($orderId); $provider = new ExpressCheckout(); $response = $provider->setExpressCheckout($checkoutData); return redirect($response['paypal_link']); } public function paypalCancel() { dd('cancel page'); } public function paypalSuccess(Request $request, $orderId) { $checkoutData = $this->checkoutData(); $token = $request->get('token'); $payerId = $request->get('PayerID'); $provider = new ExpressCheckout(); $response = $provider->getExpressCheckoutDetails($token); if (in_array(strtoupper($response['ACK']), ['SUCCESS', 'SUCCESSWITHWARNING'])) { // Perform transaction on paypal $payment_status = $provider->doExpressCheckoutPayment($checkoutData, $token, $payerId); $status = $payment_status['PAYMENTINFO_0_PAYMENTSTATUS']; } if (in_array($status, ['completed', 'Processed'])) { $order = Order::find($orderId); $order->is_paid = 1; $order->payment_method = 'paypal'; $order->save(); //clear cart contents Cart::destroy(); session()->flash('success', 'Payed & proccessed successfully'); return redirect(route('my-orders.index')); } session()->flash('success', 'Payed & proccessed successfully'); return redirect()->back(); } }