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;
741 rajveer 18
import in.shop2020.payments.Attribute;
421 rajveer 19
import in.shop2020.payments.PaymentException;
741 rajveer 20
import in.shop2020.payments.PaymentStatus;
421 rajveer 21
import in.shop2020.serving.utils.Utils;
22
import in.shop2020.thrift.clients.PaymentServiceClient;
23
import in.shop2020.thrift.clients.config.ConfigClient;
24
 
25
 
26
 
27
import com.aciworldwide.commerce.gateway.plugins.NotEnoughDataException;
28
import com.aciworldwide.commerce.gateway.plugins.e24PaymentPipe;
29
@Results({
712 rajveer 30
	@Result(name="redirect", type="redirectAction", 
31
			params = {"actionName" , "${url}"})
421 rajveer 32
})
712 rajveer 33
 
682 rajveer 34
public class HdfcPayController extends BaseController {
421 rajveer 35
 
682 rajveer 36
	private static final long serialVersionUID = 1L;
37
 
421 rajveer 38
	PaymentServiceClient paymentServiceClient = null;
39
	private String redirectURL;
682 rajveer 40
	private enum ActionType{
41
		PURCHASE("1"),
42
		AUTH ("4");
43
		private String value;
44
		ActionType(String value) {
45
			this.value = value;
46
		}
47
		public String value(){
48
			return this.value;
49
		}
50
	}
421 rajveer 51
 
682 rajveer 52
	public HdfcPayController(){
421 rajveer 53
		try {
54
			paymentServiceClient = new PaymentServiceClient();
55
		} catch (Exception e) {
56
			e.printStackTrace();
57
		}
58
	}
59
 
60
	public String getRedirectURL(){
61
		return this.redirectURL;
62
	}
63
 
712 rajveer 64
	public String index(){
421 rajveer 65
		String orderDetails = "";
66
		String email = "";
67
		String contactNumber = "";
68
		String billingAddress = "";
69
		String merchantInfo = "";
424 rajveer 70
		String amount;
693 rajveer 71
		long txnId;
712 rajveer 72
 
73
 
74
		System.out.println("amount:" + request.getParameter("amount"));
75
		System.out.println("amount:" + request.getParameter("txnid"));
76
		System.out.println("amount:" + request.getParameter("paymentid"));
77
 
424 rajveer 78
		try{
682 rajveer 79
			amount = request.getParameter("amount");
424 rajveer 80
		}catch(Exception e){
81
			amount = (new Double(Utils.getPaymentAmount(userinfo.getCartId()))).toString();
82
		}
712 rajveer 83
		txnId = Long.parseLong(request.getParameter("txnid"));
693 rajveer 84
 
421 rajveer 85
 
86
 
87
//	    Random rnd = new Random(System.currentTimeMillis());
88
//		String trackId = String.valueOf(Math.abs(rnd.nextLong()));	//Merchant must generate the Track Id
89
 
741 rajveer 90
		String merchantPaymentId = request.getParameter("paymentid");	
421 rajveer 91
 
741 rajveer 92
		session.setAttribute("trackId",merchantPaymentId);
421 rajveer 93
//		String InstituteID=request.getParameter("InstituteID");	//Must be 1 for the merchant having only one bank(mandatory must be initilized)
94
 
95
 
96
		//Following is the code which initilize e24PaymentPipe with proper value
97
		e24PaymentPipe pipe=new e24PaymentPipe();
98
		String resourceFilePath = "";
99
		String aliasName = "";
100
		String responseURL = "";
101
		String errorURL = "";
102
		try {
103
			resourceFilePath = ConfigClient.getClient().get("payment_resource_file_path");
104
			aliasName  = ConfigClient.getClient().get("payment_alias_name");
105
			responseURL = ConfigClient.getClient().get("payment_response_url");
106
			errorURL = ConfigClient.getClient().get("payment_error_url");
107
		} catch (ConfigException e1) {
108
			// TODO Auto-generated catch block
109
			resourceFilePath = "/home/rajveer/shop2020/payments/resource/";
110
			aliasName = "90001194";
111
 
517 rajveer 112
			responseURL = "http://74.207.248.175:8080/JSP-Pages/HRedirect.jsp";
113
			errorURL = "http://74.207.248.175:8080/JSP-Pages/HError.jsp";
421 rajveer 114
//			responseURL = "http://securemtp.fssnet.co.in/MerchantDemo/viren/TESTTEST/HRedirect.jsp";
115
//			errorURL = "http://securemtp.fssnet.co.in/MerchantDemo/viren/TESTTEST/HError.jsp";
116
			e1.printStackTrace();
117
		}
118
 
119
		pipe.setResourcePath(resourceFilePath);	//mandatory 
120
		String as = pipe.getResourcePath();
121
		System.out.println("Resource="+as+ "<br>");
122
 
123
		pipe.setAlias(aliasName);			//mandatory 
124
		String ab=pipe.getAlias();
125
		System.out.println("Alias="+ab+ "<br>");
126
 
682 rajveer 127
		pipe.setAction(ActionType.PURCHASE.value());			//mandatory 
421 rajveer 128
		String ac=pipe.getAction();
129
		System.out.println("Action="+ac+ "<br>");
130
 
131
		pipe.setResponseURL( responseURL );	//mandatory
132
		String at=pipe.getResponseURL();
133
		System.out.println("ResponseURL="+at+ "<br>");
134
 
135
		pipe.setErrorURL( errorURL );		//mandatory 
136
        String ak=pipe.getErrorURL();
137
        System.out.println("ErrorURL="+ak+ "<br>");
138
 
139
 
140
		pipe.setAmt(amount);		
141
		String ap=pipe.getAmt();
142
		System.out.println("Amt="+ap);
143
 
144
		pipe.setCurrency("356");
145
		String a=pipe.getCurrency();
146
		System.out.println("Currency="+a+ "<br>");
147
 
148
		pipe.setLanguage("USA");
149
		String p=pipe.getLanguage();
150
		System.out.println("Language="+p+ "<br>");
151
 
741 rajveer 152
		pipe.setTrackId(merchantPaymentId);
421 rajveer 153
 
154
 
741 rajveer 155
		orderDetails = Utils.getOrderDetails(txnId);
421 rajveer 156
		email = Utils.getEmailId(userinfo.getUserId());
741 rajveer 157
		contactNumber = Utils.getContactNumber(txnId);
158
		billingAddress = Utils.getBillingAddress(txnId);
421 rajveer 159
 
741 rajveer 160
		String regex = "[^a-zA-Z0-9\\s\\-\\@\\/\\.]";
161
		String replacement = " ";
421 rajveer 162
 
741 rajveer 163
		orderDetails = orderDetails.replaceAll(regex, replacement);
164
		email = email.replaceAll(regex, replacement);
165
		contactNumber = replacement.replaceAll(regex, replacement);
166
		billingAddress = billingAddress.replaceAll(regex, replacement);
167
		if(orderDetails.length() > 250){
168
			orderDetails = orderDetails.substring(0, 249);
169
		}
170
		if(billingAddress.length() > 250){
171
			billingAddress = billingAddress.substring(0, 249);
172
		}
173
		Random random = new Random();
174
		merchantInfo = ""+random.nextLong(); 
175
 
176
 
421 rajveer 177
//		UDF 1 - Order details
178
//		UDF 2 - Email ID
179
//		UDF 3 - Contact Number. 
180
//		UDF 4 - Billing Address
181
//		UDF 5 - Merchant specific
741 rajveer 182
 
183
 
184
        System.out.println("udf1:"  + orderDetails);
185
        System.out.println("udf2:"  + email);
186
        System.out.println("udf3:"  + contactNumber);
187
        System.out.println("udf4:"  + billingAddress);
188
        System.out.println("udf5:"  + merchantInfo);
189
 
190
 
421 rajveer 191
		pipe.setUdf1(orderDetails);
192
		pipe.setUdf2(email);
193
		pipe.setUdf3(contactNumber);
194
		pipe.setUdf4(billingAddress);
741 rajveer 195
		pipe.setUdf5(merchantInfo);
517 rajveer 196
 
741 rajveer 197
		List<Attribute> attributes = new ArrayList<Attribute>();
198
		Attribute attribute1 = new Attribute();
199
		Attribute attribute2 = new Attribute();
200
		Attribute attribute3 = new Attribute();
201
		Attribute attribute4 = new Attribute();
202
		Attribute attribute5 = new Attribute();
203
		attribute1.setName("udf1");
204
		attribute1.setValue(orderDetails);
205
		attribute2.setName("udf2");
206
		attribute2.setValue(email);
207
		attribute3.setName("udf3");
208
		attribute3.setValue(contactNumber);
209
		attribute4.setName("udf4");
210
		attribute4.setValue(billingAddress);
211
		attribute5.setName("udf5");
212
		attribute5.setValue(merchantInfo);
213
		attributes.add(attribute1);
214
		attributes.add(attribute2);
215
		attributes.add(attribute3);
216
		attributes.add(attribute4);
217
		attributes.add(attribute5);
218
 
421 rajveer 219
		try {
220
			if(pipe.performPaymentInitialization() != e24PaymentPipe.SUCCESS) 
221
				{
222
					System.out.println("Error sending Payment Initialization Request: ");
741 rajveer 223
					paymentServiceClient.getClient().updatePaymentDetails(Long.parseLong(merchantPaymentId), null, "", "", pipe.getErrorMsg(), "", "", "", "", PaymentStatus.INIT, attributes);
224
					response.sendRedirect(response.encodeRedirectURL( errorURL + "?ErrorText="+pipe.getErrorMsg()+"&paymentId="+merchantPaymentId));
421 rajveer 225
					redirectURL = errorURL + "?ErrorText="+pipe.getErrorMsg();
226
					response.setHeader("Location", redirectURL);
719 rajveer 227
					response.setStatus(HttpServletResponse.SC_FOUND);
421 rajveer 228
				}
229
			else
230
				{
741 rajveer 231
					String paymentID = pipe.getPaymentId();
232
					// Update of payment information
233
					paymentServiceClient.getClient().updatePaymentDetails(Long.parseLong(merchantPaymentId), paymentID, "", "", "", "", "", "", "", PaymentStatus.INIT, attributes);
234
 
421 rajveer 235
					String payURL = pipe.getPaymentPage();
741 rajveer 236
					redirectURL = payURL + "?PaymentID=" + paymentID;
719 rajveer 237
					response.sendRedirect(response.encodeRedirectURL( redirectURL ));
421 rajveer 238
 
239
					response.setHeader("Location", redirectURL);
719 rajveer 240
					response.setStatus(HttpServletResponse.SC_FOUND);
421 rajveer 241
				}
712 rajveer 242
			return "success";
421 rajveer 243
		} catch (NotEnoughDataException e) {
244
			// TODO Auto-generated catch block
245
			e.printStackTrace();
246
		}catch (Exception e) {
247
			// TODO Auto-generated catch block
248
			e.printStackTrace();
249
		}
250
 
251
		//response.sendRedirect(response.encodeRedirectURL( "www.yahoo.com" ));
741 rajveer 252
		redirectURL = errorURL + "?ErrorText=Error while initializing payment.";
253
		return "success";
421 rajveer 254
	}
712 rajveer 255
 
256
	String getUrl(){
257
		return this.redirectURL;
258
	}
421 rajveer 259
 
260
}
261