Subversion Repositories SmartDukaan

Rev

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