Subversion Repositories SmartDukaan

Rev

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