Subversion Repositories SmartDukaan

Rev

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