Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
421 rajveer 1
package in.shop2020.serving.controllers;
2
 
741 rajveer 3
import java.util.ArrayList;
4
import java.util.List;
5
import java.util.Random;
712 rajveer 6
 
421 rajveer 7
import javax.servlet.http.HttpServletResponse;
712 rajveer 8
 
894 rajveer 9
import org.apache.log4j.Logger;
712 rajveer 10
import org.apache.struts2.convention.annotation.Result;
11
import org.apache.struts2.convention.annotation.Results;
421 rajveer 12
import org.apache.thrift.TException;
13
 
14
import in.shop2020.config.ConfigException;
762 rajveer 15
import in.shop2020.model.v1.order.LineItem;
16
import in.shop2020.model.v1.order.Order;
17
import in.shop2020.model.v1.order.Transaction;
894 rajveer 18
import in.shop2020.model.v1.order.TransactionServiceException;
19
import in.shop2020.model.v1.user.Cart;
20
import in.shop2020.model.v1.user.Line;
762 rajveer 21
import in.shop2020.model.v1.user.ShoppingCartException;
741 rajveer 22
import in.shop2020.payments.Attribute;
23
import in.shop2020.payments.PaymentStatus;
421 rajveer 24
import in.shop2020.serving.utils.Utils;
25
import in.shop2020.thrift.clients.PaymentServiceClient;
762 rajveer 26
import in.shop2020.thrift.clients.TransactionServiceClient;
894 rajveer 27
import in.shop2020.thrift.clients.UserContextServiceClient;
421 rajveer 28
import in.shop2020.thrift.clients.config.ConfigClient;
29
 
30
 
31
 
32
import com.aciworldwide.commerce.gateway.plugins.NotEnoughDataException;
33
import com.aciworldwide.commerce.gateway.plugins.e24PaymentPipe;
34
@Results({
712 rajveer 35
	@Result(name="redirect", type="redirectAction", 
894 rajveer 36
			params = {"actionName" , "${url}"}),
37
	@Result(name="shipping-redirect", type="redirectAction", 
38
    		params = {"actionName" , "shipping"})
421 rajveer 39
})
712 rajveer 40
 
682 rajveer 41
public class HdfcPayController extends BaseController {
42
	private static final long serialVersionUID = 1L;
894 rajveer 43
	private static Logger log = Logger.getLogger(Class.class);
682 rajveer 44
 
894 rajveer 45
	private static String resourceFilePath;
46
	private static String aliasName;
47
	private static String responseURL;
48
	private static String errorURL;
49
	private static final String regex = "[^a-zA-Z0-9\\s\\-\\@\\/\\.]";
50
	private static final String replacement = " ";
51
	private static final int MAX_UDF_LENGTH = 30;
421 rajveer 52
	private String redirectURL;
894 rajveer 53
	private e24PaymentPipe pipe = null;
54
 
682 rajveer 55
	private enum ActionType{
56
		PURCHASE("1"),
57
		AUTH ("4");
58
		private String value;
59
		ActionType(String value) {
60
			this.value = value;
61
		}
62
		public String value(){
63
			return this.value;
64
		}
65
	}
421 rajveer 66
 
682 rajveer 67
	public HdfcPayController(){
894 rajveer 68
 
421 rajveer 69
	}
70
 
894 rajveer 71
	static{
421 rajveer 72
		try {
73
			resourceFilePath = ConfigClient.getClient().get("payment_resource_file_path");
74
			aliasName  = ConfigClient.getClient().get("payment_alias_name");
75
			responseURL = ConfigClient.getClient().get("payment_response_url");
76
			errorURL = ConfigClient.getClient().get("payment_error_url");
894 rajveer 77
		} catch (ConfigException e) {
78
			log.error("Unable to get data from config server.");
421 rajveer 79
		}
894 rajveer 80
	}
421 rajveer 81
 
82
 
894 rajveer 83
	public String index(){
421 rajveer 84
 
894 rajveer 85
		String merchantPaymentId = request.getParameter("paymentid");
86
		long txnId = Long.parseLong(request.getParameter("txnid"));
87
 
88
		if(merchantPaymentId == null){
89
			addActionError("We are experiencing problem. Please try later.");
90
			log.error("Merchant payment is null. It should not have reached here.");
91
			return "shipping-redirect";
92
		}
93
 
94
		PaymentServiceClient paymentServiceClient = null;
762 rajveer 95
		try {
894 rajveer 96
			paymentServiceClient = new PaymentServiceClient();
762 rajveer 97
		} catch (Exception e) {
98
			e.printStackTrace();
99
		}
421 rajveer 100
 
894 rajveer 101
		try {
102
			initializePayment(merchantPaymentId);
103
		} catch (ShoppingCartException e1) {
104
			addActionError("We are experiencing problem. Please try later.");
105
			log.error("Error while creating hdfc payment.");
106
			e1.printStackTrace();
107
			return "shipping-redirect";
108
		} catch (TException e1) {
109
			addActionError("We are experiencing problem. Please try later.");
110
			log.error("Error while creating hdfc payment.");
111
			e1.printStackTrace();
112
			return "shipping-redirect";
113
		}
421 rajveer 114
 
894 rajveer 115
		List<Attribute> attributes = null;
116
		try {
117
			attributes = getAttributesAndSetUdfs(txnId);
118
		} catch (TransactionServiceException e1) {
119
			addActionError("We are experiencing problem. Please try later.");
120
			log.error("Error while setting udfs to payment.");
121
			e1.printStackTrace();
122
			return "shipping-redirect";
123
		} catch (TException e1) {
124
			addActionError("We are experiencing problem. Please try later.");
125
			log.error("Error while setting udfs to payment.");
126
			e1.printStackTrace();
127
			return "shipping-redirect";
741 rajveer 128
		}
129
 
130
 
421 rajveer 131
		try {
132
			if(pipe.performPaymentInitialization() != e24PaymentPipe.SUCCESS) 
133
				{
134
					System.out.println("Error sending Payment Initialization Request: ");
741 rajveer 135
					paymentServiceClient.getClient().updatePaymentDetails(Long.parseLong(merchantPaymentId), null, "", "", pipe.getErrorMsg(), "", "", "", "", PaymentStatus.INIT, attributes);
136
					response.sendRedirect(response.encodeRedirectURL( errorURL + "?ErrorText="+pipe.getErrorMsg()+"&paymentId="+merchantPaymentId));
829 rajveer 137
					redirectURL = errorURL + "?paymentId="+merchantPaymentId + "&ErrorText="+pipe.getErrorMsg();
421 rajveer 138
					response.setHeader("Location", redirectURL);
719 rajveer 139
					response.setStatus(HttpServletResponse.SC_FOUND);
421 rajveer 140
				}
141
			else
142
				{
741 rajveer 143
					String paymentID = pipe.getPaymentId();
144
					// Update of payment information
145
					paymentServiceClient.getClient().updatePaymentDetails(Long.parseLong(merchantPaymentId), paymentID, "", "", "", "", "", "", "", PaymentStatus.INIT, attributes);
146
 
421 rajveer 147
					String payURL = pipe.getPaymentPage();
741 rajveer 148
					redirectURL = payURL + "?PaymentID=" + paymentID;
719 rajveer 149
					response.sendRedirect(response.encodeRedirectURL( redirectURL ));
421 rajveer 150
 
151
					response.setHeader("Location", redirectURL);
719 rajveer 152
					response.setStatus(HttpServletResponse.SC_FOUND);
421 rajveer 153
				}
712 rajveer 154
			return "success";
421 rajveer 155
		} catch (NotEnoughDataException e) {
894 rajveer 156
			log.error("Error while initializing payment." + e.getMessage());
421 rajveer 157
			e.printStackTrace();
158
		}catch (Exception e) {
894 rajveer 159
			log.error("Error while initializing payment." + e.getMessage());
421 rajveer 160
			e.printStackTrace();
161
		}
162
 
894 rajveer 163
		redirectURL = errorURL + "?paymentId="+merchantPaymentId + "?ErrorText=Error while initializing payment.";
741 rajveer 164
		return "success";
421 rajveer 165
	}
712 rajveer 166
 
894 rajveer 167
 
168
	private double getPaymentAmount(long cartId) throws ShoppingCartException, TException{
169
		double totalAmount = 0;
170
		Cart cart;
171
		UserContextServiceClient userContextServiceClient = null;
172
		try {
173
			userContextServiceClient = new UserContextServiceClient();
174
		} catch (Exception e) {
175
			e.printStackTrace();
176
		}
177
		in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
178
		cart = userClient.getCart(cartId);
179
 
180
		List<Line> lineItems = cart.getLines(); 
181
 
182
		for (Line line : lineItems) {
183
			long productId = line.getItemId();
184
			totalAmount =  totalAmount + line.getQuantity() * Utils.getItemPrice(productId);
185
		}
186
 
187
		return totalAmount;
712 rajveer 188
	}
894 rajveer 189
 
190
 
191
	private List<Attribute> getAttributesAndSetUdfs(long txnId) throws TransactionServiceException, TException{
192
		StringBuilder orderDetails = new StringBuilder();
193
		StringBuilder billingAddress = new StringBuilder();
194
		String email = "";
195
		String contactNumber = "";
196
 
197
		//get udfs
198
		Transaction transaction;
199
		TransactionServiceClient transactionServiceClient = null;
200
		try {
201
			transactionServiceClient = new TransactionServiceClient();
202
		} catch (Exception e) {
203
			// TODO Auto-generated catch block
204
			e.printStackTrace();
205
		}
206
 
207
		in.shop2020.model.v1.order.TransactionService.Client txnClient = transactionServiceClient.getClient();
208
		transaction = txnClient.getTransaction(txnId);
209
		orderDetails.append(transaction.getOrdersSize() + " ");
210
		for (Order order : transaction.getOrders()) {
211
			contactNumber= order.getCustomer_mobilenumber();
212
			email = order.getCustomer_email();
213
			billingAddress.append(" ");
214
			if(order.getCustomer_pincode()!=null){
215
				billingAddress.append(order.getCustomer_pincode());
216
			}
217
			if(order.getCustomer_city()!=null){
218
				billingAddress.append(" " + order.getCustomer_city());
219
			}
220
			if(order.getCustomer_address1()!=null){
221
				billingAddress.append(" " + order.getCustomer_address1());
222
			}
223
			if(order.getCustomer_address2()!=null){
224
				billingAddress.append(" " + order.getCustomer_address2());
225
			}
226
			if(order.getCustomer_state()!=null){
227
				billingAddress.append(" " + order.getCustomer_state());
228
			}
229
 
421 rajveer 230
 
894 rajveer 231
			for(LineItem line: order.getLineitems()){
232
				if(line.getBrand() != null){
233
					orderDetails.append(line.getBrand());
234
				}
235
				if(line.getModel_name() != null){
236
					orderDetails.append(line.getModel_name()); 
237
				}
238
				if(line.getModel_number() != null){
239
					orderDetails.append(line.getModel_number());
240
				}
241
				if(line.getColor() != null){
242
					orderDetails.append(line.getColor());
243
				}
244
				orderDetails.append(" ");
245
			}
246
 
247
		}
248
 
249
		Random random = new Random();
250
		String merchantInfo = ""+random.nextLong(); 
251
 
252
	    log.info("udf1:"  + orderDetails);
253
	    log.info("udf2:"  + email);
254
	    log.info("udf3:"  + contactNumber);
255
	    log.info("udf4:"  + billingAddress);
256
	    log.info("udf5:"  + merchantInfo);
257
 
258
	    String udf1 = formatUdf(orderDetails.toString()); 
259
	    String udf2 = formatUdf(email);
260
	    String udf3 = formatUdf(contactNumber);
261
	    String udf4 = formatUdf(billingAddress.toString());
262
	    String udf5 = merchantInfo;
263
 
264
	    pipe.setUdf1(udf1);       //	UDF 1 - Order details
265
		pipe.setUdf2(udf2);        	  				//	UDF 2 - Email ID
266
		pipe.setUdf3(udf3);      			//	UDF 3 - Contact Number. 
267
		pipe.setUdf4(udf4);     //	UDF 4 - Billing Address
268
		pipe.setUdf5(udf5);		  						//	UDF 5 - Merchant specific
269
 
270
		List<Attribute> attributes = new ArrayList<Attribute>();
271
		Attribute attribute1 = new Attribute("udf1",udf1);
272
		Attribute attribute2 = new Attribute("udf2",udf2);
273
		Attribute attribute3 = new Attribute("udf3",udf3);
274
		Attribute attribute4 = new Attribute("udf4",udf4);
275
		Attribute attribute5 = new Attribute("udf5",udf5);
276
 
277
		attributes.add(attribute1);
278
		attributes.add(attribute2);
279
		attributes.add(attribute3);
280
		attributes.add(attribute4);
281
		attributes.add(attribute5);
282
 
283
		return attributes;
284
	}
762 rajveer 285
 
894 rajveer 286
 
287
 
288
	private void initializePayment(String merchantPaymentId) throws ShoppingCartException, TException{
289
		String amount;
290
 
291
		amount = (new Double(getPaymentAmount(userinfo.getCartId()))).toString();
292
 
293
		//Following is the code which initilize e24PaymentPipe with proper value		
294
		pipe=new e24PaymentPipe();
295
		pipe.setResourcePath(resourceFilePath);	//mandatory 
296
		String as = pipe.getResourcePath();
297
		log.info("Resource="+as+ "<br>");
298
 
299
		pipe.setAlias(aliasName);			//mandatory 
300
		String ab=pipe.getAlias();
301
		log.info("Alias="+ab+ "<br>");
302
 
303
		pipe.setAction(ActionType.PURCHASE.value());			//mandatory 
304
		String ac=pipe.getAction();
305
		log.info("Action="+ac+ "<br>");
306
 
307
		pipe.setResponseURL( responseURL );	//mandatory
308
		String at=pipe.getResponseURL();
309
		log.info("ResponseURL="+at+ "<br>");
310
 
972 chandransh 311
		//pipe.setErrorURL( errorURL + "?paymentId=" + merchantPaymentId );		//mandatory
312
		pipe.setErrorURL( errorURL);
894 rajveer 313
	    String ak=pipe.getErrorURL();
314
	    log.info("ErrorURL="+ak+ "<br>");
315
 
316
 
317
		pipe.setAmt(amount);		
318
		String ap=pipe.getAmt();
319
		log.info("Amt="+ap);
320
 
321
		pipe.setCurrency("356");
322
		String a=pipe.getCurrency();
323
		log.info("Currency="+a+ "<br>");
324
 
325
		pipe.setLanguage("USA");
326
		String p=pipe.getLanguage();
327
		log.info("Language="+p+ "<br>");
328
 
329
		pipe.setTrackId(merchantPaymentId);
330
 
331
	}
332
 
333
	String formatUdf(String udfString){
334
		udfString.replaceAll(regex, replacement);
335
		if(udfString.length() > MAX_UDF_LENGTH){
336
			udfString = udfString.substring(0, MAX_UDF_LENGTH);
337
		}
338
		return udfString;
339
	}
340
 
421 rajveer 341
}
342