Subversion Repositories SmartDukaan

Rev

Rev 853 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 853 Rev 894
Line 1... Line 1...
1
package in.shop2020.serving.controllers;
1
package in.shop2020.serving.controllers;
2
 
2
 
-
 
3
import java.util.List;
-
 
4
 
-
 
5
import in.shop2020.model.v1.user.Cart;
-
 
6
import in.shop2020.model.v1.user.Line;
-
 
7
import in.shop2020.model.v1.user.ShoppingCartException;
-
 
8
import in.shop2020.payments.PaymentException;
3
import in.shop2020.payments.PaymentService.Client;
9
import in.shop2020.payments.PaymentService.Client;
4
import in.shop2020.serving.controllers.BaseController;
10
import in.shop2020.serving.controllers.BaseController;
5
import in.shop2020.serving.utils.Utils;
11
import in.shop2020.serving.utils.Utils;
6
import in.shop2020.thrift.clients.PaymentServiceClient;
12
import in.shop2020.thrift.clients.PaymentServiceClient;
7
import in.shop2020.thrift.clients.UserContextServiceClient;
13
import in.shop2020.thrift.clients.UserContextServiceClient;
Line 18... Line 24...
18
    @InterceptorRef("login")
24
    @InterceptorRef("login")
19
})
25
})
20
 
26
 
21
@Results({
27
@Results({
22
    @Result(name="payredirect", type="redirectAction", 
28
    @Result(name="payredirect", type="redirectAction", 
23
			params = {"actionName" , "${url}", "paymentid", "${pid}", "txnid", "${txn}", "amount", "${amount}"}),
29
			params = {"actionName" , "${url}", "paymentid", "${pid}", "txnid", "${txn}"}),
24
    @Result(name="shipping-redirect", type="redirectAction", 
30
    @Result(name="shipping-redirect", type="redirectAction", 
25
    		params = {"actionName" , "shipping"}),
31
    		params = {"actionName" , "shipping"})
26
    @Result(name="failure", type="redirectAction", 
-
 
27
    		params = {"actionName" , "shipping"})    		
-
 
28
		
-
 
29
})
32
})
30
public class OrderController extends BaseController {
33
public class OrderController extends BaseController {
31
	
34
	
32
	private static final long serialVersionUID = 1L;
35
	private static final long serialVersionUID = 1L;
33
	
36
	
Line 57... Line 60...
57
 
60
 
58
    // POST /order/
61
    // POST /order/
59
    public String create(){
62
    public String create(){
60
    	String addressIdString = this.request.getParameter("addressid");
63
    	String addressIdString = this.request.getParameter("addressid");
61
    	if(addressIdString == null){
64
    	if(addressIdString == null){
62
    		addActionError("Please login to continue checkout.");
65
    		addActionError("Please specify shipping address to continue.");
63
    		return "shipping-redirect";
66
    		return "shipping-redirect";
64
    	}
67
    	}
65
    	long addressId = Long.parseLong(addressIdString);
68
    	long addressId = Long.parseLong(addressIdString);
66
    	long currentCartId = userinfo.getCartId();
69
    	long currentCartId = userinfo.getCartId();
67
    	
-
 
68
		try{
70
    	try{
69
			amount = Double.parseDouble(request.getParameter("amount"));
-
 
70
		}catch(Exception e){
-
 
71
			amount = Utils.getPaymentAmount(userinfo.getCartId());
-
 
72
		}
-
 
73
 
-
 
74
		try {
-
 
75
			UserContextServiceClient userServiceClient = new UserContextServiceClient();
-
 
76
			in.shop2020.model.v1.user.UserContextService.Client userClient = userServiceClient.getClient();
-
 
77
			
-
 
78
			userClient.addAddressToCart(currentCartId, addressId);
-
 
79
			//TODO validate only item quantity change. Other validations should not be done. 
-
 
80
			if(!userClient.validateCart(currentCartId)){
71
    		if(!createOrdersAndPayment(addressId, currentCartId)){
81
				addActionError("Some items are added in your cart.");
72
    			addActionError("We are experiencing problem. Please try later.");
82
				return "shipping-redirect";
73
    			return "shipping-redirect";
83
			}
74
    		}
84
			txnId = userClient.createOrders(currentCartId);
-
 
85
			
-
 
86
			PaymentServiceClient paymentServiceClient = new PaymentServiceClient();
-
 
87
			Client paymentClient = paymentServiceClient.getClient();
-
 
88
			
-
 
89
			this.paymentId = paymentClient.createPayment(userinfo.getUserId(), amount, gatewayId, txnId);
-
 
90
			return "payredirect";
-
 
91
			
-
 
92
		} catch (TException e) {
75
    	}catch (Exception e) {
93
			log.error("Thrift exception, payment failed");
76
    		addActionError("We are experiencing problem. Please try later.");
94
			e.printStackTrace();
-
 
95
		} catch (Exception e) {
-
 
96
			log.error("Thrift exception, payment failed");
77
    		log.error("Exception in createOrders function. Something want wrong.");
97
			e.printStackTrace();
78
    		e.printStackTrace();
-
 
79
    		return "shipping-redirect";
98
		}
80
		}
99
    	
81
		
100
		return "failure";
82
		return "payredirect";
-
 
83
		
101
    }
84
    }
102
    
85
    
103
	public String getId(){
86
	public String getId(){
104
		return id;
87
		return id;
105
	}
88
	}
Line 129... Line 112...
129
	}
112
	}
130
	
113
	
131
	public double getAmount(){
114
	public double getAmount(){
132
		return this.amount;
115
		return this.amount;
133
	}
116
	}
-
 
117
 
-
 
118
	private double getPaymentAmount(long cartId) throws ShoppingCartException, TException{
-
 
119
		double totalAmount = 0;
-
 
120
		Cart cart;
-
 
121
		UserContextServiceClient userContextServiceClient = null;
-
 
122
		try {
-
 
123
			userContextServiceClient = new UserContextServiceClient();
-
 
124
		} catch (Exception e) {
-
 
125
			e.printStackTrace();
-
 
126
		}
-
 
127
		in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
-
 
128
		cart = userClient.getCart(cartId);
134
	
129
	
-
 
130
		List<Line> lineItems = cart.getLines(); 
-
 
131
	
-
 
132
		for (Line line : lineItems) {
-
 
133
			long productId = line.getItemId();
-
 
134
			totalAmount =  totalAmount + line.getQuantity() * Utils.getItemPrice(productId);
-
 
135
		}
-
 
136
 
-
 
137
		return totalAmount;
-
 
138
	}
135
 
139
 
-
 
140
 
-
 
141
	/**
-
 
142
	 * 
-
 
143
	 * @param addressId
-
 
144
	 * @param currentCartId
-
 
145
	 * @return
-
 
146
	 */
-
 
147
	private boolean createOrdersAndPayment(long addressId, long currentCartId){
-
 
148
		UserContextServiceClient userServiceClient = null;
-
 
149
		try {
-
 
150
			userServiceClient = new UserContextServiceClient();
-
 
151
		} catch (Exception e) {
-
 
152
			e.printStackTrace();
-
 
153
			return false;
-
 
154
		}
-
 
155
		
-
 
156
		in.shop2020.model.v1.user.UserContextService.Client userClient = userServiceClient.getClient();
-
 
157
		try {
-
 
158
			userClient.addAddressToCart(currentCartId, addressId);
-
 
159
		} catch (ShoppingCartException e1) {
-
 
160
			log.error("Not able to set address in the cart." + e1.getId() + e1.getMessage());
-
 
161
			e1.printStackTrace();
-
 
162
			return false;
-
 
163
		} catch (TException e1) {
-
 
164
			log.error("Thrift exception while setting address in cart." + e1.getMessage());
-
 
165
			e1.printStackTrace();
-
 
166
			return false;
-
 
167
		}
-
 
168
		
-
 
169
		
-
 
170
		try {
-
 
171
			if(!userClient.validateCart(currentCartId)){
-
 
172
				addActionError("Your cart has been updated.");
-
 
173
				return false;
-
 
174
			}
-
 
175
		} catch (ShoppingCartException e1) {
-
 
176
			log.error("Error while validating shopping cart." + e1.getId() + e1.getMessage());
-
 
177
			e1.printStackTrace();
-
 
178
			return false;
-
 
179
		} catch (TException e) {
-
 
180
			log.error("Thrift exception while validating cart." + e.getMessage());
-
 
181
			e.printStackTrace();
-
 
182
			return false;
-
 
183
		}
-
 
184
		
-
 
185
		
-
 
186
		try {
-
 
187
			txnId = userClient.createOrders(currentCartId);
-
 
188
		} catch (ShoppingCartException e1) {
-
 
189
			log.error("Error while creating orders from cart." + e1.getId() + e1.getMessage());
-
 
190
			e1.printStackTrace();
-
 
191
			return false;
-
 
192
		} catch (TException e) {
-
 
193
			log.error("Thrift exception while creating orders from cart." + e.getMessage());
-
 
194
			e.printStackTrace();
-
 
195
			return false;
-
 
196
		}
-
 
197
		
-
 
198
		
-
 
199
		PaymentServiceClient paymentServiceClient = null;
-
 
200
		try {
-
 
201
			paymentServiceClient = new PaymentServiceClient();
-
 
202
		} catch (Exception e) {
-
 
203
			log.error("Error while getting payment client");
-
 
204
			e.printStackTrace();
-
 
205
			return false;
-
 
206
		}
-
 
207
		
-
 
208
		
-
 
209
		try {
-
 
210
			amount = getPaymentAmount(userinfo.getCartId());
-
 
211
		} catch (ShoppingCartException e1) {
-
 
212
			log.error("Not able to fetch payment amount from cart id." + e1.getId() + e1.getMessage());
-
 
213
			e1.printStackTrace();
-
 
214
			return false;
-
 
215
		} catch (TException e1) {
-
 
216
			log.error("Not able to fetch payment amount." + e1.getMessage());
-
 
217
			e1.printStackTrace();
-
 
218
			return false;
-
 
219
		}
-
 
220
		
-
 
221
		Client paymentClient = paymentServiceClient.getClient();
-
 
222
		try {
-
 
223
			this.paymentId = paymentClient.createPayment(userinfo.getUserId(), amount, gatewayId, txnId);
-
 
224
		} catch (PaymentException e1) {
-
 
225
			log.error("Not able to create payment object." + e1.getError_code() + e1.getMessage());
-
 
226
			e1.printStackTrace();
-
 
227
			return false;
-
 
228
		} catch (TException e) {
-
 
229
			log.error("Not able to create payment object." + e.getMessage());
-
 
230
			e.printStackTrace();
-
 
231
			return false;
-
 
232
		}
-
 
233
		
-
 
234
		return true;
-
 
235
	}
136
}
236
}