Subversion Repositories SmartDukaan

Rev

Rev 4882 | Rev 5613 | 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
 
92
            if (deliveryLocation.equals("HotSpot")) {
93
                addressId = hotSpotAddressId ;
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
 
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.
121
            }
122
 
5572 anupam.sin 123
 
124
 
3209 vikas 125
            DataLogger.logData(EventType.PROCEED_TO_PAY, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
126
                    Long.toString(cartId), itemIdString);
3101 chandransh 127
        } catch(Exception e) {
128
            logger.error("Error while either validating the cart or getting the address", e);
129
            addActionError("We are experiencing some problem. Please try again.");
130
            return "shipping-redirect";
131
        }
132
 
133
        Collection<String> actionErrors = getActionErrors();
134
        if(actionErrors != null && !actionErrors.isEmpty()){
135
            for (String str : actionErrors) {
136
                errorMsg += "<BR/>" + str;
137
            }
138
        }
139
 
140
        return "index";
141
    }
142
 
143
    private void setTotalAmount(Cart cart) {
144
        FormattingUtils formattingUtils = new FormattingUtils();
145
        String couponCode = cart.getCouponCode();
146
        if(couponCode == null || "".equals(couponCode))
147
            totalAmount = formattingUtils.formatPrice(cart.getTotalPrice());
148
        else
149
            totalAmount = formattingUtils.formatPrice(cart.getDiscountedPrice());
150
    }
151
 
152
    public long getAddressId(){
153
        return this.addressId;
154
    }
155
 
156
    public String getErrorMsg(){
4815 phani.kuma 157
    	logger.info("added error msg:" + this.errorMsg);
3101 chandransh 158
        return this.errorMsg;
159
    }
160
 
161
    public boolean shouldShowCodOption() {
162
        return showCodOption;
163
    }
164
 
3616 chandransh 165
    public boolean shouldShowEmiOption() {
166
        return showEmiOption;
167
    }
168
 
3101 chandransh 169
    public String getTotalAmount(){
170
        return totalAmount;
171
    }
172
 
173
    public boolean shouldShowEbsTestGateway() {
174
        return SHOW_EBS_TEST_GATEWAY;
175
    }
176
 
3903 varun.gupt 177
	@Override
178
	public String getHeaderSnippet() {
179
		String url = request.getQueryString();
180
		if (url == null) {
181
			url = "";
182
		} else {
183
			url = "?" + url;
184
		}
185
		url = request.getRequestURI() + url;
4453 varun.gupt 186
		return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(), url , 0, false);
3903 varun.gupt 187
	}
5572 anupam.sin 188
 
189
    public String getDeliveryLocation() {
190
        return deliveryLocation;
191
    }
192
 
193
    public void setDeliveryLocation(String deliveryLocation) {
194
        this.deliveryLocation = deliveryLocation;
195
    }
196
 
197
    public long getHotSpotAddressId() {
198
        return hotSpotAddressId;
199
    }
200
 
201
    public void setHotSpotAddressId(long hotSpotAddressId) {
202
        this.hotSpotAddressId = hotSpotAddressId;
203
    }
204
 
205
    public boolean shouldShowStorePickUpOption() {
206
        return showStorePickUpOption;
207
    }
3903 varun.gupt 208
}