Subversion Repositories SmartDukaan

Rev

Rev 2199 | Rev 2296 | 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.");
894 rajveer 85
    		log.error("Exception in createOrders function. Something want wrong.");
86
    		e.printStackTrace();
87
    		return "shipping-redirect";
693 rajveer 88
		}
2159 chandransh 89
 
2199 chandransh 90
		if (paymentOption.equals(IPaymentService.EBS_MASTERCARD) || paymentOption.equals(IPaymentService.EBS_VISA) || paymentOption.equals(IPaymentService.EBS_VISA_ELECTRON)) {
2159 chandransh 91
			// User has selected Visa or MasterCard CC
92
			IPaymentService hdfcPaymentService = new HdfcPaymentService();
93
			paymentId = hdfcPaymentService.createPayment(userinfo.getCartId(), userinfo.getUserId(), txnId, paymentOption);
1905 chandransh 94
			if (paymentId == IPaymentService.PAYMENT_NOT_CREATED) {
2199 chandransh 95
				log.error("Unable to process payment through HDFC. Falling through to EBS.");
1905 chandransh 96
			} else {
2159 chandransh 97
				this.redirectURL = ((HdfcPaymentService)hdfcPaymentService).getRedirectUrl();
1905 chandransh 98
				log.info(this.redirectURL);
99
				return "success";
100
			}
101
		}
2199 chandransh 102
 
103
		if(paymentOption.equals(IPaymentService.EBS_VISA_ELECTRON))
104
			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.
105
 
106
		IPaymentService ebsPaymentService = new EbsPaymentService();
107
		paymentId = ebsPaymentService.createPayment(userinfo.getCartId(), userinfo.getUserId(), txnId, paymentOption);
108
		if(paymentId == IPaymentService.PAYMENT_NOT_CREATED){
109
			addActionError("We are experiencing some problems. Please try later.");
110
			return "shipping-redirect";
111
		}else{
112
			log.info("Successfully created payment for EBS to process. Redirecting to /ebs-pay/" + paymentId);
113
			return "ebs-pay-redirect";
114
		}
572 chandransh 115
    }
116
 
507 rajveer 117
	public String getId(){
118
		return id;
119
	}
419 rajveer 120
 
507 rajveer 121
	public void setId(String id){
122
		this.id = id;
123
	}
419 rajveer 124
 
507 rajveer 125
	public String getMyaccountHeaderSnippet(){
126
		return htmlSnippets.get("MYACCOUNT_HEADER");
127
	}
128
 
129
	public String getOrderDetailsSnippet(){
130
		return htmlSnippets.get("ORDER_DETAILS");
131
	}
132
 
712 rajveer 133
	public long getTxn(){
134
		return this.txnId;
517 rajveer 135
	}
894 rajveer 136
 
137
	/**
138
	 * 
139
	 * @param addressId
140
	 * @param currentCartId
141
	 * @return
142
	 */
1318 rajveer 143
	private boolean createOrders(long addressId, long currentCartId){
894 rajveer 144
		UserContextServiceClient userServiceClient = null;
145
		try {
146
			userServiceClient = new UserContextServiceClient();
147
		} catch (Exception e) {
148
			e.printStackTrace();
149
			return false;
150
		}
151
 
152
		in.shop2020.model.v1.user.UserContextService.Client userClient = userServiceClient.getClient();
153
		try {
154
			userClient.addAddressToCart(currentCartId, addressId);
155
		} catch (ShoppingCartException e1) {
156
			log.error("Not able to set address in the cart." + e1.getId() + e1.getMessage());
157
			e1.printStackTrace();
158
			return false;
159
		} catch (TException e1) {
160
			log.error("Thrift exception while setting address in cart." + e1.getMessage());
161
			e1.printStackTrace();
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) {
173
			log.error("Error while validating shopping cart." + e1.getId() + e1.getMessage());
174
			e1.printStackTrace();
175
			return false;
176
		} catch (TException e) {
177
			log.error("Thrift exception while validating cart." + e.getMessage());
178
			e.printStackTrace();
179
			return false;
180
		}
181
 
182
 
183
		try {
184
			txnId = userClient.createOrders(currentCartId);
185
		} catch (ShoppingCartException e1) {
186
			log.error("Error while creating orders from cart." + e1.getId() + e1.getMessage());
187
			e1.printStackTrace();
188
			return false;
189
		} catch (TException e) {
190
			log.error("Thrift exception while creating orders from cart." + e.getMessage());
191
			e.printStackTrace();
192
			return false;
193
		}
194
 
2183 vikas 195
		TransactionServiceClient transactionServiceClient = null;
196
        try {
197
            transactionServiceClient = new TransactionServiceClient();
198
            List<Order> orders = transactionServiceClient.getClient().getOrdersForTransaction(txnId, userinfo.getUserId());
199
            for (Order order : orders) {
2263 vikas 200
                DataLogger.logData(EventType.ORDER_CREATION.name(), session.getId(),
2183 vikas 201
                        Long.toString(userinfo.getUserId()),
202
                        userinfo.getEmail(), Long.toString(order.getId()));
203
            }
204
        } catch (Exception e1) {
205
            e1.printStackTrace();
206
        }
894 rajveer 207
 
208
		return true;
209
	}
1318 rajveer 210
 
211
	public String getRedirectURL(){
212
		return this.redirectURL;
213
	}
419 rajveer 214
}