Subversion Repositories SmartDukaan

Rev

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