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