Subversion Repositories SmartDukaan

Rev

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

Rev 693 Rev 712
Line 1... Line 1...
1
package in.shop2020.serving.controllers;
1
package in.shop2020.serving.controllers;
2
 
2
 
3
import java.io.IOException;
3
import java.io.IOException;
-
 
4
import java.util.HashMap;
4
import java.util.List;
5
import java.util.List;
-
 
6
import java.util.Map;
5
 
7
 
-
 
8
import org.apache.struts2.convention.annotation.Result;
-
 
9
import org.apache.struts2.convention.annotation.Results;
6
import org.apache.thrift.TException;
10
import org.apache.thrift.TException;
7
 
11
 
-
 
12
import in.shop2020.model.v1.order.LineItem;
8
import in.shop2020.model.v1.order.Order;
13
import in.shop2020.model.v1.order.Order;
9
import in.shop2020.model.v1.order.TransactionServiceException;
14
import in.shop2020.model.v1.order.TransactionServiceException;
-
 
15
import in.shop2020.model.v1.order.TransactionStatus;
-
 
16
import in.shop2020.model.v1.user.ShoppingCartException;
10
import in.shop2020.payments.PaymentException;
17
import in.shop2020.payments.PaymentException;
11
import in.shop2020.payments.PaymentStatus;
18
import in.shop2020.payments.PaymentStatus;
-
 
19
import in.shop2020.serving.utils.Utils;
12
import in.shop2020.thrift.clients.PaymentServiceClient;
20
import in.shop2020.thrift.clients.PaymentServiceClient;
13
import in.shop2020.thrift.clients.TransactionServiceClient;
21
import in.shop2020.thrift.clients.TransactionServiceClient;
-
 
22
import in.shop2020.thrift.clients.UserContextServiceClient;
14
import in.shop2020.utils.Logger;
23
import in.shop2020.utils.Logger;
15
 
24
 
-
 
25
@Results({
-
 
26
    @Result(name="redirect", type="redirectAction", 
-
 
27
    		params = {"actionName" , "login"}),
-
 
28
    @Result(name="shipping-redirect", type="redirectAction", 
-
 
29
    		params = {"actionName" , "shipping"})
-
 
30
		
-
 
31
})
16
 
32
 
17
public class PayResponseController extends BaseController{
33
public class PayResponseController extends BaseController{
18
	private static final long serialVersionUID = 1L;
34
	private static final long serialVersionUID = 1L;
19
 
35
 
-
 
36
	private enum PaymentReturnStatus{
-
 
37
		CAPTURED("CAPTURED"),
-
 
38
		NOT_CAPTURED ("NOT CAPTURED"),
-
 
39
		CANCELLED ("CANCELLED"),
-
 
40
		DENIED_BY_RISK("DENIED BY RISK"),
-
 
41
		HOST_TIMEOUT("HOST TIMEOUT");
-
 
42
		private String value;
-
 
43
		PaymentReturnStatus(String value) {
-
 
44
			this.value = value;
-
 
45
		}
-
 
46
		public String value(){
-
 
47
			return this.value;
-
 
48
		}
-
 
49
	}
-
 
50
 
20
	public static String AMOUNT = "amount";
51
	public static String AMOUNT = "amt";
21
	public static String TRACKID = "trackid";
52
	public static String TRACKID = "trackid";
22
	public static String TRACKID_CANCELLED = "trackId";
53
	public static String TRACKID_CANCELLED = "trackId";
23
	public static String RESULT = "result";
54
	public static String RESULT = "result";
24
	public static String AUTH = "auth";
55
	public static String AUTH = "auth";
25
	public static String TRANID = "tranid";
56
	public static String TRANID = "tranid";
26
	public static String PAYMENTID = "paymentId";
57
	public static String PAYMENTID = "paymentid";
27
	public static String REF = "ref";
58
	public static String REF = "ref";
28
	public static String POSTDATE = "postdate";
59
	public static String POSTDATE = "postdate";
29
	public static String CANCELED = "CANCELED";
-
 
30
	public static String APPROVED = "APPROVED";
60
	public static String ERROR = "Error";
31
	public static String CAPTURED = "CAPTURED";
61
	public static String ERRORTEXT = "ErrorText";
32
 
62
	
33
	PaymentServiceClient pclient = null;
63
	PaymentServiceClient pclient = null;
34
	TransactionServiceClient tsc = null;
64
	TransactionServiceClient tsc = null;
-
 
65
	UserContextServiceClient usc = null;
35
	
66
	
36
	String amount;
67
	String amount;
37
	String trackId;
68
	String trackId;
38
	String result;
69
	String result;
39
	String postdate;
70
	String postdate;
40
	String auth;
71
	String auth;
41
	String ref;
72
	String ref;
42
	String tranId;
73
	String tranId;
43
	String paymentId;
74
	String paymentId;
44
	String sessionId;
75
	String sessionId;
-
 
76
	String errorText;
-
 
77
	String errorNo;
-
 
78
	long txnId;
-
 
79
	long merchantPaymentId;
-
 
80
	List<Order> orders = null;
45
 
81
 
-
 
82
	/*
-
 
83
	 * 	pw.println("RESULT="+result);
-
 
84
			pw.println("PDATE="+postdate);
-
 
85
			pw.println("TXNID="+tranid);
-
 
86
			pw.println("AUTH="+auth);
-
 
87
			pw.println("TRACKID="+trackid);
-
 
88
			pw.println("REF="+ref);
-
 
89
			pw.println("AMT="+amount);
-
 
90
			
-
 
91
			pw.flush();
-
 
92
		*/	
-
 
93
			
46
	String message = "Unable to process the payment. PLease try Again.";
94
	String message = "Unable to process the payment. PLease try Again.";
47
	
95
	
48
	public PayResponseController(){
96
	public PayResponseController(){
49
		super();
97
		super();
50
		try {
98
		try {
51
			pclient = new PaymentServiceClient();
99
			pclient = new PaymentServiceClient();
52
			tsc = new TransactionServiceClient();
100
			tsc = new TransactionServiceClient();
-
 
101
			usc = new UserContextServiceClient();
53
		} catch (Exception e) {
102
		} catch (Exception e) {
54
			Logger.log("Could not initialize the paymentservice client", this);
103
			Logger.log("Could not initialize the paymentservice client", this);
55
		}
104
		}
56
	}
105
	}
57
	
106
	
58
	public String index() throws IOException, SecurityException{
107
	public String index() throws IOException, SecurityException{
-
 
108
    	if(!userinfo.isLoggedIn()){
-
 
109
    		setRedirectUrl();
-
 
110
    		return "redirect";
-
 
111
    	}
-
 
112
 
-
 
113
    	result = this.request.getParameter(RESULT);
-
 
114
		postdate = this.request.getParameter(POSTDATE);
-
 
115
		tranId = this.request.getParameter(TRANID);
-
 
116
		auth = this.request.getParameter(AUTH);
-
 
117
		ref = this.request.getParameter(REF);
59
		amount = this.request.getParameter(AMOUNT);
118
		amount = this.request.getParameter(AMOUNT);
60
		paymentId = this.request.getParameter(PAYMENTID);
119
		paymentId = this.request.getParameter(PAYMENTID);
61
		tranId = this.request.getParameter(TRANID);
-
 
62
		result = this.request.getParameter(RESULT);
-
 
63
		if (result.equalsIgnoreCase(CANCELED)){
120
		if (result.equalsIgnoreCase(PaymentReturnStatus.CANCELLED.value())){
64
			trackId = this.request.getParameter(TRACKID_CANCELLED);
121
			trackId = this.request.getParameter(TRACKID_CANCELLED);
65
		}else{
122
		}else{
66
			trackId = this.request.getParameter(TRACKID);
123
			trackId = this.request.getParameter(TRACKID);
67
		}
124
		}
68
		auth = this.request.getParameter(AUTH);
-
 
69
		ref = this.request.getParameter(REF);
-
 
70
		postdate = this.request.getParameter(POSTDATE);
125
		merchantPaymentId = Long.parseLong(trackId);
71
		sessionId = this.request.getSession().getId();
126
		sessionId = this.request.getSession().getId();
-
 
127
		errorNo = request.getParameter(ERROR);
-
 
128
		errorText = request.getParameter(ERRORTEXT);
-
 
129
		/*
-
 
130
		String udf1=request.getParameter("udf1");
-
 
131
		String udf2=request.getParameter("udf2");
-
 
132
		String udf3=request.getParameter("udf3");
-
 
133
		String udf4=request.getParameter("udf4");
-
 
134
		String udf5=request.getParameter("udf5");
72
 
135
		*/
73
		//update the payment info object
136
		//update the payment info object
74
		try {
137
		try {
75
			if(result.trim().equals(CAPTURED)){
138
			if(result.trim().equals(PaymentReturnStatus.CAPTURED.value())){
76
				String message = "Payment successful";
139
				String message = "Payment successful";
77
				pclient.getClient().updatePaymentDetails(Long.parseLong(trackId), paymentId, sessionId, result, message, tranId, auth, ref, null, PaymentStatus.SUCCESS, null);
140
				pclient.getClient().updatePaymentDetails(merchantPaymentId, paymentId, sessionId, result, message, tranId, auth, ref, errorNo, PaymentStatus.SUCCESS, null);
-
 
141
				txnId = pclient.getClient().getPayment(merchantPaymentId).getMerchantTxnId();
-
 
142
				
-
 
143
				tsc.getClient().changeTransactionStatus(txnId, TransactionStatus.IN_PROCESS, "Payment received for the order");
-
 
144
				
-
 
145
		    	orders = tsc.getClient().getOrdersForTransaction(txnId);
-
 
146
		    	Map<Long,Double> items = new HashMap<Long, Double>();
-
 
147
		    	
-
 
148
				for(Order order: orders){
-
 
149
					for(LineItem lineitem: order.getLineitems()){
-
 
150
						items.put(lineitem.getItem_id(),lineitem.getQuantity());
-
 
151
					}
-
 
152
				}
-
 
153
				usc.getClient().resetCart(userinfo.getCartId(), items);
-
 
154
				//TODO Optimize the function to send less number of data over network
-
 
155
				userinfo.setTotalItems(Utils.getNumberOfItemsInCart(userinfo.getCartId()));
-
 
156
 
78
				return "index";
157
				return "index";
79
			}else{
158
			}else{
80
				String message = "Unable to process the payment.";
159
				String message = "Unable to process the payment.";
81
				addActionError("Previous payment failed. Try again.");
160
				addActionError("Previous payment failed. Try again.");
-
 
161
				
-
 
162
				txnId = pclient.getClient().getPayment(merchantPaymentId).getMerchantTxnId();
-
 
163
				tsc.getClient().changeTransactionStatus(txnId, TransactionStatus.FAILED, "Payment failed for the transaction.");
-
 
164
				
82
				pclient.getClient().updatePaymentDetails(Long.parseLong(trackId), paymentId, sessionId, result, message, tranId, auth, ref, null, PaymentStatus.FAILED, null);
165
				pclient.getClient().updatePaymentDetails(merchantPaymentId, paymentId, sessionId, result, errorText, tranId, auth, ref, errorNo, PaymentStatus.FAILED, null);
83
				return "failure";
166
				return "shipping-redirect";
84
			}
167
			}
85
			
168
			
-
 
169
		}
86
		} catch (NumberFormatException e) {
170
		catch (NumberFormatException e) {
87
			Logger.log(e.toString(), this);
171
			Logger.log(e.toString(), this);
88
		} catch (PaymentException e) {
172
		} catch (PaymentException e) {
89
			
-
 
90
			Logger.log(e.toString(), this);
173
			Logger.log(e.toString(), this);
91
		} catch (TException e) {
174
		} catch (TException e) {
92
			
-
 
93
			Logger.log(e.toString(), this);
175
			Logger.log(e.toString(), this);
-
 
176
		} catch (TransactionServiceException e) {
-
 
177
			Logger.log(e.toString(), this);
-
 
178
		} catch (ShoppingCartException e) {
-
 
179
			// TODO Auto-generated catch block
-
 
180
			e.printStackTrace();
94
		}
181
		}
95
		return "index";
182
		return "index";
96
	}
183
	}
97
	
184
	
98
	public List<Order> getOrders() throws TransactionServiceException, TException{
185
	public List<Order> getOrders(){
99
		//FIXME
-
 
100
		return tsc.getClient().getOrdersForTransaction(1);
186
		return this.orders;
101
		
-
 
102
		
-
 
103
	}
187
	}
104
	
188
	
105
	public String getMessage(){
189
	public String getMessage(){
106
		return this.message;
190
		return this.message;
107
	}
191
	}