Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
419 rajveer 1
package in.shop2020.serving.controllers;
2
 
894 rajveer 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;
693 rajveer 9
import in.shop2020.payments.PaymentService.Client;
419 rajveer 10
import in.shop2020.serving.controllers.BaseController;
693 rajveer 11
import in.shop2020.serving.utils.Utils;
12
import in.shop2020.thrift.clients.PaymentServiceClient;
517 rajveer 13
import in.shop2020.thrift.clients.UserContextServiceClient;
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
 
822 vikas 22
@InterceptorRefs({
23
    @InterceptorRef("myDefault"),
24
    @InterceptorRef("login")
25
})
26
 
419 rajveer 27
@Results({
822 vikas 28
    @Result(name="payredirect", type="redirectAction", 
894 rajveer 29
			params = {"actionName" , "${url}", "paymentid", "${pid}", "txnid", "${txn}"}),
693 rajveer 30
    @Result(name="shipping-redirect", type="redirectAction", 
894 rajveer 31
    		params = {"actionName" , "shipping"})
419 rajveer 32
})
650 rajveer 33
public class OrderController extends BaseController {
419 rajveer 34
 
35
	private static final long serialVersionUID = 1L;
36
 
832 rajveer 37
	private static Logger log = Logger.getLogger(Class.class);
507 rajveer 38
	private String id;
712 rajveer 39
	private long txnId = 0;
650 rajveer 40
 
693 rajveer 41
	//FIXME right now only one PG. Once we will have more, need to fix it.
681 rajveer 42
	private String paymentUrl="hdfc-pay";
693 rajveer 43
	private int gatewayId=1;
681 rajveer 44
 
693 rajveer 45
	private long paymentId;
46
 
712 rajveer 47
	private double amount;
48
 
419 rajveer 49
	public OrderController(){
507 rajveer 50
		super();
419 rajveer 51
	}
52
 
507 rajveer 53
    // GET /order/ orderid
54
    public String show() {
55
    	log.info("id=" + id);
822 vikas 56
    	htmlSnippets.put("MYACCOUNT_HEADER", pageLoader.getMyaccountHeaderHtml());
843 chandransh 57
    	htmlSnippets.put("ORDER_DETAILS", pageLoader.getOrderDetailsHtml(Long.parseLong(id)));
507 rajveer 58
    	return "show";
419 rajveer 59
    }
517 rajveer 60
 
572 chandransh 61
    // POST /order/
62
    public String create(){
786 rajveer 63
    	String addressIdString = this.request.getParameter("addressid");
64
    	if(addressIdString == null){
894 rajveer 65
    		addActionError("Please specify shipping address to continue.");
786 rajveer 66
    		return "shipping-redirect";
67
    	}
68
    	long addressId = Long.parseLong(addressIdString);
572 chandransh 69
    	long currentCartId = userinfo.getCartId();
894 rajveer 70
    	try{
71
    		if(!createOrdersAndPayment(addressId, currentCartId)){
72
    			addActionError("We are experiencing problem. Please try later.");
73
    			return "shipping-redirect";
74
    		}
75
    	}catch (Exception e) {
76
    		addActionError("We are experiencing problem. Please try later.");
77
    		log.error("Exception in createOrders function. Something want wrong.");
78
    		e.printStackTrace();
79
    		return "shipping-redirect";
693 rajveer 80
		}
894 rajveer 81
 
82
		return "payredirect";
83
 
572 chandransh 84
    }
85
 
507 rajveer 86
	public String getId(){
87
		return id;
88
	}
419 rajveer 89
 
507 rajveer 90
	public void setId(String id){
91
		this.id = id;
92
	}
419 rajveer 93
 
507 rajveer 94
	public String getMyaccountHeaderSnippet(){
95
		return htmlSnippets.get("MYACCOUNT_HEADER");
96
	}
97
 
98
	public String getOrderDetailsSnippet(){
99
		return htmlSnippets.get("ORDER_DETAILS");
100
	}
101
 
681 rajveer 102
	public String getUrl(){
103
		return this.paymentUrl;
104
	}
507 rajveer 105
 
712 rajveer 106
	public long getPid(){
693 rajveer 107
		return this.paymentId;
108
	}
109
 
712 rajveer 110
	public long getTxn(){
111
		return this.txnId;
517 rajveer 112
	}
712 rajveer 113
 
114
	public double getAmount(){
115
		return this.amount;
116
	}
894 rajveer 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);
712 rajveer 129
 
894 rajveer 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
		}
517 rajveer 136
 
894 rajveer 137
		return totalAmount;
138
	}
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
	}
419 rajveer 236
}