Subversion Repositories SmartDukaan

Rev

Rev 5613 | Rev 5716 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3101 chandransh 1
package in.shop2020.serving.controllers;
2
 
3209 vikas 3
import in.shop2020.datalogger.EventType;
3101 chandransh 4
import in.shop2020.model.v1.user.Address;
5
import in.shop2020.model.v1.user.Cart;
6
import in.shop2020.serving.utils.FormattingUtils;
3209 vikas 7
import in.shop2020.serving.utils.Utils;
3126 rajveer 8
import in.shop2020.thrift.clients.UserClient;
3209 vikas 9
import in.shop2020.utils.DataLogger;
3101 chandransh 10
 
11
import java.util.Collection;
12
import java.util.ResourceBundle;
13
 
14
import org.apache.log4j.Logger;
15
import org.apache.struts2.convention.annotation.InterceptorRef;
16
import org.apache.struts2.convention.annotation.InterceptorRefs;
17
import org.apache.struts2.convention.annotation.Result;
18
import org.apache.struts2.convention.annotation.Results;
19
 
20
@SuppressWarnings("serial")
21
@InterceptorRefs({
22
    @InterceptorRef("myDefault"),
23
    @InterceptorRef("login")
24
})
25
 
26
@Results({
27
    @Result(name="shipping-redirect", type="redirectAction", params = {"actionName" , "shipping"})
28
})
29
 
30
public class ProceedToPayController extends BaseController {
31
 
32
    private static Logger logger = Logger.getLogger(ProceedToPayController.class);
33
 
34
    private static final ResourceBundle resource = ResourceBundle.getBundle(ProceedToPayController.class.getName());
35
    private static final boolean SHOW_EBS_TEST_GATEWAY = Boolean.parseBoolean(resource.getString("show_ebs_test_gateway"));
36
 
37
    private long addressId = -1;
38
    private String totalAmount = "";
39
    private boolean showCodOption = true;
3616 chandransh 40
    private boolean showEmiOption = false;
3101 chandransh 41
    private String errorMsg = "";
42
 
43
    public String index(){
44
        return processPaymentOptions();
45
    }
46
 
47
    public String create(){
48
        String addressIdString = this.request.getParameter("addressid");
49
        if(addressIdString == null){
50
            addActionError("Please specify shipping address to continue.");
51
            return "shipping-redirect";
52
        }
53
 
54
        try {
55
            addressId = Long.parseLong(addressIdString);
56
        } catch(NumberFormatException nfe) {
57
            logger.error("Unable to parse address id", nfe);
58
            addActionError("Please specify shipping address to continue.");
59
            return "shipping-redirect";
60
        }
61
        return processPaymentOptions();
62
    }
63
 
64
    private String processPaymentOptions() {
3616 chandransh 65
        String showEmiOptionStr = this.request.getParameter("showEmiOption");
66
        if(showEmiOptionStr!=null){
67
            try{
68
                showEmiOption = Boolean.parseBoolean(showEmiOptionStr);
69
            }catch(Exception e){
70
                logger.info("A non-boolean value passed for showing EMI option");
71
            }
72
        }
73
 
3126 rajveer 74
        UserClient userContextServiceClient = null;
3101 chandransh 75
        in.shop2020.model.v1.user.UserContextService.Client userClient = null;
76
 
77
        Address address;
3209 vikas 78
        String itemIdString = "";
3101 chandransh 79
        try {
3126 rajveer 80
            userContextServiceClient = new UserClient();
3101 chandransh 81
            userClient = userContextServiceClient.getClient();
82
 
83
            long cartId = userinfo.getCartId();
5614 rajveer 84
 
3101 chandransh 85
            // Validate the cart to ensure that we are not accepting payment for
86
            // an invalid order.
3561 rajveer 87
            errorMsg = userClient.validateCart(cartId, sourceId);
3101 chandransh 88
 
89
            Cart cart = userClient.getCart(cartId);
4516 varun.gupt 90
 
91
            String couponCode = cart.getCouponCode();
92
            logger.info("Coupon: " + couponCode);
93
 
3101 chandransh 94
            setTotalAmount(cart);
3209 vikas 95
            itemIdString = Utils.getItemIdStringInCart(cart);
3101 chandransh 96
 
97
 
98
            // Get the address to check if COD option is available for this
99
            // address.
100
            if(addressId == -1){
101
                addressId = cart.getAddressId();
102
            }
103
            address = userClient.getAddressById(addressId);
4668 varun.gupt 104
 
5614 rajveer 105
            try {
106
                showCodOption = userClient.showCODOption(cartId, sourceId, address.getPin());
107
            } catch(Exception e) {
108
                logger.error("Error while checking if COD is available for: " + showCodOption, e);
109
                showCodOption = false; //Not a critical error, proceeding with defensive behaviour.
4668 varun.gupt 110
            }
111
 
3209 vikas 112
            DataLogger.logData(EventType.PROCEED_TO_PAY, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
113
                    Long.toString(cartId), itemIdString);
3101 chandransh 114
        } catch(Exception e) {
115
            logger.error("Error while either validating the cart or getting the address", e);
116
            addActionError("We are experiencing some problem. Please try again.");
117
            return "shipping-redirect";
118
        }
119
 
120
        Collection<String> actionErrors = getActionErrors();
121
        if(actionErrors != null && !actionErrors.isEmpty()){
122
            for (String str : actionErrors) {
123
                errorMsg += "<BR/>" + str;
124
            }
125
        }
126
 
127
        return "index";
128
    }
129
 
130
    private void setTotalAmount(Cart cart) {
131
        FormattingUtils formattingUtils = new FormattingUtils();
132
        String couponCode = cart.getCouponCode();
133
        if(couponCode == null || "".equals(couponCode))
134
            totalAmount = formattingUtils.formatPrice(cart.getTotalPrice());
135
        else
136
            totalAmount = formattingUtils.formatPrice(cart.getDiscountedPrice());
137
    }
138
 
139
    public long getAddressId(){
140
        return this.addressId;
141
    }
142
 
143
    public String getErrorMsg(){
4815 phani.kuma 144
    	logger.info("added error msg:" + this.errorMsg);
3101 chandransh 145
        return this.errorMsg;
146
    }
147
 
148
    public boolean shouldShowCodOption() {
149
        return showCodOption;
150
    }
151
 
3616 chandransh 152
    public boolean shouldShowEmiOption() {
153
        return showEmiOption;
154
    }
155
 
3101 chandransh 156
    public String getTotalAmount(){
157
        return totalAmount;
158
    }
159
 
160
    public boolean shouldShowEbsTestGateway() {
161
        return SHOW_EBS_TEST_GATEWAY;
162
    }
163
 
3903 varun.gupt 164
	@Override
165
	public String getHeaderSnippet() {
166
		String url = request.getQueryString();
167
		if (url == null) {
168
			url = "";
169
		} else {
170
			url = "?" + url;
171
		}
172
		url = request.getRequestURI() + url;
4453 varun.gupt 173
		return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(), url , 0, false);
3903 varun.gupt 174
	}
175
}