I have a shopping cart with a checkout page. On the checkout page is a payment button which takes them to a stripe payment form.
I've read some books on building eCommerce shopping carts in which the order is created when the user clicks the payment button and the order id is passed to the payment gateway. In this scenario if the user abandons the payment form then they are left with an unpaid order linked to their account.
So my question is what is preferable. Do you create the order only after a successful payment charge is made or would you create the order before the payment is made?
I suppose the only benefit to creating an order before the payment is made is that the site becomes more flexible in the sense that offline orders could be made as well i.e. and invoiced order.
* UPDATE *
An example of how an order can be created after payment charge has been made.
public function checkout(Request $request)
{
$token = $request->input('stripeToken');
//Retriieve cart information
$cart = Cart::where('user_id',Auth::user()->id)->first();
$items = $cart->cartItems;
$total=0;
foreach($items as $item){
$total+=$item->product->price;
}
if(
Auth::user()->charge($total*100, [
'source' => $token,
'receipt_email' => Auth::user()->email,
])){
$order = new Order();
$order->total_paid= $total;
$order->user_id=Auth::user()->id;
$order->save();
foreach($items as $item){
$orderItem = new OrderItem();
$orderItem->order_id=$order->id;
$orderItem->product_id=$item->product->id;
$orderItem->file_id=$item->product->file->id;
$orderItem->save();
CartItem::destroy($item->id);
}
return redirect('/order/'.$order->id);
}else{
return redirect('/cart');
}
}