Subversion Repositories SmartDukaan

Rev

Rev 2511 | Rev 2681 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2511 Rev 2673
Line 32... Line 32...
32
import org.apache.log4j.Logger;
32
import org.apache.log4j.Logger;
33
import org.apache.thrift.TException;
33
import org.apache.thrift.TException;
34
 
34
 
35
@SuppressWarnings("serial")
35
@SuppressWarnings("serial")
36
public class EbsPayResponseController extends BaseController{
36
public class EbsPayResponseController extends BaseController{
37
	
37
 
38
	private static Logger log = Logger.getLogger(Class.class);
38
	private static Logger log = Logger.getLogger(Class.class);
39
	
39
 
40
	private static final String FLAG_KEY = "IsFlagged";
40
	private static final String FLAG_KEY = "IsFlagged";
41
	private static final String TXN_KEY = "TransactionID";
41
	private static final String TXN_KEY = "TransactionID";
42
	private static final String AUTH_TXN_ID = "AuthTxnId";
42
	private static final String AUTH_TXN_ID = "AuthTxnId";
43
	
43
 
44
	private static String successUrl;
44
	private static String successUrl;
45
	private static String errorUrl;
45
	private static String errorUrl;
46
	
46
 
47
	/**
47
	/**
48
	 * The secret key used to decode RC4 encoded data.
48
	 * The secret key used to decode RC4 encoded data.
49
	 */
49
	 */
50
	private static String accountKey;
50
	private static String accountKey;
51
	
51
 
52
	private String redirectUrl;
52
	private String redirectUrl;
53
	private String id;
53
	private String id;
54
	
54
 
55
	static{
55
	static{
56
		try {
56
		try {
57
			successUrl = ConfigClient.getClient().get("ebs_success_url");
57
			successUrl = ConfigClient.getClient().get("ebs_success_url");
58
			errorUrl = ConfigClient.getClient().get("ebs_error_url");
58
			errorUrl = ConfigClient.getClient().get("ebs_error_url");
59
			accountKey = ConfigClient.getClient().get("ebs_secret_key");
59
			accountKey = ConfigClient.getClient().get("ebs_secret_key");
60
		} catch (ConfigException e) {
60
		} catch (ConfigException e) {
61
			log.error("Unable to get success and error usr info from config server.");
61
			log.error("Unable to get success and error usr info from config server.");
62
		}
62
		}
63
	}
63
	}
64
	
64
 
65
	private Map<String, String> paymentParams = new TreeMap<String, String>();
65
	private final Map<String, String> paymentParams = new TreeMap<String, String>();
66
	
66
 
67
	public String index() {
67
	public String index() {
68
		StringBuffer data1 = new StringBuffer(request.getParameter("DR"));
68
		StringBuffer data1 = new StringBuffer(request.getParameter("DR"));
69
		log.info("Received data string: " + data1.toString());
69
		log.info("Received data string: " + data1.toString());
70
		byte[] result = decodeRecvdData(data1);
70
		byte[] result = decodeRecvdData(data1);
71
 
71
 
72
		String recvString = parseRecvdData(result);
72
		String recvString = parseRecvdData(result);
73
		updatePaymentParams(recvString);
73
		updatePaymentParams(recvString);
74
		
74
 
75
		PaymentServiceClient paymentServiceClient = null;
75
		PaymentServiceClient paymentServiceClient = null;
76
		TransactionServiceClient transactionServiceClient = null;
76
		TransactionServiceClient transactionServiceClient = null;
77
		UserContextServiceClient userServiceClient = null;
77
		UserContextServiceClient userServiceClient = null;
78
		try {
78
		try {
79
			paymentServiceClient = new PaymentServiceClient();
79
			paymentServiceClient = new PaymentServiceClient();
Line 81... Line 81...
81
			userServiceClient = new UserContextServiceClient();
81
			userServiceClient = new UserContextServiceClient();
82
		} catch (Exception e) {
82
		} catch (Exception e) {
83
			log.error("Unable to initialize one of the clients");
83
			log.error("Unable to initialize one of the clients");
84
			e.printStackTrace();
84
			e.printStackTrace();
85
		}
85
		}
86
		
86
 
87
		
87
 
88
		long merchantPaymentId = Long.parseLong(paymentParams.get("MerchantRefNo"));
88
		long merchantPaymentId = Long.parseLong(paymentParams.get("MerchantRefNo"));
89
		String gatewayPaymentId = paymentParams.get("PaymentID");
89
		String gatewayPaymentId = paymentParams.get("PaymentID");
90
		double amount = Double.parseDouble(paymentParams.get("Amount"));
90
		double amount = Double.parseDouble(paymentParams.get("Amount"));
91
		String isFlagged = paymentParams.get(FLAG_KEY);
91
		String isFlagged = paymentParams.get(FLAG_KEY);
92
		String gatewayTxnStatus = paymentParams.get("ResponseCode");
92
		String gatewayTxnStatus = paymentParams.get("ResponseCode");
93
		String description = paymentParams.get("ResponseMessage");
93
		String description = paymentParams.get("ResponseMessage");
94
		String authTxnId = paymentParams.get(TXN_KEY);
94
		String authTxnId = paymentParams.get(TXN_KEY);
95
		
95
 
96
		
96
 
97
		List<Attribute> attributes = new ArrayList<Attribute>();
97
		List<Attribute> attributes = new ArrayList<Attribute>();
98
		attributes.add(new Attribute(FLAG_KEY, isFlagged));
98
		attributes.add(new Attribute(FLAG_KEY, isFlagged));
99
		attributes.add(new Attribute(AUTH_TXN_ID, authTxnId));
99
		attributes.add(new Attribute(AUTH_TXN_ID, authTxnId));
100
		
100
 
101
		Payment payment = null;
101
		Payment payment = null;
102
		Long txnId = null;
102
		Long txnId = null;
103
		try {
103
		try {
104
			payment = paymentServiceClient.getClient().getPayment(merchantPaymentId);
104
			payment = paymentServiceClient.getClient().getPayment(merchantPaymentId);
105
			txnId = payment.getMerchantTxnId();
105
			txnId = payment.getMerchantTxnId();
106
		} catch (PaymentException e1) {
106
		} catch (PaymentException e1) {
107
			log.error("Payment exception. It is serious, check merchant payment id + " + merchantPaymentId, e1);
107
			log.error("Payment exception. It is serious, check merchant payment id + " + merchantPaymentId, e1);
108
		} catch (TException e1) {
108
		} catch (TException e1) {
109
			log.error("Thrift exception. Check payment id "+ merchantPaymentId, e1);
109
			log.error("Thrift exception. Check payment id "+ merchantPaymentId, e1);
110
		}
110
		}
111
		
111
 
112
		if(payment.getStatus() != PaymentStatus.INIT){
112
		if(payment.getStatus() != PaymentStatus.INIT){
113
			// We have already processed a response for this payment. Processing
113
			// We have already processed a response for this payment. Processing
114
			// it again may fail his orders. So, let's ask him to check his
114
			// it again may fail his orders. So, let's ask him to check his
115
			// account.
115
			// account.
116
			return "maybe";
116
			return "maybe";
117
		}
117
		}
118
		
118
 
119
		if(!validatePaymentParams(amount, payment)){
119
		if(!validatePaymentParams(amount, payment)){
120
			this.redirectUrl = errorUrl + "?paymentId=" + merchantPaymentId;
120
			this.redirectUrl = errorUrl + "?paymentId=" + merchantPaymentId;
121
			return "index";
121
			return "index";
122
		}
122
		}
123
		
123
 
124
		if(gatewayTxnStatus.equals("0")){
124
		if(gatewayTxnStatus.equals("0")){
125
			//Update payment status as authorized
125
			//Update payment status as authorized
126
			try {
126
			try {
127
				paymentServiceClient.getClient().updatePaymentDetails(merchantPaymentId, gatewayPaymentId,
127
				paymentServiceClient.getClient().updatePaymentDetails(merchantPaymentId, gatewayPaymentId,
128
						"", gatewayTxnStatus, description, "", "", "", "", PaymentStatus.AUTHORIZED, "", attributes);
128
						"", gatewayTxnStatus, description, "", "", "", "", PaymentStatus.AUTHORIZED, "", attributes);
Line 132... Line 132...
132
				e.printStackTrace();
132
				e.printStackTrace();
133
			}
133
			}
134
 
134
 
135
			Map<String, String> captureResult = EbsPaymentService.capturePayment(amount, gatewayPaymentId);
135
			Map<String, String> captureResult = EbsPaymentService.capturePayment(amount, gatewayPaymentId);
136
			String captureStatus = captureResult.get(IPaymentService.STATUS);
136
			String captureStatus = captureResult.get(IPaymentService.STATUS);
137
			
137
 
138
			if("".equals(captureStatus)){
138
			if("".equals(captureStatus)){
139
				//Failure
139
				//Failure
140
				description = captureResult.get(EbsPaymentService.ERROR);
140
				description = captureResult.get(EbsPaymentService.ERROR);
141
				String errorCode = captureResult.get(EbsPaymentService.ERR_CODE);
141
				String errorCode = captureResult.get(EbsPaymentService.ERR_CODE);
142
				try {
142
				try {
Line 153... Line 153...
153
			}else{
153
			}else{
154
				//Success
154
				//Success
155
				try {
155
				try {
156
					attributes.add(new Attribute(IPaymentService.CAPTURE_TXN_ID, captureResult.get(IPaymentService.CAPTURE_TXN_ID)));
156
					attributes.add(new Attribute(IPaymentService.CAPTURE_TXN_ID, captureResult.get(IPaymentService.CAPTURE_TXN_ID)));
157
					attributes.add(new Attribute(IPaymentService.CAPTURE_TIME, captureResult.get(IPaymentService.CAPTURE_TIME)));
157
					attributes.add(new Attribute(IPaymentService.CAPTURE_TIME, captureResult.get(IPaymentService.CAPTURE_TIME)));
158
					
158
 
159
					paymentServiceClient.getClient().updatePaymentDetails(merchantPaymentId, gatewayPaymentId,
159
					paymentServiceClient.getClient().updatePaymentDetails(merchantPaymentId, gatewayPaymentId,
160
							"", captureStatus, description, "", "", "", "", PaymentStatus.SUCCESS, "", attributes);
160
							"", captureStatus, description, "", "", "", "", PaymentStatus.SUCCESS, "", attributes);
161
				} catch (PaymentException e) {
161
				} catch (PaymentException e) {
162
					log.error("Error while updating successful capture payment attempt: ", e);
162
					log.error("Error while updating successful capture payment attempt: ", e);
163
				} catch (TException e) {
163
				} catch (TException e) {
164
					log.error("Error while updating successful capture payment attempt: ", e);
164
					log.error("Error while updating successful capture payment attempt: ", e);
165
				}
165
				}
166
				
166
 
167
				CommonPaymentService.processSuccessfulTxn(txnId, userServiceClient, transactionServiceClient);
167
				CommonPaymentService.processSuccessfulTxn(txnId, userServiceClient, transactionServiceClient);
168
 
168
 
169
				DataLogger.logData(EventType.PAYMENT_SUCCESS, session.getId(), userinfo.getUserId(), userinfo.getEmail(), Long.toString(merchantPaymentId), gatewayPaymentId,
169
				//DataLogger.logData(EventType.PAYMENT_SUCCESS, session.getId(), userinfo.getUserId(), userinfo.getEmail(), Long.toString(merchantPaymentId), gatewayPaymentId,
170
                        gatewayTxnStatus, description, captureStatus);
170
                //        gatewayTxnStatus, description, captureStatus);
171
				this.redirectUrl = successUrl + "?paymentId=" + merchantPaymentId;				
171
				this.redirectUrl = successUrl + "?paymentId=" + merchantPaymentId;
172
			}
172
			}
173
		}else{
173
		}else{
174
			try {
174
			try {
175
				paymentServiceClient.getClient().updatePaymentDetails(merchantPaymentId, gatewayPaymentId,
175
				paymentServiceClient.getClient().updatePaymentDetails(merchantPaymentId, gatewayPaymentId,
176
						"", gatewayTxnStatus, description, "", "", "", "", PaymentStatus.FAILED, "", attributes);
176
						"", gatewayTxnStatus, description, "", "", "", "", PaymentStatus.FAILED, "", attributes);
177
			} catch (PaymentException e) {
177
			} catch (PaymentException e) {
178
				e.printStackTrace();
178
				e.printStackTrace();
179
			} catch (TException e) {
179
			} catch (TException e) {
180
				e.printStackTrace();
180
				e.printStackTrace();
181
			}
181
			}
182
			
182
 
183
			CommonPaymentService.processFailedTxn(txnId, transactionServiceClient);
183
			CommonPaymentService.processFailedTxn(txnId, transactionServiceClient);
184
			DataLogger.logData(EventType.PAYMENT_FAILURE, session.getId(), userinfo.getUserId(), userinfo.getEmail(), Long.toString(merchantPaymentId), gatewayPaymentId,
184
			DataLogger.logData(EventType.PAYMENT_FAILURE, session.getId(), userinfo.getUserId(), userinfo.getEmail(), Long.toString(merchantPaymentId), gatewayPaymentId,
185
                    gatewayTxnStatus, description);
185
                    gatewayTxnStatus, description);
186
 
186
 
187
			this.redirectUrl = errorUrl + "?paymentId=" + merchantPaymentId;
187
			this.redirectUrl = errorUrl + "?paymentId=" + merchantPaymentId;
188
		}
188
		}
189
		
189
 
190
		log.info("User will be redirected to: " + this.redirectUrl);
190
		log.info("User will be redirected to: " + this.redirectUrl);
191
		return "index";
191
		return "index";
192
	}
192
	}
193
	
193
 
194
	public String getId() {
194
	public String getId() {
195
		return id;
195
		return id;
196
	}
196
	}
197
 
197
 
198
	private boolean validatePaymentParams(double returnedAmount, Payment payment){
198
	private boolean validatePaymentParams(double returnedAmount, Payment payment){
Line 207... Line 207...
207
	private byte[] decodeRecvdData(StringBuffer data1) {
207
	private byte[] decodeRecvdData(StringBuffer data1) {
208
		for (int i = 0; i < data1.length(); i++) {
208
		for (int i = 0; i < data1.length(); i++) {
209
			if (data1.charAt(i) == ' ')
209
			if (data1.charAt(i) == ' ')
210
				data1.setCharAt(i, '+');
210
				data1.setCharAt(i, '+');
211
		}
211
		}
212
		
212
 
213
		Base64 base64 = new Base64();
213
		Base64 base64 = new Base64();
214
		byte[] data = base64.decode(data1.toString());
214
		byte[] data = base64.decode(data1.toString());
215
		RC4 rc4 = new RC4(accountKey);
215
		RC4 rc4 = new RC4(accountKey);
216
		byte[] result = rc4.rc4(data);
216
		byte[] result = rc4.rc4(data);
217
		return result;
217
		return result;
Line 239... Line 239...
239
		return recvString;
239
		return recvString;
240
	}
240
	}
241
 
241
 
242
	private void updatePaymentParams(String str){
242
	private void updatePaymentParams(String str){
243
		StringTokenizer st = new StringTokenizer(str, "=&");
243
		StringTokenizer st = new StringTokenizer(str, "=&");
244
		String key, value; 
244
		String key, value;
245
		while(st.hasMoreTokens()) {
245
		while(st.hasMoreTokens()) {
246
			key = st.nextToken();
246
			key = st.nextToken();
247
			value = st.nextToken();
247
			value = st.nextToken();
248
			log.info("Key: " + key + ", Value: " + value);
248
			log.info("Key: " + key + ", Value: " + value);
249
			paymentParams.put(key, value);
249
			paymentParams.put(key, value);
250
		}
250
		}
251
	}
251
	}
252
	
252
 
253
	public String getRedirectUrl(){
253
	public String getRedirectUrl(){
254
		return this.redirectUrl;
254
		return this.redirectUrl;
255
	}
255
	}
256
	
256
 
257
	@Override
257
	@Override
258
	public void setServletRequest(HttpServletRequest request) {
258
	public void setServletRequest(HttpServletRequest request) {
259
		this.request = request;
259
		this.request = request;
260
	}
260
	}
261
 
261