Subversion Repositories SmartDukaan

Rev

Rev 3101 | Rev 3209 | 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
 
3
import in.shop2020.model.v1.catalog.Item;
4
import in.shop2020.model.v1.user.Address;
5
import in.shop2020.model.v1.user.Cart;
6
import in.shop2020.model.v1.user.Line;
7
import in.shop2020.serving.utils.FormattingUtils;
3126 rajveer 8
import in.shop2020.thrift.clients.CatalogClient;
9
import in.shop2020.thrift.clients.LogisticsClient;
10
import in.shop2020.thrift.clients.UserClient;
3101 chandransh 11
 
12
import java.util.Collection;
13
import java.util.List;
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
 
36
    private static final ResourceBundle resource = ResourceBundle.getBundle(ProceedToPayController.class.getName());
37
    private static final double COD_CUTOFF_AMOUNT = Double.parseDouble(resource.getString("cod_cutoff_amount")); 
38
    private static final String RECAPTCHA_PUBLIC_KEY = resource.getString("recaptcha_public_key");
39
    private static final boolean SHOW_EBS_TEST_GATEWAY = Boolean.parseBoolean(resource.getString("show_ebs_test_gateway"));
40
 
41
    private long addressId = -1;
42
    private String totalAmount = "";
43
    private boolean showCodOption = true;
44
    private String errorMsg = "";
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() {
3126 rajveer 68
        UserClient userContextServiceClient = null;
3101 chandransh 69
        in.shop2020.model.v1.user.UserContextService.Client userClient = null;
70
 
71
        Address address;
72
        List<Line> lineItems;
73
 
74
        try {
3126 rajveer 75
            userContextServiceClient = new UserClient();
3101 chandransh 76
            userClient = userContextServiceClient.getClient();
77
 
78
            long cartId = userinfo.getCartId();
79
 
80
            // Validate the cart to ensure that we are not accepting payment for
81
            // an invalid order.
82
            errorMsg = userClient.validateCart(cartId);
83
 
84
 
85
            Cart cart = userClient.getCart(cartId);
86
            setTotalAmount(cart);
87
 
88
            //Get the line items to check if COD option should be shown.
89
            lineItems = cart.getLines();
90
 
91
            // Get the address to check if COD option is available for this
92
            // address.
93
            if(addressId == -1){
94
                addressId = cart.getAddressId();
95
            }
96
            address = userClient.getAddressById(addressId);
97
        } catch(Exception e) {
98
            logger.error("Error while either validating the cart or getting the address", e);
99
            addActionError("We are experiencing some problem. Please try again.");
100
            return "shipping-redirect";
101
        }
102
 
103
        try {
3126 rajveer 104
            CatalogClient catalogServiceClient  = new CatalogClient();
3101 chandransh 105
            in.shop2020.model.v1.catalog.InventoryService.Client catalogClient = catalogServiceClient.getClient();
106
            for (Line line : lineItems) {
107
                Item item = catalogClient.getItem(line.getItemId());
108
                if(item.getSellingPrice() > COD_CUTOFF_AMOUNT)
109
                    showCodOption = false;    
110
            }
111
        } catch(Exception e) {
112
            logger.error("Error while getting item info from the catalog service", e);
113
            addActionError("We are experiencing some problem. Please try again.");
114
            return "shipping-redirect";
115
        }
116
 
117
        try {
3126 rajveer 118
            LogisticsClient lClient = new LogisticsClient();
3101 chandransh 119
            showCodOption = showCodOption && lClient.getClient().isCodAllowed(address.getPin());
120
        } catch(Exception e) {
121
            logger.error("Error while checking if COD is available for: " + showCodOption, e);
122
            showCodOption = false; //Not a critical error, proceeding with defensive behaviour.
123
        }
124
 
125
        Collection<String> actionErrors = getActionErrors();
126
        if(actionErrors != null && !actionErrors.isEmpty()){
127
            for (String str : actionErrors) {
128
                errorMsg += "<BR/>" + str;
129
            }
130
        }
131
 
132
        return "index";
133
    }
134
 
135
    private void setTotalAmount(Cart cart) {
136
        FormattingUtils formattingUtils = new FormattingUtils();
137
        String couponCode = cart.getCouponCode();
138
        if(couponCode == null || "".equals(couponCode))
139
            totalAmount = formattingUtils.formatPrice(cart.getTotalPrice());
140
        else
141
            totalAmount = formattingUtils.formatPrice(cart.getDiscountedPrice());
142
    }
143
 
144
    public long getAddressId(){
145
        return this.addressId;
146
    }
147
 
148
    public String getErrorMsg(){
149
        return this.errorMsg;
150
    }
151
 
152
    public boolean shouldShowCodOption() {
153
        return showCodOption;
154
    }
155
 
156
    public String getTotalAmount(){
157
        return totalAmount;
158
    }
159
 
160
    public boolean shouldShowEbsTestGateway() {
161
        return SHOW_EBS_TEST_GATEWAY;
162
    }
163
 
164
    public String getPublicKey(){
165
        return RECAPTCHA_PUBLIC_KEY;
166
    }
167
}