Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
719 rajveer 1
package in.shop2020.serving.controllers;
2
 
3
import in.shop2020.model.v1.order.LineItem;
4
import in.shop2020.model.v1.order.Order;
5
import in.shop2020.model.v1.order.Transaction;
6
import in.shop2020.model.v1.order.TransactionStatus;
7
import in.shop2020.payments.PaymentStatus;
8
import in.shop2020.thrift.clients.PaymentServiceClient;
9
import in.shop2020.thrift.clients.TransactionServiceClient;
10
import in.shop2020.thrift.clients.UserContextServiceClient;
11
import in.shop2020.thrift.clients.config.ConfigClient;
12
import in.shop2020.utils.Logger;
13
 
14
import java.io.IOException;
15
import java.util.HashMap;
16
import java.util.Map;
17
 
18
import javax.servlet.http.HttpServletRequest;
19
import javax.servlet.http.HttpServletResponse;
20
 
21
import org.apache.juli.logging.Log;
22
import org.apache.juli.logging.LogFactory;
23
import org.apache.struts2.interceptor.ServletRequestAware;
24
import org.apache.struts2.interceptor.ServletResponseAware;
25
 
26
public class HdfcPayResponseController implements ServletResponseAware, ServletRequestAware{
27
	private static final long serialVersionUID = 1L;
28
 
29
	private enum PaymentReturnStatus{
30
		CAPTURED("CAPTURED"),
31
		NOT_CAPTURED ("NOT CAPTURED"),
32
		CANCELLED ("CANCELLED"),
33
		DENIED_BY_RISK("DENIED BY RISK"),
34
		HOST_TIMEOUT("HOST TIMEOUT");
35
		private String value;
36
		PaymentReturnStatus(String value) {
37
			this.value = value;
38
		}
39
		public String value(){
40
			return this.value;
41
		}
42
	}
43
 
44
	HttpServletRequest request;
45
	HttpServletResponse response;
46
 
47
	PaymentServiceClient pclient = null;
48
	TransactionServiceClient tsc = null;
49
	UserContextServiceClient usc = null;
50
	private static Log log = LogFactory.getLog(HdfcPayResponseController.class);
51
 
52
	public static String AMOUNT = "amt";
53
	public static String TRACKID = "trackid";
54
	public static String RESULT = "result";
55
	public static String AUTH = "auth";
56
	public static String TRANID = "tranid";
57
	public static String PAYMENTID = "paymentid";
58
	public static String REF = "ref";
59
	public static String POSTDATE = "postdate";
60
	public static String ERROR = "Error";
61
	public static String ERRORTEXT = "ErrorText";
62
	String redirectUrl;
63
	String amount;
64
	String trackId;
65
	String result;
66
	String postdate;
67
	String auth;
68
	String ref;
69
	String tranId;
70
	String paymentId;
71
	String sessionId;
72
	String errorText;
73
	String errorNo;
74
	long txnId;
75
	long merchantPaymentId;
76
	Transaction transaction = null;
77
 
78
	public HdfcPayResponseController() {
79
		try {
80
			pclient = new PaymentServiceClient();
81
			tsc = new TransactionServiceClient();
82
			usc = new UserContextServiceClient();
83
		} catch (Exception e) {
84
			Logger.log("Could not initialize the paymentservice client", this);
85
		}
86
	}
87
 
88
	public String create() throws IOException, SecurityException{
89
		System.out.println("Inside hdfc pay response Create");
90
 
91
		result = this.request.getParameter(RESULT);
92
		postdate = this.request.getParameter(POSTDATE);
93
		tranId = this.request.getParameter(TRANID);
94
		auth = this.request.getParameter(AUTH);
95
		ref = this.request.getParameter(REF);
96
		amount = this.request.getParameter(AMOUNT);
97
		paymentId = this.request.getParameter(PAYMENTID);
98
		trackId = this.request.getParameter(TRACKID);
99
 
100
		merchantPaymentId = Long.parseLong(trackId);
101
		sessionId = this.request.getSession().getId();
102
		errorNo = request.getParameter(ERROR);
103
		errorText = request.getParameter(ERRORTEXT);
104
		//FIXME dump them somewhere
105
		String udf1=request.getParameter("udf1");
106
		String udf2=request.getParameter("udf2");
107
		String udf3=request.getParameter("udf3");
108
		String udf4=request.getParameter("udf4");
109
		String udf5=request.getParameter("udf5");
110
 
111
 
112
 
113
		if(result.trim().equals(PaymentReturnStatus.CAPTURED.value())){
114
			try {
115
				String message = "Payment successful";
116
				pclient.getClient().updatePaymentDetails(merchantPaymentId, paymentId, sessionId, result, message, tranId, auth, ref, errorNo, PaymentStatus.SUCCESS, null);
117
				txnId = pclient.getClient().getPayment(merchantPaymentId).getMerchantTxnId();
118
 
119
				tsc.getClient().changeTransactionStatus(txnId, TransactionStatus.IN_PROCESS, "Payment received for the order");
120
 
121
		    	transaction = tsc.getClient().getTransaction(txnId);
122
 
123
		    	Map<Long,Double> items = new HashMap<Long, Double>();
124
		    	for(Order order: transaction.getOrders()){
125
					for(LineItem lineitem: order.getLineitems()){
126
						items.put(lineitem.getItem_id(),lineitem.getQuantity());
127
					}
128
				}
129
				//TODO Optimize the function to send less number of data over network
130
				usc.getClient().resetCart(transaction.getShoppingCartid(), items);
131
 
132
 
133
				/* 
134
				 this.redirectUrl = "http://173.230.147.250:8080/pay-response?paymentId="+paymentId+"&result="+result+"&auth="+auth+
135
				"&ref="+ref+"&postdate="+postdate+"&trackid="+trackId+"&tranid="+tranId+"&udf1="+udf1+"&udf2="+udf2+"&udf3="+udf3+"&udf4="+udf4+"&udf5="+udf5+
136
				"&amt="+amount;
137
				*/
138
 
139
			}catch (Exception e) {
140
				log.error("Error while updating information.");
141
				//FIXME Even we get exception we should sent url back to payment gateway. And some thing back channel should be done
142
 
143
			}
144
			try{
145
				this.redirectUrl = ConfigClient.getClient().get("payment_success_url") + "?paymentId="+merchantPaymentId;
146
			}catch(Exception ex){
147
				this.redirectUrl = "http://173.230.147.250:8080/pay-success?paymentId="+merchantPaymentId;	
148
			}
149
 
150
		}else{
151
			try{
152
				pclient.getClient().updatePaymentDetails(merchantPaymentId, paymentId, sessionId, result, errorText, tranId, auth, ref, errorNo, PaymentStatus.FAILED, null);
153
				txnId = pclient.getClient().getPayment(merchantPaymentId).getMerchantTxnId();
154
				tsc.getClient().changeTransactionStatus(txnId, TransactionStatus.FAILED, "Payment failed for the transaction.");
155
			}catch(Exception e){
156
				log.error("Error while updating information.");
157
				//FIXME Even we get exception we should sent url back to payment gateway. And some thing back channel should be done
158
			}
159
 
160
			try{
161
				this.redirectUrl = ConfigClient.getClient().get("payment_error_url") + "?paymentId="+merchantPaymentId;
162
			}catch(Exception ex){
163
				this.redirectUrl = "http://173.230.147.250:8080/pay-error?paymentId="+merchantPaymentId;	
164
			}
165
 
166
		}
167
 
168
		return "index";
169
	}
170
 
171
 
172
	public String getRedirectUrl(){
173
		return this.redirectUrl;
174
	}
175
 
176
	@Override
177
	public void setServletRequest(HttpServletRequest request) {
178
		this.request = request;
179
		for(Object param: request.getParameterMap().keySet()){
180
			System.out.println("PARAMS: " + param + " = "+ request.getParameter((String)param));
181
		}
182
 
183
	}
184
 
185
	@Override
186
	public void setServletResponse(HttpServletResponse response) {
187
		this.response = response;
188
	}
189
 
190
}