Subversion Repositories SmartDukaan

Rev

Rev 689 | Blame | Last modification | View Log | RSS feed

package in.shop2020.serving.controllers;

import in.shop2020.model.v1.order.Order;
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 java.util.*;

import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.thrift.TException;

@Results({
    @Result(name="redirect", type="redirectAction", 
                params = {"actionName" , "login"}),
        @Result(name="payredirect", location="${url}", type="redirect", params = {"paymentid" , "${payment-id}"}),
    @Result(name="shipping-redirect", type="redirectAction", 
                params = {"actionName" , "shipping"})
                
})
public class OrderController extends BaseController {
        
        private static final long serialVersionUID = 1L;
        
        private static Log log = LogFactory.getLog(OrderController.class);
        private String id;
        
        private String message;
        
        //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;
        
        public OrderController(){
                super();
        }
        
    // GET /order/ orderid
    public String show() {
        log.info("id=" + id);
        if(!userinfo.isLoggedIn()){
                setRedirectUrl();
                return "redirect";
        }
                htmlSnippets.put("MYACCOUNT_HEADER", pageLoader.getMyaccountHeaderHtml());
                htmlSnippets.put("MYACCOUNT_HEADER", pageLoader.getOrderDetailsHtml(Long.parseLong(id)));
        return "show";
    }

    // POST /order/
    public String create(){
        if(!userinfo.isLoggedIn()){
                setRedirectUrl();
                return "redirect";
        }
        long addressId = Long.parseLong(this.request.getParameter("addressid"));
        long currentCartId = userinfo.getCartId();
        double amount;
                try{
                        amount = Double.parseDouble(request.getParameter("amount"));
                }catch(Exception e){
                        amount = Utils.getPaymentAmount(userinfo.getCartId());
                }

                
        List<Order> orders = null;
                try {
                        UserContextServiceClient userServiceClient = new UserContextServiceClient();
                        in.shop2020.model.v1.user.UserContextService.Client userClient = userServiceClient.getClient();
                        
                        userClient.addAddressToCart(currentCartId, addressId);
                        if(!userClient.validateCart(currentCartId)){
                                return "shipping-redirect";
                        }
                        Long txnId = userClient.createOrders(currentCartId);
                        
                        PaymentServiceClient paymentServiceClient = new PaymentServiceClient();
                        Client paymentClient = paymentServiceClient.getClient();
                        
                        this.paymentId = paymentClient.createPayment(userinfo.getUserId(), amount, gatewayId, txnId);
                        return "payredirect";
                        
                        /*
                        TransactionServiceClient tsc = new TransactionServiceClient();
                        orders = tsc.getClient().getOrdersForTransaction(txnId);
                        
                        long newCartId = userClient.getUserById(userinfo.getUserId()).getActiveCartId();
                        userinfo.setCartId(newCartId);
                        userinfo.setTotalItems(0);
                        
                        011-46010946
                        */
                } catch (TException e) {
                        e.printStackTrace();
                } catch (Exception e) {
                        e.printStackTrace();
                }
        
                this.message = "Your order numbers are as below:";
                
                for(Order order: orders){
                        this.message = this.message  + "<br> <a href=\"./order/" + order.getId() + "\">" + order.getId() + "</a>";
                }
                
                return "success";
    }
    
        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 getPaymentId(){
                return this.paymentId;
        }
        
        public String getMessage(){
                return this.message;
        }

}