site ecommerce ب laravel 7 الجزء السادس
فهاد الجزء السادس من site ecommerce ب laravel 7 غادي نزيدو les routes ديالنا باش داكشي لي زدنا فالجزء السابق يخدم ومنبعد غادي نزيدو la page لي غادي نعرض فيها les produits لي عندي فل panier وفالأخير غادي ندوزو ل paiement ب paypal.
نظرة سريعة بالفيديو
1- إضافة les routes
غادي تمشي ل dossier routes ف web.php غادي تزيد les routes ديال ل produits ول panier.
الكود لي غادي تزيد هو :
//home route
Route::get('/', 'HomeController@index')->name('home');
ute::resource('products', 'ProductController');
Route::get('products/category/{category}', 'HomeController@getProductByCategory')->name("category.products");
//cart routes
Route::get('/cart', 'CartController@index')->name('cart.index');
Route::post('/add/cart/{product}', 'CartController@addProductToCart')->name('add.cart');
Route::delete('/remove/{product}/cart', 'CartController@removeProductFromCart')->name('remove.cart');
Route::put('/update/{product}/cart', 'CartController@updateProductOnCart')->name('update.cart');
2- عرض les produits لي فل panier
ف dossier views زيد dossier cart فيه زيد fichier cart.blade.php لي فيه كنعرض les produits لي عندي فل panier.
الكود ديال الملف هو :
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-12 card p-3">
<h4 class="text-dark">Your cart</h4>
<table class="table table-hover">
<thead>
<tr>
<th>Image</th>
<th>Title</th>
<th>Qty</th>
<th>Price</th>
<th>Total</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach ($items as $item)
<tr>
<td>
<img src="{{ asset($item->associatedModel->image) }}"
alt="{{ $item->title }}"
width="50"
height="50"
class="img-fluid rounded"
>
</td>
<td>
{{ $item->name }}
</td>
<td>
<form class="d-flex flex-row justify-content-center align-items-center" action="{{ route("update.cart",$item->associatedModel->slug) }}" method="post">
@csrf
@method("PUT")
<div class="form-group">
<input type="number" name="qty" id="qty"
value="{{ $item->quantity }}"
placeholder="Quantité"
max="{{ $item->associatedModel->inStock }}"
min="1"
class="form-control"
>
</div>
<div class="form-group">
<button type="submit" class="btn btn-sm btn-warning">
<i class="fa fa-edit"></i>
</button>
</div>
</form>
</td>
<td>
{{ $item->price }} $
</td>
<td>
{{ $item->price * $item->quantity}} $
</td>
<td>
<form class="d-flex flex-row justify-content-center align-items-center" action="{{ route("remove.cart",$item->associatedModel->slug) }}" method="post">
@csrf
@method("DELETE")
<div class="form-group">
<button type="submit" class="btn btn-sm btn-danger">
<i class="fa fa-trash"></i>
</button>
</div>
</form>
</td>
</tr>
@endforeach
<tr class="text-dark font-weight-bold">
<td colspan="3" class="border border-success">
Total
</td>
<td colspan="3" class="border border-success">
{{ Cart::getSubtotal() }} $
</td>
</tr>
</tbody>
</table>
@if(Cart::getSubtotal() > 0)
<div class="form-group">
<a href="{{ route("make.payment") }}" class="btn btn-primary mt-3">
Pay {{ Cart::getSubtotal() }} $ via PayPal
</a>
</div>
@endif
</div>
</div>
</div>
@endsection
3- إضافة ل controller PaypalPaymentController
باش نديرو ل paiement غادي نزيدو واحد ل package لي ممكن تشوف الفيديو لي لفوق باش تزيدو من بعد غادي تزيد controller PaypalPaymentController لي فيه les fonctions ديال paiement.
الكود ديال الملف هو :
<?php
namespace App\Http\Controllers;
use App\Order;
use Srmklive\PayPal\Services\ExpressCheckout;
use Illuminate\Http\Request;
class PaypalPaymentController extends Controller
{
//
public function __construct()
{
$this->middleware("auth");
}
public function handlePayment()
{
$data = [];
$data['items'] = [];
foreach (\Cart::getContent() as $item) {
array_push($data['items'], [
'name' => $item->name,
'price' => (int) ($item->price / 9),
'desc' => $item->associatedModel->description,
'qty' => $item->quantity
]);
}
$data['invoice_id'] = auth()->user()->id;
$data['invoice_description'] = "Commande #{$data['invoice_id']}";
$data['return_url'] = route('success.payment');
$data['cancel_url'] = route('cancel.payment');
$total = 0;
foreach ($data['items'] as $item) {
$total += $item['price'] * $item['qty'];
}
$data['total'] = $total;
$paypalModule = new ExpressCheckout;
$res = $paypalModule->setExpressCheckout($data);
$res = $paypalModule->setExpressCheckout($data, true);
return redirect($res['paypal_link']);
}
public function paymentCancel()
{
return redirect()->route('cart.index')->with([
'info' => 'Order canceled'
]);
}
public function paymentSuccess(Request $request)
{
$paypalModule = new ExpressCheckout;
$response = $paypalModule->getExpressCheckoutDetails($request->token);
if (in_array(strtoupper($response['ACK']), ['SUCCESS', 'SUCCESSWITHWARNING'])) {
foreach (\Cart::getContent() as $item) {
Order::create([
"user_id" => auth()->user()->id,
"product_name" => $item->name,
"qty" => $item->quantity,
"price" => $item->price,
"total" => $item->price * $item->quantity,
"paid" => 1
]);
\Cart::clear();
}
return redirect()->route('cart.index')->with([
'success' => 'Paid successfully'
]);
}
}
}
4- إضافة les routes ديال ل paiement
غادي تمشي ل dossier routes ف web.php غادي تزيد les routes ديال ل paiement.
الكود لي غادي تزيد هو :
//payment routes
Route::get('/handle-payment', 'PaypalPaymentController@handlePayment')->name('make.payment');
Route::get('/cancel-payment', 'PaypalPaymentController@paymentCancel')->name('cancel.payment');
Route::get('/payment-success', 'PaypalPaymentController@paymentSuccess')->name('success.payment');