Rev 894 | Rev 1318 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.serving.controllers;import java.util.List;import in.shop2020.model.v1.user.Cart;import in.shop2020.model.v1.user.Line;import in.shop2020.model.v1.user.ShoppingCartException;import in.shop2020.payments.PaymentException;import in.shop2020.payments.PaymentService.Client;import in.shop2020.serving.controllers.BaseController;import in.shop2020.serving.utils.Utils;import in.shop2020.thrift.clients.PaymentServiceClient;import in.shop2020.thrift.clients.UserContextServiceClient;import org.apache.log4j.Logger;import org.apache.struts2.convention.annotation.InterceptorRef;import org.apache.struts2.convention.annotation.InterceptorRefs;import org.apache.struts2.convention.annotation.Result;import org.apache.struts2.convention.annotation.Results;import org.apache.thrift.TException;@InterceptorRefs({@InterceptorRef("myDefault"),@InterceptorRef("login")})@Results({@Result(name="payredirect", type="redirectAction",params = {"actionName" , "${url}", "paymentid", "${pid}", "txnid", "${txn}"}),@Result(name="shipping-redirect", type="redirectAction",params = {"actionName" , "shipping"})})public class OrderController extends BaseController {private static final long serialVersionUID = 1L;private static Logger log = Logger.getLogger(Class.class);private String id;private long txnId = 0;//FIXME right now only one PG. Once we will have more, need to fix it.private String paymentUrl="hdfc-pay";private int gatewayId=1;private long paymentId;private double amount;public OrderController(){super();}// GET /order/ orderidpublic String show() {log.info("id=" + id);htmlSnippets.put("MYACCOUNT_HEADER", pageLoader.getMyaccountHeaderHtml());htmlSnippets.put("ORDER_DETAILS", pageLoader.getOrderDetailsHtml(Long.parseLong(id)));return "show";}// POST /order/public String create(){String addressIdString = this.request.getParameter("addressid");if(addressIdString == null){addActionError("Please specify shipping address to continue.");return "shipping-redirect";}long addressId = Long.parseLong(addressIdString);long currentCartId = userinfo.getCartId();try{if(!createOrdersAndPayment(addressId, currentCartId)){addActionError("We are experiencing problem. Please try later.");return "shipping-redirect";}}catch (Exception e) {addActionError("We are experiencing problem. Please try later.");log.error("Exception in createOrders function. Something want wrong.");e.printStackTrace();return "shipping-redirect";}return "payredirect";}public String getId(){return id;}public void setId(String id){this.id = id;}public String getMyaccountHeaderSnippet(){return htmlSnippets.get("MYACCOUNT_HEADER");}public String getOrderDetailsSnippet(){return htmlSnippets.get("ORDER_DETAILS");}public String getUrl(){return this.paymentUrl;}public long getPid(){return this.paymentId;}public long getTxn(){return this.txnId;}public double getAmount(){return this.amount;}private double getPaymentAmount(long cartId) throws ShoppingCartException, TException{double totalAmount = 0;Cart cart;UserContextServiceClient userContextServiceClient = null;try {userContextServiceClient = new UserContextServiceClient();} catch (Exception e) {e.printStackTrace();}in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();cart = userClient.getCart(cartId);List<Line> lineItems = cart.getLines();for (Line line : lineItems) {long productId = line.getItemId();totalAmount = totalAmount + line.getQuantity() * Utils.getItemPrice(productId);}return totalAmount;}/**** @param addressId* @param currentCartId* @return*/private boolean createOrdersAndPayment(long addressId, long currentCartId){UserContextServiceClient userServiceClient = null;try {userServiceClient = new UserContextServiceClient();} catch (Exception e) {e.printStackTrace();return false;}in.shop2020.model.v1.user.UserContextService.Client userClient = userServiceClient.getClient();try {userClient.addAddressToCart(currentCartId, addressId);} catch (ShoppingCartException e1) {log.error("Not able to set address in the cart." + e1.getId() + e1.getMessage());e1.printStackTrace();return false;} catch (TException e1) {log.error("Thrift exception while setting address in cart." + e1.getMessage());e1.printStackTrace();return false;}try {if(!userClient.validateCart(currentCartId)){addActionError("Your cart has been updated.");return false;}} catch (ShoppingCartException e1) {log.error("Error while validating shopping cart." + e1.getId() + e1.getMessage());e1.printStackTrace();return false;} catch (TException e) {log.error("Thrift exception while validating cart." + e.getMessage());e.printStackTrace();return false;}try {txnId = userClient.createOrders(currentCartId);} catch (ShoppingCartException e1) {log.error("Error while creating orders from cart." + e1.getId() + e1.getMessage());e1.printStackTrace();return false;} catch (TException e) {log.error("Thrift exception while creating orders from cart." + e.getMessage());e.printStackTrace();return false;}PaymentServiceClient paymentServiceClient = null;try {paymentServiceClient = new PaymentServiceClient();} catch (Exception e) {log.error("Error while getting payment client");e.printStackTrace();return false;}try {amount = getPaymentAmount(userinfo.getCartId());} catch (ShoppingCartException e1) {log.error("Not able to fetch payment amount from cart id." + e1.getId() + e1.getMessage());e1.printStackTrace();return false;} catch (TException e1) {log.error("Not able to fetch payment amount." + e1.getMessage());e1.printStackTrace();return false;}Client paymentClient = paymentServiceClient.getClient();try {this.paymentId = paymentClient.createPayment(userinfo.getUserId(), amount, gatewayId, txnId);} catch (PaymentException e1) {log.error("Not able to create payment object." + e1.getError_code() + e1.getMessage());e1.printStackTrace();return false;} catch (TException e) {log.error("Not able to create payment object." + e.getMessage());e.printStackTrace();return false;}return true;}}