Subversion Repositories SmartDukaan

Rev

Rev 1128 | Rev 1466 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1128 Rev 1318
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;
3
import in.shop2020.model.v1.user.ShoppingCartException;
8
import in.shop2020.payments.PaymentException;
-
 
9
import in.shop2020.payments.PaymentService.Client;
-
 
10
import in.shop2020.serving.controllers.BaseController;
4
import in.shop2020.serving.controllers.BaseController;
11
import in.shop2020.serving.utils.Utils;
-
 
12
import in.shop2020.thrift.clients.PaymentServiceClient;
5
import in.shop2020.serving.services.HdfcPaymentService;
13
import in.shop2020.thrift.clients.UserContextServiceClient;
6
import in.shop2020.thrift.clients.UserContextServiceClient;
14
 
7
 
15
import org.apache.log4j.Logger;
8
import org.apache.log4j.Logger;
16
import org.apache.struts2.convention.annotation.InterceptorRef;
9
import org.apache.struts2.convention.annotation.InterceptorRef;
17
import org.apache.struts2.convention.annotation.InterceptorRefs;
10
import org.apache.struts2.convention.annotation.InterceptorRefs;
Line 23... Line 16...
23
    @InterceptorRef("myDefault"),
16
    @InterceptorRef("myDefault"),
24
    @InterceptorRef("login")
17
    @InterceptorRef("login")
25
})
18
})
26
 
19
 
27
@Results({
20
@Results({
28
    @Result(name="payredirect", type="redirectAction", 
-
 
29
			params = {"actionName" , "${url}", "paymentid", "${pid}", "txnid", "${txn}"}),
-
 
30
    @Result(name="shipping-redirect", type="redirectAction", 
21
    @Result(name="shipping-redirect", type="redirectAction", 
31
    		params = {"actionName" , "shipping"})
22
    		params = {"actionName" , "shipping"})
32
})
23
})
33
public class OrderController extends BaseController {
24
public class OrderController extends BaseController {
34
	
25
	
Line 36... Line 27...
36
	
27
	
37
	private static Logger log = Logger.getLogger(Class.class);
28
	private static Logger log = Logger.getLogger(Class.class);
38
	private String id;
29
	private String id;
39
	private long txnId = 0;
30
	private long txnId = 0;
40
	
31
	
41
	//FIXME right now only one PG. Once we will have more, need to fix it.
-
 
42
	private String paymentUrl="hdfc-pay";
-
 
43
	private int gatewayId=1;
-
 
44
	
-
 
45
	private long paymentId;
32
	private String redirectURL;
46
	
-
 
47
	private double amount;
-
 
48
	
33
	
49
	public OrderController(){
34
	public OrderController(){
50
		super();
35
		super();
51
	}
36
	}
52
	
37
	
Line 66... Line 51...
66
    		return "shipping-redirect";
51
    		return "shipping-redirect";
67
    	}
52
    	}
68
    	long addressId = Long.parseLong(addressIdString);
53
    	long addressId = Long.parseLong(addressIdString);
69
    	long currentCartId = userinfo.getCartId();
54
    	long currentCartId = userinfo.getCartId();
70
    	try{
55
    	try{
71
    		if(!createOrdersAndPayment(addressId, currentCartId)){
56
    		if(!createOrders(addressId, currentCartId)){
72
    			addActionError("We are experiencing some problems. Please try later.");
57
    			addActionError("We are experiencing some problems. Please try later.");
73
    			return "shipping-redirect";
58
    			return "shipping-redirect";
74
    		}
59
    		}
75
    	}catch (Exception e) {
60
    	}catch (Exception e) {
76
    		addActionError("We are experiencing some problems. Please try later.");
61
    		addActionError("We are experiencing some problems. Please try later.");
77
    		log.error("Exception in createOrders function. Something want wrong.");
62
    		log.error("Exception in createOrders function. Something want wrong.");
78
    		e.printStackTrace();
63
    		e.printStackTrace();
79
    		return "shipping-redirect";
64
    		return "shipping-redirect";
80
		}
65
		}
81
		
66
		
-
 
67
    	HdfcPaymentService hdfcPaymentService = new HdfcPaymentService();
-
 
68
    	boolean paymentStatus = hdfcPaymentService.createPayment(userinfo.getCartId(), userinfo.getUserId(), txnId);
-
 
69
    	if(paymentStatus == true){
-
 
70
    		this.redirectURL = hdfcPaymentService.getRedirectUrl();
-
 
71
    		log.info(this.redirectURL);
-
 
72
    		return "success";
-
 
73
    	}else{
-
 
74
    		addActionError("We are experiencing some problems. Please try later.");
82
		return "payredirect";
75
			return "shipping-redirect";
-
 
76
    	}
83
		
77
    	
84
    }
78
    }
85
    
79
    
86
	public String getId(){
80
	public String getId(){
87
		return id;
81
		return id;
88
	}
82
	}
Line 97... Line 91...
97
	
91
	
98
	public String getOrderDetailsSnippet(){
92
	public String getOrderDetailsSnippet(){
99
		return htmlSnippets.get("ORDER_DETAILS");
93
		return htmlSnippets.get("ORDER_DETAILS");
100
	}
94
	}
101
	
95
	
102
	public String getUrl(){
-
 
103
		return this.paymentUrl;
-
 
104
	}
-
 
105
	
-
 
106
	public long getPid(){
-
 
107
		return this.paymentId;
-
 
108
	}
-
 
109
	
-
 
110
	public long getTxn(){
96
	public long getTxn(){
111
		return this.txnId;
97
		return this.txnId;
112
	}
98
	}
113
	
-
 
114
	public double getAmount(){
-
 
115
		return this.amount;
-
 
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);
-
 
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
	}
-
 
139
 
-
 
140
 
99
 
141
	/**
100
	/**
142
	 * 
101
	 * 
143
	 * @param addressId
102
	 * @param addressId
144
	 * @param currentCartId
103
	 * @param currentCartId
145
	 * @return
104
	 * @return
146
	 */
105
	 */
147
	private boolean createOrdersAndPayment(long addressId, long currentCartId){
106
	private boolean createOrders(long addressId, long currentCartId){
148
		UserContextServiceClient userServiceClient = null;
107
		UserContextServiceClient userServiceClient = null;
149
		try {
108
		try {
150
			userServiceClient = new UserContextServiceClient();
109
			userServiceClient = new UserContextServiceClient();
151
		} catch (Exception e) {
110
		} catch (Exception e) {
152
			e.printStackTrace();
111
			e.printStackTrace();
Line 194... Line 153...
194
			e.printStackTrace();
153
			e.printStackTrace();
195
			return false;
154
			return false;
196
		}
155
		}
197
		
156
		
198
		
157
		
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
		
158
		
234
		return true;
159
		return true;
235
	}
160
	}
-
 
161
	
-
 
162
	public String getRedirectURL(){
-
 
163
		return this.redirectURL;
-
 
164
	}
236
}
165
}