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