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.HashMap;
5
import java.util.List;
712 rajveer 6
import java.util.Map;
741 rajveer 7
import java.util.Random;
712 rajveer 8
 
421 rajveer 9
import javax.servlet.http.HttpServletResponse;
712 rajveer 10
 
11
import org.apache.struts2.convention.annotation.Result;
12
import org.apache.struts2.convention.annotation.Results;
421 rajveer 13
import org.apache.struts2.rest.DefaultHttpHeaders;
14
import org.apache.struts2.rest.HttpHeaders;
15
import org.apache.thrift.TException;
16
 
17
import in.shop2020.config.ConfigException;
762 rajveer 18
import in.shop2020.model.v1.order.LineItem;
19
import in.shop2020.model.v1.order.Order;
20
import in.shop2020.model.v1.order.Transaction;
21
import in.shop2020.model.v1.user.ShoppingCartException;
741 rajveer 22
import in.shop2020.payments.Attribute;
421 rajveer 23
import in.shop2020.payments.PaymentException;
741 rajveer 24
import in.shop2020.payments.PaymentStatus;
421 rajveer 25
import in.shop2020.serving.utils.Utils;
26
import in.shop2020.thrift.clients.PaymentServiceClient;
762 rajveer 27
import in.shop2020.thrift.clients.TransactionServiceClient;
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", 
36
			params = {"actionName" , "${url}"})
421 rajveer 37
})
712 rajveer 38
 
682 rajveer 39
public class HdfcPayController extends BaseController {
421 rajveer 40
 
682 rajveer 41
	private static final long serialVersionUID = 1L;
42
 
421 rajveer 43
	PaymentServiceClient paymentServiceClient = null;
44
	private String redirectURL;
682 rajveer 45
	private enum ActionType{
46
		PURCHASE("1"),
47
		AUTH ("4");
48
		private String value;
49
		ActionType(String value) {
50
			this.value = value;
51
		}
52
		public String value(){
53
			return this.value;
54
		}
55
	}
421 rajveer 56
 
682 rajveer 57
	public HdfcPayController(){
421 rajveer 58
		try {
59
			paymentServiceClient = new PaymentServiceClient();
60
		} catch (Exception e) {
61
			e.printStackTrace();
62
		}
63
	}
64
 
65
	public String getRedirectURL(){
66
		return this.redirectURL;
67
	}
68
 
712 rajveer 69
	public String index(){
421 rajveer 70
		String orderDetails = "";
71
		String email = "";
72
		String contactNumber = "";
73
		String billingAddress = "";
74
		String merchantInfo = "";
424 rajveer 75
		String amount;
693 rajveer 76
		long txnId;
712 rajveer 77
 
78
 
79
		System.out.println("amount:" + request.getParameter("amount"));
80
		System.out.println("amount:" + request.getParameter("txnid"));
81
		System.out.println("amount:" + request.getParameter("paymentid"));
82
 
424 rajveer 83
		try{
682 rajveer 84
			amount = request.getParameter("amount");
424 rajveer 85
		}catch(Exception e){
86
			amount = (new Double(Utils.getPaymentAmount(userinfo.getCartId()))).toString();
87
		}
712 rajveer 88
		txnId = Long.parseLong(request.getParameter("txnid"));
693 rajveer 89
 
421 rajveer 90
 
91
 
92
//	    Random rnd = new Random(System.currentTimeMillis());
93
//		String trackId = String.valueOf(Math.abs(rnd.nextLong()));	//Merchant must generate the Track Id
94
 
741 rajveer 95
		String merchantPaymentId = request.getParameter("paymentid");	
421 rajveer 96
 
741 rajveer 97
		session.setAttribute("trackId",merchantPaymentId);
421 rajveer 98
//		String InstituteID=request.getParameter("InstituteID");	//Must be 1 for the merchant having only one bank(mandatory must be initilized)
99
 
100
 
101
		//Following is the code which initilize e24PaymentPipe with proper value
102
		e24PaymentPipe pipe=new e24PaymentPipe();
103
		String resourceFilePath = "";
104
		String aliasName = "";
105
		String responseURL = "";
106
		String errorURL = "";
107
		try {
108
			resourceFilePath = ConfigClient.getClient().get("payment_resource_file_path");
109
			aliasName  = ConfigClient.getClient().get("payment_alias_name");
110
			responseURL = ConfigClient.getClient().get("payment_response_url");
111
			errorURL = ConfigClient.getClient().get("payment_error_url");
112
		} catch (ConfigException e1) {
113
			// TODO Auto-generated catch block
114
			resourceFilePath = "/home/rajveer/shop2020/payments/resource/";
115
			aliasName = "90001194";
116
 
517 rajveer 117
			responseURL = "http://74.207.248.175:8080/JSP-Pages/HRedirect.jsp";
118
			errorURL = "http://74.207.248.175:8080/JSP-Pages/HError.jsp";
421 rajveer 119
//			responseURL = "http://securemtp.fssnet.co.in/MerchantDemo/viren/TESTTEST/HRedirect.jsp";
120
//			errorURL = "http://securemtp.fssnet.co.in/MerchantDemo/viren/TESTTEST/HError.jsp";
121
			e1.printStackTrace();
122
		}
123
 
124
		pipe.setResourcePath(resourceFilePath);	//mandatory 
125
		String as = pipe.getResourcePath();
126
		System.out.println("Resource="+as+ "<br>");
127
 
128
		pipe.setAlias(aliasName);			//mandatory 
129
		String ab=pipe.getAlias();
130
		System.out.println("Alias="+ab+ "<br>");
131
 
682 rajveer 132
		pipe.setAction(ActionType.PURCHASE.value());			//mandatory 
421 rajveer 133
		String ac=pipe.getAction();
134
		System.out.println("Action="+ac+ "<br>");
135
 
136
		pipe.setResponseURL( responseURL );	//mandatory
137
		String at=pipe.getResponseURL();
138
		System.out.println("ResponseURL="+at+ "<br>");
139
 
140
		pipe.setErrorURL( errorURL );		//mandatory 
141
        String ak=pipe.getErrorURL();
142
        System.out.println("ErrorURL="+ak+ "<br>");
143
 
144
 
145
		pipe.setAmt(amount);		
146
		String ap=pipe.getAmt();
147
		System.out.println("Amt="+ap);
148
 
149
		pipe.setCurrency("356");
150
		String a=pipe.getCurrency();
151
		System.out.println("Currency="+a+ "<br>");
152
 
153
		pipe.setLanguage("USA");
154
		String p=pipe.getLanguage();
155
		System.out.println("Language="+p+ "<br>");
156
 
741 rajveer 157
		pipe.setTrackId(merchantPaymentId);
421 rajveer 158
 
762 rajveer 159
		//get udfs
160
		Transaction transaction;
161
		try {
162
			TransactionServiceClient transactionServiceClient = new TransactionServiceClient();
163
			in.shop2020.model.v1.order.TransactionService.Client txnClient = transactionServiceClient.getClient();
164
			transaction = txnClient.getTransaction(txnId);
165
			orderDetails = orderDetails + transaction.getOrdersSize() + " ";
166
			for (Order order : transaction.getOrders()) {
167
				contactNumber = order.getCustomer_mobilenumber();
168
				email = order.getCustomer_email();
169
				billingAddress = " ";
170
				if(order.getCustomer_pincode()!=null){
171
					billingAddress = billingAddress + order.getCustomer_pincode();
172
				}
173
				if(order.getCustomer_city()!=null){
174
					billingAddress = billingAddress + " " + order.getCustomer_city();
175
				}
176
				if(order.getCustomer_address1()!=null){
177
					billingAddress = billingAddress + " " + order.getCustomer_address1();
178
				}
179
				if(order.getCustomer_address2()!=null){
180
					billingAddress = billingAddress + order.getCustomer_address2();
181
				}
182
				if(order.getCustomer_state()!=null){
183
					billingAddress = billingAddress + order.getCustomer_state();
184
				}
185
 
421 rajveer 186
 
762 rajveer 187
				for(LineItem line: order.getLineitems()){
188
					if(line.getBrand() != null){
189
						orderDetails = orderDetails + line.getBrand();
190
					}
191
					if(line.getModel_name() != null){
192
						orderDetails = orderDetails + line.getModel_name(); 
193
					}
194
					if(line.getModel_number() != null){
195
						orderDetails = orderDetails + line.getModel_number();
196
					}
197
					if(line.getColor() != null){
198
						orderDetails = orderDetails + line.getColor();
199
					}
200
					orderDetails = orderDetails + " ";
201
				}
202
 
203
			}
204
		} catch (TException e) {
205
			e.printStackTrace();
206
		} catch (Exception e) {
207
			e.printStackTrace();
208
		}
421 rajveer 209
 
762 rajveer 210
 
741 rajveer 211
		String regex = "[^a-zA-Z0-9\\s\\-\\@\\/\\.]";
212
		String replacement = " ";
421 rajveer 213
 
741 rajveer 214
		orderDetails = orderDetails.replaceAll(regex, replacement);
215
		email = email.replaceAll(regex, replacement);
762 rajveer 216
		//contactNumber = replacement.replaceAll(regex, replacement);
741 rajveer 217
		billingAddress = billingAddress.replaceAll(regex, replacement);
762 rajveer 218
		if(email.length() > 30){
219
			email = email.substring(0, 30);
741 rajveer 220
		}
762 rajveer 221
		if(contactNumber.length() > 30){
222
			contactNumber = contactNumber.substring(0, 30);
741 rajveer 223
		}
762 rajveer 224
		if(orderDetails.length() > 30){
225
			orderDetails = orderDetails.substring(0, 30);
226
		}
227
		if(billingAddress.length() > 30){
228
			billingAddress = billingAddress.substring(0, 30);
229
		}
741 rajveer 230
		Random random = new Random();
231
		merchantInfo = ""+random.nextLong(); 
232
 
233
 
421 rajveer 234
//		UDF 1 - Order details
235
//		UDF 2 - Email ID
236
//		UDF 3 - Contact Number. 
237
//		UDF 4 - Billing Address
238
//		UDF 5 - Merchant specific
741 rajveer 239
 
240
 
241
        System.out.println("udf1:"  + orderDetails);
242
        System.out.println("udf2:"  + email);
243
        System.out.println("udf3:"  + contactNumber);
244
        System.out.println("udf4:"  + billingAddress);
245
        System.out.println("udf5:"  + merchantInfo);
246
 
247
 
421 rajveer 248
		pipe.setUdf1(orderDetails);
249
		pipe.setUdf2(email);
250
		pipe.setUdf3(contactNumber);
251
		pipe.setUdf4(billingAddress);
741 rajveer 252
		pipe.setUdf5(merchantInfo);
517 rajveer 253
 
741 rajveer 254
		List<Attribute> attributes = new ArrayList<Attribute>();
255
		Attribute attribute1 = new Attribute();
256
		Attribute attribute2 = new Attribute();
257
		Attribute attribute3 = new Attribute();
258
		Attribute attribute4 = new Attribute();
259
		Attribute attribute5 = new Attribute();
260
		attribute1.setName("udf1");
261
		attribute1.setValue(orderDetails);
262
		attribute2.setName("udf2");
263
		attribute2.setValue(email);
264
		attribute3.setName("udf3");
265
		attribute3.setValue(contactNumber);
266
		attribute4.setName("udf4");
267
		attribute4.setValue(billingAddress);
268
		attribute5.setName("udf5");
269
		attribute5.setValue(merchantInfo);
270
		attributes.add(attribute1);
271
		attributes.add(attribute2);
272
		attributes.add(attribute3);
273
		attributes.add(attribute4);
274
		attributes.add(attribute5);
275
 
421 rajveer 276
		try {
277
			if(pipe.performPaymentInitialization() != e24PaymentPipe.SUCCESS) 
278
				{
279
					System.out.println("Error sending Payment Initialization Request: ");
741 rajveer 280
					paymentServiceClient.getClient().updatePaymentDetails(Long.parseLong(merchantPaymentId), null, "", "", pipe.getErrorMsg(), "", "", "", "", PaymentStatus.INIT, attributes);
281
					response.sendRedirect(response.encodeRedirectURL( errorURL + "?ErrorText="+pipe.getErrorMsg()+"&paymentId="+merchantPaymentId));
829 rajveer 282
					redirectURL = errorURL + "?paymentId="+merchantPaymentId + "&ErrorText="+pipe.getErrorMsg();
421 rajveer 283
					response.setHeader("Location", redirectURL);
719 rajveer 284
					response.setStatus(HttpServletResponse.SC_FOUND);
421 rajveer 285
				}
286
			else
287
				{
741 rajveer 288
					String paymentID = pipe.getPaymentId();
289
					// Update of payment information
290
					paymentServiceClient.getClient().updatePaymentDetails(Long.parseLong(merchantPaymentId), paymentID, "", "", "", "", "", "", "", PaymentStatus.INIT, attributes);
291
 
421 rajveer 292
					String payURL = pipe.getPaymentPage();
741 rajveer 293
					redirectURL = payURL + "?PaymentID=" + paymentID;
719 rajveer 294
					response.sendRedirect(response.encodeRedirectURL( redirectURL ));
421 rajveer 295
 
296
					response.setHeader("Location", redirectURL);
719 rajveer 297
					response.setStatus(HttpServletResponse.SC_FOUND);
421 rajveer 298
				}
712 rajveer 299
			return "success";
421 rajveer 300
		} catch (NotEnoughDataException e) {
301
			// TODO Auto-generated catch block
302
			e.printStackTrace();
303
		}catch (Exception e) {
304
			// TODO Auto-generated catch block
305
			e.printStackTrace();
306
		}
307
 
308
		//response.sendRedirect(response.encodeRedirectURL( "www.yahoo.com" ));
741 rajveer 309
		redirectURL = errorURL + "?ErrorText=Error while initializing payment.";
310
		return "success";
421 rajveer 311
	}
712 rajveer 312
 
313
	String getUrl(){
314
		return this.redirectURL;
315
	}
421 rajveer 316
 
762 rajveer 317
 
421 rajveer 318
}
319