Subversion Repositories SmartDukaan

Rev

Rev 712 | 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", type="redirectAction", 
                        params = {"actionName" , "${url}", "paymentid", "${pid}", "txnid", "${txn}", "amount", "${amount}"}),
    @Result(name="shipping-redirect", type="redirectAction", 
                params = {"actionName" , "shipping"}),
    @Result(name="failure", 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 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/ orderid
    public String show() {
        log.info("id=" + id);
        if(!userinfo.isLoggedIn()){
                setRedirectUrl();
                addActionError("Please login to see order details.");
                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();
                addActionError("Please login to continue checkout.");
                return "redirect";
        }
        long addressId = Long.parseLong(this.request.getParameter("addressid"));
        long currentCartId = userinfo.getCartId();
        
                try{
                        amount = Double.parseDouble(request.getParameter("amount"));
                }catch(Exception e){
                        amount = Utils.getPaymentAmount(userinfo.getCartId());
                }

                try {
                        UserContextServiceClient userServiceClient = new UserContextServiceClient();
                        in.shop2020.model.v1.user.UserContextService.Client userClient = userServiceClient.getClient();
                        
                        userClient.addAddressToCart(currentCartId, addressId);
                        //TODO validate only item quantity change. Other validations should not be done. 
                        if(!userClient.validateCart(currentCartId)){
                                addActionError("Some items are added in your cart.");
                                return "shipping-redirect";
                        }
                        txnId = userClient.createOrders(currentCartId);
                        
                        PaymentServiceClient paymentServiceClient = new PaymentServiceClient();
                        Client paymentClient = paymentServiceClient.getClient();
                        
                        this.paymentId = paymentClient.createPayment(userinfo.getUserId(), amount, gatewayId, txnId);
                        return "payredirect";
                        
                } catch (TException e) {
                        e.printStackTrace();
                } catch (Exception e) {
                        e.printStackTrace();
                }
        
                return "failure";
    }
    
        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;
        }
        

}