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
 
3
import java.io.IOException;
4
import java.util.Date;
5
import java.util.Map;
6
import java.util.Random;
7
 
8
import javax.servlet.ServletContext;
9
import javax.servlet.http.Cookie;
10
import javax.servlet.http.HttpServletResponse;
11
 
12
import org.apache.struts2.convention.annotation.Result;
13
import org.apache.struts2.convention.annotation.Results;
14
import org.apache.struts2.interceptor.ParameterAware;
15
import org.apache.struts2.rest.DefaultHttpHeaders;
16
import org.apache.struts2.rest.HttpHeaders;
17
import org.apache.struts2.util.ServletContextAware;
18
import org.apache.thrift.TException;
19
 
20
import in.shop2020.config.ConfigException;
21
import in.shop2020.model.v1.user.AuthenticationException;
22
import in.shop2020.model.v1.user.UserContextException;
23
import in.shop2020.model.v1.user.UserContextService.Client;
24
import in.shop2020.payments.PaymentException;
25
import in.shop2020.payments.PaymentService;
26
import in.shop2020.serving.services.UserSessionInfo;
27
import in.shop2020.serving.utils.Utils;
28
import in.shop2020.thrift.clients.PaymentServiceClient;
29
import in.shop2020.thrift.clients.UserContextServiceClient;
30
import in.shop2020.thrift.clients.UserProfileClient;
31
import in.shop2020.thrift.clients.config.ConfigClient;
32
 
33
 
34
 
35
import com.aciworldwide.commerce.gateway.plugins.NotEnoughDataException;
36
import com.aciworldwide.commerce.gateway.plugins.e24PaymentPipe;
37
import com.opensymphony.xwork2.ModelDriven;
38
/*
39
@Results({
40
    @Result(name="success", type="redirectAction", params = {"actionName" , 
41
    		"pay"})
42
})
43
*/
44
public class PayController extends BaseController implements ParameterAware, ServletContextAware {
45
 
46
	Map<String, String[]> reqparams;
47
 
48
	private String id;
49
	PaymentServiceClient paymentServiceClient = null;
50
	private String redirectURL;
51
 
52
	private ServletContext servletContext;
53
 
54
	public PayController(){
55
 
56
		try {
57
			paymentServiceClient = new PaymentServiceClient();
58
		} catch (Exception e) {
59
			e.printStackTrace();
60
		}
61
	}
62
 
63
	public String getRedirectURL(){
64
		return this.redirectURL;
65
	}
66
 
67
	public HttpHeaders show(){
68
		return new DefaultHttpHeaders("lfail");
69
 
70
	}
71
 
72
	public HttpHeaders create(){
73
		String orderDetails = "";
74
		String email = "";
75
		String contactNumber = "";
76
		String billingAddress = "";
77
		String merchantInfo = "";
78
 
79
 
80
		String amount =   this.reqparams.get("amount")[0];
81
		session.setAttribute("amount", amount);
82
		// May be we can use some utils function to get the prices in cart but it is better to get price from post form
83
 
84
		long paymentId = 0;
85
		in.shop2020.payments.PaymentService.Client client = paymentServiceClient.getClient();
86
 
87
 
88
		try {
89
			paymentId = client.createPayment(userinfo.getUserId(), userinfo.getCartId(), Double.parseDouble(amount), 1);
90
		} catch (NumberFormatException e1) {
91
			// TODO Auto-generated catch block
92
			e1.printStackTrace();
93
		} catch (PaymentException e1) {
94
			// TODO Auto-generated catch block
95
			e1.printStackTrace();
96
		} catch (TException e1) {
97
			// TODO Auto-generated catch block
98
			e1.printStackTrace();
99
		}
100
 
101
 
102
 
103
 
104
//	    Random rnd = new Random(System.currentTimeMillis());
105
//		String trackId = String.valueOf(Math.abs(rnd.nextLong()));	//Merchant must generate the Track Id
106
 
107
		String trackId = (new Long(paymentId)).toString();
108
 
109
 
110
		session.setAttribute("trackId",trackId);
111
//		String InstituteID=request.getParameter("InstituteID");	//Must be 1 for the merchant having only one bank(mandatory must be initilized)
112
 
113
 
114
		//Following is the code which initilize e24PaymentPipe with proper value
115
		e24PaymentPipe pipe=new e24PaymentPipe();
116
		String resourceFilePath = "";
117
		String aliasName = "";
118
		String responseURL = "";
119
		String errorURL = "";
120
		try {
121
			resourceFilePath = ConfigClient.getClient().get("payment_resource_file_path");
122
			aliasName  = ConfigClient.getClient().get("payment_alias_name");
123
			responseURL = ConfigClient.getClient().get("payment_response_url");
124
			errorURL = ConfigClient.getClient().get("payment_error_url");
125
		} catch (ConfigException e1) {
126
			// TODO Auto-generated catch block
127
			resourceFilePath = "/home/rajveer/shop2020/payments/resource/";
128
			aliasName = "90001194";
129
 
130
			responseURL = "http://74.207.249.10:8080/JSP-Pages/HRedirect.jsp";
131
			errorURL = "http://74.207.249.10:8080/JSP-Pages/HError.jsp";
132
//			responseURL = "http://securemtp.fssnet.co.in/MerchantDemo/viren/TESTTEST/HRedirect.jsp";
133
//			errorURL = "http://securemtp.fssnet.co.in/MerchantDemo/viren/TESTTEST/HError.jsp";
134
			e1.printStackTrace();
135
		}
136
 
137
		pipe.setResourcePath(resourceFilePath);	//mandatory 
138
		String as = pipe.getResourcePath();
139
		System.out.println("Resource="+as+ "<br>");
140
 
141
		pipe.setAlias(aliasName);			//mandatory 
142
		String ab=pipe.getAlias();
143
		System.out.println("Alias="+ab+ "<br>");
144
 
145
		pipe.setAction("1");			//mandatory 
146
		String ac=pipe.getAction();
147
		System.out.println("Action="+ac+ "<br>");
148
 
149
		pipe.setResponseURL( responseURL );	//mandatory
150
		String at=pipe.getResponseURL();
151
		System.out.println("ResponseURL="+at+ "<br>");
152
 
153
		pipe.setErrorURL( errorURL );		//mandatory 
154
        String ak=pipe.getErrorURL();
155
        System.out.println("ErrorURL="+ak+ "<br>");
156
 
157
 
158
		pipe.setAmt(amount);		
159
		String ap=pipe.getAmt();
160
		System.out.println("Amt="+ap);
161
 
162
		pipe.setCurrency("356");
163
		String a=pipe.getCurrency();
164
		System.out.println("Currency="+a+ "<br>");
165
 
166
		pipe.setLanguage("USA");
167
		String p=pipe.getLanguage();
168
		System.out.println("Language="+p+ "<br>");
169
 
170
		pipe.setTrackId(trackId);
171
 
172
 
173
		orderDetails = Utils.getOrderDetails(userinfo.getCartId());
174
		email = Utils.getEmailId(userinfo.getUserId());
175
		contactNumber = Utils.getContactNumber(userinfo.getCartId());
176
		billingAddress = Utils.getBillingAddress(userinfo.getCartId());
177
		merchantInfo = "Shop2020";
178
 
179
 
180
//		UDF 1 - Order details
181
//		UDF 2 - Email ID
182
//		UDF 3 - Contact Number. 
183
//		UDF 4 - Billing Address
184
//		UDF 5 - Merchant specific
185
 
186
		pipe.setUdf1(orderDetails);
187
		pipe.setUdf2(email);
188
		pipe.setUdf3(contactNumber);
189
		pipe.setUdf4(billingAddress);
190
        pipe.setUdf5(merchantInfo);
191
 
192
		try {
193
			if(pipe.performPaymentInitialization() != e24PaymentPipe.SUCCESS) 
194
				{
195
					System.out.println("Error sending Payment Initialization Request: ");
196
					response.sendRedirect(response.encodeRedirectURL( errorURL + "?ErrorText="+pipe.getErrorMsg()));
197
					redirectURL = errorURL + "?ErrorText="+pipe.getErrorMsg();
198
					response.setHeader("Location", redirectURL);
199
					response.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
200
				}
201
			else
202
				{
203
					String PaymentID = pipe.getPaymentId();
204
					String payURL = pipe.getPaymentPage();
205
					response.sendRedirect(response.encodeRedirectURL( payURL + "?PaymentID=" + PaymentID ));
206
 
207
					redirectURL = payURL + "?PaymentID=" + PaymentID;
208
					response.setHeader("Location", redirectURL);
209
					response.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
210
				}
211
			return new DefaultHttpHeaders("success");
212
		} catch (NotEnoughDataException e) {
213
			// TODO Auto-generated catch block
214
			e.printStackTrace();
215
		}catch (Exception e) {
216
			// TODO Auto-generated catch block
217
			e.printStackTrace();
218
		}
219
 
220
		//response.sendRedirect(response.encodeRedirectURL( "www.yahoo.com" ));
221
 
222
		return new DefaultHttpHeaders("fail");
223
	}
224
 
225
	public String getId(){
226
		return id;
227
	}
228
 
229
	public void setId(String id){
230
		this.id = id;
231
	}
232
 
233
	@Override
234
	public void setParameters(Map<String, String[]> reqmap) {
235
		//log.info("setParameters:" + reqmap);
236
 
237
		this.reqparams = reqmap;
238
	}
239
 
240
	@Override
241
	public void setServletContext(ServletContext servletContext) {
242
		this.servletContext = servletContext;
243
	}
244
 
245
}
246