Manage orders, create, list, view details and refund.
Checkout
grails-app/controllers/OrdersController.groovy
def checkout (){
def user = springSecurityService.principal
Customer customer = customerService.findByEmail(user.getEmail());
Order order = new Order(customer: customer, shippingAddress: address, billingAddress: address);
["categories": categoryService.findAll(),
"cart": shoppingCartService.get(),
"order": order]
}
<g:form name="payment-form" controller="orders" action="create" method="post">
<input type="hidden" name="token" value="" />
<div class="reg-form">
<div>
<h3>Personal Information</h3>
<ul>
<li class="text-info">First Name: </li>
<li><g:textField name="order.customer.firstName"
value="${order.customer.firstName}" /></li>
</ul>
<ul>
<li class="text-info">Last Name: </li>
<li><g:textField name="order.customer.lastName"
value="${order.customer.lastName}"/></li>
</ul>
<ul>
<li class="text-info">Email: </li>
<li><g:textField name="order.customer.email"
value="${order.customer.email}"/></li>
</ul>
<h3>Billing Address</h3>
<ul>
<li class="text-info">Address Line 1: </li>
<li><g:textField name="order.billingAddress.streetLine1"
value="${order.billingAddress.streetLine1}"/></li>
</ul>
<ul>
<li class="text-info">Address Line 2:</li>
<li><g:textField name="order.billingAddress.streetLine2"
value="${order.billingAddress.streetLine2}"/></li>
</ul>
<ul>
<li class="text-info">City:</li>
<li><g:textField name="order.billingAddress.city"
value="${order.billingAddress.city}"/></li>
</ul>
<ul>
<li class="text-info">State:</li>
<li><g:textField name="order.billingAddress.state"
value="${order.billingAddress.state}"/></li>
</ul>
<ul>
<li class="text-info">Zip code:</li>
<li><g:textField name="order.billingAddress.zipCode"
value="${order.billingAddress.zipCode}"/></li>
</ul>
<ul>
<li class="text-info">Country:</li>
<li><g:textField name="order.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><g:textField name="order.shippingAddress.streetLine1"
value="${order.shippingAddress.streetLine1}"/></li>
</ul>
<ul>
<li class="text-info">Address Line 2:</li>
<li><g:textField name="order.shippingAddress.streetLine2"
value="${order.shippingAddress.streetLine2}"/></li>
</ul>
<ul>
<li class="text-info">City:</li>
<li><g:textField name="order.shippingAddress.city"
value="${order.shippingAddress.city}"/></li>
</ul>
<ul>
<li class="text-info">State:</li>
<li><g:textField name="order.shippingAddress.state"
value="${order.shippingAddress.state}"/></li>
</ul>
<ul>
<li class="text-info">Zip code:</li>
<li><g:textField name="order.shippingAddress.zipCode"
value="${order.shippingAddress.zipCode}"/></li>
</ul>
<ul>
<li class="text-info">Country:</li>
<li><g:textField name="order.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" />
<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>
</g:form>
Placing orders
def create(){
def user = springSecurityService.principal
def order = new Order()
order.customer = user.toCustomer()
println "TOKEN: $params.token"
bindData(order, params.order)
def transaction = orderService.checkout(order, params.token)
redirect(action: 'show', params:['orderId': transaction.order.id])
}
List all customer orders
grails-app/controllers/OrdersController.groovy
def index() {
def user = springSecurityService.principal
["categories": categoryService.findAll(), "cart": shoppingCartService.get(),
"orders": orderService.findAllByCustomer(user.toCustomer(), null)]
}
grails-app/views/orders/index.gsp
<h2>MY ORDERS </h2>
<g:if test="${orders.isEmpty()}">
<div >
<h1>You do not have any orders yet.</h1>
</div>
</g:if>
<g: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>
<g:each in="${orders}" var="o">
<tr>
<td>
<g:link controller="orders" action="show" params="[orderId: o.id]"> ${o.id}</g:link>
</td>
<td>${o.createdAt}</td>
<td>${o.total}</td>
<td>${o.status}</td>
<td>
<g:form controller="orders" action="refund" id="${o.id}" method="post">
<input type="submit" value="Refund"/>
</g:form>
</td>
</tr>
</g:each>
</tbody>
</table>
</div>
</g:else>
Display order details
grails-app/controllers/OrdersController.groovy
def show(){
["categories": categoryService.findAll(),
"cart": shoppingCartService.get(),
"order": orderService.findById(params.orderId)]
}
grails-app/views/orders/show.gsp
<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>
<g:each in="${order.items}" var="i">
<tr>
<td>${i.product.title}</td>
<td>${i.quantity}</td>
<td>${i.unitPrice}</td>
<td>${i.total}</td>
</tr>
</g:each>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
Refund Order
grails-app/views/orders.index.gsp
<td>
<g:form controller="orders" action="refund" id="${o.id}" method="post">
<input type="submit" value="Refund"/>
</g:form>
</td>
grails-app/controllers/OrdersController.groovy
def refund(){
orderService.refund(params.orderId);
redirect(action: 'show', params:['orderId': params.orderId])
}