Ecommerce App ب laravel & stripe paiement الجزء الرابع
نظرة سريعة بالفيديو
1- إضافة gloudemans/shoppingcart لل projet
composer require gloudemans/shoppingcart
//ajouter au providers array:
Gloudemans\Shoppingcart\ShoppingcartServiceProvider::class
//ajouter au aliases array:
'Cart' => Gloudemans\Shoppingcart\Facades\Cart::class,
2- الملف ShopController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Cart;
use App\Product;
class ShopController extends Controller
{
//
public function add_to_cart(){
$product = Product::find(request()->product_id);
$item = Cart::add([
'id' => $product->id,
'name' => $product->name,
'qty' => request()->qte,
'price' => $product->prix
]);
Cart::associate($item->rowId,'App\Product');
return redirect()->route('cart.index');
}
public function cart(){
//Cart::destroy();
return view('admin.products.cart');
}
public function cart_delete($id){
Cart::remove($id);
return redirect()->back();
}
public function cart_dec($id,$qte){
Cart::update($id,$qte - 1);
return redirect()->back();
}
public function cart_inc($id,$qte){
Cart::update($id,$qte + 1);
return redirect()->back();
}
}
3- الملف products/cart.blade.php
@extends ('includes.main-layout')
@section('styles')
@endsection
@section('header')
@include('includes.header')
@endsection
@section('content')
<div class="row mt-4">
<div class="col-md-12">
@include('includes.errors')
<div class="card">
<div class="card-body">
<h3 class="card-title">Produits en panier : <span class="badge bg-dark text-white p-2">{{Cart::content()->count()}}</span></h3>
<table class="table table-dark mx-auto">
<thead class="thead-default">
<tr>
<th></th>
<th>Libellé</th>
<th>Prix</th>
<th>Qté</th>
<th>Total</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach(Cart::content() as $product)
<tr>
<td><img src="{{URL::to('images/'.$product->model->file)}}" height="50" width="50" alt=""></td>
<td scope="row">{{$product->name}}</td>
<td>{{$product->price}} dh</td>
<td>
<a href="{{route('cart.decrease',['id'=>$product->rowId,'qte'=>$product->qty])}}" class="btn bg-light" id="moins">-</a>
<input type="text" style="width:8%;text-align:center;padding:5px;" class="qte" id="qte" name="qte" value="{{$product->qty}}">
<a href="{{route('cart.increase',['id'=>$product->rowId,'qte'=>$product->qty])}}" class="btn bg-light" id="plus">+</a>
</td>
<td>{{$product->total}} dh</td>
<td><a href="{{route('cart.delete',['id'=>$product->rowId])}}" class="btn btn-danger btn-xs"><i class="fa fa-trash"></i></a></td>
</tr>
@endforeach
</tbody>
</table>
<p class="lead">Total: <span class="badge bg-success text-white">{{Cart::total()}} dh</span></p>
<a href="{{route('cart.checkout')}}" class="btn btn-success">Valider</a>
</div>
</div>
</div>
</div>
@endsection
@section('footer')
@endsection
@section('scripts')
@endsection
4- الملف products/checkout.blade.php
@extends ('includes.main-layout')
@section('styles')
@endsection
@section('header')
@include('includes.header')
@endsection
@section('content')
<div class="row mt-4">
<div class="col-md-12">
@include('includes.errors')
<div class="card">
<div class="card-body">
<h3 class="card-title">Vous avez commandez :</h3>
<table class="table table-dark mx-auto">
<thead class="thead-default">
<tr>
<th></th>
<th>Libellé</th>
<th>Prix</th>
<th>Qté</th>
<th>Total</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach(Cart::content() as $product)
<tr>
<td><img src="{{URL::to('images/'.$product->model->file)}}" height="50" width="50" alt=""></td>
<td scope="row">{{$product->name}}</td>
<td>{{$product->price}} dh</td>
<td>
{{$product->qty}}
</td>
<td>{{Cart::subtotal()}} dh</td>
</tr>
@endforeach
</tbody>
</table>
<p class="lead">Total: <span class="badge bg-success text-white">{{Cart::total()}} dh</span></p>
<form action="{{route('cart.pay')}}" method="POST">
{{csrf_field()}}
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="api key"
data-amount= {{Cart::total() * 9.95}}
data-name="Ecommerce Site"
data-description="Commande"
data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
data-locale="fr">
</script>
</form>
</div>
</div>
</div>
</div>
@endsection
@section('footer')
@endsection
@section('scripts')
@endsection
5- الملف CheckoutController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Cart;
use Stripe\Stripe;
use Stripe\Charge;
class CheckoutController extends Controller
{
//
public function index(){
return view('admin.products.checkout');
}
public function pay(){
Stripe::setApiKey("votre api key");
$token = request()->stripeToken;
// Charge the user's card:
$charge = Charge::create(array(
"amount" => Cart::total() * 9.95,
"currency" => "usd",
"description" => "Darija-Coding ecommerce site",
"source" => $token,
));
Cart::destroy();
return redirect('/')->with(['success','Commande payée avec succés!']);
}
}