Subversion Repositories SmartDukaan

Rev

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