You already have the order object right?
You should send the email after you saved the order and the payment is processed. That should be the postCheckout method for you.
Another option is using an event and a listener to perform the same aciton.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi everyone. I hope I am being cleared with that explanation. When a customer buy a product it will get an email with order confirmation with details of the product. First of all I am using stripe. I am guess that I have to use notification email or just use this function:
public function sendEmail($user, $code){
Mail::send(
'shop.order_confirmation',
['user' => $user, 'code' => $code],
function($message) use ($user){
$message->to($user->email);
$message->subject($user->name, "Your order confirmation");
}
);
}
My ProductController do this:
<?php
namespace App\Http\Controllers;
use App\Cart;
use App\Product;
use App\Order;
use Illuminate\Http\Request;
use App\Http\Requests;
use Session;
use Auth;
use Stripe\Charge;
use Stripe\Stripe;
class ProductController extends Controller
{
public function getIndex()
{
$products = Product::all();
return view('shop.index', ['products' => $products]);
}
public function getAddToCart(Request $request, $id)
{
$product = Product::find($id);
$oldCart = Session::has('cart') ? Session::get('cart') : null;
$cart = new Cart($oldCart);
$cart->add($product, $product->id);
$request->session()->put('cart', $cart);
return redirect()->route('product.index');
}
public function getCart()
{
if (!Session::has('cart')) {
return view('shop.shopping-cart');
}
$oldCart = Session::get('cart');
$cart = new Cart($oldCart);
return view('shop.shopping-cart', ['products' => $cart->items, 'totalPrice' => $cart->totalPrice]);
}
public function getCheckout()
{
if (!Session::has('cart')) {
return view('shop.shopping-cart');
}
$oldCart = Session::get('cart');
$cart = new Cart($oldCart);
$total = $cart->totalPrice;
return view('shop.checkout', ['total' => $total]);
}
public function getReduceByOne($id){
$oldCart = Session::has('cart') ? Session::get('cart') : null;
$cart = new Cart($oldCart);
$cart->reduceByOne($id);
if (count($cart->items) > 0){
Session::put('cart', $cart);
} else{
Session::forget('cart');
}
return redirect()->route('product.shoppingCart');
}
public function getRemoveItem($id){
$oldCart = Session::has('cart') ? Session::get('cart') : null;
$cart = new Cart($oldCart);
$cart->removeItem($id);
if (count($cart->items) > 0){
Session::put('cart', $cart);
} else{
Session::forget('cart');
}
return redirect()->route('product.shoppingCart');
}
public function postCheckout(Request $request)
{
if (!Session::has('cart')) {
return redirect()->route('shop.shoppingCart');
}
$oldCart = Session::get('cart');
$cart = new Cart($oldCart);
Stripe::setApiKey('');
try {
$charge = Charge::create(array(
"amount" => $cart->totalPrice * 100,
"currency" => "usd",
"source" => 'tok_mastercard', // obtained with Stripe.js
"description" => "Test Charge"
));
$order = new Order();
$order->cart = serialize($cart);
$order->address = $request->input('address');
$order->name = $request->input('name');
$order->surname = $request->input('surname');
$order->city = $request->input('city');
$order->zip = $request->input('zip');
$order->phone = $request->input('phone');
$order->payment_id = $charge->id;
Auth::user()->orders()->save($order);
} catch (\Exception $e) {
return redirect()->route('checkout')->with('error', $e->getMessage());
}
Session::forget('cart');
return redirect()->route('product.index')->with('success', 'Successfully purchased products!');
}
public function sendEmail($user, $code){
Mail::send(
'shop.order_confirmation',
['user' => $user, 'code' => $code],
function($message) use ($user){
$message->to($user->email);
$message->subject($user->name, "Your order confirmation");
}
);
}
}
order_confirmation.blade.php
@extends('layouts.master')
@section('title')
Laravel Shopping Cart
@endsection
@section('content')
@if(Session::has('cart'))
<div class="container-fluid">
<div class="row">
<div class="col-md-6 m-auto">
@foreach ($products as $product)
<li class="list-group-item">
<img src="{{ $product['item']['imagePath'] }}" height="150" width="150">
<span class="badge badge-secondary">Quantity: {{ $product['qty'] }}</span>
<span class="badge badge-secondary">Item: {{ $product['item']['title'] }}</span>
<span class="badge badge-secondary">Price: ${{ $product['price'] }}</span>
</li>
@endforeach
</ul>
</div>
</div>
</div>
<div class="container-fluid">
<div class="row">
<div class="col-md-4 m-auto">
<strong>Total Cost: ${{ $totalPrice }}</strong>
</ul>
</div>
</div>
</div>
@endif
@endsection
web.php
Route::post('/order_confirmation', [
'uses' => 'ProductController@sendEmail',
'as' => 'shop.order_confirmation'
]);
Which of the 2 ways is best for me? Using notiification emaill I dont know how can customer get the information of the product that bought. Also how can I know the informations of the customer and which product bought.
You already have the order object right?
You should send the email after you saved the order and the payment is processed. That should be the postCheckout method for you.
Another option is using an event and a listener to perform the same aciton.
Please or to participate in this conversation.