Subversion Repositories SmartDukaan

Rev

Rev 1554 | Rev 1905 | Go to most recent revision | Details | Compare with Previous | 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;
894 rajveer 13
import in.shop2020.payments.PaymentService.Client;
719 rajveer 14
import in.shop2020.payments.PaymentStatus;
15
import in.shop2020.thrift.clients.PaymentServiceClient;
16
import in.shop2020.thrift.clients.TransactionServiceClient;
17
import in.shop2020.thrift.clients.UserContextServiceClient;
18
import in.shop2020.thrift.clients.config.ConfigClient;
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.struts2.interceptor.ServletRequestAware;
28
import org.apache.struts2.interceptor.ServletResponseAware;
741 rajveer 29
import org.apache.thrift.TException;
1044 chandransh 30
import org.apache.log4j.Logger;
719 rajveer 31
 
32
public class HdfcPayResponseController implements ServletResponseAware, ServletRequestAware{
33
	private static final long serialVersionUID = 1L;
34
 
35
	private enum PaymentReturnStatus{
36
		CAPTURED("CAPTURED"),
37
		NOT_CAPTURED ("NOT CAPTURED"),
38
		CANCELLED ("CANCELLED"),
39
		DENIED_BY_RISK("DENIED BY RISK"),
40
		HOST_TIMEOUT("HOST TIMEOUT");
41
		private String value;
42
		PaymentReturnStatus(String value) {
43
			this.value = value;
44
		}
45
		public String value(){
46
			return this.value;
47
		}
48
	}
49
 
50
	HttpServletRequest request;
51
	HttpServletResponse response;
52
 
1044 chandransh 53
	//private static Logger log = LoggerFactory.getLogger(HdfcPayResponseController.class);
54
	private static Logger log = Logger.getLogger(Class.class);
719 rajveer 55
 
894 rajveer 56
	public static String successUrl;
57
	public static String errorUrl;
58
 
719 rajveer 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
 
894 rajveer 86
 
87
	static{
719 rajveer 88
		try {
894 rajveer 89
			successUrl = ConfigClient.getClient().get("payment_success_url");
90
			errorUrl = ConfigClient.getClient().get("payment_error_url");
91
		} catch (ConfigException e) {
92
			log.error("Unable to get success and error usr info from config server.");
719 rajveer 93
		}
94
	}
95
 
894 rajveer 96
 
97
	public HdfcPayResponseController() {
98
 
99
	}
100
 
719 rajveer 101
	public String create() throws IOException, SecurityException{
894 rajveer 102
		log.info("Inside hdfc pay response Create");
719 rajveer 103
 
894 rajveer 104
		PaymentServiceClient paymentServiceClient = null;
105
		TransactionServiceClient transactionServiceClient = null;
106
		UserContextServiceClient userServiceClient = null;
107
		try{
108
			paymentServiceClient = new PaymentServiceClient();
109
			transactionServiceClient = new TransactionServiceClient();
110
			userServiceClient = new UserContextServiceClient();
111
		}catch(Exception e){
112
			//Nothing to worry. lets move forward
113
			log.error("Unable to initialize one of the clients");
114
			e.printStackTrace();
115
		}
116
 
719 rajveer 117
		result = this.request.getParameter(RESULT);
118
		postdate = this.request.getParameter(POSTDATE);
119
		tranId = this.request.getParameter(TRANID);
120
		auth = this.request.getParameter(AUTH);
121
		ref = this.request.getParameter(REF);
122
		amount = this.request.getParameter(AMOUNT);
123
		paymentId = this.request.getParameter(PAYMENTID);
124
		trackId = this.request.getParameter(TRACKID);
125
 
126
		merchantPaymentId = Long.parseLong(trackId);
127
		sessionId = this.request.getSession().getId();
128
		errorNo = request.getParameter(ERROR);
129
		errorText = request.getParameter(ERRORTEXT);
130
		//FIXME dump them somewhere
131
		String udf1=request.getParameter("udf1");
132
		String udf2=request.getParameter("udf2");
133
		String udf3=request.getParameter("udf3");
134
		String udf4=request.getParameter("udf4");
853 rajveer 135
		String udf5=request.getParameter(UDF5);
894 rajveer 136
		//FIXME hdfc is sending comma separated amount, which is very disappointing. May be we get more surprises moving forward.
856 rajveer 137
		amount= amount.replace(",", "");
894 rajveer 138
 
139
		Client paymentClient = paymentServiceClient.getClient();
140
		in.shop2020.model.v1.order.TransactionService.Client transactionClient = transactionServiceClient.getClient();
741 rajveer 141
		try {
894 rajveer 142
			Payment payment = paymentClient.getPayment(merchantPaymentId);
853 rajveer 143
			String dbUdf5="";
144
			double dbAmount = payment.getAmount();
145
			for(Attribute attribute: payment.getAttributes()){
146
				if(attribute.getName().trim().equalsIgnoreCase(UDF5)){
147
					dbUdf5 = attribute.getValue();
148
				}
149
			}
150
			// verify 3 things:  udf5, amount and paymentid
894 rajveer 151
			//FIXME should we first dump the data and than verify these things. ?? 
856 rajveer 152
			log.info(paymentId+ ":"+ payment.getGatewayPaymentId() + "\n" + Double.parseDouble(amount) + ":" + dbAmount + "\n" + dbUdf5 + ":" + udf5 );
153
			if(!(paymentId.equalsIgnoreCase(payment.getGatewayPaymentId()) && dbAmount == Double.parseDouble(amount) && udf5.equalsIgnoreCase(dbUdf5))){
154
				log.error("Checks and balance failed on returned date");
894 rajveer 155
				this.redirectUrl =  errorUrl + "?paymentId="+merchantPaymentId;
741 rajveer 156
				return "index";
157
			}
158
		} catch (PaymentException e1) {
853 rajveer 159
			log.error("Payment exception. It is serious, check merchant payment id + " + merchantPaymentId);
741 rajveer 160
			e1.printStackTrace();
161
		} catch (TException e1) {
853 rajveer 162
			log.error("Thrift exception. Check payment id "+ merchantPaymentId);
741 rajveer 163
			e1.printStackTrace();
164
		}
719 rajveer 165
 
913 rajveer 166
		if(result != null && result.trim().equals(PaymentReturnStatus.CAPTURED.value())){
894 rajveer 167
			String message = "Payment successful";
168
 
719 rajveer 169
			try {
1128 rajveer 170
				paymentClient.updatePaymentDetails(merchantPaymentId, paymentId, sessionId, result, message, tranId, auth, ref, errorNo, PaymentStatus.SUCCESS, postdate, null);
894 rajveer 171
			} catch (PaymentException e1) {
172
				log.error("Unable to update payment details in our database." + e1.getError_code() + e1.getMessage());
173
				e1.printStackTrace();
174
			} catch (TException e1) {
175
				log.error("Unable to update payment details in our database. Thrift exception.");
176
				e1.printStackTrace();
177
			}
178
 
179
			Map<Long,Double> items = new HashMap<Long, Double>();
180
			try {
181
				txnId = paymentServiceClient.getClient().getPayment(merchantPaymentId).getMerchantTxnId();
182
				transactionClient.changeTransactionStatus(txnId, TransactionStatus.IN_PROCESS, "Payment received for the order");
183
				transaction = transactionClient.getTransaction(txnId);
184
				for(Order order: transaction.getOrders()){
719 rajveer 185
					for(LineItem lineitem: order.getLineitems()){
889 chandransh 186
						Long itemId = lineitem.getItem_id();
187
						Double quantity = items.get(itemId);
907 rajveer 188
						if(quantity==null){
189
							quantity = lineitem.getQuantity();
190
						}
191
						else{
192
							quantity= quantity + lineitem.getQuantity();
193
						}
889 chandransh 194
						items.put(itemId, quantity);
719 rajveer 195
					}
894 rajveer 196
				}	
197
			} catch (PaymentException e1) {
198
				log.error("Unable to get transaction id from payment service." + e1.getError_code() + e1.getMessage());
199
				e1.printStackTrace();
200
			} catch (TException e1) {
201
				log.error("Unable to get transaction id from payment service. Thrift Exception" + e1.getMessage());
202
				e1.printStackTrace();
203
			} catch (TransactionServiceException e) {
204
				log.error("Unable to get transaction id from payment service. Thrift Exception" + e.getErrorCode() + e.getMessage());
205
				e.printStackTrace();
206
			}
207
 
208
 
209
 
210
			try {
719 rajveer 211
				//TODO Optimize the function to send less number of data over network
907 rajveer 212
				System.out.println("Transaction shopping cart id is: " + transaction.getShoppingCartid());
213
				System.out.println("Items to restr in cart are: " + items );
214
				log.info("Transaction shopping cart id is: " + transaction.getShoppingCartid());
215
				log.info("Items to restr in cart are: " + items );
894 rajveer 216
				userServiceClient.getClient().resetCart(transaction.getShoppingCartid(), items);	
853 rajveer 217
			}catch (TException e) {
218
				log.error("Error while updating information in payment database.");
219
				e.printStackTrace();
719 rajveer 220
				//FIXME Even we get exception we should sent url back to payment gateway. And some thing back channel should be done
221
 
853 rajveer 222
			} catch (ShoppingCartException e) {
894 rajveer 223
				log.error("Error while reseting the cart in cart database.");
853 rajveer 224
				e.printStackTrace();
719 rajveer 225
			}
894 rajveer 226
 
1554 varun.gupt 227
			// Enqueue the email, containing transaction info, to be sent later by batch job  
228
			try {
229
				transactionClient.enqueueTransactionInfoEmail(txnId);
230
			} catch (Exception e) {
1555 varun.gupt 231
				log.error("Error while adding email to depatch queue.");
1554 varun.gupt 232
				e.printStackTrace();
233
			}
234
 
894 rajveer 235
			this.redirectUrl = successUrl + "?paymentId="+merchantPaymentId;
1554 varun.gupt 236
		}
237
		else	{
894 rajveer 238
			try {
1128 rajveer 239
				paymentClient.updatePaymentDetails(merchantPaymentId, paymentId, sessionId, result, errorText, tranId, auth, ref, errorNo, PaymentStatus.FAILED, postdate, null);
894 rajveer 240
			} catch (PaymentException e1) {
241
				log.error("Unable to update payment details in our database." + e1.getError_code() + e1.getMessage());
242
				e1.printStackTrace();
243
			} catch (TException e1) {
244
				log.error("Unable to update payment details in our database. Thrift exception.");
245
				e1.printStackTrace();
719 rajveer 246
			}
247
 
248
			try{
894 rajveer 249
				txnId = paymentClient.getPayment(merchantPaymentId).getMerchantTxnId();
250
				transactionClient.changeTransactionStatus(txnId, TransactionStatus.FAILED, "Payment failed for the transaction.");
853 rajveer 251
			}catch(TException e){
894 rajveer 252
				log.error("Thrift exception while getting information from transaction service.");
853 rajveer 253
				e.printStackTrace();
254
			} catch (PaymentException e) {
894 rajveer 255
				log.error("Error while getting information from merchant.");
853 rajveer 256
				e.printStackTrace();
257
			} catch (TransactionServiceException e) {
894 rajveer 258
				log.error("Error while updating status information in transaction database.");
853 rajveer 259
				e.printStackTrace();
719 rajveer 260
			}
261
 
894 rajveer 262
			this.redirectUrl = errorUrl + "?paymentId="+merchantPaymentId;
719 rajveer 263
		}
264
 
265
		return "index";
266
	}
267
 
268
 
269
	public String getRedirectUrl(){
270
		return this.redirectUrl;
271
	}
272
 
273
	@Override
274
	public void setServletRequest(HttpServletRequest request) {
275
		this.request = request;
276
		for(Object param: request.getParameterMap().keySet()){
277
			System.out.println("PARAMS: " + param + " = "+ request.getParameter((String)param));
278
		}
279
	}
280
 
281
	@Override
282
	public void setServletResponse(HttpServletResponse response) {
283
		this.response = response;
284
	}
1554 varun.gupt 285
}