Subversion Repositories SmartDukaan

Rev

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