Subversion Repositories SmartDukaan

Rev

Rev 6903 | Rev 7862 | 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;
6390 rajveer 4
import in.shop2020.model.v1.order.EmiScheme;
5
import in.shop2020.model.v1.order.TransactionService.Client;
3101 chandransh 6
import in.shop2020.model.v1.user.Address;
7
import in.shop2020.model.v1.user.Cart;
6302 amit.gupta 8
import in.shop2020.model.v1.user.PromotionService;
3101 chandransh 9
import in.shop2020.serving.utils.FormattingUtils;
3209 vikas 10
import in.shop2020.serving.utils.Utils;
6302 amit.gupta 11
import in.shop2020.thrift.clients.PromotionClient;
6390 rajveer 12
import in.shop2020.thrift.clients.TransactionClient;
3126 rajveer 13
import in.shop2020.thrift.clients.UserClient;
3209 vikas 14
import in.shop2020.utils.DataLogger;
3101 chandransh 15
 
6407 amit.gupta 16
import java.util.ArrayList;
3101 chandransh 17
import java.util.Collection;
7853 amit.gupta 18
import java.util.HashMap;
6390 rajveer 19
import java.util.List;
7853 amit.gupta 20
import java.util.Map;
3101 chandransh 21
import java.util.ResourceBundle;
22
 
6309 amit.gupta 23
import org.apache.commons.lang.StringUtils;
3101 chandransh 24
import org.apache.log4j.Logger;
25
import org.apache.struts2.convention.annotation.InterceptorRef;
26
import org.apache.struts2.convention.annotation.InterceptorRefs;
27
import org.apache.struts2.convention.annotation.Result;
28
import org.apache.struts2.convention.annotation.Results;
6390 rajveer 29
import org.apache.thrift.TException;
30
import org.apache.thrift.transport.TTransportException;
3101 chandransh 31
 
6407 amit.gupta 32
import com.google.gson.Gson;
33
 
3101 chandransh 34
@SuppressWarnings("serial")
35
@InterceptorRefs({
36
    @InterceptorRef("myDefault"),
37
    @InterceptorRef("login")
38
})
39
 
40
@Results({
41
    @Result(name="shipping-redirect", type="redirectAction", params = {"actionName" , "shipping"})
42
})
43
 
44
public class ProceedToPayController extends BaseController {
45
 
46
    private static Logger logger = Logger.getLogger(ProceedToPayController.class);
47
    private static final ResourceBundle resource = ResourceBundle.getBundle(ProceedToPayController.class.getName());
48
    private static final boolean SHOW_EBS_TEST_GATEWAY = Boolean.parseBoolean(resource.getString("show_ebs_test_gateway"));
49
 
6390 rajveer 50
    private static List<EmiScheme> emiSchemes;
7853 amit.gupta 51
    private static String emiSchemesInJSON;
6302 amit.gupta 52
    private boolean hasGiftVoucher = false;
3101 chandransh 53
    private long addressId = -1;
54
    private String totalAmount = "";
6407 amit.gupta 55
    private double totalAmountD = 0l;
3101 chandransh 56
    private boolean showCodOption = true;
3616 chandransh 57
    private boolean showEmiOption = false;
3101 chandransh 58
    private String errorMsg = "";
5716 anupam.sin 59
    private String deliveryLocation = ""; //This could be set as myLocation or HotSpot
60
    private boolean showStorePickUpOption = false;
61
    private long hotSpotAddressId = 1;
3101 chandransh 62
 
6407 amit.gupta 63
 
3101 chandransh 64
    public String index(){
65
        return processPaymentOptions();
66
    }
67
 
68
    public String create(){
69
        String addressIdString = this.request.getParameter("addressid");
70
        if(addressIdString == null){
71
            addActionError("Please specify shipping address to continue.");
72
            return "shipping-redirect";
73
        }
74
 
75
        try {
76
            addressId = Long.parseLong(addressIdString);
77
        } catch(NumberFormatException nfe) {
78
            logger.error("Unable to parse address id", nfe);
79
            addActionError("Please specify shipping address to continue.");
80
            return "shipping-redirect";
81
        }
82
        return processPaymentOptions();
83
    }
84
 
85
    private String processPaymentOptions() {
6390 rajveer 86
    	if(emiSchemes == null){
87
    		populateEmiSchemes();
88
    	}
89
 
3616 chandransh 90
        String showEmiOptionStr = this.request.getParameter("showEmiOption");
91
        if(showEmiOptionStr!=null){
92
            try{
93
                showEmiOption = Boolean.parseBoolean(showEmiOptionStr);
94
            }catch(Exception e){
95
                logger.info("A non-boolean value passed for showing EMI option");
96
            }
97
        }
98
 
3126 rajveer 99
        UserClient userContextServiceClient = null;
3101 chandransh 100
        in.shop2020.model.v1.user.UserContextService.Client userClient = null;
101
 
102
        Address address;
3209 vikas 103
        String itemIdString = "";
3101 chandransh 104
        try {
3126 rajveer 105
            userContextServiceClient = new UserClient();
3101 chandransh 106
            userClient = userContextServiceClient.getClient();
107
 
108
            long cartId = userinfo.getCartId();
5716 anupam.sin 109
            if(deliveryLocation.equals("myLocation")) {
110
                userClient.addStoreToCart(cartId, 0);
111
            }
3101 chandransh 112
            // Validate the cart to ensure that we are not accepting payment for
113
            // an invalid order.
6736 amit.gupta 114
            errorMsg = userClient.validateCart(cartId, sourceId).get(0);
3101 chandransh 115
 
116
            Cart cart = userClient.getCart(cartId);
4516 varun.gupt 117
            String couponCode = cart.getCouponCode();
118
            logger.info("Coupon: " + couponCode);
119
 
6302 amit.gupta 120
            PromotionService.Client pc = new PromotionClient().getClient();
6309 amit.gupta 121
            if(StringUtils.isNotEmpty(couponCode) && pc.isGiftVoucher(couponCode)){
6302 amit.gupta 122
            	hasGiftVoucher = true;
123
            }
124
 
125
            if (!hasGiftVoucher && deliveryLocation.equals("HotSpot")) {
126
            	userClient.addStoreToCart(cartId, hotSpotAddressId);
127
            	showStorePickUpOption = true;
128
            }
3101 chandransh 129
            setTotalAmount(cart);
3209 vikas 130
            itemIdString = Utils.getItemIdStringInCart(cart);
3101 chandransh 131
 
132
 
133
            // Get the address to check if COD option is available for this
134
            // address.
135
            if(addressId == -1){
136
                addressId = cart.getAddressId();
137
            }
138
            address = userClient.getAddressById(addressId);
4668 varun.gupt 139
 
5614 rajveer 140
            try {
6356 amit.gupta 141
                showCodOption = userClient.showCODOption(cartId, sourceId, address.getPin());
5614 rajveer 142
            } catch(Exception e) {
143
                logger.error("Error while checking if COD is available for: " + showCodOption, e);
144
                showCodOption = false; //Not a critical error, proceeding with defensive behaviour.
4668 varun.gupt 145
            }
146
 
6353 rajveer 147
            showStorePickUpOption = showStorePickUpOption && showCodOption;
148
 
3209 vikas 149
            DataLogger.logData(EventType.PROCEED_TO_PAY, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
150
                    Long.toString(cartId), itemIdString);
3101 chandransh 151
        } catch(Exception e) {
152
            logger.error("Error while either validating the cart or getting the address", e);
153
            addActionError("We are experiencing some problem. Please try again.");
154
            return "shipping-redirect";
155
        }
156
 
157
        Collection<String> actionErrors = getActionErrors();
158
        if(actionErrors != null && !actionErrors.isEmpty()){
159
            for (String str : actionErrors) {
6903 anupam.sin 160
                errorMsg += str + "<BR/>";
3101 chandransh 161
            }
162
        }
163
 
164
        return "index";
165
    }
166
 
7853 amit.gupta 167
    public static void populateEmiSchemes(){
6390 rajveer 168
    	try {
169
			Client tClient = new TransactionClient().getClient();
170
			emiSchemes = tClient.getAvailableEmiSchemes();
171
		} catch (TTransportException e) {
172
			logger.error("Error while getting EMI schemes: ", e);
173
		} catch (TException e) {
174
			logger.error("Error while getting EMI schemes: ", e);
175
		}
176
    }
177
 
3101 chandransh 178
    private void setTotalAmount(Cart cart) {
6407 amit.gupta 179
    	String couponCode = cart.getCouponCode();
3101 chandransh 180
        if(couponCode == null || "".equals(couponCode))
6407 amit.gupta 181
        	totalAmountD = cart.getTotalPrice();
3101 chandransh 182
        else
6407 amit.gupta 183
            totalAmountD = cart.getDiscountedPrice();
184
 
185
        FormattingUtils formattingUtils = new FormattingUtils();
186
        totalAmount = formattingUtils.formatPrice(totalAmountD);
3101 chandransh 187
    }
188
 
189
    public long getAddressId(){
190
        return this.addressId;
191
    }
192
 
193
    public String getErrorMsg(){
4815 phani.kuma 194
    	logger.info("added error msg:" + this.errorMsg);
3101 chandransh 195
        return this.errorMsg;
196
    }
197
 
198
    public boolean shouldShowCodOption() {
199
        return showCodOption;
200
    }
201
 
3616 chandransh 202
    public boolean shouldShowEmiOption() {
203
        return showEmiOption;
204
    }
205
 
3101 chandransh 206
    public String getTotalAmount(){
207
        return totalAmount;
208
    }
209
 
6407 amit.gupta 210
    public double getTotalAmountL(){
211
    	return totalAmountD;
212
    }
213
 
3101 chandransh 214
    public boolean shouldShowEbsTestGateway() {
215
        return SHOW_EBS_TEST_GATEWAY;
216
    }
217
 
3903 varun.gupt 218
	@Override
219
	public String getHeaderSnippet() {
220
		String url = request.getQueryString();
221
		if (url == null) {
222
			url = "";
223
		} else {
224
			url = "?" + url;
225
		}
226
		url = request.getRequestURI() + url;
6152 amit.gupta 227
		return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(), userinfo.getTotalItems(), url , 0, false);
3903 varun.gupt 228
	}
5716 anupam.sin 229
 
230
    public String getDeliveryLocation() {
231
        return deliveryLocation;
232
    }
233
 
234
    public void setDeliveryLocation(String deliveryLocation) {
235
        this.deliveryLocation = deliveryLocation;
236
    }
237
 
238
    public long getHotSpotAddressId() {
239
        return hotSpotAddressId;
240
    }
241
 
242
    public void setHotSpotAddressId(long hotSpotAddressId) {
243
        this.hotSpotAddressId = hotSpotAddressId;
244
    }
245
 
246
    public boolean shouldShowStorePickUpOption() {
247
        return showStorePickUpOption;
248
    }
6390 rajveer 249
 
6407 amit.gupta 250
    public String getJSONEmiSchemes(){
251
    	getEmiSchemes();
252
    	Double totalAmount = getTotalAmountL();
253
    	List<EmiScheme> schemes = new ArrayList<EmiScheme>();
254
    	for(EmiScheme emiScheme : emiSchemes) {
255
    		if(emiScheme.getMinAmount() <= totalAmount ) {
256
    			schemes.add(emiScheme);
257
    		}
258
    	}
259
    	Gson gson  = new Gson();
260
    	return gson.toJson(schemes);
261
    }
262
 
6736 amit.gupta 263
    public String getJSONEmiDiscountSchemes(){
264
    	try {
265
    	PromotionService.Client pc = new PromotionClient().getClient();
266
    	Gson gson  = new Gson();
267
    	return gson.toJson(pc.getEmiDiscount(userinfo.getCartId()));
268
    	}catch (Exception e){
269
    		logger.warn("Could not retrive emi based promotions for cart: " + userinfo.getCartId() + ".\n" + e);
270
    		return "{}";
271
    	}
272
    }
273
 
7853 amit.gupta 274
    public static List<EmiScheme> getEmiSchemes(){
275
    	if (emiSchemes != null) { 
276
    		return emiSchemes;
277
    	} else {
278
    		populateEmiSchemes();
279
    		return emiSchemes;
280
    	}
6390 rajveer 281
    }
282
 
283
    public static long getGatewayId(long emiSchemeId){
284
    	for(EmiScheme scheme: emiSchemes){
285
    		if(scheme.getId() == emiSchemeId){
286
    			return scheme.getGatewayId();
287
    		}
288
    	}
289
    	return 0;
290
    }
6442 rajveer 291
 
292
    public static double getInterestRate(long emiSchemeId){
293
    	for(EmiScheme scheme: emiSchemes){
294
    		if(scheme.getId() == emiSchemeId){
295
    			return scheme.getChargeValue();
296
    		}
297
    	}
298
    	return 0;
299
    }
7853 amit.gupta 300
 
301
    public static String getEmiSchemesInJSON(){
302
    	if(emiSchemesInJSON==null){
303
    		List<Map<String, Object>> schemeList = new ArrayList<Map<String,Object>>();
304
    		for(EmiScheme scheme: getEmiSchemes()){
305
    			Map<String,Object> arrayMap = new HashMap<String, Object>();
306
    			arrayMap.put("bankId",scheme.getBankId());
307
    			arrayMap.put("bankName",scheme.getBankName());
308
    			arrayMap.put("chargeType",scheme.getChargeType());
309
    			arrayMap.put("chargeValue",scheme.getChargeValue());
310
    			arrayMap.put("gatewayId",scheme.getGatewayId());
311
    			arrayMap.put("minAmount",scheme.getMinAmount());
312
    			arrayMap.put("tenure",scheme.getTenure());
313
    			arrayMap.put("tenureDescription",scheme.getTenureDescription());
314
    			arrayMap.put("id",scheme.getId());
315
    			schemeList.add(arrayMap);
316
    		}
317
    		return new Gson().toJson(schemeList);
318
    	} else {
319
    		return emiSchemesInJSON;
320
    	}
321
    }
3903 varun.gupt 322
}