Subversion Repositories SmartDukaan

Rev

Rev 5716 | Rev 6152 | 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 = "";
5716 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
 
5716 anupam.sin 86
 
3101 chandransh 87
            long cartId = userinfo.getCartId();
5716 anupam.sin 88
            if(deliveryLocation.equals("myLocation")) {
89
                userClient.addStoreToCart(cartId, 0);
90
            }
91
 
92
            if (deliveryLocation.equals("HotSpot")) {
5725 anupam.sin 93
                userClient.addStoreToCart(cartId, hotSpotAddressId);
5716 anupam.sin 94
                showStorePickUpOption = true;
95
            }
3101 chandransh 96
            // Validate the cart to ensure that we are not accepting payment for
97
            // an invalid order.
3561 rajveer 98
            errorMsg = userClient.validateCart(cartId, sourceId);
3101 chandransh 99
 
100
            Cart cart = userClient.getCart(cartId);
4516 varun.gupt 101
 
102
            String couponCode = cart.getCouponCode();
103
            logger.info("Coupon: " + couponCode);
104
 
3101 chandransh 105
            setTotalAmount(cart);
3209 vikas 106
            itemIdString = Utils.getItemIdStringInCart(cart);
3101 chandransh 107
 
108
 
109
            // Get the address to check if COD option is available for this
110
            // address.
111
            if(addressId == -1){
112
                addressId = cart.getAddressId();
113
            }
114
            address = userClient.getAddressById(addressId);
4668 varun.gupt 115
 
5614 rajveer 116
            try {
117
                showCodOption = userClient.showCODOption(cartId, sourceId, address.getPin());
118
            } catch(Exception e) {
119
                logger.error("Error while checking if COD is available for: " + showCodOption, e);
120
                showCodOption = false; //Not a critical error, proceeding with defensive behaviour.
4668 varun.gupt 121
            }
122
 
3209 vikas 123
            DataLogger.logData(EventType.PROCEED_TO_PAY, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
124
                    Long.toString(cartId), itemIdString);
3101 chandransh 125
        } catch(Exception e) {
126
            logger.error("Error while either validating the cart or getting the address", e);
127
            addActionError("We are experiencing some problem. Please try again.");
128
            return "shipping-redirect";
129
        }
130
 
131
        Collection<String> actionErrors = getActionErrors();
132
        if(actionErrors != null && !actionErrors.isEmpty()){
133
            for (String str : actionErrors) {
134
                errorMsg += "<BR/>" + str;
135
            }
136
        }
137
 
138
        return "index";
139
    }
140
 
141
    private void setTotalAmount(Cart cart) {
142
        FormattingUtils formattingUtils = new FormattingUtils();
143
        String couponCode = cart.getCouponCode();
144
        if(couponCode == null || "".equals(couponCode))
145
            totalAmount = formattingUtils.formatPrice(cart.getTotalPrice());
146
        else
147
            totalAmount = formattingUtils.formatPrice(cart.getDiscountedPrice());
148
    }
149
 
150
    public long getAddressId(){
151
        return this.addressId;
152
    }
153
 
154
    public String getErrorMsg(){
4815 phani.kuma 155
    	logger.info("added error msg:" + this.errorMsg);
3101 chandransh 156
        return this.errorMsg;
157
    }
158
 
159
    public boolean shouldShowCodOption() {
160
        return showCodOption;
161
    }
162
 
3616 chandransh 163
    public boolean shouldShowEmiOption() {
164
        return showEmiOption;
165
    }
166
 
3101 chandransh 167
    public String getTotalAmount(){
168
        return totalAmount;
169
    }
170
 
171
    public boolean shouldShowEbsTestGateway() {
172
        return SHOW_EBS_TEST_GATEWAY;
173
    }
174
 
3903 varun.gupt 175
	@Override
176
	public String getHeaderSnippet() {
177
		String url = request.getQueryString();
178
		if (url == null) {
179
			url = "";
180
		} else {
181
			url = "?" + url;
182
		}
183
		url = request.getRequestURI() + url;
4453 varun.gupt 184
		return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(), url , 0, false);
3903 varun.gupt 185
	}
5716 anupam.sin 186
 
187
    public String getDeliveryLocation() {
188
        return deliveryLocation;
189
    }
190
 
191
    public void setDeliveryLocation(String deliveryLocation) {
192
        this.deliveryLocation = deliveryLocation;
193
    }
194
 
195
    public long getHotSpotAddressId() {
196
        return hotSpotAddressId;
197
    }
198
 
199
    public void setHotSpotAddressId(long hotSpotAddressId) {
200
        this.hotSpotAddressId = hotSpotAddressId;
201
    }
202
 
203
    public boolean shouldShowStorePickUpOption() {
204
        return showStorePickUpOption;
205
    }
3903 varun.gupt 206
}