Subversion Repositories SmartDukaan

Rev

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