Subversion Repositories SmartDukaan

Rev

Rev 3903 | Rev 4453 | 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
 
99
            Cart cart = userClient.getCart(cartId);
100
            setTotalAmount(cart);
3209 vikas 101
            itemIdString = Utils.getItemIdStringInCart(cart);
3101 chandransh 102
 
103
            //Get the line items to check if COD option should be shown.
104
            lineItems = cart.getLines();
105
 
106
            // Get the address to check if COD option is available for this
107
            // address.
108
            if(addressId == -1){
109
                addressId = cart.getAddressId();
110
            }
111
            address = userClient.getAddressById(addressId);
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
        try {
3126 rajveer 121
            CatalogClient catalogServiceClient  = new CatalogClient();
3101 chandransh 122
            in.shop2020.model.v1.catalog.InventoryService.Client catalogClient = catalogServiceClient.getClient();
123
            for (Line line : lineItems) {
3561 rajveer 124
                Item item = catalogClient.getItemForSource(line.getItemId(), sourceId);
4392 rajveer 125
                if(item.getSellingPrice() > COD_MAX_AMOUNT || item.getSellingPrice() < COD_MIN_AMOUNT )
3101 chandransh 126
                    showCodOption = false;    
127
            }
128
        } catch(Exception e) {
129
            logger.error("Error while getting item info from the catalog service", e);
130
            addActionError("We are experiencing some problem. Please try again.");
131
            return "shipping-redirect";
132
        }
133
 
134
        try {
3126 rajveer 135
            LogisticsClient lClient = new LogisticsClient();
3101 chandransh 136
            showCodOption = showCodOption && lClient.getClient().isCodAllowed(address.getPin());
137
        } catch(Exception e) {
138
            logger.error("Error while checking if COD is available for: " + showCodOption, e);
139
            showCodOption = false; //Not a critical error, proceeding with defensive behaviour.
140
        }
141
 
142
        Collection<String> actionErrors = getActionErrors();
143
        if(actionErrors != null && !actionErrors.isEmpty()){
144
            for (String str : actionErrors) {
145
                errorMsg += "<BR/>" + str;
146
            }
147
        }
148
 
149
        return "index";
150
    }
151
 
152
    private void setTotalAmount(Cart cart) {
153
        FormattingUtils formattingUtils = new FormattingUtils();
154
        String couponCode = cart.getCouponCode();
155
        if(couponCode == null || "".equals(couponCode))
156
            totalAmount = formattingUtils.formatPrice(cart.getTotalPrice());
157
        else
158
            totalAmount = formattingUtils.formatPrice(cart.getDiscountedPrice());
159
    }
160
 
161
    public long getAddressId(){
162
        return this.addressId;
163
    }
164
 
165
    public String getErrorMsg(){
166
        return this.errorMsg;
167
    }
168
 
169
    public boolean shouldShowCodOption() {
170
        return showCodOption;
171
    }
172
 
3616 chandransh 173
    public boolean shouldShowEmiOption() {
174
        return showEmiOption;
175
    }
176
 
3101 chandransh 177
    public String getTotalAmount(){
178
        return totalAmount;
179
    }
180
 
181
    public boolean shouldShowEbsTestGateway() {
182
        return SHOW_EBS_TEST_GATEWAY;
183
    }
184
 
185
    public String getPublicKey(){
186
        return RECAPTCHA_PUBLIC_KEY;
187
    }
3903 varun.gupt 188
 
189
	@Override
190
	public String getHeaderSnippet() {
191
		String url = request.getQueryString();
192
		if (url == null) {
193
			url = "";
194
		} else {
195
			url = "?" + url;
196
		}
197
		url = request.getRequestURI() + url;
198
		return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getNameOfUser(), url , 0, false);
199
	}
200
}