Subversion Repositories SmartDukaan

Rev

Rev 2511 | Rev 2958 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
419 rajveer 1
package in.shop2020.serving.controllers;
2
 
2263 vikas 3
import in.shop2020.datalogger.EventType;
2183 vikas 4
import in.shop2020.model.v1.order.Order;
894 rajveer 5
import in.shop2020.model.v1.user.ShoppingCartException;
2817 vikas 6
import in.shop2020.serving.interceptors.TrackingInterceptor;
1905 chandransh 7
import in.shop2020.serving.services.EbsPaymentService;
1318 rajveer 8
import in.shop2020.serving.services.HdfcPaymentService;
1905 chandransh 9
import in.shop2020.serving.services.IPaymentService;
2817 vikas 10
import in.shop2020.serving.utils.DesEncrypter;
2183 vikas 11
import in.shop2020.thrift.clients.TransactionServiceClient;
517 rajveer 12
import in.shop2020.thrift.clients.UserContextServiceClient;
2511 vikas 13
import in.shop2020.utils.DataLogger;
517 rajveer 14
 
2419 vikas 15
import java.util.List;
16
 
2817 vikas 17
import javax.servlet.http.Cookie;
18
 
832 rajveer 19
import org.apache.log4j.Logger;
822 vikas 20
import org.apache.struts2.convention.annotation.InterceptorRef;
21
import org.apache.struts2.convention.annotation.InterceptorRefs;
419 rajveer 22
import org.apache.struts2.convention.annotation.Result;
23
import org.apache.struts2.convention.annotation.Results;
517 rajveer 24
import org.apache.thrift.TException;
419 rajveer 25
 
2159 chandransh 26
@SuppressWarnings("serial")
822 vikas 27
@InterceptorRefs({
28
    @InterceptorRef("myDefault"),
29
    @InterceptorRef("login")
30
})
31
 
419 rajveer 32
@Results({
693 rajveer 33
    @Result(name="shipping-redirect", type="redirectAction", 
1905 chandransh 34
    		params = {"actionName" , "shipping"}),
35
	@Result(name="ebs-pay-redirect", type="redirect", location="/ebs-pay/${paymentId}")
419 rajveer 36
})
650 rajveer 37
public class OrderController extends BaseController {
419 rajveer 38
 
1905 chandransh 39
	public long getPaymentId() {
40
		return paymentId;
41
	}
419 rajveer 42
 
832 rajveer 43
	private static Logger log = Logger.getLogger(Class.class);
507 rajveer 44
	private String id;
712 rajveer 45
	private long txnId = 0;
650 rajveer 46
 
1905 chandransh 47
	private long paymentId;
48
 
1318 rajveer 49
	private String redirectURL;
681 rajveer 50
 
419 rajveer 51
	public OrderController(){
507 rajveer 52
		super();
419 rajveer 53
	}
54
 
507 rajveer 55
    // GET /order/ orderid
56
    public String show() {
57
    	log.info("id=" + id);
822 vikas 58
    	htmlSnippets.put("MYACCOUNT_HEADER", pageLoader.getMyaccountHeaderHtml());
1527 ankur.sing 59
    	htmlSnippets.put("ORDER_DETAILS", pageLoader.getOrderDetailsHtml(Long.parseLong(id), userinfo.getUserId()));
507 rajveer 60
    	return "show";
419 rajveer 61
    }
517 rajveer 62
 
572 chandransh 63
    // POST /order/
64
    public String create(){
786 rajveer 65
    	String addressIdString = this.request.getParameter("addressid");
66
    	if(addressIdString == null){
894 rajveer 67
    		addActionError("Please specify shipping address to continue.");
786 rajveer 68
    		return "shipping-redirect";
69
    	}
2159 chandransh 70
 
71
    	String paymentOption = request.getParameter("payment_option");
72
    	log.info("Payment Option Selected: " + paymentOption);
73
    	if(paymentOption == null || paymentOption.equals("")){
74
    		addActionError("Please select a payment method to continue.");
75
    		return "shipping-redirect";
76
    	}
77
 
78
 
786 rajveer 79
    	long addressId = Long.parseLong(addressIdString);
572 chandransh 80
    	long currentCartId = userinfo.getCartId();
894 rajveer 81
    	try{
1318 rajveer 82
    		if(!createOrders(addressId, currentCartId)){
1128 rajveer 83
    			addActionError("We are experiencing some problems. Please try later.");
894 rajveer 84
    			return "shipping-redirect";
85
    		}
86
    	}catch (Exception e) {
1128 rajveer 87
    		addActionError("We are experiencing some problems. Please try later.");
2296 chandransh 88
    		log.error("Exception in createOrders function. Something went wrong.", e);
894 rajveer 89
    		return "shipping-redirect";
693 rajveer 90
		}
2159 chandransh 91
 
2316 chandransh 92
		if (paymentOption.equals(IPaymentService.HDFC_MASTERCARD) || paymentOption.equals(IPaymentService.HDFC_VISA) || paymentOption.equals(IPaymentService.HDFC_VISA_ELECTRON)) {
2159 chandransh 93
			// User has selected Visa or MasterCard CC
94
			IPaymentService hdfcPaymentService = new HdfcPaymentService();
95
			paymentId = hdfcPaymentService.createPayment(userinfo.getCartId(), userinfo.getUserId(), txnId, paymentOption);
1905 chandransh 96
			if (paymentId == IPaymentService.PAYMENT_NOT_CREATED) {
2199 chandransh 97
				log.error("Unable to process payment through HDFC. Falling through to EBS.");
1905 chandransh 98
			} else {
2159 chandransh 99
				this.redirectURL = ((HdfcPaymentService)hdfcPaymentService).getRedirectUrl();
1905 chandransh 100
				log.info(this.redirectURL);
101
				return "success";
102
			}
103
		}
2199 chandransh 104
 
2316 chandransh 105
		if(paymentOption.equals(IPaymentService.HDFC_VISA))
106
			paymentOption = IPaymentService.EBS_VISA;
107
		else if(paymentOption.equals(IPaymentService.HDFC_MASTERCARD))
108
			paymentOption = IPaymentService.EBS_MASTERCARD;
109
		else if(paymentOption.equals(IPaymentService.HDFC_VISA_ELECTRON))
2199 chandransh 110
			paymentOption = null;			//Since we don't know the bank's name in this case, we'll let the user select the bank on the EBS page.
111
 
112
		IPaymentService ebsPaymentService = new EbsPaymentService();
113
		paymentId = ebsPaymentService.createPayment(userinfo.getCartId(), userinfo.getUserId(), txnId, paymentOption);
114
		if(paymentId == IPaymentService.PAYMENT_NOT_CREATED){
115
			addActionError("We are experiencing some problems. Please try later.");
116
			return "shipping-redirect";
117
		}else{
118
			log.info("Successfully created payment for EBS to process. Redirecting to /ebs-pay/" + paymentId);
119
			return "ebs-pay-redirect";
120
		}
572 chandransh 121
    }
122
 
507 rajveer 123
	public String getId(){
124
		return id;
125
	}
419 rajveer 126
 
507 rajveer 127
	public void setId(String id){
128
		this.id = id;
129
	}
419 rajveer 130
 
507 rajveer 131
	public String getMyaccountHeaderSnippet(){
132
		return htmlSnippets.get("MYACCOUNT_HEADER");
133
	}
134
 
135
	public String getOrderDetailsSnippet(){
136
		return htmlSnippets.get("ORDER_DETAILS");
137
	}
138
 
712 rajveer 139
	public long getTxn(){
140
		return this.txnId;
517 rajveer 141
	}
894 rajveer 142
 
143
	/**
144
	 * 
145
	 * @param addressId
146
	 * @param currentCartId
147
	 * @return
148
	 */
1318 rajveer 149
	private boolean createOrders(long addressId, long currentCartId){
894 rajveer 150
		UserContextServiceClient userServiceClient = null;
151
		try {
152
			userServiceClient = new UserContextServiceClient();
153
		} catch (Exception e) {
2296 chandransh 154
			log.error("Unable to talk to the user context service.", e);
894 rajveer 155
			return false;
156
		}
157
 
158
		in.shop2020.model.v1.user.UserContextService.Client userClient = userServiceClient.getClient();
159
		try {
160
			userClient.addAddressToCart(currentCartId, addressId);
161
		} catch (ShoppingCartException e1) {
2296 chandransh 162
			log.error("Not able to set address in the cart.", e1);
894 rajveer 163
			return false;
164
		} catch (TException e1) {
2296 chandransh 165
			log.error("Thrift exception while setting address in cart.", e1);
894 rajveer 166
			return false;
167
		}
168
 
169
 
170
		try {
1466 ankur.sing 171
			String errorMsg = userClient.validateCart(currentCartId); 
172
			if(!errorMsg.isEmpty()){
173
				addActionError(errorMsg);
894 rajveer 174
				return false;
175
			}
176
		} catch (ShoppingCartException e1) {
2296 chandransh 177
			log.error("Error while validating shopping cart.", e1);
894 rajveer 178
			return false;
179
		} catch (TException e) {
2296 chandransh 180
			log.error("Thrift exception while validating cart.", e);
894 rajveer 181
			return false;
182
		}
183
 
184
 
185
		try {
2817 vikas 186
		    String sessionSrc = null;
187
		    Cookie sessionSrcCookie = (Cookie) cookiesMap.get(TrackingInterceptor.SESSION_SRC_COOKIE);
188
            if (sessionSrcCookie != null) {
189
                DesEncrypter desEncrypter = new DesEncrypter(TrackingInterceptor.ENCRIPTION_STRING);
190
                sessionSrc = desEncrypter.decrypt(sessionSrcCookie.getValue());
191
            }
192
 
193
            Cookie sessionSrcTimeCookie = (Cookie) cookiesMap.get(TrackingInterceptor.SESSION_SRC_TIME_COOKIE);
194
            long sessionTime = 0;
195
            if (sessionSrcTimeCookie != null) {
196
                try {
197
                    sessionTime = Long.parseLong(sessionSrcTimeCookie.getValue());
198
                }
199
                catch (Exception e) {
200
                    log.warn("Unable to parse session src time cookie.");
201
                }
202
            }
203
		    txnId = userClient.createOrders(currentCartId, sessionSrc, sessionTime);
894 rajveer 204
		} catch (ShoppingCartException e1) {
2296 chandransh 205
			log.error("Error while creating orders from cart.", e1);
894 rajveer 206
			return false;
207
		} catch (TException e) {
2296 chandransh 208
			log.error("Thrift exception while creating orders from cart.", e);
894 rajveer 209
			return false;
210
		}
211
 
2183 vikas 212
		TransactionServiceClient transactionServiceClient = null;
213
        try {
214
            transactionServiceClient = new TransactionServiceClient();
215
            List<Order> orders = transactionServiceClient.getClient().getOrdersForTransaction(txnId, userinfo.getUserId());
216
            for (Order order : orders) {
2419 vikas 217
                DataLogger.logData(EventType.ORDER_CREATION, session.getId(),
218
                        userinfo.getUserId(), userinfo.getEmail(), Long.toString(order.getId()));
2183 vikas 219
            }
220
        } catch (Exception e1) {
2296 chandransh 221
        	log.warn("Unable to log orders through the datalogger", e1);
2183 vikas 222
        }
894 rajveer 223
 
224
		return true;
225
	}
1318 rajveer 226
 
227
	public String getRedirectURL(){
228
		return this.redirectURL;
229
	}
419 rajveer 230
}