Subversion Repositories SmartDukaan

Rev

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