Subversion Repositories SmartDukaan

Rev

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