Subversion Repositories SmartDukaan

Rev

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