Manage orders, create, list, view details and refund.

Checkout

app/Http/Controllers/OrdersController.php

<?php

public function checkout(Request $request)
{
    $order = new Order();
    $order->customer = $request->user()->toCustomer();
    $order->billingAddress = $this->createAddress();
    $order->shippingAddress = $this->createAddress();
    return view('orders/checkout', array(
        'order' => $order,
    ));
}

resources/views/orders/checkout.blade.php


<h2>ORDER INFORMATION </h2>
<div class="registration-grids reg">
    <form id="payment-form" action="/orders/create" method="post">
        <input type="hidden" name="token" value="" />
        {{ csrf_field() }}
        <div class="reg-form">
            <div>
                <h3>Personal Information</h3>

                <ul>
                    <li class="text-info">First Name: </li>
                    <li><input type="text" name="customer_firstName" value="{{ $order->customer->firstName }}" /></li>
                </ul>
                <ul>
                    <li class="text-info">Last Name: </li>
                    <li><input type="text" name="customer_lastName" value="{{ $order->customer->lastName }}"/></li>
                </ul>
                <ul>
                    <li class="text-info">Email: </li>
                    <li><input type="text" name="customer_email" value="{{ $order->customer->email }}"/></li>
                </ul>
                <ul>
                    <li class="text-info">Phone number: </li>
                    <li><input type="text" name="" value=""/></li>
                </ul>
                <h3>Billing Address</h3>
                <ul>
                    <li class="text-info">Address Line 1: </li>
                    <li><input type="text" name="billingAddress_streetLine1" value="{{ $order->billingAddress->streetLine1 }}"/></li>
                </ul>
                <ul>
                    <li class="text-info">Address Line 2:</li>
                    <li><input type="text" name="billingAddress_streetLine2" value="{{ $order->billingAddress->streetLine2 }}"/></li>
                </ul>
                <ul>
                    <li class="text-info">City:</li>
                    <li><input type="text" name="billingAddress_city" value="{{ $order->billingAddress->city }}"/></li>
                </ul>
                <ul>
                    <li class="text-info">State:</li>
                    <li><input type="text" name="billingAddress_state" value="{{ $order->billingAddress->state }}"/></li>
                </ul>
                <ul>
                    <li class="text-info">Zip code:</li>
                    <li><input type="text"  name="billingAddress_zipCode" value="{{ $order->billingAddress->zipCode }}"/></li>
                </ul>
                <ul>
                    <li class="text-info">Country:</li>
                    <li><input type="text" name="billingAddress_country" value="{{ $order->billingAddress->country }}"/></li>
                </ul>
                <h3>Shipping Address</h3>
                <ul>
                    <li><input type="checkbox" value="" /></li>
                    <li class="text-info">Same as Billing address: </li>
                </ul>
                <div id="shipping_address_container">
                    <ul>
                        <li class="text-info">Address Line 1: </li>
                        <li><input type="text" name="shippingAddress_streetLine1" value="{{ $order->shippingAddress->streetLine1 }}"/></li>
                    </ul>
                    <ul>
                        <li class="text-info">Address Line 2:</li>
                        <li><input type="text" name="shippingAddress_streetLine2" value="{{ $order->shippingAddress->streetLine2 }}"/></li>
                    </ul>
                    <ul>
                        <li class="text-info">City:</li>
                        <li><input type="text" name="shippingAddress_city" value="{{ $order->shippingAddress->city }}"/></li>
                    </ul>
                    <ul>
                        <li class="text-info">State:</li>
                        <li><input type="text" name="shippingAddress_state" value="{{ $order->shippingAddress->state }}"/></li>
                    </ul>
                    <ul>
                        <li class="text-info">Zip code:</li>
                        <li><input type="text" name="shippingAddress_zipCode" value="{{ $order->shippingAddress->zipCode }}"/></li>
                    </ul>
                    <ul>
                        <li class="text-info">Country:</li>
                        <li><input type="text" name="shippingAddress_country" value="{{ $order->shippingAddress->country }}"/></li>
                    </ul>
                </div>

            </div>
        </div>
        <div class="reg-right">
            <h3>Credit Card Information</h3>
            <ul>
                <li class="text-info">Card Holder Name: </li>
                <li><input id="card_holder_name" type="text" value="" /></li>
            </ul>
            <ul>
                <li class="text-info">Card Number: </li>
                <li><input id="card_number" type="text" value="4242424242424242" data-stripe="number"/></li>
            </ul>
            <ul>
                <li class="text-info">CVC: </li>
                <li><input id="cvc" type="text" value="123" data-stripe="cvc"/></li>
            </ul>
            <ul>
                <li class="text-info">Expiration Date: </li>
                <li>
                    Month: <input type="text" value="12" style="width: 100px;" data-stripe="exp-month"/>
                    Year: <input type="text" value="2017" style="width: 100px;" data-stripe="exp-year"/>
                </li>
            </ul>
            <p style="display: none;" id="card_error_message"></p>
            <input type="submit" value="PLACE ORDER" />
        </div>
        <div class="clearfix"></div>
    </form>
</div>

Placing orders

app/Http/Controllers/OrdersController.php

<?php

public function create(Request $request)
{
    $token = $request->input('token');
    $input = $request->all();

    $order = new Order();
    $order->customer = $request->user()->toCustomer();
    $order->billingAddress = $this->getBillingAddress($input);
    $order->shippingAddress = $this->getShippingAddress($input);

    $transaction = $order->checkout($token);
    $orderId = $transaction->order->id;
    return redirect("/orders/${orderId}");

}

List all customer orders

app/Http/Controllers/OrdersController.php

<?php

public function index(Request $request)
{
    $customer = $request->user()->toCustomer();
    $orders = Order::findAllByCustomer($customer);
    return view('orders/index', array(
        'orders' => $orders,
    ));
}

resources/views/orders/index.blade.php


<h2>MY ORDERS </h2>
@if( $orders->count() <= 0 )
<div>
    <h1>You do not have any orders yet!</h1>
</div>
@else
<div  class="cart-gd">
    <table class="table">
        <thead>
        <tr>
            <th>Order ID</th>
            <th>Creation Date</th>
            <th>Total Price</th>
            <th>Status</th>
            <th>Action</th>
        </tr>
        </thead>
        <tbody>
        @foreach($orders as $o)
        <tr>
            <td>
                <a href="/orders/{{$o->id}}">{{ $o->id }}</a>
            </td>
            <td>{{ $o->createdAt }}</td>
            <td>{{ $o->total }}</td>
            <td>{{ $o->status }}</td>
            <td>
                <form action="/orders/refund/{{$o->id}}" method="post">
                    <input type="submit" value="Refund"/>
                </form>
            </td>
        </tr>
        @endforeach
        </tbody>
    </table>
</div>
@endif

Display order details

app/Http/Controllers/OrdersController.php

<?php

public function show($id)
{
    $order = Order::findById($id);
    return view('orders/show', array(
        'order' => $order,
    ));
}

resources/views/orders/show.blade.php


<h2>Order Details </h2>
<table class="table">
    <tbody>
    <tr>
        <td colspan="2">
            <h4 style="text-align: center;">Summery</h4>
        </td>
    </tr>
    <tr>
        <th>Order ID</th>
        <td>{{ $order->id }}</td>
    </tr>
    <tr>
        <th>Creation Date</th>
        <td>{{ $order->createdAt }}</td>
    </tr>
    <tr>
        <th>Total price</th>
        <td>{{ $order->total }}</td>
    </tr>
    <tr>
        <th>Status</th>
        <td>{{ $order->status }}</td>
    </tr>
    <tr>
        <td colspan="2">
            <h4 style="text-align: center;">Item details</h4>
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <table class="table">
                <thead>
                <tr>
                    <th>Product</th>
                    <th>Quantity</th>
                    <th>Unit Price</th>
                    <th>Total</th>
                </tr>
                </thead>
                <tbody>
                @foreach ( $order->items as $i )
                <tr>
                    <td>{{ $i->product->title }}</td>
                    <td>{{ $i->quantity }}</td>
                    <td>{{ $i->unitPrice }}</td>
                    <td>{{ $i->total }}</td>
                </tr>
                @endforeach
                </tbody>
            </table>
        </td>
    </tr>
    </tbody>
</table>

Refund Order

resources/views/orders/index.blade.php


<form action="/orders/refund/{{ $o->id }}" method="post">
    <input type="submit" value="Refund"/>
</form>

app/Http/Controllers/OrdersController.php

<?php

public function refund($id)
{
    $transaction = Order::refund($id);
    $orderId = $transaction->order->id;
    return redirect("/orders/${orderId}");
}