Manage orders, create, list, view details and refund.
Checkout
app/controllers/orders_controller.rb
def checkout
customer = Customer.find_by_id session[:customer_id]
@order = Order.new(customer: customer, billingAddress: create_address, shippingAddress: create_address)
end
app/views/orders/checkout.html.erb
<h2>ORDER INFORMATION </h2>
<div class="registration-grids reg">
<%= form_tag orders_path, id: 'payment-form', method: 'post' do %>
<input type="hidden" name="token" value="" />
<div class="reg-form">
<div>
<h3>Personal Information</h3>
<ul>
<li class="text-info">First Name: </li>
<li><%= text_field_tag 'customer[firstName]', @order.customer.firstName %></li>
</ul>
<ul>
<li class="text-info">Last Name: </li>
<li><%= text_field_tag "customer[lastName]", @order.customer.lastName %></li>
</ul>
<ul>
<li class="text-info">Email: </li>
<li><%= text_field_tag "customer[email]", @order.customer.email %></li>
</ul>
<ul>
<li class="text-info">Phone number: </li>
<li><input type="text" value="" name="customer[phoneNumber]"/></li>
</ul>
<h3>Billing Address</h3>
<ul>
<li class="text-info">Address Line 1: </li>
<li><%= text_field_tag "billingAddress[streetLine1]", @order.billingAddress.streetLine1 %></li>
</ul>
<ul>
<li class="text-info">Address Line 2:</li>
<li><%= text_field_tag "billingAddress[streetLine2]", @order.billingAddress.streetLine2 %></li>
</ul>
<ul>
<li class="text-info">City:</li>
<li><%= text_field_tag "billingAddress[city]", @order.billingAddress.city %></li>
</ul>
<ul>
<li class="text-info">State:</li>
<li><%= text_field_tag "billingAddress[state]", @order.billingAddress.zipCode %></li>
</ul>
<ul>
<li class="text-info">Zip code:</li>
<li><%= text_field_tag "billingAddress[zipCode]", @order.billingAddress.zipCode %></li>
</ul>
<ul>
<li class="text-info">Country:</li>
<li><%= text_field_tag "billingAddress[country]", @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><%= text_field_tag "shippingAddress[streetLine1]", @order.shippingAddress.streetLine1 %></li>
</ul>
<ul>
<li class="text-info">Address Line 2:</li>
<li><%= text_field_tag "shippingAddress[streetLine2]", @order.shippingAddress.streetLine2 %></li>
</ul>
<ul>
<li class="text-info">City:</li>
<li><%= text_field_tag "shippingAddress[city]", @order.shippingAddress.city %></li>
</ul>
<ul>
<li class="text-info">State:</li>
<li><%= text_field_tag "shippingAddress[state]", @order.shippingAddress.state %></li>
</ul>
<ul>
<li class="text-info">Zip code:</li>
<li><%= text_field_tag "shippingAddress[zipCode]", @order.shippingAddress.zipCode %></li>
</ul>
<ul>
<li class="text-info">Country:</li>
<li><%= text_field_tag "shippingAddress[country]", @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" />
<p class="click">By clicking this button, you are agree to my <a href="#">Policy Terms and Conditions.</a></p>
</div>
<div class="clearfix"></div>
<% end %>
</div>
Placing orders
app/controllers/orders_controller.rb
def create
customer = Customer.new
customer.id = session[:customer_id]
order = Order.new(customer: customer,
billingAddress: Address.new(params[:billingAddress]),
shippingAddress: Address.new(params[:shippingAddress]))
transaction = order.checkout(params[:token])
redirect_to("/orders/#{transaction.order.id}")
end
List all customer orders
app/controllers/orders_controller.rb
def index
@orders = Order.find_all_by_customer(session[:customer_id])
end
app/views/orders/index.html.erb
<h2>MY ORDERS </h2>
<% if @orders.is_empty? %>
<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>
<% for o in @orders %>
<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>
<% end %>
</tbody>
</table>
</div>
<% end %>
Display order details
app/controllers/orders_controller.rb
def show
@order = Order.find_by_id(params[:id])
end
app/views/orders/show.html.erb
<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>
<% for i in @order.items %>
<tr>
<td><%= i.product.title %></td>
<td><%= i.quantity %></td>
<td><%= i.unitPrice %></td>
<td><%= i.total %></td>
</tr>
<% end %>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
Refund Order
app/views/orders/index.html.erb
<form action="/orders/refund/<%= o.id %>" method="post">
<input type="submit" value="Refund"/>
</form>
app/controllers/orders_controller.rb
def refund
transaction = Order.refund(params[:id])
redirect_to("/orders/#{transaction.order.id}")
end