Subversion Repositories SmartDukaan

Rev

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