Subversion Repositories SmartDukaan

Rev

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