Subversion Repositories SmartDukaan

Rev

Rev 3209 | Rev 3616 | 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;
47
    private String errorMsg = "";
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() {
3126 rajveer 71
        UserClient userContextServiceClient = null;
3101 chandransh 72
        in.shop2020.model.v1.user.UserContextService.Client userClient = null;
73
 
74
        Address address;
75
        List<Line> lineItems;
3209 vikas 76
        String itemIdString = "";
3101 chandransh 77
        try {
3126 rajveer 78
            userContextServiceClient = new UserClient();
3101 chandransh 79
            userClient = userContextServiceClient.getClient();
80
 
81
            long cartId = userinfo.getCartId();
82
 
83
            // Validate the cart to ensure that we are not accepting payment for
84
            // an invalid order.
3561 rajveer 85
            errorMsg = userClient.validateCart(cartId, sourceId);
3101 chandransh 86
 
87
 
88
            Cart cart = userClient.getCart(cartId);
89
            setTotalAmount(cart);
3209 vikas 90
            itemIdString = Utils.getItemIdStringInCart(cart);
3101 chandransh 91
 
92
            //Get the line items to check if COD option should be shown.
93
            lineItems = cart.getLines();
94
 
95
            // Get the address to check if COD option is available for this
96
            // address.
97
            if(addressId == -1){
98
                addressId = cart.getAddressId();
99
            }
100
            address = userClient.getAddressById(addressId);
3209 vikas 101
            DataLogger.logData(EventType.PROCEED_TO_PAY, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
102
                    Long.toString(cartId), itemIdString);
3101 chandransh 103
        } catch(Exception e) {
104
            logger.error("Error while either validating the cart or getting the address", e);
105
            addActionError("We are experiencing some problem. Please try again.");
106
            return "shipping-redirect";
107
        }
108
 
109
        try {
3126 rajveer 110
            CatalogClient catalogServiceClient  = new CatalogClient();
3101 chandransh 111
            in.shop2020.model.v1.catalog.InventoryService.Client catalogClient = catalogServiceClient.getClient();
112
            for (Line line : lineItems) {
3561 rajveer 113
                Item item = catalogClient.getItemForSource(line.getItemId(), sourceId);
3101 chandransh 114
                if(item.getSellingPrice() > COD_CUTOFF_AMOUNT)
115
                    showCodOption = false;    
116
            }
117
        } catch(Exception e) {
118
            logger.error("Error while getting item info from the catalog service", e);
119
            addActionError("We are experiencing some problem. Please try again.");
120
            return "shipping-redirect";
121
        }
122
 
123
        try {
3126 rajveer 124
            LogisticsClient lClient = new LogisticsClient();
3101 chandransh 125
            showCodOption = showCodOption && lClient.getClient().isCodAllowed(address.getPin());
126
        } catch(Exception e) {
127
            logger.error("Error while checking if COD is available for: " + showCodOption, e);
128
            showCodOption = false; //Not a critical error, proceeding with defensive behaviour.
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(){
155
        return this.errorMsg;
156
    }
157
 
158
    public boolean shouldShowCodOption() {
159
        return showCodOption;
160
    }
161
 
162
    public String getTotalAmount(){
163
        return totalAmount;
164
    }
165
 
166
    public boolean shouldShowEbsTestGateway() {
167
        return SHOW_EBS_TEST_GATEWAY;
168
    }
169
 
170
    public String getPublicKey(){
171
        return RECAPTCHA_PUBLIC_KEY;
172
    }
173
}