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